blob: 5886c5c387ec142e2a1f2689730c7627ed2394f5 [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
49private:
50 // Helpers.
51 Token formToken(Token::TokenKind kind, const char *tokStart) {
52 return Token(kind, StringRef(tokStart, curPtr-tokStart));
53 }
54
55 Token emitError(const char *loc, const Twine &message);
56
57 // Lexer implementation methods.
58 Token lexComment();
59 Token lexBareIdentifierOrKeyword(const char *tokStart);
60 Token lexAtIdentifier(const char *tokStart);
61};
62
63} // end namespace mlir
64
65#endif // MLIR_LIB_PARSER_LEXER_H