150
|
1 //===- Token.h - MLIR Token Interface ---------------------------*- C++ -*-===//
|
|
2 //
|
|
3 // Part of the LLVM 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
|
|
18 /// This represents a token in the MLIR syntax.
|
|
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_KEYWORD(SPELLING) kw_##SPELLING,
|
|
27 #include "TokenKinds.def"
|
|
28 };
|
|
29
|
|
30 Token(Kind kind, StringRef spelling) : kind(kind), spelling(spelling) {}
|
|
31
|
|
32 // Return the bytes that make up this token.
|
|
33 StringRef getSpelling() const { return spelling; }
|
|
34
|
|
35 // Token classification.
|
|
36 Kind getKind() const { return kind; }
|
|
37 bool is(Kind K) const { return kind == K; }
|
|
38
|
|
39 bool isAny(Kind k1, Kind k2) const { return is(k1) || is(k2); }
|
|
40
|
|
41 /// Return true if this token is one of the specified kinds.
|
|
42 template <typename... T>
|
|
43 bool isAny(Kind k1, Kind k2, Kind k3, T... others) const {
|
|
44 if (is(k1))
|
|
45 return true;
|
|
46 return isAny(k2, k3, others...);
|
|
47 }
|
|
48
|
|
49 bool isNot(Kind k) const { return kind != k; }
|
|
50
|
|
51 /// Return true if this token isn't one of the specified kinds.
|
173
|
52 template <typename... T>
|
|
53 bool isNot(Kind k1, Kind k2, T... others) const {
|
150
|
54 return !isAny(k1, k2, others...);
|
|
55 }
|
|
56
|
|
57 /// Return true if this is one of the keyword token kinds (e.g. kw_if).
|
|
58 bool isKeyword() const;
|
|
59
|
|
60 // Helpers to decode specific sorts of tokens.
|
|
61
|
|
62 /// For an integer token, return its value as an unsigned. If it doesn't fit,
|
|
63 /// return None.
|
|
64 Optional<unsigned> getUnsignedIntegerValue() const;
|
|
65
|
|
66 /// For an integer token, return its value as an uint64_t. If it doesn't fit,
|
|
67 /// return None.
|
173
|
68 static Optional<uint64_t> getUInt64IntegerValue(StringRef spelling);
|
|
69 Optional<uint64_t> getUInt64IntegerValue() const {
|
|
70 return getUInt64IntegerValue(getSpelling());
|
|
71 }
|
150
|
72
|
|
73 /// For a floatliteral token, return its value as a double. Returns None in
|
|
74 /// the case of underflow or overflow.
|
|
75 Optional<double> getFloatingPointValue() const;
|
|
76
|
|
77 /// For an inttype token, return its bitwidth.
|
|
78 Optional<unsigned> getIntTypeBitwidth() const;
|
|
79
|
173
|
80 /// For an inttype token, return its signedness semantics: llvm::None means no
|
|
81 /// signedness semantics; true means signed integer type; false means unsigned
|
|
82 /// integer type.
|
|
83 Optional<bool> getIntTypeSignedness() const;
|
|
84
|
150
|
85 /// Given a hash_identifier token like #123, try to parse the number out of
|
|
86 /// the identifier, returning None if it is a named identifier like #x or
|
|
87 /// if the integer doesn't fit.
|
|
88 Optional<unsigned> getHashIdentifierNumber() const;
|
|
89
|
|
90 /// Given a token containing a string literal, return its value, including
|
|
91 /// removing the quote characters and unescaping the contents of the string.
|
|
92 std::string getStringValue() const;
|
|
93
|
|
94 // Location processing.
|
|
95 llvm::SMLoc getLoc() const;
|
|
96 llvm::SMLoc getEndLoc() const;
|
|
97 llvm::SMRange getLocRange() const;
|
|
98
|
|
99 /// Given a punctuation or keyword token kind, return the spelling of the
|
|
100 /// token as a string. Warning: This will abort on markers, identifiers and
|
|
101 /// literal tokens since they have no fixed spelling.
|
|
102 static StringRef getTokenSpelling(Kind kind);
|
|
103
|
|
104 private:
|
|
105 /// Discriminator that indicates the sort of token this is.
|
|
106 Kind kind;
|
|
107
|
|
108 /// A reference to the entire token contents; this is always a pointer into
|
|
109 /// a memory buffer owned by the source manager.
|
|
110 StringRef spelling;
|
|
111 };
|
|
112
|
|
113 } // end namespace mlir
|
|
114
|
|
115 #endif // MLIR_LIB_PARSER_TOKEN_H
|