blob: 4f364bc1d351d90c95b5621a77fa3706e1ba7c9d [file] [log] [blame]
Chris Lattnere79379a2018-06-22 10:39:19 -07001//===- 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
27namespace llvm {
28 class SourceMgr;
29}
30
31namespace mlir {
32
33/// This class breaks up the current file into a token stream.
34class Lexer {
35 llvm::SourceMgr &sourceMgr;
36
37 StringRef curBuffer;
38 const char *curPtr;
39
40 Lexer(const Lexer&) = delete;
41 void operator=(const Lexer&) = delete;
42public:
43 explicit Lexer(llvm::SourceMgr &sourceMgr);
44
45 llvm::SourceMgr &getSourceMgr() { return sourceMgr; }
46
47 Token lexToken();
48
Chris Lattnerbb8fafc2018-06-22 15:52:02 -070049 /// Change the position of the lexer cursor. The next token we lex will start
50 /// at the designated point in the input.
51 void resetPointer(const char *newPointer) {
52 curPtr = newPointer;
53 }
Chris Lattnere79379a2018-06-22 10:39:19 -070054private:
55 // Helpers.
56 Token formToken(Token::TokenKind kind, const char *tokStart) {
57 return Token(kind, StringRef(tokStart, curPtr-tokStart));
58 }
59
60 Token emitError(const char *loc, const Twine &message);
61
62 // Lexer implementation methods.
63 Token lexComment();
64 Token lexBareIdentifierOrKeyword(const char *tokStart);
65 Token lexAtIdentifier(const char *tokStart);
Chris Lattnerbb8fafc2018-06-22 15:52:02 -070066 Token lexNumber(const char *tokStart);
Chris Lattnere79379a2018-06-22 10:39:19 -070067};
68
69} // end namespace mlir
70
71#endif // MLIR_LIB_PARSER_LEXER_H