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 |
| 37 | // TODO: @@foo, etc. |
| 38 | |
| 39 | // Punctuation. |
| 40 | l_paren, r_paren, // ( ) |
| 41 | less, greater, // < > |
| 42 | // TODO: More punctuation. |
| 43 | |
| 44 | // Keywords. |
| 45 | kw_cfgfunc, |
| 46 | kw_extfunc, |
| 47 | kw_mlfunc, |
| 48 | // TODO: More keywords. |
| 49 | }; |
| 50 | |
| 51 | Token(TokenKind kind, StringRef spelling) |
| 52 | : kind(kind), spelling(spelling) {} |
| 53 | |
| 54 | // Return the bytes that make up this token. |
| 55 | StringRef getSpelling() const { return spelling; } |
| 56 | |
| 57 | // Token classification. |
| 58 | TokenKind getKind() const { return kind; } |
| 59 | bool is(TokenKind K) const { return kind == K; } |
| 60 | |
| 61 | bool isAny(TokenKind k1, TokenKind k2) const { |
| 62 | return is(k1) || is(k2); |
| 63 | } |
| 64 | |
| 65 | /// Return true if this token is one of the specified kinds. |
| 66 | template <typename ...T> |
| 67 | bool isAny(TokenKind k1, TokenKind k2, TokenKind k3, T... others) const { |
| 68 | if (is(k1)) |
| 69 | return true; |
| 70 | return isAny(k2, k3, others...); |
| 71 | } |
| 72 | |
| 73 | bool isNot(TokenKind k) const { return kind != k; } |
| 74 | |
| 75 | /// Return true if this token isn't one of the specified kinds. |
| 76 | template <typename ...T> |
| 77 | bool isNot(TokenKind k1, TokenKind k2, T... others) const { |
| 78 | return !isAny(k1, k2, others...); |
| 79 | } |
| 80 | |
| 81 | |
| 82 | /// Location processing. |
| 83 | llvm::SMLoc getLoc() const; |
| 84 | llvm::SMLoc getEndLoc() const; |
| 85 | llvm::SMRange getLocRange() const; |
| 86 | |
| 87 | private: |
| 88 | /// Discriminator that indicates the sort of token this is. |
| 89 | TokenKind kind; |
| 90 | |
| 91 | /// A reference to the entire token contents; this is always a pointer into |
| 92 | /// a memory buffer owned by the source manager. |
| 93 | StringRef spelling; |
| 94 | }; |
| 95 | |
| 96 | } // end namespace mlir |
| 97 | |
| 98 | #endif // MLIR_LIB_PARSER_TOKEN_H |