blob: 220860c2599104b611a98d5a41a33690ee105252 [file] [log] [blame]
Chris Lattnere79379a2018-06-22 10:39:19 -07001//===- Lexer.h - MLIR Lexer Interface ---------------------------*- C++ -*-===//
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 declares the MLIR Lexer class.
19//
20//===----------------------------------------------------------------------===//
21
22#ifndef MLIR_LIB_PARSER_LEXER_H
23#define MLIR_LIB_PARSER_LEXER_H
24
Jacques Pienaar9c411be2018-06-24 19:17:35 -070025#include "mlir/Parser.h"
Chris Lattnere79379a2018-06-22 10:39:19 -070026#include "Token.h"
27
Chris Lattnere79379a2018-06-22 10:39:19 -070028namespace mlir {
Chris Lattner7879f842018-09-02 22:01:45 -070029class Location;
Chris Lattnere79379a2018-06-22 10:39:19 -070030
31/// This class breaks up the current file into a token stream.
32class Lexer {
Chris Lattnere79379a2018-06-22 10:39:19 -070033public:
Jacques Pienaardadee542018-09-08 18:37:27 -070034 explicit Lexer(const llvm::SourceMgr &sourceMgr, MLIRContext *context);
Chris Lattnere79379a2018-06-22 10:39:19 -070035
Jacques Pienaardadee542018-09-08 18:37:27 -070036 const llvm::SourceMgr &getSourceMgr() { return sourceMgr; }
Chris Lattnere79379a2018-06-22 10:39:19 -070037
Chris Lattner7879f842018-09-02 22:01:45 -070038 Token lexToken();
Chris Lattnere79379a2018-06-22 10:39:19 -070039
Chris Lattner7879f842018-09-02 22:01:45 -070040 /// Encode the specified source location information into a Location object
41 /// for attachment to the IR or error reporting.
42 Location *getEncodedSourceLocation(llvm::SMLoc loc);
43
44 /// Change the position of the lexer cursor. The next token we lex will start
45 /// at the designated point in the input.
46 void resetPointer(const char *newPointer) { curPtr = newPointer; }
47
Chris Lattnere79379a2018-06-22 10:39:19 -070048private:
49 // Helpers.
Chris Lattner8da0c282018-06-29 11:15:56 -070050 Token formToken(Token::Kind kind, const char *tokStart) {
Chris Lattnere79379a2018-06-22 10:39:19 -070051 return Token(kind, StringRef(tokStart, curPtr-tokStart));
52 }
53
54 Token emitError(const char *loc, const Twine &message);
55
56 // Lexer implementation methods.
57 Token lexComment();
58 Token lexBareIdentifierOrKeyword(const char *tokStart);
59 Token lexAtIdentifier(const char *tokStart);
Uday Bondhugulabc535622018-08-07 14:24:38 -070060 Token lexDoubleAtIdentifier(const char *tokStart);
Chris Lattner78276e32018-07-07 15:48:26 -070061 Token lexPrefixedIdentifier(const char *tokStart);
Chris Lattnerbb8fafc2018-06-22 15:52:02 -070062 Token lexNumber(const char *tokStart);
Chris Lattnered65a732018-06-28 20:45:33 -070063 Token lexString(const char *tokStart);
Jacques Pienaardadee542018-09-08 18:37:27 -070064
65 const llvm::SourceMgr &sourceMgr;
66 MLIRContext *context;
67
68 StringRef curBuffer;
69 const char *curPtr;
70
71 Lexer(const Lexer &) = delete;
72 void operator=(const Lexer &) = delete;
Chris Lattnere79379a2018-06-22 10:39:19 -070073};
74
75} // end namespace mlir
76
77#endif // MLIR_LIB_PARSER_LEXER_H