MLIR  22.0.0git
Lexer.h
Go to the documentation of this file.
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_ASMPARSER_LEXER_H
14 #define MLIR_LIB_ASMPARSER_LEXER_H
15 
16 #include "Token.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  AsmParserCodeCompleteContext *codeCompleteContext);
27 
28  const llvm::SourceMgr &getSourceMgr() { return sourceMgr; }
29 
30  Token lexToken();
31 
32  /// Encode the specified source location information into a Location object
33  /// for attachment to the IR or error reporting.
35 
36  /// Change the position of the lexer cursor. The next token we lex will start
37  /// at the designated point in the input.
38  void resetPointer(const char *newPointer) { curPtr = newPointer; }
39 
40  /// Returns the start of the buffer.
41  const char *getBufferBegin() { return curBuffer.data(); }
42 
43  /// Returns the end of the buffer.
44  const char *getBufferEnd() { return curBuffer.end(); }
45 
46  /// Return the code completion location of the lexer, or nullptr if there is
47  /// none.
48  const char *getCodeCompleteLoc() const { return codeCompleteLoc; }
49 
50 private:
51  // Helpers.
52  Token formToken(Token::Kind kind, const char *tokStart) {
53  return Token(kind, StringRef(tokStart, curPtr - tokStart));
54  }
55 
56  Token emitError(const char *loc, const Twine &message);
57 
58  // Lexer implementation methods.
59  Token lexAtIdentifier(const char *tokStart);
60  Token lexBareIdentifierOrKeyword(const char *tokStart);
61  Token lexEllipsis(const char *tokStart);
62  Token lexNumber(const char *tokStart);
63  Token lexPrefixedIdentifier(const char *tokStart);
64  Token lexString(const char *tokStart);
65 
66  /// Skip a comment line, starting with a '//'.
67  void skipComment();
68 
69  const llvm::SourceMgr &sourceMgr;
70  MLIRContext *context;
71 
72  StringRef curBuffer;
73  const char *curPtr;
74 
75  /// An optional code completion point within the input file, used to indicate
76  /// the position of a code completion token.
77  const char *codeCompleteLoc;
78 
79  Lexer(const Lexer &) = delete;
80  void operator=(const Lexer &) = delete;
81 };
82 
83 } // namespace mlir
84 
85 #endif // MLIR_LIB_ASMPARSER_LEXER_H
union mlir::linalg::@1244::ArityGroupAndKind::Kind kind
This class provides an abstract interface into the parser for hooking in code completion events.
Definition: CodeComplete.h:24
This class breaks up the current file into a token stream.
Definition: Lexer.h:23
Token lexToken()
Definition: Lexer.cpp:85
const llvm::SourceMgr & getSourceMgr()
Definition: Lexer.h:28
const char * getBufferEnd()
Returns the end of the buffer.
Definition: Lexer.h:44
Location getEncodedSourceLocation(SMLoc loc)
Encode the specified source location information into a Location object for attachment to the IR or e...
Definition: Lexer.cpp:62
void resetPointer(const char *newPointer)
Change the position of the lexer cursor.
Definition: Lexer.h:38
const char * getBufferBegin()
Returns the start of the buffer.
Definition: Lexer.h:41
const char * getCodeCompleteLoc() const
Return the code completion location of the lexer, or nullptr if there is none.
Definition: Lexer.h:48
Lexer(const llvm::SourceMgr &sourceMgr, MLIRContext *context, AsmParserCodeCompleteContext *codeCompleteContext)
Definition: Lexer.cpp:36
This class defines the main interface for locations in MLIR and acts as a non-nullable wrapper around...
Definition: Location.h:76
MLIRContext is the top-level object for a collection of MLIR operations.
Definition: MLIRContext.h:63
This represents a token in the MLIR syntax.
Definition: Token.h:20
Include the generated interface declarations.