blob: 5d224c7cb75b4f66b1756a240ba4d48dbcab5cae [file] [log] [blame]
Chris Lattnere79379a2018-06-22 10:39:19 -07001//===- 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"
24using namespace mlir;
25using llvm::SMLoc;
26using llvm::SourceMgr;
27
28Lexer::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.
35Token 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
43Token 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 Lattner4c95a502018-06-23 16:03:42 -070069 case ':': return formToken(Token::colon, tokStart);
Chris Lattnerbb8fafc2018-06-22 15:52:02 -070070 case ',': return formToken(Token::comma, tokStart);
Chris Lattnere79379a2018-06-22 10:39:19 -070071 case '(': return formToken(Token::l_paren, tokStart);
72 case ')': return formToken(Token::r_paren, tokStart);
Chris Lattner4c95a502018-06-23 16:03:42 -070073 case '{': return formToken(Token::l_brace, tokStart);
74 case '}': return formToken(Token::r_brace, tokStart);
Chris Lattnere79379a2018-06-22 10:39:19 -070075 case '<': return formToken(Token::less, tokStart);
76 case '>': return formToken(Token::greater, tokStart);
77
Chris Lattnerbb8fafc2018-06-22 15:52:02 -070078 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 Lattnere79379a2018-06-22 10:39:19 -070093 case ';': return lexComment();
94 case '@': return lexAtIdentifier(tokStart);
Chris Lattnerbb8fafc2018-06-22 15:52:02 -070095
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 Lattnere79379a2018-06-22 10:39:19 -070099 }
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///
106Token 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///
131Token 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 Lattnerbb8fafc2018-06-22 15:52:02 -0700140 .Case("bf16", Token::kw_bf16)
Chris Lattnere79379a2018-06-22 10:39:19 -0700141 .Case("cfgfunc", Token::kw_cfgfunc)
142 .Case("extfunc", Token::kw_extfunc)
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700143 .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 Lattnere79379a2018-06-22 10:39:19 -0700153 .Case("mlfunc", Token::kw_mlfunc)
Chris Lattner4c95a502018-06-23 16:03:42 -0700154 .Case("return", Token::kw_return)
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700155 .Case("tensor", Token::kw_tensor)
156 .Case("vector", Token::kw_vector)
Chris Lattnere79379a2018-06-22 10:39:19 -0700157 .Default(Token::bare_identifier);
158
159 return Token(kind, spelling);
160}
161
162/// Lex an '@foo' identifier.
163///
164/// function-id ::= `@` bare-id
165///
166Token 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 Lattnerbb8fafc2018-06-22 15:52:02 -0700175
176/// Lex an integer literal.
177///
178/// integer-literal ::= digit+ | `0x` hex_digit+
179///
180Token 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}