github.com/johnnyeven/libtools@v0.0.0-20191126065708-61829c1adf46/third_party/mlir/lib/Parser/Lexer.h (about) 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 25 #include "Token.h" 26 #include "mlir/Parser.h" 27 28 namespace mlir { 29 class Location; 30 31 /// This class breaks up the current file into a token stream. 32 class Lexer { 33 public: 34 explicit Lexer(const llvm::SourceMgr &sourceMgr, MLIRContext *context); 35 36 const llvm::SourceMgr &getSourceMgr() { return sourceMgr; } 37 38 Token lexToken(); 39 40 /// 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 48 private: 49 // Helpers. 50 Token formToken(Token::Kind kind, const char *tokStart) { 51 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 lexAtIdentifier(const char *tokStart); 58 Token lexBareIdentifierOrKeyword(const char *tokStart); 59 Token lexComment(); 60 Token lexEllipsis(const char *tokStart); 61 Token lexNumber(const char *tokStart); 62 Token lexPrefixedIdentifier(const char *tokStart); 63 Token lexString(const char *tokStart); 64 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; 73 }; 74 75 } // end namespace mlir 76 77 #endif // MLIR_LIB_PARSER_LEXER_H