summaryrefslogtreecommitdiffstats
path: root/llvm/lib
diff options
context:
space:
mode:
authorAlex Lorenz <arphaman@gmail.com>2015-06-30 16:51:29 +0000
committerAlex Lorenz <arphaman@gmail.com>2015-06-30 16:51:29 +0000
commit6c6c46e4dfa9e3e16a7b40bffd90438ce5d38095 (patch)
tree4867db38ea76b9573e4c10dee8ce1c3a2199f85a /llvm/lib
parent3123eff5ebea42904ffb9297e6f0a2eb403ea03d (diff)
downloadbcm5719-llvm-6c6c46e4dfa9e3e16a7b40bffd90438ce5d38095.tar.gz
bcm5719-llvm-6c6c46e4dfa9e3e16a7b40bffd90438ce5d38095.zip
MIR Parser: adopt the 'maybeLex...' pattern. NFC.
This commit refactors the machine instruction lexer so that the lexing functions use the 'maybeLex...' pattern, where they determine if they can lex the current token by themselves. Reviewers: Sean Silva Differential Revision: http://reviews.llvm.org/D10817 llvm-svn: 241078
Diffstat (limited to 'llvm/lib')
-rw-r--r--llvm/lib/CodeGen/MIRParser/MILexer.cpp59
1 files changed, 37 insertions, 22 deletions
diff --git a/llvm/lib/CodeGen/MIRParser/MILexer.cpp b/llvm/lib/CodeGen/MIRParser/MILexer.cpp
index d15b6909f1c..e9b3916a11f 100644
--- a/llvm/lib/CodeGen/MIRParser/MILexer.cpp
+++ b/llvm/lib/CodeGen/MIRParser/MILexer.cpp
@@ -26,6 +26,8 @@ class Cursor {
const char *End;
public:
+ Cursor(NoneType) : Ptr(nullptr), End(nullptr) {}
+
explicit Cursor(StringRef Str) {
Ptr = Str.data();
End = Ptr + Str.size();
@@ -45,6 +47,8 @@ public:
}
StringRef::iterator location() const { return Ptr; }
+
+ operator bool() const { return Ptr != nullptr; }
};
} // end anonymous namespace
@@ -60,7 +64,9 @@ static bool isIdentifierChar(char C) {
return isalpha(C) || isdigit(C) || C == '_' || C == '-' || C == '.';
}
-static Cursor lexIdentifier(Cursor C, MIToken &Token) {
+static Cursor maybeLexIdentifier(Cursor C, MIToken &Token) {
+ if (!isalpha(C.peek()) && C.peek() != '_')
+ return None;
auto Range = C;
while (isIdentifierChar(C.peek()))
C.advance();
@@ -70,9 +76,11 @@ static Cursor lexIdentifier(Cursor C, MIToken &Token) {
return C;
}
-static Cursor lexMachineBasicBlock(
+static Cursor maybeLexMachineBasicBlock(
Cursor C, MIToken &Token,
function_ref<void(StringRef::iterator Loc, const Twine &)> ErrorCallback) {
+ if (!C.remaining().startswith("%bb."))
+ return None;
auto Range = C;
C.advance(4); // Skip '%bb.'
if (!isdigit(C.peek())) {
@@ -96,7 +104,9 @@ static Cursor lexMachineBasicBlock(
return C;
}
-static Cursor lexPercent(Cursor C, MIToken &Token) {
+static Cursor maybeLexRegister(Cursor C, MIToken &Token) {
+ if (C.peek() != '%')
+ return None;
auto Range = C;
C.advance(); // Skip '%'
while (isIdentifierChar(C.peek()))
@@ -106,7 +116,9 @@ static Cursor lexPercent(Cursor C, MIToken &Token) {
return C;
}
-static Cursor lexGlobalValue(Cursor C, MIToken &Token) {
+static Cursor maybeLexGlobalValue(Cursor C, MIToken &Token) {
+ if (C.peek() != '@')
+ return None;
auto Range = C;
C.advance(); // Skip the '@'
// TODO: add support for quoted names.
@@ -125,7 +137,9 @@ static Cursor lexGlobalValue(Cursor C, MIToken &Token) {
return C;
}
-static Cursor lexIntegerLiteral(Cursor C, MIToken &Token) {
+static Cursor maybeLexIntegerLiteral(Cursor C, MIToken &Token) {
+ if (!isdigit(C.peek()) && (C.peek() != '-' || !isdigit(C.peek(1))))
+ return None;
auto Range = C;
C.advance();
while (isdigit(C.peek()))
@@ -146,7 +160,10 @@ static MIToken::TokenKind symbolToken(char C) {
}
}
-static Cursor lexSymbol(Cursor C, MIToken::TokenKind Kind, MIToken &Token) {
+static Cursor maybeLexSymbol(Cursor C, MIToken &Token) {
+ auto Kind = symbolToken(C.peek());
+ if (Kind == MIToken::Error)
+ return None;
auto Range = C;
C.advance();
Token = MIToken(Kind, Range.upto(C));
@@ -162,23 +179,21 @@ StringRef llvm::lexMIToken(
return C.remaining();
}
- auto Char = C.peek();
- if (isalpha(Char) || Char == '_')
- return lexIdentifier(C, Token).remaining();
- if (Char == '%') {
- if (C.remaining().startswith("%bb."))
- return lexMachineBasicBlock(C, Token, ErrorCallback).remaining();
- return lexPercent(C, Token).remaining();
- }
- if (Char == '@')
- return lexGlobalValue(C, Token).remaining();
- if (isdigit(Char) || (Char == '-' && isdigit(C.peek(1))))
- return lexIntegerLiteral(C, Token).remaining();
- MIToken::TokenKind Kind = symbolToken(Char);
- if (Kind != MIToken::Error)
- return lexSymbol(C, Kind, Token).remaining();
+ if (Cursor R = maybeLexIdentifier(C, Token))
+ return R.remaining();
+ if (Cursor R = maybeLexMachineBasicBlock(C, Token, ErrorCallback))
+ return R.remaining();
+ if (Cursor R = maybeLexRegister(C, Token))
+ return R.remaining();
+ if (Cursor R = maybeLexGlobalValue(C, Token))
+ return R.remaining();
+ if (Cursor R = maybeLexIntegerLiteral(C, Token))
+ return R.remaining();
+ if (Cursor R = maybeLexSymbol(C, Token))
+ return R.remaining();
+
Token = MIToken(MIToken::Error, C.remaining());
ErrorCallback(C.location(),
- Twine("unexpected character '") + Twine(Char) + "'");
+ Twine("unexpected character '") + Twine(C.peek()) + "'");
return C.remaining();
}
OpenPOWER on IntegriCloud