My Project
Token.h
Go to the documentation of this file.
1 //===- Token.h - MLIR Token Interface ---------------------------*- C++ -*-===//
2 //
3 // Part of the MLIR Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #ifndef MLIR_LIB_PARSER_TOKEN_H
10 #define MLIR_LIB_PARSER_TOKEN_H
11 
12 #include "mlir/Support/LLVM.h"
13 #include "llvm/ADT/StringRef.h"
14 #include "llvm/Support/SMLoc.h"
15 
16 namespace mlir {
17 
19 class Token {
20 public:
21  enum Kind {
22 #define TOK_MARKER(NAME) NAME,
23 #define TOK_IDENTIFIER(NAME) NAME,
24 #define TOK_LITERAL(NAME) NAME,
25 #define TOK_PUNCTUATION(NAME, SPELLING) NAME,
26 #define TOK_OPERATOR(NAME, SPELLING) NAME,
27 #define TOK_KEYWORD(SPELLING) kw_##SPELLING,
28 #include "TokenKinds.def"
29  };
30 
31  Token(Kind kind, StringRef spelling) : kind(kind), spelling(spelling) {}
32 
33  // Return the bytes that make up this token.
34  StringRef getSpelling() const { return spelling; }
35 
36  // Token classification.
37  Kind getKind() const { return kind; }
38  bool is(Kind K) const { return kind == K; }
39 
40  bool isAny(Kind k1, Kind k2) const { return is(k1) || is(k2); }
41 
43  template <typename... T>
44  bool isAny(Kind k1, Kind k2, Kind k3, T... others) const {
45  if (is(k1))
46  return true;
47  return isAny(k2, k3, others...);
48  }
49 
50  bool isNot(Kind k) const { return kind != k; }
51 
53  template <typename... T> bool isNot(Kind k1, Kind k2, T... others) const {
54  return !isAny(k1, k2, others...);
55  }
56 
58  bool isKeyword() const;
59 
60  // Helpers to decode specific sorts of tokens.
61 
64  Optional<unsigned> getUnsignedIntegerValue() const;
65 
68  Optional<uint64_t> getUInt64IntegerValue() const;
69 
73 
76 
81 
84  std::string getStringValue() const;
85 
86  // Location processing.
87  llvm::SMLoc getLoc() const;
88  llvm::SMLoc getEndLoc() const;
89  llvm::SMRange getLocRange() const;
90 
94  static StringRef getTokenSpelling(Kind kind);
95 
96 private:
98  Kind kind;
99 
102  StringRef spelling;
103 };
104 
105 } // end namespace mlir
106 
107 #endif // MLIR_LIB_PARSER_TOKEN_H
Definition: InferTypeOpInterface.cpp:20
bool isAny(Kind k1, Kind k2, Kind k3, T... others) const
Return true if this token is one of the specified kinds.
Definition: Token.h:44
bool isNot(Kind k) const
Definition: Token.h:50
bool isKeyword() const
Return true if this is one of the keyword token kinds (e.g. kw_if).
Definition: Token.cpp:146
Definition: LLVM.h:40
bool is(Kind K) const
Definition: Token.h:38
bool isAny(Kind k1, Kind k2) const
Definition: Token.h:40
Optional< unsigned > getIntTypeBitwidth() const
For an inttype token, return its bitwidth.
Definition: Token.cpp:59
llvm::SMLoc getEndLoc() const
Definition: Token.cpp:21
Token(Kind kind, StringRef spelling)
Definition: Token.h:31
std::string getStringValue() const
Definition: Token.cpp:70
Kind
Definition: Token.h:21
Kind getKind() const
Definition: Token.h:37
Optional< unsigned > getUnsignedIntegerValue() const
Definition: Token.cpp:29
StringRef getSpelling() const
Definition: Token.h:34
static StringRef getTokenSpelling(Kind kind)
Definition: Token.cpp:128
llvm::SMLoc getLoc() const
Definition: Token.cpp:19
bool isNot(Kind k1, Kind k2, T... others) const
Return true if this token isn&#39;t one of the specified kinds.
Definition: Token.h:53
Optional< uint64_t > getUInt64IntegerValue() const
Definition: Token.cpp:40
Optional< double > getFloatingPointValue() const
Definition: Token.cpp:51
llvm::SMRange getLocRange() const
Definition: Token.cpp:25
Optional< unsigned > getHashIdentifierNumber() const
Definition: Token.cpp:117
This represents a token in the MLIR syntax.
Definition: Token.h:19