From: Benjamin Kramer Date: Mon, 17 Aug 2015 14:35:25 +0000 (+0000) Subject: Extend MCAsmLexer so that it can peek forward several tokens X-Git-Url: http://demsky.eecs.uci.edu/git/?a=commitdiff_plain;h=ac67f04f79d556001d2e3f836522ec1d5a8a1f06;p=oota-llvm.git Extend MCAsmLexer so that it can peek forward several tokens This commit adds a virtual `peekTokens()` function to `MCAsmLexer` which can peek forward an arbitrary number of tokens. It also makes the `peekTok()` method call `peekTokens()` method, but only requesting one token. The idea is to better support targets which more more ambiguous assembly syntaxes. Patch by Dylan McKay! git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@245221 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/llvm/MC/MCParser/AsmLexer.h b/include/llvm/MC/MCParser/AsmLexer.h index 62d39b26c86..1bb6d212784 100644 --- a/include/llvm/MC/MCParser/AsmLexer.h +++ b/include/llvm/MC/MCParser/AsmLexer.h @@ -47,7 +47,8 @@ public: StringRef LexUntilEndOfStatement() override; StringRef LexUntilEndOfLine(); - const AsmToken peekTok(bool ShouldSkipSpace = true) override; + size_t peekTokens(MutableArrayRef Buf, + bool ShouldSkipSpace = true) override; bool isAtStartOfComment(const char *Ptr); bool isAtStatementSeparator(const char *Ptr); diff --git a/include/llvm/MC/MCParser/MCAsmLexer.h b/include/llvm/MC/MCParser/MCAsmLexer.h index 71f15b37c33..644dbd90997 100644 --- a/include/llvm/MC/MCParser/MCAsmLexer.h +++ b/include/llvm/MC/MCParser/MCAsmLexer.h @@ -162,7 +162,21 @@ public: } /// Look ahead at the next token to be lexed. - virtual const AsmToken peekTok(bool ShouldSkipSpace = true) = 0; + const AsmToken peekTok(bool ShouldSkipSpace = true) { + AsmToken Tok; + + MutableArrayRef Buf(Tok); + size_t ReadCount = peekTokens(Buf, ShouldSkipSpace); + + assert(ReadCount == 1); + (void)ReadCount; + + return Tok; + } + + /// Look ahead an arbitrary number of tokens. + virtual size_t peekTokens(MutableArrayRef Buf, + bool ShouldSkipSpace = true) = 0; /// Get the current error location const SMLoc &getErrLoc() { diff --git a/lib/MC/MCParser/AsmLexer.cpp b/lib/MC/MCParser/AsmLexer.cpp index b983d9995f4..36c19202685 100644 --- a/lib/MC/MCParser/AsmLexer.cpp +++ b/lib/MC/MCParser/AsmLexer.cpp @@ -436,7 +436,8 @@ StringRef AsmLexer::LexUntilEndOfLine() { return StringRef(TokStart, CurPtr-TokStart); } -const AsmToken AsmLexer::peekTok(bool ShouldSkipSpace) { +size_t AsmLexer::peekTokens(MutableArrayRef Buf, + bool ShouldSkipSpace) { const char *SavedTokStart = TokStart; const char *SavedCurPtr = CurPtr; bool SavedAtStartOfLine = isAtStartOfLine; @@ -446,7 +447,16 @@ const AsmToken AsmLexer::peekTok(bool ShouldSkipSpace) { SMLoc SavedErrLoc = getErrLoc(); SkipSpace = ShouldSkipSpace; - AsmToken Token = LexToken(); + + size_t ReadCount; + for (ReadCount = 0; ReadCount < Buf.size(); ++ReadCount) { + AsmToken Token = LexToken(); + + Buf[ReadCount] = Token; + + if (Token.is(AsmToken::Eof)) + break; + } SetError(SavedErrLoc, SavedErr); @@ -455,7 +465,7 @@ const AsmToken AsmLexer::peekTok(bool ShouldSkipSpace) { CurPtr = SavedCurPtr; TokStart = SavedTokStart; - return Token; + return ReadCount; } bool AsmLexer::isAtStartOfComment(const char *Ptr) {