IR: de-duplicate two CmpInst routines (NFC) (#116866)
[llvm-project.git] / mlir / lib / AsmParser / Lexer.h
blob4085a9b73854b922ba5630aebc9bd2005175210b
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.
11 //===----------------------------------------------------------------------===//
13 #ifndef MLIR_LIB_ASMPARSER_LEXER_H
14 #define MLIR_LIB_ASMPARSER_LEXER_H
16 #include "Token.h"
17 #include "mlir/AsmParser/AsmParser.h"
19 namespace mlir {
20 class Location;
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);
28 const llvm::SourceMgr &getSourceMgr() { return sourceMgr; }
30 Token lexToken();
32 /// Encode the specified source location information into a Location object
33 /// for attachment to the IR or error reporting.
34 Location getEncodedSourceLocation(SMLoc loc);
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; }
40 /// Returns the start of the buffer.
41 const char *getBufferBegin() { return curBuffer.data(); }
43 /// Return the code completion location of the lexer, or nullptr if there is
44 /// none.
45 const char *getCodeCompleteLoc() const { return codeCompleteLoc; }
47 private:
48 // Helpers.
49 Token formToken(Token::Kind kind, const char *tokStart) {
50 return Token(kind, StringRef(tokStart, curPtr - tokStart));
53 Token emitError(const char *loc, const Twine &message);
55 // Lexer implementation methods.
56 Token lexAtIdentifier(const char *tokStart);
57 Token lexBareIdentifierOrKeyword(const char *tokStart);
58 Token lexEllipsis(const char *tokStart);
59 Token lexNumber(const char *tokStart);
60 Token lexPrefixedIdentifier(const char *tokStart);
61 Token lexString(const char *tokStart);
63 /// Skip a comment line, starting with a '//'.
64 void skipComment();
66 const llvm::SourceMgr &sourceMgr;
67 MLIRContext *context;
69 StringRef curBuffer;
70 const char *curPtr;
72 /// An optional code completion point within the input file, used to indicate
73 /// the position of a code completion token.
74 const char *codeCompleteLoc;
76 Lexer(const Lexer &) = delete;
77 void operator=(const Lexer &) = delete;
80 } // namespace mlir
82 #endif // MLIR_LIB_ASMPARSER_LEXER_H