github.com/johnnyeven/libtools@v0.0.0-20191126065708-61829c1adf46/third_party/mlir/lib/Parser/Token.h (about) 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 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_OPERATOR(NAME, SPELLING) NAME, 36 #define TOK_KEYWORD(SPELLING) kw_##SPELLING, 37 #include "TokenKinds.def" 38 }; 39 40 Token(Kind kind, StringRef spelling) : kind(kind), spelling(spelling) {} 41 42 // Return the bytes that make up this token. 43 StringRef getSpelling() const { return spelling; } 44 45 // Token classification. 46 Kind getKind() const { return kind; } 47 bool is(Kind K) const { return kind == K; } 48 49 bool isAny(Kind k1, Kind k2) const { return is(k1) || is(k2); } 50 51 /// Return true if this token is one of the specified kinds. 52 template <typename... T> 53 bool isAny(Kind k1, Kind k2, Kind k3, T... others) const { 54 if (is(k1)) 55 return true; 56 return isAny(k2, k3, others...); 57 } 58 59 bool isNot(Kind k) const { return kind != k; } 60 61 /// Return true if this token isn't one of the specified kinds. 62 template <typename... T> bool isNot(Kind k1, Kind k2, T... others) const { 63 return !isAny(k1, k2, others...); 64 } 65 66 /// Return true if this is one of the keyword token kinds (e.g. kw_if). 67 bool isKeyword() const; 68 69 // Helpers to decode specific sorts of tokens. 70 71 /// For an integer token, return its value as an unsigned. If it doesn't fit, 72 /// return None. 73 Optional<unsigned> getUnsignedIntegerValue() const; 74 75 /// For an integer token, return its value as an uint64_t. If it doesn't fit, 76 /// return None. 77 Optional<uint64_t> getUInt64IntegerValue() const; 78 79 /// For a floatliteral token, return its value as a double. Returns None in 80 /// the case of underflow or overflow. 81 Optional<double> getFloatingPointValue() const; 82 83 /// For an inttype token, return its bitwidth. 84 Optional<unsigned> getIntTypeBitwidth() const; 85 86 /// Given a hash_identifier token like #123, try to parse the number out of 87 /// the identifier, returning None if it is a named identifier like #x or 88 /// if the integer doesn't fit. 89 Optional<unsigned> getHashIdentifierNumber() const; 90 91 /// Given a 'string' token, return its value, including removing the quote 92 /// characters and unescaping the contents of the string. 93 std::string getStringValue() const; 94 95 // Location processing. 96 llvm::SMLoc getLoc() const; 97 llvm::SMLoc getEndLoc() const; 98 llvm::SMRange getLocRange() const; 99 100 /// Given a punctuation or keyword token kind, return the spelling of the 101 /// token as a string. Warning: This will abort on markers, identifiers and 102 /// literal tokens since they have no fixed spelling. 103 static StringRef getTokenSpelling(Kind kind); 104 105 private: 106 /// Discriminator that indicates the sort of token this is. 107 Kind kind; 108 109 /// A reference to the entire token contents; this is always a pointer into 110 /// a memory buffer owned by the source manager. 111 StringRef spelling; 112 }; 113 114 } // end namespace mlir 115 116 #endif // MLIR_LIB_PARSER_TOKEN_H