From 7529b164104f802b59fbf96f5a88d9709ac3ff1a Mon Sep 17 00:00:00 2001 From: Roman Divacky Date: Sat, 18 Dec 2010 08:56:37 +0000 Subject: [PATCH] Add support for lexing single quotes like 'c'. This fixed 8615. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@122150 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/llvm/MC/MCParser/AsmLexer.h | 1 + lib/MC/MCParser/AsmLexer.cpp | 37 ++++++++++++++++++++++++++++ test/MC/AsmParser/directive_values.s | 16 ++++++++++++ 3 files changed, 54 insertions(+) diff --git a/include/llvm/MC/MCParser/AsmLexer.h b/include/llvm/MC/MCParser/AsmLexer.h index 26641688092..99caebfab7f 100644 --- a/include/llvm/MC/MCParser/AsmLexer.h +++ b/include/llvm/MC/MCParser/AsmLexer.h @@ -60,6 +60,7 @@ private: AsmToken LexSlash(); AsmToken LexLineComment(); AsmToken LexDigit(); + AsmToken LexSingleQuote(); AsmToken LexQuote(); AsmToken LexFloatLiteral(); }; diff --git a/lib/MC/MCParser/AsmLexer.cpp b/lib/MC/MCParser/AsmLexer.cpp index 867cb208cec..085d519406d 100644 --- a/lib/MC/MCParser/AsmLexer.cpp +++ b/lib/MC/MCParser/AsmLexer.cpp @@ -265,6 +265,42 @@ AsmToken AsmLexer::LexDigit() { return AsmToken(AsmToken::Integer, Result, Value); } +/// LexSingleQuote: Integer: 'b' +AsmToken AsmLexer::LexSingleQuote() { + int CurChar = getNextChar(); + + if (CurChar == '\\') + CurChar = getNextChar(); + + if (CurChar == EOF) + return ReturnError(TokStart, "unterminated single quote"); + + CurChar = getNextChar(); + + if (CurChar != '\'') + return ReturnError(TokStart, "single quote way too long"); + + // The idea here being that 'c' is basically just an integral + // constant. + StringRef Res = StringRef(TokStart,CurPtr - TokStart); + long long Value; + + if (Res.startswith("\'\\")) { + char theChar = Res[2]; + switch (theChar) { + default: Value = theChar; break; + case '\'': Value = '\''; break; + case 't': Value = '\t'; break; + case 'n': Value = '\n'; break; + case 'b': Value = '\b'; break; + } + } else + Value = TokStart[1]; + + return AsmToken(AsmToken::Integer, Res, Value); +} + + /// LexQuote: String: "..." AsmToken AsmLexer::LexQuote() { int CurChar = getNextChar(); @@ -361,6 +397,7 @@ AsmToken AsmLexer::LexToken() { case '%': return AsmToken(AsmToken::Percent, StringRef(TokStart, 1)); case '/': return LexSlash(); case '#': return AsmToken(AsmToken::Hash, StringRef(TokStart, 1)); + case '\'': return LexSingleQuote(); case '"': return LexQuote(); case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': diff --git a/test/MC/AsmParser/directive_values.s b/test/MC/AsmParser/directive_values.s index c222a304848..98259bdb29d 100644 --- a/test/MC/AsmParser/directive_values.s +++ b/test/MC/AsmParser/directive_values.s @@ -40,3 +40,19 @@ TEST5: .value 8 # CHECK: TEST5: # CHECK: .short 8 + +TEST6: + .byte 'c' + .byte '\'' + .byte '\\' + .byte '\#' + .byte '\t' + .byte '\n' + +# CHECK: TEST6 +# CHECK: .byte 99 +# CHECK: .byte 39 +# CHECK: .byte 92 +# CHECK: .byte 35 +# CHECK: .byte 9 +# CHECK: .byte 10 -- 2.34.1