llvm.org GIT mirror llvm / 1991c59
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 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@241078 91177308-0d34-0410-b5e6-96231b3b80d8 Alex Lorenz 4 years ago
1 changed file(s) with 37 addition(s) and 22 deletion(s). Raw diff Collapse all Expand all
2525 const char *End;
2626
2727 public:
28 Cursor(NoneType) : Ptr(nullptr), End(nullptr) {}
29
2830 explicit Cursor(StringRef Str) {
2931 Ptr = Str.data();
3032 End = Ptr + Str.size();
4446 }
4547
4648 StringRef::iterator location() const { return Ptr; }
49
50 operator bool() const { return Ptr != nullptr; }
4751 };
4852
4953 } // end anonymous namespace
5963 return isalpha(C) || isdigit(C) || C == '_' || C == '-' || C == '.';
6064 }
6165
62 static Cursor lexIdentifier(Cursor C, MIToken &Token) {
66 static Cursor maybeLexIdentifier(Cursor C, MIToken &Token) {
67 if (!isalpha(C.peek()) && C.peek() != '_')
68 return None;
6369 auto Range = C;
6470 while (isIdentifierChar(C.peek()))
6571 C.advance();
6975 return C;
7076 }
7177
72 static Cursor lexMachineBasicBlock(
78 static Cursor maybeLexMachineBasicBlock(
7379 Cursor C, MIToken &Token,
7480 function_ref ErrorCallback) {
81 if (!C.remaining().startswith("%bb."))
82 return None;
7583 auto Range = C;
7684 C.advance(4); // Skip '%bb.'
7785 if (!isdigit(C.peek())) {
95103 return C;
96104 }
97105
98 static Cursor lexPercent(Cursor C, MIToken &Token) {
106 static Cursor maybeLexRegister(Cursor C, MIToken &Token) {
107 if (C.peek() != '%')
108 return None;
99109 auto Range = C;
100110 C.advance(); // Skip '%'
101111 while (isIdentifierChar(C.peek()))
105115 return C;
106116 }
107117
108 static Cursor lexGlobalValue(Cursor C, MIToken &Token) {
118 static Cursor maybeLexGlobalValue(Cursor C, MIToken &Token) {
119 if (C.peek() != '@')
120 return None;
109121 auto Range = C;
110122 C.advance(); // Skip the '@'
111123 // TODO: add support for quoted names.
124136 return C;
125137 }
126138
127 static Cursor lexIntegerLiteral(Cursor C, MIToken &Token) {
139 static Cursor maybeLexIntegerLiteral(Cursor C, MIToken &Token) {
140 if (!isdigit(C.peek()) && (C.peek() != '-' || !isdigit(C.peek(1))))
141 return None;
128142 auto Range = C;
129143 C.advance();
130144 while (isdigit(C.peek()))
145159 }
146160 }
147161
148 static Cursor lexSymbol(Cursor C, MIToken::TokenKind Kind, MIToken &Token) {
162 static Cursor maybeLexSymbol(Cursor C, MIToken &Token) {
163 auto Kind = symbolToken(C.peek());
164 if (Kind == MIToken::Error)
165 return None;
149166 auto Range = C;
150167 C.advance();
151168 Token = MIToken(Kind, Range.upto(C));
161178 return C.remaining();
162179 }
163180
164 auto Char = C.peek();
165 if (isalpha(Char) || Char == '_')
166 return lexIdentifier(C, Token).remaining();
167 if (Char == '%') {
168 if (C.remaining().startswith("%bb."))
169 return lexMachineBasicBlock(C, Token, ErrorCallback).remaining();
170 return lexPercent(C, Token).remaining();
171 }
172 if (Char == '@')
173 return lexGlobalValue(C, Token).remaining();
174 if (isdigit(Char) || (Char == '-' && isdigit(C.peek(1))))
175 return lexIntegerLiteral(C, Token).remaining();
176 MIToken::TokenKind Kind = symbolToken(Char);
177 if (Kind != MIToken::Error)
178 return lexSymbol(C, Kind, Token).remaining();
181 if (Cursor R = maybeLexIdentifier(C, Token))
182 return R.remaining();
183 if (Cursor R = maybeLexMachineBasicBlock(C, Token, ErrorCallback))
184 return R.remaining();
185 if (Cursor R = maybeLexRegister(C, Token))
186 return R.remaining();
187 if (Cursor R = maybeLexGlobalValue(C, Token))
188 return R.remaining();
189 if (Cursor R = maybeLexIntegerLiteral(C, Token))
190 return R.remaining();
191 if (Cursor R = maybeLexSymbol(C, Token))
192 return R.remaining();
193
179194 Token = MIToken(MIToken::Error, C.remaining());
180195 ErrorCallback(C.location(),
181 Twine("unexpected character '") + Twine(Char) + "'");
196 Twine("unexpected character '") + Twine(C.peek()) + "'");
182197 return C.remaining();
183198 }