My Project
Lexer.h
Go to the documentation of this file.
1 //===- Lexer.h - MLIR Lexer Interface ---------------------------*- C++ -*-===//
2 //
3 // Part of the MLIR Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file declares the MLIR Lexer class.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #ifndef MLIR_LIB_PARSER_LEXER_H
14 #define MLIR_LIB_PARSER_LEXER_H
15 
16 #include "Token.h"
17 #include "mlir/Parser.h"
18 
19 namespace mlir {
20 class Location;
21 
23 class Lexer {
24 public:
25  explicit Lexer(const llvm::SourceMgr &sourceMgr, MLIRContext *context);
26 
27  const llvm::SourceMgr &getSourceMgr() { return sourceMgr; }
28 
29  Token lexToken();
30 
33  Location getEncodedSourceLocation(llvm::SMLoc loc);
34 
37  void resetPointer(const char *newPointer) { curPtr = newPointer; }
38 
40  const char *getBufferBegin() { return curBuffer.data(); }
41 
42 private:
43  // Helpers.
44  Token formToken(Token::Kind kind, const char *tokStart) {
45  return Token(kind, StringRef(tokStart, curPtr - tokStart));
46  }
47 
48  Token emitError(const char *loc, const Twine &message);
49 
50  // Lexer implementation methods.
51  Token lexAtIdentifier(const char *tokStart);
52  Token lexBareIdentifierOrKeyword(const char *tokStart);
53  Token lexEllipsis(const char *tokStart);
54  Token lexNumber(const char *tokStart);
55  Token lexPrefixedIdentifier(const char *tokStart);
56  Token lexString(const char *tokStart);
57 
59  void skipComment();
60 
61  const llvm::SourceMgr &sourceMgr;
62  MLIRContext *context;
63 
64  StringRef curBuffer;
65  const char *curPtr;
66 
67  Lexer(const Lexer &) = delete;
68  void operator=(const Lexer &) = delete;
69 };
70 
71 } // end namespace mlir
72 
73 #endif // MLIR_LIB_PARSER_LEXER_H
Definition: InferTypeOpInterface.cpp:20
const char * getBufferBegin()
Returns the start of the buffer.
Definition: Lexer.h:40
Lexer(const llvm::SourceMgr &sourceMgr, MLIRContext *context)
Definition: Lexer.cpp:30
Definition: Location.h:52
Token lexToken()
Definition: Lexer.cpp:56
Kind
Definition: Token.h:21
void resetPointer(const char *newPointer)
Definition: Lexer.h:37
const llvm::SourceMgr & getSourceMgr()
Definition: Lexer.h:27
Location getEncodedSourceLocation(llvm::SMLoc loc)
Definition: Lexer.cpp:39
Definition: MLIRContext.h:34
This class breaks up the current file into a token stream.
Definition: Lexer.h:23
This represents a token in the MLIR syntax.
Definition: Token.h:19