2018-06-23 01:39:19 +08:00
|
|
|
//===- Lexer.h - MLIR Lexer Interface ---------------------------*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// Copyright 2019 The MLIR Authors.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
// =============================================================================
|
|
|
|
//
|
|
|
|
// This file declares the MLIR Lexer class.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#ifndef MLIR_LIB_PARSER_LEXER_H
|
|
|
|
#define MLIR_LIB_PARSER_LEXER_H
|
|
|
|
|
2018-06-25 10:17:35 +08:00
|
|
|
#include "mlir/Parser.h"
|
2018-06-23 01:39:19 +08:00
|
|
|
#include "Token.h"
|
|
|
|
|
|
|
|
namespace mlir {
|
|
|
|
|
|
|
|
/// This class breaks up the current file into a token stream.
|
|
|
|
class Lexer {
|
|
|
|
llvm::SourceMgr &sourceMgr;
|
2018-07-12 04:26:23 +08:00
|
|
|
const SMDiagnosticHandlerTy errorReporter;
|
2018-06-23 01:39:19 +08:00
|
|
|
|
|
|
|
StringRef curBuffer;
|
|
|
|
const char *curPtr;
|
|
|
|
|
|
|
|
Lexer(const Lexer&) = delete;
|
|
|
|
void operator=(const Lexer&) = delete;
|
|
|
|
public:
|
2018-07-12 04:26:23 +08:00
|
|
|
explicit Lexer(llvm::SourceMgr &sourceMgr,
|
|
|
|
SMDiagnosticHandlerTy errorReporter);
|
2018-06-23 01:39:19 +08:00
|
|
|
|
2018-07-12 04:26:23 +08:00
|
|
|
llvm::SourceMgr &getSourceMgr() { return sourceMgr; }
|
2018-06-23 01:39:19 +08:00
|
|
|
|
2018-07-12 04:26:23 +08:00
|
|
|
Token lexToken();
|
2018-06-23 01:39:19 +08:00
|
|
|
|
2018-07-12 04:26:23 +08:00
|
|
|
/// Change the position of the lexer cursor. The next token we lex will start
|
|
|
|
/// at the designated point in the input.
|
|
|
|
void resetPointer(const char *newPointer) { curPtr = newPointer; }
|
2018-06-23 01:39:19 +08:00
|
|
|
private:
|
|
|
|
// Helpers.
|
2018-06-30 02:15:56 +08:00
|
|
|
Token formToken(Token::Kind kind, const char *tokStart) {
|
2018-06-23 01:39:19 +08:00
|
|
|
return Token(kind, StringRef(tokStart, curPtr-tokStart));
|
|
|
|
}
|
|
|
|
|
|
|
|
Token emitError(const char *loc, const Twine &message);
|
|
|
|
|
|
|
|
// Lexer implementation methods.
|
|
|
|
Token lexComment();
|
|
|
|
Token lexBareIdentifierOrKeyword(const char *tokStart);
|
|
|
|
Token lexAtIdentifier(const char *tokStart);
|
2018-08-08 05:24:38 +08:00
|
|
|
Token lexDoubleAtIdentifier(const char *tokStart);
|
2018-07-08 06:48:26 +08:00
|
|
|
Token lexPrefixedIdentifier(const char *tokStart);
|
2018-06-23 06:52:02 +08:00
|
|
|
Token lexNumber(const char *tokStart);
|
2018-06-29 11:45:33 +08:00
|
|
|
Token lexString(const char *tokStart);
|
2018-06-23 01:39:19 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
} // end namespace mlir
|
|
|
|
|
|
|
|
#endif // MLIR_LIB_PARSER_LEXER_H
|