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, |
| 35 | #define TOK_KEYWORD(SPELLING) kw_##SPELLING, |
| 36 | #include "TokenKinds.def" |
Chris Lattner | e79379a | 2018-06-22 10:39:19 -0700 | [diff] [blame] | 37 | }; |
| 38 | |
Chris Lattner | 8da0c28 | 2018-06-29 11:15:56 -0700 | [diff] [blame^] | 39 | Token(Kind kind, StringRef spelling) |
Chris Lattner | e79379a | 2018-06-22 10:39:19 -0700 | [diff] [blame] | 40 | : kind(kind), spelling(spelling) {} |
| 41 | |
| 42 | // Return the bytes that make up this token. |
| 43 | StringRef getSpelling() const { return spelling; } |
| 44 | |
| 45 | // Token classification. |
Chris Lattner | 8da0c28 | 2018-06-29 11:15:56 -0700 | [diff] [blame^] | 46 | Kind getKind() const { return kind; } |
| 47 | bool is(Kind K) const { return kind == K; } |
Chris Lattner | e79379a | 2018-06-22 10:39:19 -0700 | [diff] [blame] | 48 | |
Chris Lattner | 8da0c28 | 2018-06-29 11:15:56 -0700 | [diff] [blame^] | 49 | bool isAny(Kind k1, Kind k2) const { |
Chris Lattner | e79379a | 2018-06-22 10:39:19 -0700 | [diff] [blame] | 50 | return is(k1) || is(k2); |
| 51 | } |
| 52 | |
| 53 | /// Return true if this token is one of the specified kinds. |
| 54 | template <typename ...T> |
Chris Lattner | 8da0c28 | 2018-06-29 11:15:56 -0700 | [diff] [blame^] | 55 | bool isAny(Kind k1, Kind k2, Kind k3, T... others) const { |
Chris Lattner | e79379a | 2018-06-22 10:39:19 -0700 | [diff] [blame] | 56 | if (is(k1)) |
| 57 | return true; |
| 58 | return isAny(k2, k3, others...); |
| 59 | } |
| 60 | |
Chris Lattner | 8da0c28 | 2018-06-29 11:15:56 -0700 | [diff] [blame^] | 61 | bool isNot(Kind k) const { return kind != k; } |
Chris Lattner | e79379a | 2018-06-22 10:39:19 -0700 | [diff] [blame] | 62 | |
| 63 | /// Return true if this token isn't one of the specified kinds. |
| 64 | template <typename ...T> |
Chris Lattner | 8da0c28 | 2018-06-29 11:15:56 -0700 | [diff] [blame^] | 65 | bool isNot(Kind k1, Kind k2, T... others) const { |
Chris Lattner | e79379a | 2018-06-22 10:39:19 -0700 | [diff] [blame] | 66 | return !isAny(k1, k2, others...); |
| 67 | } |
| 68 | |
Chris Lattner | bb8fafc | 2018-06-22 15:52:02 -0700 | [diff] [blame] | 69 | // Helpers to decode specific sorts of tokens. |
Chris Lattner | e79379a | 2018-06-22 10:39:19 -0700 | [diff] [blame] | 70 | |
Chris Lattner | bb8fafc | 2018-06-22 15:52:02 -0700 | [diff] [blame] | 71 | /// For an integer token, return its value as an unsigned. If it doesn't fit, |
| 72 | /// return None. |
Chris Lattner | ed65a73 | 2018-06-28 20:45:33 -0700 | [diff] [blame] | 73 | Optional<unsigned> getUnsignedIntegerValue() const; |
| 74 | |
| 75 | /// Given a 'string' token, return its value, including removing the quote |
| 76 | /// characters and unescaping the contents of the string. |
| 77 | std::string getStringValue() const; |
Chris Lattner | bb8fafc | 2018-06-22 15:52:02 -0700 | [diff] [blame] | 78 | |
| 79 | // Location processing. |
Chris Lattner | e79379a | 2018-06-22 10:39:19 -0700 | [diff] [blame] | 80 | llvm::SMLoc getLoc() const; |
| 81 | llvm::SMLoc getEndLoc() const; |
| 82 | llvm::SMRange getLocRange() const; |
| 83 | |
Chris Lattner | 8da0c28 | 2018-06-29 11:15:56 -0700 | [diff] [blame^] | 84 | |
| 85 | /// Given a punctuation or keyword token kind, return the spelling of the |
| 86 | /// token as a string. Warning: This will abort on markers, identifiers and |
| 87 | /// literal tokens since they have no fixed spelling. |
| 88 | static StringRef getTokenSpelling(Kind kind); |
| 89 | |
Chris Lattner | e79379a | 2018-06-22 10:39:19 -0700 | [diff] [blame] | 90 | private: |
| 91 | /// Discriminator that indicates the sort of token this is. |
Chris Lattner | 8da0c28 | 2018-06-29 11:15:56 -0700 | [diff] [blame^] | 92 | Kind kind; |
Chris Lattner | e79379a | 2018-06-22 10:39:19 -0700 | [diff] [blame] | 93 | |
| 94 | /// A reference to the entire token contents; this is always a pointer into |
| 95 | /// a memory buffer owned by the source manager. |
| 96 | StringRef spelling; |
| 97 | }; |
| 98 | |
| 99 | } // end namespace mlir |
| 100 | |
| 101 | #endif // MLIR_LIB_PARSER_TOKEN_H |