blob: 2e792719e0246057eeab66d12df5a8a54daf5e54 [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 Lattnerbb8fafc2018-06-22 15:52:02 -070069 case ',': return formToken(Token::comma, tokStart);
Chris Lattnere79379a2018-06-22 10:39:19 -070070 case '(': return formToken(Token::l_paren, tokStart);
71 case ')': return formToken(Token::r_paren, tokStart);
72 case '<': return formToken(Token::less, tokStart);
73 case '>': return formToken(Token::greater, tokStart);
74
Chris Lattnerbb8fafc2018-06-22 15:52:02 -070075 case '-':
76 if (*curPtr == '>') {
77 ++curPtr;
78 return formToken(Token::arrow, tokStart);
79 }
80 return emitError(tokStart, "unexpected character");
81
82 case '?':
83 if (*curPtr == '?') {
84 ++curPtr;
85 return formToken(Token::questionquestion, tokStart);
86 }
87
88 return formToken(Token::question, tokStart);
89
Chris Lattnere79379a2018-06-22 10:39:19 -070090 case ';': return lexComment();
91 case '@': return lexAtIdentifier(tokStart);
Chris Lattnerbb8fafc2018-06-22 15:52:02 -070092
93 case '0': case '1': case '2': case '3': case '4':
94 case '5': case '6': case '7': case '8': case '9':
95 return lexNumber(tokStart);
Chris Lattnere79379a2018-06-22 10:39:19 -070096 }
97}
98
99/// Lex a comment line, starting with a semicolon.
100///
101/// TODO: add a regex for comments here and to the spec.
102///
103Token Lexer::lexComment() {
104 while (true) {
105 switch (*curPtr++) {
106 case '\n':
107 case '\r':
108 // Newline is end of comment.
109 return lexToken();
110 case 0:
111 // If this is the end of the buffer, end the comment.
112 if (curPtr-1 == curBuffer.end()) {
113 --curPtr;
114 return lexToken();
115 }
116 LLVM_FALLTHROUGH;
117 default:
118 // Skip over other characters.
119 break;
120 }
121 }
122}
123
124/// Lex a bare identifier or keyword that starts with a letter.
125///
126/// bare-id ::= letter (letter|digit)*
127///
128Token Lexer::lexBareIdentifierOrKeyword(const char *tokStart) {
129 // Match the rest of the identifier regex: [0-9a-zA-Z]*
130 while (isalpha(*curPtr) || isdigit(*curPtr))
131 ++curPtr;
132
133 // Check to see if this identifier is a keyword.
134 StringRef spelling(tokStart, curPtr-tokStart);
135
136 Token::TokenKind kind = llvm::StringSwitch<Token::TokenKind>(spelling)
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700137 .Case("bf16", Token::kw_bf16)
Chris Lattnere79379a2018-06-22 10:39:19 -0700138 .Case("cfgfunc", Token::kw_cfgfunc)
139 .Case("extfunc", Token::kw_extfunc)
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700140 .Case("f16", Token::kw_f16)
141 .Case("f32", Token::kw_f32)
142 .Case("f64", Token::kw_f64)
143 .Case("i1", Token::kw_i1)
144 .Case("i16", Token::kw_i16)
145 .Case("i32", Token::kw_i32)
146 .Case("i64", Token::kw_i64)
147 .Case("i8", Token::kw_i8)
148 .Case("int", Token::kw_int)
149 .Case("memref", Token::kw_memref)
Chris Lattnere79379a2018-06-22 10:39:19 -0700150 .Case("mlfunc", Token::kw_mlfunc)
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700151 .Case("tensor", Token::kw_tensor)
152 .Case("vector", Token::kw_vector)
Chris Lattnere79379a2018-06-22 10:39:19 -0700153 .Default(Token::bare_identifier);
154
155 return Token(kind, spelling);
156}
157
158/// Lex an '@foo' identifier.
159///
160/// function-id ::= `@` bare-id
161///
162Token Lexer::lexAtIdentifier(const char *tokStart) {
163 // These always start with a letter.
164 if (!isalpha(*curPtr++))
165 return emitError(curPtr-1, "expected letter in @ identifier");
166
167 while (isalpha(*curPtr) || isdigit(*curPtr))
168 ++curPtr;
169 return formToken(Token::at_identifier, tokStart);
170}
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700171
172/// Lex an integer literal.
173///
174/// integer-literal ::= digit+ | `0x` hex_digit+
175///
176Token Lexer::lexNumber(const char *tokStart) {
177 assert(isdigit(curPtr[-1]));
178
179 // Handle the hexadecimal case.
180 if (curPtr[-1] == '0' && *curPtr == 'x') {
181 ++curPtr;
182
183 if (!isxdigit(*curPtr))
184 return emitError(curPtr, "expected hexadecimal digit");
185
186 while (isxdigit(*curPtr))
187 ++curPtr;
188
189 return formToken(Token::integer, tokStart);
190 }
191
192 // Handle the normal decimal case.
193 while (isdigit(*curPtr))
194 ++curPtr;
195
196 return formToken(Token::integer, tokStart);
197}