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 | |
| 28 | Lexer::Lexer(llvm::SourceMgr &sourceMgr) : sourceMgr(sourceMgr) { |
| 29 | auto bufferID = sourceMgr.getMainFileID(); |
| 30 | curBuffer = sourceMgr.getMemoryBuffer(bufferID)->getBuffer(); |
| 31 | curPtr = curBuffer.begin(); |
| 32 | } |
| 33 | |
| 34 | /// emitError - Emit an error message and return an Token::error token. |
| 35 | Token Lexer::emitError(const char *loc, const Twine &message) { |
| 36 | // TODO(clattner): If/when we want to implement a -verify mode, this will need |
| 37 | // to package up errors into SMDiagnostic and report them. |
| 38 | sourceMgr.PrintMessage(SMLoc::getFromPointer(loc), SourceMgr::DK_Error, |
| 39 | message); |
| 40 | return formToken(Token::error, loc); |
| 41 | } |
| 42 | |
| 43 | Token Lexer::lexToken() { |
| 44 | const char *tokStart = curPtr; |
| 45 | |
| 46 | switch (*curPtr++) { |
| 47 | default: |
| 48 | // Handle bare identifiers. |
| 49 | if (isalpha(curPtr[-1])) |
| 50 | return lexBareIdentifierOrKeyword(tokStart); |
| 51 | |
| 52 | // Unknown character, emit an error. |
| 53 | return emitError(tokStart, "unexpected character"); |
| 54 | |
| 55 | case 0: |
| 56 | // This may either be a nul character in the source file or may be the EOF |
| 57 | // marker that llvm::MemoryBuffer guarantees will be there. |
| 58 | if (curPtr-1 == curBuffer.end()) |
| 59 | return formToken(Token::eof, tokStart); |
| 60 | |
| 61 | LLVM_FALLTHROUGH; |
| 62 | case ' ': |
| 63 | case '\t': |
| 64 | case '\n': |
| 65 | case '\r': |
| 66 | // Ignore whitespace. |
| 67 | return lexToken(); |
| 68 | |
Chris Lattner | 4c95a50 | 2018-06-23 16:03:42 -0700 | [diff] [blame] | 69 | case ':': return formToken(Token::colon, tokStart); |
Chris Lattner | bb8fafc | 2018-06-22 15:52:02 -0700 | [diff] [blame] | 70 | case ',': return formToken(Token::comma, tokStart); |
Chris Lattner | e79379a | 2018-06-22 10:39:19 -0700 | [diff] [blame] | 71 | case '(': return formToken(Token::l_paren, tokStart); |
| 72 | case ')': return formToken(Token::r_paren, tokStart); |
Chris Lattner | 4c95a50 | 2018-06-23 16:03:42 -0700 | [diff] [blame] | 73 | case '{': return formToken(Token::l_brace, tokStart); |
| 74 | case '}': return formToken(Token::r_brace, tokStart); |
Chris Lattner | e79379a | 2018-06-22 10:39:19 -0700 | [diff] [blame] | 75 | case '<': return formToken(Token::less, tokStart); |
| 76 | case '>': return formToken(Token::greater, tokStart); |
| 77 | |
Chris Lattner | bb8fafc | 2018-06-22 15:52:02 -0700 | [diff] [blame] | 78 | case '-': |
| 79 | if (*curPtr == '>') { |
| 80 | ++curPtr; |
| 81 | return formToken(Token::arrow, tokStart); |
| 82 | } |
| 83 | return emitError(tokStart, "unexpected character"); |
| 84 | |
| 85 | case '?': |
| 86 | if (*curPtr == '?') { |
| 87 | ++curPtr; |
| 88 | return formToken(Token::questionquestion, tokStart); |
| 89 | } |
| 90 | |
| 91 | return formToken(Token::question, tokStart); |
| 92 | |
Chris Lattner | e79379a | 2018-06-22 10:39:19 -0700 | [diff] [blame] | 93 | case ';': return lexComment(); |
| 94 | case '@': return lexAtIdentifier(tokStart); |
Chris Lattner | bb8fafc | 2018-06-22 15:52:02 -0700 | [diff] [blame] | 95 | |
| 96 | case '0': case '1': case '2': case '3': case '4': |
| 97 | case '5': case '6': case '7': case '8': case '9': |
| 98 | return lexNumber(tokStart); |
Chris Lattner | e79379a | 2018-06-22 10:39:19 -0700 | [diff] [blame] | 99 | } |
| 100 | } |
| 101 | |
| 102 | /// Lex a comment line, starting with a semicolon. |
| 103 | /// |
| 104 | /// TODO: add a regex for comments here and to the spec. |
| 105 | /// |
| 106 | Token Lexer::lexComment() { |
| 107 | while (true) { |
| 108 | switch (*curPtr++) { |
| 109 | case '\n': |
| 110 | case '\r': |
| 111 | // Newline is end of comment. |
| 112 | return lexToken(); |
| 113 | case 0: |
| 114 | // If this is the end of the buffer, end the comment. |
| 115 | if (curPtr-1 == curBuffer.end()) { |
| 116 | --curPtr; |
| 117 | return lexToken(); |
| 118 | } |
| 119 | LLVM_FALLTHROUGH; |
| 120 | default: |
| 121 | // Skip over other characters. |
| 122 | break; |
| 123 | } |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | /// Lex a bare identifier or keyword that starts with a letter. |
| 128 | /// |
| 129 | /// bare-id ::= letter (letter|digit)* |
| 130 | /// |
| 131 | Token Lexer::lexBareIdentifierOrKeyword(const char *tokStart) { |
| 132 | // Match the rest of the identifier regex: [0-9a-zA-Z]* |
| 133 | while (isalpha(*curPtr) || isdigit(*curPtr)) |
| 134 | ++curPtr; |
| 135 | |
| 136 | // Check to see if this identifier is a keyword. |
| 137 | StringRef spelling(tokStart, curPtr-tokStart); |
| 138 | |
| 139 | Token::TokenKind kind = llvm::StringSwitch<Token::TokenKind>(spelling) |
Chris Lattner | bb8fafc | 2018-06-22 15:52:02 -0700 | [diff] [blame] | 140 | .Case("bf16", Token::kw_bf16) |
Chris Lattner | e79379a | 2018-06-22 10:39:19 -0700 | [diff] [blame] | 141 | .Case("cfgfunc", Token::kw_cfgfunc) |
| 142 | .Case("extfunc", Token::kw_extfunc) |
Chris Lattner | bb8fafc | 2018-06-22 15:52:02 -0700 | [diff] [blame] | 143 | .Case("f16", Token::kw_f16) |
| 144 | .Case("f32", Token::kw_f32) |
| 145 | .Case("f64", Token::kw_f64) |
| 146 | .Case("i1", Token::kw_i1) |
| 147 | .Case("i16", Token::kw_i16) |
| 148 | .Case("i32", Token::kw_i32) |
| 149 | .Case("i64", Token::kw_i64) |
| 150 | .Case("i8", Token::kw_i8) |
| 151 | .Case("int", Token::kw_int) |
| 152 | .Case("memref", Token::kw_memref) |
Chris Lattner | e79379a | 2018-06-22 10:39:19 -0700 | [diff] [blame] | 153 | .Case("mlfunc", Token::kw_mlfunc) |
Chris Lattner | 4c95a50 | 2018-06-23 16:03:42 -0700 | [diff] [blame] | 154 | .Case("return", Token::kw_return) |
Chris Lattner | bb8fafc | 2018-06-22 15:52:02 -0700 | [diff] [blame] | 155 | .Case("tensor", Token::kw_tensor) |
| 156 | .Case("vector", Token::kw_vector) |
Chris Lattner | e79379a | 2018-06-22 10:39:19 -0700 | [diff] [blame] | 157 | .Default(Token::bare_identifier); |
| 158 | |
| 159 | return Token(kind, spelling); |
| 160 | } |
| 161 | |
| 162 | /// Lex an '@foo' identifier. |
| 163 | /// |
| 164 | /// function-id ::= `@` bare-id |
| 165 | /// |
| 166 | Token Lexer::lexAtIdentifier(const char *tokStart) { |
| 167 | // These always start with a letter. |
| 168 | if (!isalpha(*curPtr++)) |
| 169 | return emitError(curPtr-1, "expected letter in @ identifier"); |
| 170 | |
| 171 | while (isalpha(*curPtr) || isdigit(*curPtr)) |
| 172 | ++curPtr; |
| 173 | return formToken(Token::at_identifier, tokStart); |
| 174 | } |
Chris Lattner | bb8fafc | 2018-06-22 15:52:02 -0700 | [diff] [blame] | 175 | |
| 176 | /// Lex an integer literal. |
| 177 | /// |
| 178 | /// integer-literal ::= digit+ | `0x` hex_digit+ |
| 179 | /// |
| 180 | Token Lexer::lexNumber(const char *tokStart) { |
| 181 | assert(isdigit(curPtr[-1])); |
| 182 | |
| 183 | // Handle the hexadecimal case. |
| 184 | if (curPtr[-1] == '0' && *curPtr == 'x') { |
| 185 | ++curPtr; |
| 186 | |
| 187 | if (!isxdigit(*curPtr)) |
| 188 | return emitError(curPtr, "expected hexadecimal digit"); |
| 189 | |
| 190 | while (isxdigit(*curPtr)) |
| 191 | ++curPtr; |
| 192 | |
| 193 | return formToken(Token::integer, tokStart); |
| 194 | } |
| 195 | |
| 196 | // Handle the normal decimal case. |
| 197 | while (isdigit(*curPtr)) |
| 198 | ++curPtr; |
| 199 | |
| 200 | return formToken(Token::integer, tokStart); |
| 201 | } |