Chris Lattner | e79379a | 2018-06-22 10:39:19 -0700 | [diff] [blame] | 1 | //===- 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 Pienaar | 9c411be | 2018-06-24 19:17:35 -0700 | [diff] [blame] | 25 | #include "mlir/Parser.h" |
Chris Lattner | e79379a | 2018-06-22 10:39:19 -0700 | [diff] [blame] | 26 | #include "Token.h" |
| 27 | |
Chris Lattner | e79379a | 2018-06-22 10:39:19 -0700 | [diff] [blame] | 28 | namespace mlir { |
| 29 | |
| 30 | /// This class breaks up the current file into a token stream. |
| 31 | class Lexer { |
| 32 | llvm::SourceMgr &sourceMgr; |
Jacques Pienaar | 0bffd86 | 2018-07-11 13:26:23 -0700 | [diff] [blame] | 33 | const SMDiagnosticHandlerTy errorReporter; |
Chris Lattner | e79379a | 2018-06-22 10:39:19 -0700 | [diff] [blame] | 34 | |
| 35 | StringRef curBuffer; |
| 36 | const char *curPtr; |
| 37 | |
| 38 | Lexer(const Lexer&) = delete; |
| 39 | void operator=(const Lexer&) = delete; |
| 40 | public: |
Jacques Pienaar | 0bffd86 | 2018-07-11 13:26:23 -0700 | [diff] [blame] | 41 | explicit Lexer(llvm::SourceMgr &sourceMgr, |
| 42 | SMDiagnosticHandlerTy errorReporter); |
Chris Lattner | e79379a | 2018-06-22 10:39:19 -0700 | [diff] [blame] | 43 | |
Jacques Pienaar | 0bffd86 | 2018-07-11 13:26:23 -0700 | [diff] [blame] | 44 | llvm::SourceMgr &getSourceMgr() { return sourceMgr; } |
Chris Lattner | e79379a | 2018-06-22 10:39:19 -0700 | [diff] [blame] | 45 | |
Jacques Pienaar | 0bffd86 | 2018-07-11 13:26:23 -0700 | [diff] [blame] | 46 | Token lexToken(); |
Chris Lattner | e79379a | 2018-06-22 10:39:19 -0700 | [diff] [blame] | 47 | |
Jacques Pienaar | 0bffd86 | 2018-07-11 13:26:23 -0700 | [diff] [blame] | 48 | /// Change the position of the lexer cursor. The next token we lex will start |
| 49 | /// at the designated point in the input. |
| 50 | void resetPointer(const char *newPointer) { curPtr = newPointer; } |
Chris Lattner | e79379a | 2018-06-22 10:39:19 -0700 | [diff] [blame] | 51 | private: |
| 52 | // Helpers. |
Chris Lattner | 8da0c28 | 2018-06-29 11:15:56 -0700 | [diff] [blame] | 53 | Token formToken(Token::Kind kind, const char *tokStart) { |
Chris Lattner | e79379a | 2018-06-22 10:39:19 -0700 | [diff] [blame] | 54 | return Token(kind, StringRef(tokStart, curPtr-tokStart)); |
| 55 | } |
| 56 | |
| 57 | Token emitError(const char *loc, const Twine &message); |
| 58 | |
| 59 | // Lexer implementation methods. |
| 60 | Token lexComment(); |
| 61 | Token lexBareIdentifierOrKeyword(const char *tokStart); |
| 62 | Token lexAtIdentifier(const char *tokStart); |
Chris Lattner | 78276e3 | 2018-07-07 15:48:26 -0700 | [diff] [blame] | 63 | Token lexPrefixedIdentifier(const char *tokStart); |
Chris Lattner | bb8fafc | 2018-06-22 15:52:02 -0700 | [diff] [blame] | 64 | Token lexNumber(const char *tokStart); |
Chris Lattner | ed65a73 | 2018-06-28 20:45:33 -0700 | [diff] [blame] | 65 | Token lexString(const char *tokStart); |
Chris Lattner | e79379a | 2018-06-22 10:39:19 -0700 | [diff] [blame] | 66 | }; |
| 67 | |
| 68 | } // end namespace mlir |
| 69 | |
| 70 | #endif // MLIR_LIB_PARSER_LEXER_H |