blob: 4e3b8318eee3b5d7c6f805ec27f247cde5c360ff [file] [log] [blame]
Chris Lattnere79379a2018-06-22 10:39:19 -07001//===- Token.h - MLIR Token Interface ---------------------------*- C++ -*-===//
2//
3// Copyright 2019 The MLIR Authors.
4//
5// Licensed under the Apache License, Version 2.0 (the "License");
6// you may not use this file except in compliance with the License.
7// You may obtain a copy of the License at
8//
9// http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing, software
12// distributed under the License is distributed on an "AS IS" BASIS,
13// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14// See the License for the specific language governing permissions and
15// limitations under the License.
16// =============================================================================
17
18#ifndef MLIR_LIB_PARSER_TOKEN_H
19#define MLIR_LIB_PARSER_TOKEN_H
20
21#include "mlir/Support/LLVM.h"
22#include "llvm/ADT/StringRef.h"
23#include "llvm/Support/SMLoc.h"
24
25namespace mlir {
26
27/// This represents a token in the MLIR syntax.
28class Token {
29public:
Chris Lattner8da0c282018-06-29 11:15:56 -070030 enum Kind {
31#define TOK_MARKER(NAME) NAME,
32#define TOK_IDENTIFIER(NAME) NAME,
33#define TOK_LITERAL(NAME) NAME,
34#define TOK_PUNCTUATION(NAME, SPELLING) NAME,
Uday Bondhugulafaf37dd2018-06-29 18:09:29 -070035#define TOK_OPERATOR(NAME, SPELLING) NAME,
Chris Lattner8da0c282018-06-29 11:15:56 -070036#define TOK_KEYWORD(SPELLING) kw_##SPELLING,
37#include "TokenKinds.def"
Chris Lattnere79379a2018-06-22 10:39:19 -070038 };
39
Chris Lattner8da0c282018-06-29 11:15:56 -070040 Token(Kind kind, StringRef spelling)
Chris Lattnere79379a2018-06-22 10:39:19 -070041 : kind(kind), spelling(spelling) {}
42
43 // Return the bytes that make up this token.
44 StringRef getSpelling() const { return spelling; }
45
46 // Token classification.
Chris Lattner8da0c282018-06-29 11:15:56 -070047 Kind getKind() const { return kind; }
48 bool is(Kind K) const { return kind == K; }
Chris Lattnere79379a2018-06-22 10:39:19 -070049
Chris Lattner8da0c282018-06-29 11:15:56 -070050 bool isAny(Kind k1, Kind k2) const {
Chris Lattnere79379a2018-06-22 10:39:19 -070051 return is(k1) || is(k2);
52 }
53
54 /// Return true if this token is one of the specified kinds.
55 template <typename ...T>
Chris Lattner8da0c282018-06-29 11:15:56 -070056 bool isAny(Kind k1, Kind k2, Kind k3, T... others) const {
Chris Lattnere79379a2018-06-22 10:39:19 -070057 if (is(k1))
58 return true;
59 return isAny(k2, k3, others...);
60 }
61
Chris Lattner8da0c282018-06-29 11:15:56 -070062 bool isNot(Kind k) const { return kind != k; }
Chris Lattnere79379a2018-06-22 10:39:19 -070063
64 /// Return true if this token isn't one of the specified kinds.
65 template <typename ...T>
Chris Lattner8da0c282018-06-29 11:15:56 -070066 bool isNot(Kind k1, Kind k2, T... others) const {
Chris Lattnere79379a2018-06-22 10:39:19 -070067 return !isAny(k1, k2, others...);
68 }
69
Chris Lattner7121b802018-07-04 20:45:39 -070070 /// Return true if this is one of the keyword token kinds (e.g. kw_if).
71 bool isKeyword() const;
72
Chris Lattnerbb8fafc2018-06-22 15:52:02 -070073 // Helpers to decode specific sorts of tokens.
Chris Lattnere79379a2018-06-22 10:39:19 -070074
Chris Lattnerbb8fafc2018-06-22 15:52:02 -070075 /// For an integer token, return its value as an unsigned. If it doesn't fit,
76 /// return None.
Chris Lattnered65a732018-06-28 20:45:33 -070077 Optional<unsigned> getUnsignedIntegerValue() const;
78
Tatiana Shpeisman1da50c42018-07-19 09:52:39 -070079 /// For an integer token, return its value as an uint64_t. If it doesn't fit,
Chris Lattner7121b802018-07-04 20:45:39 -070080 /// return None.
81 Optional<uint64_t> getUInt64IntegerValue() const;
82
Jacques Pienaar84491092018-07-31 17:15:15 -070083 /// For a floatliteral token, return its value as a double. Returns None in
84 /// the case of underflow or overflow.
85 Optional<double> getFloatingPointValue() const;
86
Chris Lattnerf958bbe2018-06-29 22:08:05 -070087 /// For an inttype token, return its bitwidth.
88 Optional<unsigned> getIntTypeBitwidth() const;
89
Chris Lattner6119d382018-07-20 18:41:34 -070090 /// Given a hash_identifier token like #123, try to parse the number out of
91 /// the identifier, returning None if it is a named identifier like #x or
92 /// if the integer doesn't fit.
93 Optional<unsigned> getHashIdentifierNumber() const;
94
Chris Lattnered65a732018-06-28 20:45:33 -070095 /// Given a 'string' token, return its value, including removing the quote
96 /// characters and unescaping the contents of the string.
97 std::string getStringValue() const;
Chris Lattnerbb8fafc2018-06-22 15:52:02 -070098
99 // Location processing.
Chris Lattnere79379a2018-06-22 10:39:19 -0700100 llvm::SMLoc getLoc() const;
101 llvm::SMLoc getEndLoc() const;
102 llvm::SMRange getLocRange() const;
103
Chris Lattner8da0c282018-06-29 11:15:56 -0700104
105 /// Given a punctuation or keyword token kind, return the spelling of the
106 /// token as a string. Warning: This will abort on markers, identifiers and
107 /// literal tokens since they have no fixed spelling.
108 static StringRef getTokenSpelling(Kind kind);
109
Chris Lattnere79379a2018-06-22 10:39:19 -0700110private:
111 /// Discriminator that indicates the sort of token this is.
Chris Lattner8da0c282018-06-29 11:15:56 -0700112 Kind kind;
Chris Lattnere79379a2018-06-22 10:39:19 -0700113
114 /// A reference to the entire token contents; this is always a pointer into
115 /// a memory buffer owned by the source manager.
116 StringRef spelling;
117};
118
119} // end namespace mlir
120
121#endif // MLIR_LIB_PARSER_TOKEN_H