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: |
Chris Lattner | 8da0c28 | 2018-06-29 11:15:56 -0700 | [diff] [blame] | 30 | 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 Bondhugula | faf37dd | 2018-06-29 18:09:29 -0700 | [diff] [blame] | 35 | #define TOK_OPERATOR(NAME, SPELLING) NAME, |
Chris Lattner | 8da0c28 | 2018-06-29 11:15:56 -0700 | [diff] [blame] | 36 | #define TOK_KEYWORD(SPELLING) kw_##SPELLING, |
| 37 | #include "TokenKinds.def" |
Chris Lattner | e79379a | 2018-06-22 10:39:19 -0700 | [diff] [blame] | 38 | }; |
| 39 | |
Chris Lattner | 8da0c28 | 2018-06-29 11:15:56 -0700 | [diff] [blame] | 40 | Token(Kind kind, StringRef spelling) |
Chris Lattner | e79379a | 2018-06-22 10:39:19 -0700 | [diff] [blame] | 41 | : 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 Lattner | 8da0c28 | 2018-06-29 11:15:56 -0700 | [diff] [blame] | 47 | Kind getKind() const { return kind; } |
| 48 | bool is(Kind K) const { return kind == K; } |
Chris Lattner | e79379a | 2018-06-22 10:39:19 -0700 | [diff] [blame] | 49 | |
Chris Lattner | 8da0c28 | 2018-06-29 11:15:56 -0700 | [diff] [blame] | 50 | bool isAny(Kind k1, Kind k2) const { |
Chris Lattner | e79379a | 2018-06-22 10:39:19 -0700 | [diff] [blame] | 51 | return is(k1) || is(k2); |
| 52 | } |
| 53 | |
| 54 | /// Return true if this token is one of the specified kinds. |
| 55 | template <typename ...T> |
Chris Lattner | 8da0c28 | 2018-06-29 11:15:56 -0700 | [diff] [blame] | 56 | bool isAny(Kind k1, Kind k2, Kind k3, T... others) const { |
Chris Lattner | e79379a | 2018-06-22 10:39:19 -0700 | [diff] [blame] | 57 | if (is(k1)) |
| 58 | return true; |
| 59 | return isAny(k2, k3, others...); |
| 60 | } |
| 61 | |
Chris Lattner | 8da0c28 | 2018-06-29 11:15:56 -0700 | [diff] [blame] | 62 | bool isNot(Kind k) const { return kind != k; } |
Chris Lattner | e79379a | 2018-06-22 10:39:19 -0700 | [diff] [blame] | 63 | |
| 64 | /// Return true if this token isn't one of the specified kinds. |
| 65 | template <typename ...T> |
Chris Lattner | 8da0c28 | 2018-06-29 11:15:56 -0700 | [diff] [blame] | 66 | bool isNot(Kind k1, Kind k2, T... others) const { |
Chris Lattner | e79379a | 2018-06-22 10:39:19 -0700 | [diff] [blame] | 67 | return !isAny(k1, k2, others...); |
| 68 | } |
| 69 | |
Chris Lattner | 7121b80 | 2018-07-04 20:45:39 -0700 | [diff] [blame] | 70 | /// Return true if this is one of the keyword token kinds (e.g. kw_if). |
| 71 | bool isKeyword() const; |
| 72 | |
Chris Lattner | bb8fafc | 2018-06-22 15:52:02 -0700 | [diff] [blame] | 73 | // Helpers to decode specific sorts of tokens. |
Chris Lattner | e79379a | 2018-06-22 10:39:19 -0700 | [diff] [blame] | 74 | |
Chris Lattner | bb8fafc | 2018-06-22 15:52:02 -0700 | [diff] [blame] | 75 | /// For an integer token, return its value as an unsigned. If it doesn't fit, |
| 76 | /// return None. |
Chris Lattner | ed65a73 | 2018-06-28 20:45:33 -0700 | [diff] [blame] | 77 | Optional<unsigned> getUnsignedIntegerValue() const; |
| 78 | |
Tatiana Shpeisman | 1da50c4 | 2018-07-19 09:52:39 -0700 | [diff] [blame] | 79 | /// For an integer token, return its value as an uint64_t. If it doesn't fit, |
Chris Lattner | 7121b80 | 2018-07-04 20:45:39 -0700 | [diff] [blame] | 80 | /// return None. |
| 81 | Optional<uint64_t> getUInt64IntegerValue() const; |
| 82 | |
Jacques Pienaar | 8449109 | 2018-07-31 17:15:15 -0700 | [diff] [blame] | 83 | /// 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 Lattner | f958bbe | 2018-06-29 22:08:05 -0700 | [diff] [blame] | 87 | /// For an inttype token, return its bitwidth. |
| 88 | Optional<unsigned> getIntTypeBitwidth() const; |
| 89 | |
Chris Lattner | 6119d38 | 2018-07-20 18:41:34 -0700 | [diff] [blame] | 90 | /// 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 Lattner | ed65a73 | 2018-06-28 20:45:33 -0700 | [diff] [blame] | 95 | /// 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 Lattner | bb8fafc | 2018-06-22 15:52:02 -0700 | [diff] [blame] | 98 | |
| 99 | // Location processing. |
Chris Lattner | e79379a | 2018-06-22 10:39:19 -0700 | [diff] [blame] | 100 | llvm::SMLoc getLoc() const; |
| 101 | llvm::SMLoc getEndLoc() const; |
| 102 | llvm::SMRange getLocRange() const; |
| 103 | |
Chris Lattner | 8da0c28 | 2018-06-29 11:15:56 -0700 | [diff] [blame] | 104 | |
| 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 Lattner | e79379a | 2018-06-22 10:39:19 -0700 | [diff] [blame] | 110 | private: |
| 111 | /// Discriminator that indicates the sort of token this is. |
Chris Lattner | 8da0c28 | 2018-06-29 11:15:56 -0700 | [diff] [blame] | 112 | Kind kind; |
Chris Lattner | e79379a | 2018-06-22 10:39:19 -0700 | [diff] [blame] | 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 |