diff options
author | John Hodge <tpg@mutabah.net> | 2016-02-26 21:45:37 +0800 |
---|---|---|
committer | John Hodge <tpg@mutabah.net> | 2016-02-26 21:45:37 +0800 |
commit | ba563a6e9a5d698990e1bddaeeecc512fd670bcc (patch) | |
tree | 6f1695adf13f70ca139156936d055bb16d7ea0e9 /src | |
parent | aa03f6016ce7699e167321e127691575aa191a55 (diff) | |
download | mrust-ba563a6e9a5d698990e1bddaeeecc512fd670bcc.tar.gz |
Parse/lex - Minor fix to escaped string handling
Diffstat (limited to 'src')
-rw-r--r-- | src/parse/lex.cpp | 21 | ||||
-rw-r--r-- | src/parse/lex.hpp | 7 |
2 files changed, 24 insertions, 4 deletions
diff --git a/src/parse/lex.cpp b/src/parse/lex.cpp index 1e0e0712..b6463b21 100644 --- a/src/parse/lex.cpp +++ b/src/parse/lex.cpp @@ -440,9 +440,17 @@ Token Lexer::getTokenInt() ::std::string str; while( (ch = this->getc()) != '"' ) { - if( ch == '\\' ) - ch = this->parseEscape('"'); - str.push_back(ch); + if( ch == '\\' ) { + auto v = this->parseEscape('"'); + if( v != ~0u ) { + if( v > 256 ) + throw ParseError::Generic(*this, "Value out of range for byte literal"); + str += (char)v; + } + } + else { + str.push_back(ch); + } } return Token(TOK_BYTESTRING, str); } @@ -745,7 +753,11 @@ uint32_t Lexer::parseEscape(char enclosing) m_line ++; while( isspace(ch) ) ch = this->getc(); - return ch; + this->ungetc(); + if( ch == enclosing ) + return ~0; + else + return ch; default: throw ParseError::Todo( FMT("Unknown escape sequence \\" << ch) ); } @@ -1056,6 +1068,7 @@ SERIALISE_TYPE_S(Token, { switch(tok.type()) { case TOK_STRING: + case TOK_BYTESTRING: case TOK_IDENT: case TOK_MACRO: case TOK_LIFETIME: diff --git a/src/parse/lex.hpp b/src/parse/lex.hpp index 3607fef0..3081e255 100644 --- a/src/parse/lex.hpp +++ b/src/parse/lex.hpp @@ -174,7 +174,14 @@ public: struct Codepoint { uint32_t v; + Codepoint(uint32_t v): v(v) { } friend ::std::string& operator+=(::std::string& s, const Codepoint& cp) { + if( cp.v < 128 ) { + s += (char)cp.v; + } + else { + throw ::std::runtime_error("TODO: Encode UTF-8 codepoint"); + } return s; } }; |