blob: 36b1abdfcb4fbfb3a49676aa8179e7194be65bdc [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:
30 enum TokenKind {
31 // Markers
32 eof, error,
33
34 // Identifiers.
35 bare_identifier, // foo
36 at_identifier, // @foo
37 // TODO: @@foo, etc.
38
Chris Lattnerbb8fafc2018-06-22 15:52:02 -070039 integer, // 42
40
Chris Lattnere79379a2018-06-22 10:39:19 -070041 // Punctuation.
Chris Lattnerbb8fafc2018-06-22 15:52:02 -070042 arrow, // ->
43 comma, // ,
44 question, // ?
45 questionquestion, // ??
Chris Lattnere79379a2018-06-22 10:39:19 -070046 l_paren, r_paren, // ( )
47 less, greater, // < >
48 // TODO: More punctuation.
49
50 // Keywords.
Chris Lattnerbb8fafc2018-06-22 15:52:02 -070051 kw_bf16,
Chris Lattnere79379a2018-06-22 10:39:19 -070052 kw_cfgfunc,
53 kw_extfunc,
Chris Lattnerbb8fafc2018-06-22 15:52:02 -070054 kw_f16,
55 kw_f32,
56 kw_f64,
57 kw_i1,
58 kw_i16,
59 kw_i32,
60 kw_i64,
61 kw_i8,
62 kw_int,
63 kw_memref,
Chris Lattnere79379a2018-06-22 10:39:19 -070064 kw_mlfunc,
Chris Lattnerbb8fafc2018-06-22 15:52:02 -070065 kw_tensor,
66 kw_vector,
Chris Lattnere79379a2018-06-22 10:39:19 -070067 };
68
69 Token(TokenKind kind, StringRef spelling)
70 : kind(kind), spelling(spelling) {}
71
72 // Return the bytes that make up this token.
73 StringRef getSpelling() const { return spelling; }
74
75 // Token classification.
76 TokenKind getKind() const { return kind; }
77 bool is(TokenKind K) const { return kind == K; }
78
79 bool isAny(TokenKind k1, TokenKind k2) const {
80 return is(k1) || is(k2);
81 }
82
83 /// Return true if this token is one of the specified kinds.
84 template <typename ...T>
85 bool isAny(TokenKind k1, TokenKind k2, TokenKind k3, T... others) const {
86 if (is(k1))
87 return true;
88 return isAny(k2, k3, others...);
89 }
90
91 bool isNot(TokenKind k) const { return kind != k; }
92
93 /// Return true if this token isn't one of the specified kinds.
94 template <typename ...T>
95 bool isNot(TokenKind k1, TokenKind k2, T... others) const {
96 return !isAny(k1, k2, others...);
97 }
98
Chris Lattnerbb8fafc2018-06-22 15:52:02 -070099 // Helpers to decode specific sorts of tokens.
Chris Lattnere79379a2018-06-22 10:39:19 -0700100
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700101 /// For an integer token, return its value as an unsigned. If it doesn't fit,
102 /// return None.
103 Optional<unsigned> getUnsignedIntegerValue();
104
105 // Location processing.
Chris Lattnere79379a2018-06-22 10:39:19 -0700106 llvm::SMLoc getLoc() const;
107 llvm::SMLoc getEndLoc() const;
108 llvm::SMRange getLocRange() const;
109
110private:
111 /// Discriminator that indicates the sort of token this is.
112 TokenKind kind;
113
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