Chris Lattner | e79379a | 2018-06-22 10:39:19 -0700 | [diff] [blame] | 1 | //===- 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 | |
| 25 | namespace mlir { |
| 26 | |
| 27 | /// This represents a token in the MLIR syntax. |
| 28 | class Token { |
| 29 | public: |
| 30 | enum TokenKind { |
| 31 | // Markers |
| 32 | eof, error, |
| 33 | |
| 34 | // Identifiers. |
| 35 | bare_identifier, // foo |
| 36 | at_identifier, // @foo |
MLIR Team | f85a626 | 2018-06-27 11:03:08 -0700 | [diff] [blame] | 37 | affine_map_id, // #foo |
Chris Lattner | e79379a | 2018-06-22 10:39:19 -0700 | [diff] [blame] | 38 | // TODO: @@foo, etc. |
| 39 | |
Chris Lattner | bb8fafc | 2018-06-22 15:52:02 -0700 | [diff] [blame] | 40 | integer, // 42 |
Chris Lattner | ed65a73 | 2018-06-28 20:45:33 -0700 | [diff] [blame^] | 41 | string, // "foo" |
Chris Lattner | bb8fafc | 2018-06-22 15:52:02 -0700 | [diff] [blame] | 42 | |
Chris Lattner | e79379a | 2018-06-22 10:39:19 -0700 | [diff] [blame] | 43 | // Punctuation. |
Chris Lattner | bb8fafc | 2018-06-22 15:52:02 -0700 | [diff] [blame] | 44 | arrow, // -> |
Chris Lattner | 4c95a50 | 2018-06-23 16:03:42 -0700 | [diff] [blame] | 45 | colon, // : |
Chris Lattner | bb8fafc | 2018-06-22 15:52:02 -0700 | [diff] [blame] | 46 | comma, // , |
| 47 | question, // ? |
| 48 | questionquestion, // ?? |
Chris Lattner | e79379a | 2018-06-22 10:39:19 -0700 | [diff] [blame] | 49 | l_paren, r_paren, // ( ) |
Chris Lattner | 4c95a50 | 2018-06-23 16:03:42 -0700 | [diff] [blame] | 50 | l_brace, r_brace, // { } |
Chris Lattner | e79379a | 2018-06-22 10:39:19 -0700 | [diff] [blame] | 51 | less, greater, // < > |
| 52 | // TODO: More punctuation. |
| 53 | |
| 54 | // Keywords. |
Chris Lattner | bb8fafc | 2018-06-22 15:52:02 -0700 | [diff] [blame] | 55 | kw_bf16, |
Chris Lattner | f6d80a0 | 2018-06-24 11:18:29 -0700 | [diff] [blame] | 56 | kw_br, |
Chris Lattner | e79379a | 2018-06-22 10:39:19 -0700 | [diff] [blame] | 57 | kw_cfgfunc, |
| 58 | kw_extfunc, |
Chris Lattner | bb8fafc | 2018-06-22 15:52:02 -0700 | [diff] [blame] | 59 | kw_f16, |
| 60 | kw_f32, |
| 61 | kw_f64, |
| 62 | kw_i1, |
| 63 | kw_i16, |
| 64 | kw_i32, |
| 65 | kw_i64, |
| 66 | kw_i8, |
| 67 | kw_int, |
| 68 | kw_memref, |
Chris Lattner | e79379a | 2018-06-22 10:39:19 -0700 | [diff] [blame] | 69 | kw_mlfunc, |
Chris Lattner | 4c95a50 | 2018-06-23 16:03:42 -0700 | [diff] [blame] | 70 | kw_return, |
Chris Lattner | bb8fafc | 2018-06-22 15:52:02 -0700 | [diff] [blame] | 71 | kw_tensor, |
| 72 | kw_vector, |
Chris Lattner | e79379a | 2018-06-22 10:39:19 -0700 | [diff] [blame] | 73 | }; |
| 74 | |
| 75 | Token(TokenKind kind, StringRef spelling) |
| 76 | : kind(kind), spelling(spelling) {} |
| 77 | |
| 78 | // Return the bytes that make up this token. |
| 79 | StringRef getSpelling() const { return spelling; } |
| 80 | |
| 81 | // Token classification. |
| 82 | TokenKind getKind() const { return kind; } |
| 83 | bool is(TokenKind K) const { return kind == K; } |
| 84 | |
| 85 | bool isAny(TokenKind k1, TokenKind k2) const { |
| 86 | return is(k1) || is(k2); |
| 87 | } |
| 88 | |
| 89 | /// Return true if this token is one of the specified kinds. |
| 90 | template <typename ...T> |
| 91 | bool isAny(TokenKind k1, TokenKind k2, TokenKind k3, T... others) const { |
| 92 | if (is(k1)) |
| 93 | return true; |
| 94 | return isAny(k2, k3, others...); |
| 95 | } |
| 96 | |
| 97 | bool isNot(TokenKind k) const { return kind != k; } |
| 98 | |
| 99 | /// Return true if this token isn't one of the specified kinds. |
| 100 | template <typename ...T> |
| 101 | bool isNot(TokenKind k1, TokenKind k2, T... others) const { |
| 102 | return !isAny(k1, k2, others...); |
| 103 | } |
| 104 | |
Chris Lattner | bb8fafc | 2018-06-22 15:52:02 -0700 | [diff] [blame] | 105 | // Helpers to decode specific sorts of tokens. |
Chris Lattner | e79379a | 2018-06-22 10:39:19 -0700 | [diff] [blame] | 106 | |
Chris Lattner | bb8fafc | 2018-06-22 15:52:02 -0700 | [diff] [blame] | 107 | /// For an integer token, return its value as an unsigned. If it doesn't fit, |
| 108 | /// return None. |
Chris Lattner | ed65a73 | 2018-06-28 20:45:33 -0700 | [diff] [blame^] | 109 | Optional<unsigned> getUnsignedIntegerValue() const; |
| 110 | |
| 111 | /// Given a 'string' token, return its value, including removing the quote |
| 112 | /// characters and unescaping the contents of the string. |
| 113 | std::string getStringValue() const; |
Chris Lattner | bb8fafc | 2018-06-22 15:52:02 -0700 | [diff] [blame] | 114 | |
| 115 | // Location processing. |
Chris Lattner | e79379a | 2018-06-22 10:39:19 -0700 | [diff] [blame] | 116 | llvm::SMLoc getLoc() const; |
| 117 | llvm::SMLoc getEndLoc() const; |
| 118 | llvm::SMRange getLocRange() const; |
| 119 | |
| 120 | private: |
| 121 | /// Discriminator that indicates the sort of token this is. |
| 122 | TokenKind kind; |
| 123 | |
| 124 | /// A reference to the entire token contents; this is always a pointer into |
| 125 | /// a memory buffer owned by the source manager. |
| 126 | StringRef spelling; |
| 127 | }; |
| 128 | |
| 129 | } // end namespace mlir |
| 130 | |
| 131 | #endif // MLIR_LIB_PARSER_TOKEN_H |