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" |
| 24 | using namespace mlir; |
| 25 | using llvm::SMLoc; |
| 26 | using llvm::SourceMgr; |
| 27 | |
MLIR Team | f85a626 | 2018-06-27 11:03:08 -0700 | [diff] [blame] | 28 | // Returns true if 'c' is an allowable puncuation character: [$._-] |
| 29 | // Returns false otherwise. |
| 30 | static bool isPunct(char c) { |
| 31 | return c == '$' || c == '.' || c == '_' || c == '-'; |
| 32 | } |
| 33 | |
Jacques Pienaar | 0bffd86 | 2018-07-11 13:26:23 -0700 | [diff] [blame] | 34 | Lexer::Lexer(llvm::SourceMgr &sourceMgr, SMDiagnosticHandlerTy errorReporter) |
Jacques Pienaar | 9c411be | 2018-06-24 19:17:35 -0700 | [diff] [blame] | 35 | : sourceMgr(sourceMgr), errorReporter(errorReporter) { |
Chris Lattner | e79379a | 2018-06-22 10:39:19 -0700 | [diff] [blame] | 36 | auto bufferID = sourceMgr.getMainFileID(); |
| 37 | curBuffer = sourceMgr.getMemoryBuffer(bufferID)->getBuffer(); |
| 38 | curPtr = curBuffer.begin(); |
| 39 | } |
| 40 | |
| 41 | /// emitError - Emit an error message and return an Token::error token. |
| 42 | Token Lexer::emitError(const char *loc, const Twine &message) { |
Jacques Pienaar | 9c411be | 2018-06-24 19:17:35 -0700 | [diff] [blame] | 43 | errorReporter(sourceMgr.GetMessage(SMLoc::getFromPointer(loc), |
| 44 | SourceMgr::DK_Error, message)); |
Chris Lattner | e79379a | 2018-06-22 10:39:19 -0700 | [diff] [blame] | 45 | return formToken(Token::error, loc); |
| 46 | } |
| 47 | |
| 48 | Token Lexer::lexToken() { |
| 49 | const char *tokStart = curPtr; |
| 50 | |
| 51 | switch (*curPtr++) { |
| 52 | default: |
| 53 | // Handle bare identifiers. |
| 54 | if (isalpha(curPtr[-1])) |
| 55 | return lexBareIdentifierOrKeyword(tokStart); |
| 56 | |
| 57 | // Unknown character, emit an error. |
| 58 | return emitError(tokStart, "unexpected character"); |
| 59 | |
| 60 | case 0: |
| 61 | // This may either be a nul character in the source file or may be the EOF |
| 62 | // marker that llvm::MemoryBuffer guarantees will be there. |
| 63 | if (curPtr-1 == curBuffer.end()) |
| 64 | return formToken(Token::eof, tokStart); |
| 65 | |
| 66 | LLVM_FALLTHROUGH; |
| 67 | case ' ': |
| 68 | case '\t': |
| 69 | case '\n': |
| 70 | case '\r': |
| 71 | // Ignore whitespace. |
| 72 | return lexToken(); |
| 73 | |
Chris Lattner | 4c95a50 | 2018-06-23 16:03:42 -0700 | [diff] [blame] | 74 | case ':': return formToken(Token::colon, tokStart); |
Chris Lattner | bb8fafc | 2018-06-22 15:52:02 -0700 | [diff] [blame] | 75 | case ',': return formToken(Token::comma, tokStart); |
Chris Lattner | e79379a | 2018-06-22 10:39:19 -0700 | [diff] [blame] | 76 | case '(': return formToken(Token::l_paren, tokStart); |
| 77 | case ')': return formToken(Token::r_paren, tokStart); |
Chris Lattner | 4c95a50 | 2018-06-23 16:03:42 -0700 | [diff] [blame] | 78 | case '{': return formToken(Token::l_brace, tokStart); |
| 79 | case '}': return formToken(Token::r_brace, tokStart); |
Uday Bondhugula | faf37dd | 2018-06-29 18:09:29 -0700 | [diff] [blame] | 80 | case '[': return formToken(Token::l_bracket, tokStart); |
| 81 | case ']': return formToken(Token::r_bracket, tokStart); |
Chris Lattner | e79379a | 2018-06-22 10:39:19 -0700 | [diff] [blame] | 82 | case '<': return formToken(Token::less, tokStart); |
| 83 | case '>': return formToken(Token::greater, tokStart); |
Uday Bondhugula | faf37dd | 2018-06-29 18:09:29 -0700 | [diff] [blame] | 84 | case '=': return formToken(Token::equal, tokStart); |
Chris Lattner | e79379a | 2018-06-22 10:39:19 -0700 | [diff] [blame] | 85 | |
Uday Bondhugula | faf37dd | 2018-06-29 18:09:29 -0700 | [diff] [blame] | 86 | case '+': return formToken(Token::plus, tokStart); |
| 87 | case '*': return formToken(Token::star, tokStart); |
Chris Lattner | bb8fafc | 2018-06-22 15:52:02 -0700 | [diff] [blame] | 88 | case '-': |
| 89 | if (*curPtr == '>') { |
| 90 | ++curPtr; |
| 91 | return formToken(Token::arrow, tokStart); |
| 92 | } |
Uday Bondhugula | 015cbb1 | 2018-07-03 20:16:08 -0700 | [diff] [blame] | 93 | return formToken(Token::minus, tokStart); |
Chris Lattner | bb8fafc | 2018-06-22 15:52:02 -0700 | [diff] [blame] | 94 | |
| 95 | case '?': |
| 96 | if (*curPtr == '?') { |
| 97 | ++curPtr; |
| 98 | return formToken(Token::questionquestion, tokStart); |
| 99 | } |
| 100 | |
| 101 | return formToken(Token::question, tokStart); |
| 102 | |
Chris Lattner | 3e59f08 | 2018-07-14 23:06:24 -0700 | [diff] [blame] | 103 | case '/': |
| 104 | if (*curPtr == '/') |
| 105 | return lexComment(); |
| 106 | return emitError(tokStart, "unexpected character"); |
| 107 | |
Chris Lattner | e79379a | 2018-06-22 10:39:19 -0700 | [diff] [blame] | 108 | case '@': return lexAtIdentifier(tokStart); |
Chris Lattner | 78276e3 | 2018-07-07 15:48:26 -0700 | [diff] [blame] | 109 | case '#': |
| 110 | LLVM_FALLTHROUGH; |
| 111 | case '%': |
| 112 | return lexPrefixedIdentifier(tokStart); |
Chris Lattner | ed65a73 | 2018-06-28 20:45:33 -0700 | [diff] [blame] | 113 | case '"': return lexString(tokStart); |
Chris Lattner | bb8fafc | 2018-06-22 15:52:02 -0700 | [diff] [blame] | 114 | |
| 115 | case '0': case '1': case '2': case '3': case '4': |
| 116 | case '5': case '6': case '7': case '8': case '9': |
| 117 | return lexNumber(tokStart); |
Chris Lattner | e79379a | 2018-06-22 10:39:19 -0700 | [diff] [blame] | 118 | } |
| 119 | } |
| 120 | |
| 121 | /// Lex a comment line, starting with a semicolon. |
| 122 | /// |
| 123 | /// TODO: add a regex for comments here and to the spec. |
| 124 | /// |
| 125 | Token Lexer::lexComment() { |
Chris Lattner | 3e59f08 | 2018-07-14 23:06:24 -0700 | [diff] [blame] | 126 | // Advance over the second '/' in a '//' comment. |
| 127 | assert(*curPtr == '/'); |
| 128 | ++curPtr; |
| 129 | |
Chris Lattner | e79379a | 2018-06-22 10:39:19 -0700 | [diff] [blame] | 130 | while (true) { |
| 131 | switch (*curPtr++) { |
| 132 | case '\n': |
| 133 | case '\r': |
| 134 | // Newline is end of comment. |
| 135 | return lexToken(); |
| 136 | case 0: |
| 137 | // If this is the end of the buffer, end the comment. |
| 138 | if (curPtr-1 == curBuffer.end()) { |
| 139 | --curPtr; |
| 140 | return lexToken(); |
| 141 | } |
| 142 | LLVM_FALLTHROUGH; |
| 143 | default: |
| 144 | // Skip over other characters. |
| 145 | break; |
| 146 | } |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | /// Lex a bare identifier or keyword that starts with a letter. |
| 151 | /// |
Chris Lattner | f6d80a0 | 2018-06-24 11:18:29 -0700 | [diff] [blame] | 152 | /// bare-id ::= letter (letter|digit|[_])* |
Chris Lattner | f958bbe | 2018-06-29 22:08:05 -0700 | [diff] [blame] | 153 | /// integer-type ::= `i[1-9][0-9]*` |
Chris Lattner | e79379a | 2018-06-22 10:39:19 -0700 | [diff] [blame] | 154 | /// |
| 155 | Token Lexer::lexBareIdentifierOrKeyword(const char *tokStart) { |
Chris Lattner | f6d80a0 | 2018-06-24 11:18:29 -0700 | [diff] [blame] | 156 | // Match the rest of the identifier regex: [0-9a-zA-Z_]* |
| 157 | while (isalpha(*curPtr) || isdigit(*curPtr) || *curPtr == '_') |
Chris Lattner | e79379a | 2018-06-22 10:39:19 -0700 | [diff] [blame] | 158 | ++curPtr; |
| 159 | |
| 160 | // Check to see if this identifier is a keyword. |
| 161 | StringRef spelling(tokStart, curPtr-tokStart); |
| 162 | |
Chris Lattner | f958bbe | 2018-06-29 22:08:05 -0700 | [diff] [blame] | 163 | // Check for i123. |
| 164 | if (tokStart[0] == 'i') { |
| 165 | bool allDigits = true; |
| 166 | for (auto c : spelling.drop_front()) |
| 167 | allDigits &= isdigit(c) != 0; |
| 168 | if (allDigits && spelling.size() != 1) |
| 169 | return Token(Token::inttype, spelling); |
| 170 | } |
| 171 | |
Chris Lattner | 8da0c28 | 2018-06-29 11:15:56 -0700 | [diff] [blame] | 172 | Token::Kind kind = llvm::StringSwitch<Token::Kind>(spelling) |
| 173 | #define TOK_KEYWORD(SPELLING) \ |
| 174 | .Case(#SPELLING, Token::kw_##SPELLING) |
| 175 | #include "TokenKinds.def" |
Chris Lattner | e79379a | 2018-06-22 10:39:19 -0700 | [diff] [blame] | 176 | .Default(Token::bare_identifier); |
| 177 | |
| 178 | return Token(kind, spelling); |
| 179 | } |
| 180 | |
| 181 | /// Lex an '@foo' identifier. |
| 182 | /// |
| 183 | /// function-id ::= `@` bare-id |
| 184 | /// |
| 185 | Token Lexer::lexAtIdentifier(const char *tokStart) { |
| 186 | // These always start with a letter. |
| 187 | if (!isalpha(*curPtr++)) |
| 188 | return emitError(curPtr-1, "expected letter in @ identifier"); |
| 189 | |
Chris Lattner | f6d80a0 | 2018-06-24 11:18:29 -0700 | [diff] [blame] | 190 | while (isalpha(*curPtr) || isdigit(*curPtr) || *curPtr == '_') |
Chris Lattner | e79379a | 2018-06-22 10:39:19 -0700 | [diff] [blame] | 191 | ++curPtr; |
| 192 | return formToken(Token::at_identifier, tokStart); |
| 193 | } |
Chris Lattner | bb8fafc | 2018-06-22 15:52:02 -0700 | [diff] [blame] | 194 | |
Chris Lattner | 78276e3 | 2018-07-07 15:48:26 -0700 | [diff] [blame] | 195 | /// Lex an identifier that starts with a prefix followed by suffix-id. |
MLIR Team | f85a626 | 2018-06-27 11:03:08 -0700 | [diff] [blame] | 196 | /// |
| 197 | /// affine-map-id ::= `#` suffix-id |
Chris Lattner | 78276e3 | 2018-07-07 15:48:26 -0700 | [diff] [blame] | 198 | /// ssa-id ::= '%' suffix-id |
MLIR Team | f85a626 | 2018-06-27 11:03:08 -0700 | [diff] [blame] | 199 | /// suffix-id ::= digit+ | (letter|id-punct) (letter|id-punct|digit)* |
| 200 | /// |
Chris Lattner | 78276e3 | 2018-07-07 15:48:26 -0700 | [diff] [blame] | 201 | Token Lexer::lexPrefixedIdentifier(const char *tokStart) { |
| 202 | Token::Kind kind; |
| 203 | StringRef errorKind; |
| 204 | switch (*tokStart) { |
| 205 | case '#': |
| 206 | kind = Token::hash_identifier; |
| 207 | errorKind = "invalid affine map name"; |
| 208 | break; |
| 209 | case '%': |
| 210 | kind = Token::percent_identifier; |
| 211 | errorKind = "invalid SSA name"; |
| 212 | break; |
| 213 | default: |
| 214 | llvm_unreachable("invalid caller"); |
| 215 | } |
| 216 | |
MLIR Team | f85a626 | 2018-06-27 11:03:08 -0700 | [diff] [blame] | 217 | // Parse suffix-id. |
| 218 | if (isdigit(*curPtr)) { |
| 219 | // If suffix-id starts with a digit, the rest must be digits. |
| 220 | while (isdigit(*curPtr)) { |
| 221 | ++curPtr; |
| 222 | } |
| 223 | } else if (isalpha(*curPtr) || isPunct(*curPtr)) { |
| 224 | do { |
| 225 | ++curPtr; |
| 226 | } while (isalpha(*curPtr) || isdigit(*curPtr) || isPunct(*curPtr)); |
| 227 | } else { |
Chris Lattner | 78276e3 | 2018-07-07 15:48:26 -0700 | [diff] [blame] | 228 | return emitError(curPtr - 1, errorKind); |
MLIR Team | f85a626 | 2018-06-27 11:03:08 -0700 | [diff] [blame] | 229 | } |
Chris Lattner | 78276e3 | 2018-07-07 15:48:26 -0700 | [diff] [blame] | 230 | |
| 231 | return formToken(kind, tokStart); |
MLIR Team | f85a626 | 2018-06-27 11:03:08 -0700 | [diff] [blame] | 232 | } |
| 233 | |
Chris Lattner | bb8fafc | 2018-06-22 15:52:02 -0700 | [diff] [blame] | 234 | /// Lex an integer literal. |
| 235 | /// |
| 236 | /// integer-literal ::= digit+ | `0x` hex_digit+ |
| 237 | /// |
| 238 | Token Lexer::lexNumber(const char *tokStart) { |
| 239 | assert(isdigit(curPtr[-1])); |
| 240 | |
| 241 | // Handle the hexadecimal case. |
| 242 | if (curPtr[-1] == '0' && *curPtr == 'x') { |
| 243 | ++curPtr; |
| 244 | |
| 245 | if (!isxdigit(*curPtr)) |
| 246 | return emitError(curPtr, "expected hexadecimal digit"); |
| 247 | |
| 248 | while (isxdigit(*curPtr)) |
| 249 | ++curPtr; |
| 250 | |
| 251 | return formToken(Token::integer, tokStart); |
| 252 | } |
| 253 | |
| 254 | // Handle the normal decimal case. |
| 255 | while (isdigit(*curPtr)) |
| 256 | ++curPtr; |
| 257 | |
| 258 | return formToken(Token::integer, tokStart); |
| 259 | } |
Chris Lattner | ed65a73 | 2018-06-28 20:45:33 -0700 | [diff] [blame] | 260 | |
| 261 | /// Lex a string literal. |
| 262 | /// |
| 263 | /// string-literal ::= '"' [^"\n\f\v\r]* '"' |
| 264 | /// |
| 265 | /// TODO: define escaping rules. |
| 266 | Token Lexer::lexString(const char *tokStart) { |
| 267 | assert(curPtr[-1] == '"'); |
| 268 | |
| 269 | while (1) { |
| 270 | switch (*curPtr++) { |
| 271 | case '"': |
| 272 | return formToken(Token::string, tokStart); |
| 273 | case '0': |
| 274 | // If this is a random nul character in the middle of a string, just |
| 275 | // include it. If it is the end of file, then it is an error. |
| 276 | if (curPtr-1 != curBuffer.end()) |
| 277 | continue; |
| 278 | LLVM_FALLTHROUGH; |
| 279 | case '\n': |
| 280 | case '\v': |
| 281 | case '\f': |
| 282 | return emitError(curPtr-1, "expected '\"' in string literal"); |
| 283 | |
| 284 | default: |
| 285 | continue; |
| 286 | } |
| 287 | } |
| 288 | } |
Uday Bondhugula | faf37dd | 2018-06-29 18:09:29 -0700 | [diff] [blame] | 289 | |