2018-06-23 01:39:19 +08:00
|
|
|
//===- Token.h - MLIR Token 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.
|
|
|
|
// =============================================================================
|
|
|
|
|
|
|
|
#ifndef MLIR_LIB_PARSER_TOKEN_H
|
|
|
|
#define MLIR_LIB_PARSER_TOKEN_H
|
|
|
|
|
|
|
|
#include "mlir/Support/LLVM.h"
|
|
|
|
#include "llvm/ADT/StringRef.h"
|
|
|
|
#include "llvm/Support/SMLoc.h"
|
|
|
|
|
|
|
|
namespace mlir {
|
|
|
|
|
|
|
|
/// This represents a token in the MLIR syntax.
|
|
|
|
class Token {
|
|
|
|
public:
|
|
|
|
enum TokenKind {
|
|
|
|
// Markers
|
|
|
|
eof, error,
|
|
|
|
|
|
|
|
// Identifiers.
|
|
|
|
bare_identifier, // foo
|
|
|
|
at_identifier, // @foo
|
2018-06-28 02:03:08 +08:00
|
|
|
affine_map_id, // #foo
|
2018-06-23 01:39:19 +08:00
|
|
|
// TODO: @@foo, etc.
|
|
|
|
|
2018-06-23 06:52:02 +08:00
|
|
|
integer, // 42
|
2018-06-29 11:45:33 +08:00
|
|
|
string, // "foo"
|
2018-06-23 06:52:02 +08:00
|
|
|
|
2018-06-23 01:39:19 +08:00
|
|
|
// Punctuation.
|
2018-06-23 06:52:02 +08:00
|
|
|
arrow, // ->
|
2018-06-24 07:03:42 +08:00
|
|
|
colon, // :
|
2018-06-23 06:52:02 +08:00
|
|
|
comma, // ,
|
|
|
|
question, // ?
|
|
|
|
questionquestion, // ??
|
2018-06-23 01:39:19 +08:00
|
|
|
l_paren, r_paren, // ( )
|
2018-06-24 07:03:42 +08:00
|
|
|
l_brace, r_brace, // { }
|
2018-06-23 01:39:19 +08:00
|
|
|
less, greater, // < >
|
|
|
|
// TODO: More punctuation.
|
|
|
|
|
|
|
|
// Keywords.
|
2018-06-23 06:52:02 +08:00
|
|
|
kw_bf16,
|
2018-06-25 02:18:29 +08:00
|
|
|
kw_br,
|
2018-06-23 01:39:19 +08:00
|
|
|
kw_cfgfunc,
|
|
|
|
kw_extfunc,
|
2018-06-23 06:52:02 +08:00
|
|
|
kw_f16,
|
|
|
|
kw_f32,
|
|
|
|
kw_f64,
|
|
|
|
kw_i1,
|
|
|
|
kw_i16,
|
|
|
|
kw_i32,
|
|
|
|
kw_i64,
|
|
|
|
kw_i8,
|
|
|
|
kw_int,
|
|
|
|
kw_memref,
|
2018-06-23 01:39:19 +08:00
|
|
|
kw_mlfunc,
|
2018-06-24 07:03:42 +08:00
|
|
|
kw_return,
|
2018-06-23 06:52:02 +08:00
|
|
|
kw_tensor,
|
|
|
|
kw_vector,
|
2018-06-23 01:39:19 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
Token(TokenKind kind, StringRef spelling)
|
|
|
|
: kind(kind), spelling(spelling) {}
|
|
|
|
|
|
|
|
// Return the bytes that make up this token.
|
|
|
|
StringRef getSpelling() const { return spelling; }
|
|
|
|
|
|
|
|
// Token classification.
|
|
|
|
TokenKind getKind() const { return kind; }
|
|
|
|
bool is(TokenKind K) const { return kind == K; }
|
|
|
|
|
|
|
|
bool isAny(TokenKind k1, TokenKind k2) const {
|
|
|
|
return is(k1) || is(k2);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Return true if this token is one of the specified kinds.
|
|
|
|
template <typename ...T>
|
|
|
|
bool isAny(TokenKind k1, TokenKind k2, TokenKind k3, T... others) const {
|
|
|
|
if (is(k1))
|
|
|
|
return true;
|
|
|
|
return isAny(k2, k3, others...);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool isNot(TokenKind k) const { return kind != k; }
|
|
|
|
|
|
|
|
/// Return true if this token isn't one of the specified kinds.
|
|
|
|
template <typename ...T>
|
|
|
|
bool isNot(TokenKind k1, TokenKind k2, T... others) const {
|
|
|
|
return !isAny(k1, k2, others...);
|
|
|
|
}
|
|
|
|
|
2018-06-23 06:52:02 +08:00
|
|
|
// Helpers to decode specific sorts of tokens.
|
|
|
|
|
|
|
|
/// For an integer token, return its value as an unsigned. If it doesn't fit,
|
|
|
|
/// return None.
|
2018-06-29 11:45:33 +08:00
|
|
|
Optional<unsigned> getUnsignedIntegerValue() const;
|
|
|
|
|
|
|
|
/// Given a 'string' token, return its value, including removing the quote
|
|
|
|
/// characters and unescaping the contents of the string.
|
|
|
|
std::string getStringValue() const;
|
2018-06-23 01:39:19 +08:00
|
|
|
|
2018-06-23 06:52:02 +08:00
|
|
|
// Location processing.
|
2018-06-23 01:39:19 +08:00
|
|
|
llvm::SMLoc getLoc() const;
|
|
|
|
llvm::SMLoc getEndLoc() const;
|
|
|
|
llvm::SMRange getLocRange() const;
|
|
|
|
|
|
|
|
private:
|
|
|
|
/// Discriminator that indicates the sort of token this is.
|
|
|
|
TokenKind kind;
|
|
|
|
|
|
|
|
/// A reference to the entire token contents; this is always a pointer into
|
|
|
|
/// a memory buffer owned by the source manager.
|
|
|
|
StringRef spelling;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // end namespace mlir
|
|
|
|
|
|
|
|
#endif // MLIR_LIB_PARSER_TOKEN_H
|