• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===- Lexer.h - MLIR Lexer Interface ---------------------------*- C++ -*-===//
2 //
3 // Part of the LLVM 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 
22 /// This class breaks up the current file into a token stream.
23 class Lexer {
24 public:
25   explicit Lexer(const llvm::SourceMgr &sourceMgr, MLIRContext *context);
26 
getSourceMgr()27   const llvm::SourceMgr &getSourceMgr() { return sourceMgr; }
28 
29   Token lexToken();
30 
31   /// Encode the specified source location information into a Location object
32   /// for attachment to the IR or error reporting.
33   Location getEncodedSourceLocation(llvm::SMLoc loc);
34 
35   /// Change the position of the lexer cursor.  The next token we lex will start
36   /// at the designated point in the input.
resetPointer(const char * newPointer)37   void resetPointer(const char *newPointer) { curPtr = newPointer; }
38 
39   /// Returns the start of the buffer.
getBufferBegin()40   const char *getBufferBegin() { return curBuffer.data(); }
41 
42 private:
43   // Helpers.
formToken(Token::Kind kind,const char * tokStart)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 
58   /// Skip a comment line, starting with a '//'.
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
74