blob: f9cdfb61aedda1a27098d0ee3803e0fec32394ba [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
Jacques Pienaar9c411be2018-06-24 19:17:35 -070025#include "mlir/Parser.h"
Chris Lattnere79379a2018-06-22 10:39:19 -070026#include "Token.h"
27
Chris Lattnere79379a2018-06-22 10:39:19 -070028namespace mlir {
29
30/// This class breaks up the current file into a token stream.
31class Lexer {
32 llvm::SourceMgr &sourceMgr;
Jacques Pienaar0bffd862018-07-11 13:26:23 -070033 const SMDiagnosticHandlerTy errorReporter;
Chris Lattnere79379a2018-06-22 10:39:19 -070034
35 StringRef curBuffer;
36 const char *curPtr;
37
38 Lexer(const Lexer&) = delete;
39 void operator=(const Lexer&) = delete;
40public:
Jacques Pienaar0bffd862018-07-11 13:26:23 -070041 explicit Lexer(llvm::SourceMgr &sourceMgr,
42 SMDiagnosticHandlerTy errorReporter);
Chris Lattnere79379a2018-06-22 10:39:19 -070043
Jacques Pienaar0bffd862018-07-11 13:26:23 -070044 llvm::SourceMgr &getSourceMgr() { return sourceMgr; }
Chris Lattnere79379a2018-06-22 10:39:19 -070045
Jacques Pienaar0bffd862018-07-11 13:26:23 -070046 Token lexToken();
Chris Lattnere79379a2018-06-22 10:39:19 -070047
Jacques Pienaar0bffd862018-07-11 13:26:23 -070048 /// Change the position of the lexer cursor. The next token we lex will start
49 /// at the designated point in the input.
50 void resetPointer(const char *newPointer) { curPtr = newPointer; }
Chris Lattnere79379a2018-06-22 10:39:19 -070051private:
52 // Helpers.
Chris Lattner8da0c282018-06-29 11:15:56 -070053 Token formToken(Token::Kind kind, const char *tokStart) {
Chris Lattnere79379a2018-06-22 10:39:19 -070054 return Token(kind, StringRef(tokStart, curPtr-tokStart));
55 }
56
57 Token emitError(const char *loc, const Twine &message);
58
59 // Lexer implementation methods.
60 Token lexComment();
61 Token lexBareIdentifierOrKeyword(const char *tokStart);
62 Token lexAtIdentifier(const char *tokStart);
Chris Lattner78276e32018-07-07 15:48:26 -070063 Token lexPrefixedIdentifier(const char *tokStart);
Chris Lattnerbb8fafc2018-06-22 15:52:02 -070064 Token lexNumber(const char *tokStart);
Chris Lattnered65a732018-06-28 20:45:33 -070065 Token lexString(const char *tokStart);
Chris Lattnere79379a2018-06-22 10:39:19 -070066};
67
68} // end namespace mlir
69
70#endif // MLIR_LIB_PARSER_LEXER_H