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 {
|
2018-09-03 13:01:45 +08:00
|
|
|
class Location;
|
2018-06-23 01:39:19 +08:00
|
|
|
|
|
|
|
/// This class breaks up the current file into a token stream.
|
|
|
|
class Lexer {
|
|
|
|
public:
|
2018-09-09 09:37:27 +08:00
|
|
|
explicit Lexer(const llvm::SourceMgr &sourceMgr, MLIRContext *context);
|
2018-06-23 01:39:19 +08:00
|
|
|
|
2018-09-09 09:37:27 +08:00
|
|
|
const llvm::SourceMgr &getSourceMgr() { return sourceMgr; }
|
2018-06-23 01:39:19 +08:00
|
|
|
|
2018-09-03 13:01:45 +08:00
|
|
|
Token lexToken();
|
|
|
|
|
|
|
|
/// Encode the specified source location information into a Location object
|
|
|
|
/// for attachment to the IR or error reporting.
|
2018-11-09 04:28:35 +08:00
|
|
|
Location getEncodedSourceLocation(llvm::SMLoc loc);
|
2018-09-03 13:01:45 +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-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-09-09 09:37:27 +08:00
|
|
|
|
|
|
|
const llvm::SourceMgr &sourceMgr;
|
|
|
|
MLIRContext *context;
|
|
|
|
|
|
|
|
StringRef curBuffer;
|
|
|
|
const char *curPtr;
|
|
|
|
|
|
|
|
Lexer(const Lexer &) = delete;
|
|
|
|
void operator=(const Lexer &) = delete;
|
2018-06-23 01:39:19 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
} // end namespace mlir
|
|
|
|
|
|
|
|
#endif // MLIR_LIB_PARSER_LEXER_H
|