Chris Lattner | e79379a | 2018-06-22 10:39:19 -0700 | [diff] [blame] | 1 | //===- Lexer.cpp - MLIR Lexer Implementation ------------------------------===// |
| 2 | // |
| 3 | // Copyright 2019 The MLIR Authors. |
| 4 | // |
| 5 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | // you may not use this file except in compliance with the License. |
| 7 | // You may obtain a copy of the License at |
| 8 | // |
| 9 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | // |
| 11 | // Unless required by applicable law or agreed to in writing, software |
| 12 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | // See the License for the specific language governing permissions and |
| 15 | // limitations under the License. |
| 16 | // ============================================================================= |
| 17 | // |
| 18 | // This file implements the lexer for the MLIR textual form. |
| 19 | // |
| 20 | //===----------------------------------------------------------------------===// |
| 21 | |
| 22 | #include "Lexer.h" |
| 23 | #include "llvm/Support/SourceMgr.h" |
Chris Lattner | 0497c4b | 2018-08-15 09:09:54 -0700 | [diff] [blame^] | 24 | #include <cctype> |
Chris Lattner | e79379a | 2018-06-22 10:39:19 -0700 | [diff] [blame] | 25 | using namespace mlir; |
| 26 | using llvm::SMLoc; |
| 27 | using llvm::SourceMgr; |
| 28 | |
MLIR Team | f85a626 | 2018-06-27 11:03:08 -0700 | [diff] [blame] | 29 | // Returns true if 'c' is an allowable puncuation character: [$._-] |
| 30 | // Returns false otherwise. |
| 31 | static bool isPunct(char c) { |
| 32 | return c == '$' || c == '.' || c == '_' || c == '-'; |
| 33 | } |
| 34 | |
Jacques Pienaar | 0bffd86 | 2018-07-11 13:26:23 -0700 | [diff] [blame] | 35 | Lexer::Lexer(llvm::SourceMgr &sourceMgr, SMDiagnosticHandlerTy errorReporter) |
Jacques Pienaar | 9c411be | 2018-06-24 19:17:35 -0700 | [diff] [blame] | 36 | : sourceMgr(sourceMgr), errorReporter(errorReporter) { |
Chris Lattner | e79379a | 2018-06-22 10:39:19 -0700 | [diff] [blame] | 37 | auto bufferID = sourceMgr.getMainFileID(); |
| 38 | curBuffer = sourceMgr.getMemoryBuffer(bufferID)->getBuffer(); |
| 39 | curPtr = curBuffer.begin(); |
| 40 | } |
| 41 | |
| 42 | /// emitError - Emit an error message and return an Token::error token. |
| 43 | Token Lexer::emitError(const char *loc, const Twine &message) { |
Jacques Pienaar | 9c411be | 2018-06-24 19:17:35 -0700 | [diff] [blame] | 44 | errorReporter(sourceMgr.GetMessage(SMLoc::getFromPointer(loc), |
| 45 | SourceMgr::DK_Error, message)); |
Chris Lattner | e79379a | 2018-06-22 10:39:19 -0700 | [diff] [blame] | 46 | return formToken(Token::error, loc); |
| 47 | } |
| 48 | |
| 49 | Token Lexer::lexToken() { |
| 50 | const char *tokStart = curPtr; |
| 51 | |
| 52 | switch (*curPtr++) { |
| 53 | default: |
| 54 | // Handle bare identifiers. |
| 55 | if (isalpha(curPtr[-1])) |
| 56 | return lexBareIdentifierOrKeyword(tokStart); |
| 57 | |
| 58 | // Unknown character, emit an error. |
| 59 | return emitError(tokStart, "unexpected character"); |
| 60 | |
Chris Lattner | ee0c2ae | 2018-07-29 12:37:35 -0700 | [diff] [blame] | 61 | case '_': |
| 62 | // Handle bare identifiers. |
| 63 | return lexBareIdentifierOrKeyword(tokStart); |
| 64 | |
Chris Lattner | e79379a | 2018-06-22 10:39:19 -0700 | [diff] [blame] | 65 | case 0: |
| 66 | // This may either be a nul character in the source file or may be the EOF |
| 67 | // marker that llvm::MemoryBuffer guarantees will be there. |
| 68 | if (curPtr-1 == curBuffer.end()) |
| 69 | return formToken(Token::eof, tokStart); |
| 70 | |
| 71 | LLVM_FALLTHROUGH; |
| 72 | case ' ': |
| 73 | case '\t': |
| 74 | case '\n': |
| 75 | case '\r': |
| 76 | // Ignore whitespace. |
| 77 | return lexToken(); |
| 78 | |
Chris Lattner | 4c95a50 | 2018-06-23 16:03:42 -0700 | [diff] [blame] | 79 | case ':': return formToken(Token::colon, tokStart); |
Chris Lattner | bb8fafc | 2018-06-22 15:52:02 -0700 | [diff] [blame] | 80 | case ',': return formToken(Token::comma, tokStart); |
Chris Lattner | e79379a | 2018-06-22 10:39:19 -0700 | [diff] [blame] | 81 | case '(': return formToken(Token::l_paren, tokStart); |
| 82 | case ')': return formToken(Token::r_paren, tokStart); |
Chris Lattner | 4c95a50 | 2018-06-23 16:03:42 -0700 | [diff] [blame] | 83 | case '{': return formToken(Token::l_brace, tokStart); |
| 84 | case '}': return formToken(Token::r_brace, tokStart); |
Chris Lattner | 85ee151 | 2018-07-25 11:15:20 -0700 | [diff] [blame] | 85 | case '[': |
| 86 | return formToken(Token::l_square, tokStart); |
| 87 | case ']': |
| 88 | return formToken(Token::r_square, tokStart); |
Chris Lattner | e79379a | 2018-06-22 10:39:19 -0700 | [diff] [blame] | 89 | case '<': return formToken(Token::less, tokStart); |
| 90 | case '>': return formToken(Token::greater, tokStart); |
Uday Bondhugula | faf37dd | 2018-06-29 18:09:29 -0700 | [diff] [blame] | 91 | case '=': return formToken(Token::equal, tokStart); |
Chris Lattner | e79379a | 2018-06-22 10:39:19 -0700 | [diff] [blame] | 92 | |
Uday Bondhugula | faf37dd | 2018-06-29 18:09:29 -0700 | [diff] [blame] | 93 | case '+': return formToken(Token::plus, tokStart); |
| 94 | case '*': return formToken(Token::star, tokStart); |
Chris Lattner | bb8fafc | 2018-06-22 15:52:02 -0700 | [diff] [blame] | 95 | case '-': |
| 96 | if (*curPtr == '>') { |
| 97 | ++curPtr; |
| 98 | return formToken(Token::arrow, tokStart); |
| 99 | } |
Uday Bondhugula | 015cbb1 | 2018-07-03 20:16:08 -0700 | [diff] [blame] | 100 | return formToken(Token::minus, tokStart); |
Chris Lattner | bb8fafc | 2018-06-22 15:52:02 -0700 | [diff] [blame] | 101 | |
| 102 | case '?': |
| 103 | if (*curPtr == '?') { |
| 104 | ++curPtr; |
| 105 | return formToken(Token::questionquestion, tokStart); |
| 106 | } |
| 107 | |
| 108 | return formToken(Token::question, tokStart); |
| 109 | |
Chris Lattner | 3e59f08 | 2018-07-14 23:06:24 -0700 | [diff] [blame] | 110 | case '/': |
| 111 | if (*curPtr == '/') |
| 112 | return lexComment(); |
| 113 | return emitError(tokStart, "unexpected character"); |
| 114 | |
Uday Bondhugula | bc53562 | 2018-08-07 14:24:38 -0700 | [diff] [blame] | 115 | case '@': |
| 116 | if (*curPtr == '@') { |
| 117 | ++curPtr; |
| 118 | return lexDoubleAtIdentifier(tokStart); |
| 119 | } |
| 120 | return lexAtIdentifier(tokStart); |
| 121 | |
Chris Lattner | 78276e3 | 2018-07-07 15:48:26 -0700 | [diff] [blame] | 122 | case '#': |
| 123 | LLVM_FALLTHROUGH; |
| 124 | case '%': |
| 125 | return lexPrefixedIdentifier(tokStart); |
Chris Lattner | ed65a73 | 2018-06-28 20:45:33 -0700 | [diff] [blame] | 126 | case '"': return lexString(tokStart); |
Chris Lattner | bb8fafc | 2018-06-22 15:52:02 -0700 | [diff] [blame] | 127 | |
| 128 | case '0': case '1': case '2': case '3': case '4': |
| 129 | case '5': case '6': case '7': case '8': case '9': |
| 130 | return lexNumber(tokStart); |
Chris Lattner | e79379a | 2018-06-22 10:39:19 -0700 | [diff] [blame] | 131 | } |
| 132 | } |
| 133 | |
| 134 | /// Lex a comment line, starting with a semicolon. |
| 135 | /// |
| 136 | /// TODO: add a regex for comments here and to the spec. |
| 137 | /// |
| 138 | Token Lexer::lexComment() { |
Chris Lattner | 3e59f08 | 2018-07-14 23:06:24 -0700 | [diff] [blame] | 139 | // Advance over the second '/' in a '//' comment. |
| 140 | assert(*curPtr == '/'); |
| 141 | ++curPtr; |
| 142 | |
Chris Lattner | e79379a | 2018-06-22 10:39:19 -0700 | [diff] [blame] | 143 | while (true) { |
| 144 | switch (*curPtr++) { |
| 145 | case '\n': |
| 146 | case '\r': |
| 147 | // Newline is end of comment. |
| 148 | return lexToken(); |
| 149 | case 0: |
| 150 | // If this is the end of the buffer, end the comment. |
| 151 | if (curPtr-1 == curBuffer.end()) { |
| 152 | --curPtr; |
| 153 | return lexToken(); |
| 154 | } |
| 155 | LLVM_FALLTHROUGH; |
| 156 | default: |
| 157 | // Skip over other characters. |
| 158 | break; |
| 159 | } |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | /// Lex a bare identifier or keyword that starts with a letter. |
| 164 | /// |
Jacques Pienaar | 4451c57 | 2018-07-31 15:40:09 -0700 | [diff] [blame] | 165 | /// bare-id ::= (letter|[_]) (letter|digit|[_$.])* |
Chris Lattner | f958bbe | 2018-06-29 22:08:05 -0700 | [diff] [blame] | 166 | /// integer-type ::= `i[1-9][0-9]*` |
Chris Lattner | e79379a | 2018-06-22 10:39:19 -0700 | [diff] [blame] | 167 | /// |
| 168 | Token Lexer::lexBareIdentifierOrKeyword(const char *tokStart) { |
Jacques Pienaar | 4451c57 | 2018-07-31 15:40:09 -0700 | [diff] [blame] | 169 | // Match the rest of the identifier regex: [0-9a-zA-Z_.$]* |
Jacques Pienaar | c0d6930 | 2018-07-27 11:07:12 -0700 | [diff] [blame] | 170 | while (isalpha(*curPtr) || isdigit(*curPtr) || *curPtr == '_' || |
Jacques Pienaar | 4451c57 | 2018-07-31 15:40:09 -0700 | [diff] [blame] | 171 | *curPtr == '$' || *curPtr == '.') |
Chris Lattner | e79379a | 2018-06-22 10:39:19 -0700 | [diff] [blame] | 172 | ++curPtr; |
| 173 | |
| 174 | // Check to see if this identifier is a keyword. |
| 175 | StringRef spelling(tokStart, curPtr-tokStart); |
| 176 | |
Chris Lattner | f958bbe | 2018-06-29 22:08:05 -0700 | [diff] [blame] | 177 | // Check for i123. |
| 178 | if (tokStart[0] == 'i') { |
| 179 | bool allDigits = true; |
| 180 | for (auto c : spelling.drop_front()) |
| 181 | allDigits &= isdigit(c) != 0; |
| 182 | if (allDigits && spelling.size() != 1) |
| 183 | return Token(Token::inttype, spelling); |
| 184 | } |
| 185 | |
Chris Lattner | 8da0c28 | 2018-06-29 11:15:56 -0700 | [diff] [blame] | 186 | Token::Kind kind = llvm::StringSwitch<Token::Kind>(spelling) |
| 187 | #define TOK_KEYWORD(SPELLING) \ |
| 188 | .Case(#SPELLING, Token::kw_##SPELLING) |
| 189 | #include "TokenKinds.def" |
Chris Lattner | e79379a | 2018-06-22 10:39:19 -0700 | [diff] [blame] | 190 | .Default(Token::bare_identifier); |
| 191 | |
| 192 | return Token(kind, spelling); |
| 193 | } |
| 194 | |
| 195 | /// Lex an '@foo' identifier. |
| 196 | /// |
| 197 | /// function-id ::= `@` bare-id |
| 198 | /// |
| 199 | Token Lexer::lexAtIdentifier(const char *tokStart) { |
| 200 | // These always start with a letter. |
| 201 | if (!isalpha(*curPtr++)) |
| 202 | return emitError(curPtr-1, "expected letter in @ identifier"); |
| 203 | |
Chris Lattner | f6d80a0 | 2018-06-24 11:18:29 -0700 | [diff] [blame] | 204 | while (isalpha(*curPtr) || isdigit(*curPtr) || *curPtr == '_') |
Chris Lattner | e79379a | 2018-06-22 10:39:19 -0700 | [diff] [blame] | 205 | ++curPtr; |
| 206 | return formToken(Token::at_identifier, tokStart); |
| 207 | } |
Chris Lattner | bb8fafc | 2018-06-22 15:52:02 -0700 | [diff] [blame] | 208 | |
Uday Bondhugula | bc53562 | 2018-08-07 14:24:38 -0700 | [diff] [blame] | 209 | /// Lex an '@@foo' identifier. |
| 210 | /// |
| 211 | /// function-id ::= `@@` bare-id |
| 212 | /// |
| 213 | Token Lexer::lexDoubleAtIdentifier(const char *tokStart) { |
| 214 | // These always start with a letter. |
| 215 | if (!isalpha(*curPtr++)) |
| 216 | return emitError(curPtr - 1, "expected letter in @@ identifier"); |
| 217 | |
| 218 | while (isalpha(*curPtr) || isdigit(*curPtr) || *curPtr == '_') |
| 219 | ++curPtr; |
| 220 | return formToken(Token::double_at_identifier, tokStart); |
| 221 | } |
| 222 | |
Chris Lattner | 78276e3 | 2018-07-07 15:48:26 -0700 | [diff] [blame] | 223 | /// Lex an identifier that starts with a prefix followed by suffix-id. |
MLIR Team | f85a626 | 2018-06-27 11:03:08 -0700 | [diff] [blame] | 224 | /// |
| 225 | /// affine-map-id ::= `#` suffix-id |
Chris Lattner | 78276e3 | 2018-07-07 15:48:26 -0700 | [diff] [blame] | 226 | /// ssa-id ::= '%' suffix-id |
MLIR Team | f85a626 | 2018-06-27 11:03:08 -0700 | [diff] [blame] | 227 | /// suffix-id ::= digit+ | (letter|id-punct) (letter|id-punct|digit)* |
| 228 | /// |
Chris Lattner | 78276e3 | 2018-07-07 15:48:26 -0700 | [diff] [blame] | 229 | Token Lexer::lexPrefixedIdentifier(const char *tokStart) { |
| 230 | Token::Kind kind; |
| 231 | StringRef errorKind; |
| 232 | switch (*tokStart) { |
| 233 | case '#': |
| 234 | kind = Token::hash_identifier; |
| 235 | errorKind = "invalid affine map name"; |
| 236 | break; |
| 237 | case '%': |
| 238 | kind = Token::percent_identifier; |
| 239 | errorKind = "invalid SSA name"; |
| 240 | break; |
| 241 | default: |
| 242 | llvm_unreachable("invalid caller"); |
| 243 | } |
| 244 | |
MLIR Team | f85a626 | 2018-06-27 11:03:08 -0700 | [diff] [blame] | 245 | // Parse suffix-id. |
| 246 | if (isdigit(*curPtr)) { |
| 247 | // If suffix-id starts with a digit, the rest must be digits. |
| 248 | while (isdigit(*curPtr)) { |
| 249 | ++curPtr; |
| 250 | } |
| 251 | } else if (isalpha(*curPtr) || isPunct(*curPtr)) { |
| 252 | do { |
| 253 | ++curPtr; |
| 254 | } while (isalpha(*curPtr) || isdigit(*curPtr) || isPunct(*curPtr)); |
| 255 | } else { |
Chris Lattner | 78276e3 | 2018-07-07 15:48:26 -0700 | [diff] [blame] | 256 | return emitError(curPtr - 1, errorKind); |
MLIR Team | f85a626 | 2018-06-27 11:03:08 -0700 | [diff] [blame] | 257 | } |
Chris Lattner | 78276e3 | 2018-07-07 15:48:26 -0700 | [diff] [blame] | 258 | |
| 259 | return formToken(kind, tokStart); |
MLIR Team | f85a626 | 2018-06-27 11:03:08 -0700 | [diff] [blame] | 260 | } |
| 261 | |
Jacques Pienaar | 8449109 | 2018-07-31 17:15:15 -0700 | [diff] [blame] | 262 | /// Lex a number literal. |
Chris Lattner | bb8fafc | 2018-06-22 15:52:02 -0700 | [diff] [blame] | 263 | /// |
| 264 | /// integer-literal ::= digit+ | `0x` hex_digit+ |
Jacques Pienaar | 8449109 | 2018-07-31 17:15:15 -0700 | [diff] [blame] | 265 | /// float-literal ::= [-+]?[0-9]+[.][0-9]*([eE][-+]?[0-9]+)? |
Chris Lattner | bb8fafc | 2018-06-22 15:52:02 -0700 | [diff] [blame] | 266 | /// |
| 267 | Token Lexer::lexNumber(const char *tokStart) { |
| 268 | assert(isdigit(curPtr[-1])); |
| 269 | |
| 270 | // Handle the hexadecimal case. |
| 271 | if (curPtr[-1] == '0' && *curPtr == 'x') { |
| 272 | ++curPtr; |
| 273 | |
| 274 | if (!isxdigit(*curPtr)) |
| 275 | return emitError(curPtr, "expected hexadecimal digit"); |
| 276 | |
| 277 | while (isxdigit(*curPtr)) |
| 278 | ++curPtr; |
| 279 | |
| 280 | return formToken(Token::integer, tokStart); |
| 281 | } |
| 282 | |
| 283 | // Handle the normal decimal case. |
| 284 | while (isdigit(*curPtr)) |
| 285 | ++curPtr; |
| 286 | |
Jacques Pienaar | 8449109 | 2018-07-31 17:15:15 -0700 | [diff] [blame] | 287 | if (*curPtr != '.') |
| 288 | return formToken(Token::integer, tokStart); |
| 289 | ++curPtr; |
| 290 | |
| 291 | // Skip over [0-9]*([eE][-+]?[0-9]+)? |
| 292 | while (isdigit(*curPtr)) ++curPtr; |
| 293 | |
| 294 | if (*curPtr == 'e' || *curPtr == 'E') { |
| 295 | if (isdigit(static_cast<unsigned char>(curPtr[1])) || |
| 296 | ((curPtr[1] == '-' || curPtr[1] == '+') && |
| 297 | isdigit(static_cast<unsigned char>(curPtr[2])))) { |
| 298 | curPtr += 2; |
| 299 | while (isdigit(*curPtr)) ++curPtr; |
| 300 | } |
| 301 | } |
| 302 | return formToken(Token::floatliteral, tokStart); |
Chris Lattner | bb8fafc | 2018-06-22 15:52:02 -0700 | [diff] [blame] | 303 | } |
Chris Lattner | ed65a73 | 2018-06-28 20:45:33 -0700 | [diff] [blame] | 304 | |
| 305 | /// Lex a string literal. |
| 306 | /// |
| 307 | /// string-literal ::= '"' [^"\n\f\v\r]* '"' |
| 308 | /// |
| 309 | /// TODO: define escaping rules. |
| 310 | Token Lexer::lexString(const char *tokStart) { |
| 311 | assert(curPtr[-1] == '"'); |
| 312 | |
| 313 | while (1) { |
| 314 | switch (*curPtr++) { |
| 315 | case '"': |
| 316 | return formToken(Token::string, tokStart); |
| 317 | case '0': |
| 318 | // If this is a random nul character in the middle of a string, just |
| 319 | // include it. If it is the end of file, then it is an error. |
| 320 | if (curPtr-1 != curBuffer.end()) |
| 321 | continue; |
| 322 | LLVM_FALLTHROUGH; |
| 323 | case '\n': |
| 324 | case '\v': |
| 325 | case '\f': |
| 326 | return emitError(curPtr-1, "expected '\"' in string literal"); |
James Molloy | 3cdb8aa | 2018-08-14 01:16:45 -0700 | [diff] [blame] | 327 | case '\\': |
Chris Lattner | 0497c4b | 2018-08-15 09:09:54 -0700 | [diff] [blame^] | 328 | // Handle explicitly a few escapes. |
| 329 | if (*curPtr == '"' || *curPtr == '\\' || *curPtr == 'n' || *curPtr == 't') |
James Molloy | 3cdb8aa | 2018-08-14 01:16:45 -0700 | [diff] [blame] | 330 | ++curPtr; |
Chris Lattner | 0497c4b | 2018-08-15 09:09:54 -0700 | [diff] [blame^] | 331 | else if (llvm::isHexDigit(*curPtr) && llvm::isHexDigit(curPtr[1])) |
| 332 | // Support \xx for two hex digits. |
| 333 | curPtr += 2; |
| 334 | else |
| 335 | return emitError(curPtr - 1, "unknown escape in string literal"); |
James Molloy | 3cdb8aa | 2018-08-14 01:16:45 -0700 | [diff] [blame] | 336 | continue; |
Chris Lattner | ed65a73 | 2018-06-28 20:45:33 -0700 | [diff] [blame] | 337 | |
| 338 | default: |
| 339 | continue; |
| 340 | } |
| 341 | } |
| 342 | } |