alokp@chromium.org | 4e4b807 | 2011-08-07 05:36:04 +0000 | [diff] [blame] | 1 | // |
| 2 | // Copyright (c) 2011 The ANGLE Project Authors. All rights reserved. |
| 3 | // Use of this source code is governed by a BSD-style license that can be |
| 4 | // found in the LICENSE file. |
| 5 | // |
| 6 | |
| 7 | #ifndef COMPILER_PREPROCESSOR_TOKEN_H_ |
| 8 | #define COMPILER_PREPROCESSOR_TOKEN_H_ |
| 9 | |
zmo@google.com | 5e75f34 | 2012-04-12 23:47:10 +0000 | [diff] [blame] | 10 | #include <ostream> |
alokp@chromium.org | 4e4b807 | 2011-08-07 05:36:04 +0000 | [diff] [blame] | 11 | #include <string> |
alokp@chromium.org | 3a01d1b | 2011-08-30 05:10:53 +0000 | [diff] [blame] | 12 | |
alokp@chromium.org | 2c958ee | 2012-05-17 20:35:42 +0000 | [diff] [blame] | 13 | #include "SourceLocation.h" |
| 14 | |
alokp@chromium.org | 4e4b807 | 2011-08-07 05:36:04 +0000 | [diff] [blame] | 15 | namespace pp |
| 16 | { |
| 17 | |
alokp@chromium.org | 4b2a522 | 2012-04-03 17:19:42 +0000 | [diff] [blame] | 18 | struct Token |
alokp@chromium.org | 4e4b807 | 2011-08-07 05:36:04 +0000 | [diff] [blame] | 19 | { |
alokp@chromium.org | 4b2a522 | 2012-04-03 17:19:42 +0000 | [diff] [blame] | 20 | enum Type |
| 21 | { |
alokp@chromium.org | 2c958ee | 2012-05-17 20:35:42 +0000 | [diff] [blame] | 22 | LAST = 0, // EOF. |
alokp@chromium.org | 78a3519 | 2012-04-19 17:16:26 +0000 | [diff] [blame] | 23 | |
alokp@chromium.org | 4b2a522 | 2012-04-03 17:19:42 +0000 | [diff] [blame] | 24 | IDENTIFIER = 258, |
alokp@chromium.org | 4e4b807 | 2011-08-07 05:36:04 +0000 | [diff] [blame] | 25 | |
alokp@chromium.org | 4b2a522 | 2012-04-03 17:19:42 +0000 | [diff] [blame] | 26 | CONST_INT, |
| 27 | CONST_FLOAT, |
alokp@chromium.org | b81c401 | 2011-08-21 06:53:11 +0000 | [diff] [blame] | 28 | |
alokp@chromium.org | 3f990c4 | 2012-04-03 19:50:35 +0000 | [diff] [blame] | 29 | OP_INC, |
| 30 | OP_DEC, |
| 31 | OP_LEFT, |
| 32 | OP_RIGHT, |
| 33 | OP_LE, |
| 34 | OP_GE, |
| 35 | OP_EQ, |
| 36 | OP_NE, |
| 37 | OP_AND, |
| 38 | OP_XOR, |
| 39 | OP_OR, |
| 40 | OP_ADD_ASSIGN, |
| 41 | OP_SUB_ASSIGN, |
| 42 | OP_MUL_ASSIGN, |
| 43 | OP_DIV_ASSIGN, |
| 44 | OP_MOD_ASSIGN, |
| 45 | OP_LEFT_ASSIGN, |
| 46 | OP_RIGHT_ASSIGN, |
| 47 | OP_AND_ASSIGN, |
| 48 | OP_XOR_ASSIGN, |
alokp@chromium.org | 432d6fc | 2012-06-27 22:13:21 +0000 | [diff] [blame] | 49 | OP_OR_ASSIGN, |
| 50 | |
| 51 | // Preprocessing token types. |
| 52 | // These types are used by the preprocessor internally. |
| 53 | // Preprocessor clients must not depend or check for them. |
| 54 | PP_HASH, |
| 55 | PP_NUMBER, |
| 56 | PP_OTHER |
alokp@chromium.org | 4b2a522 | 2012-04-03 17:19:42 +0000 | [diff] [blame] | 57 | }; |
alokp@chromium.org | 40da4c5 | 2012-04-12 05:23:19 +0000 | [diff] [blame] | 58 | enum Flags |
| 59 | { |
alokp@chromium.org | 7fc38dd | 2012-06-14 18:23:23 +0000 | [diff] [blame] | 60 | AT_START_OF_LINE = 1 << 0, |
| 61 | HAS_LEADING_SPACE = 1 << 1, |
| 62 | EXPANSION_DISABLED = 1 << 2 |
alokp@chromium.org | 40da4c5 | 2012-04-12 05:23:19 +0000 | [diff] [blame] | 63 | }; |
alokp@chromium.org | 4e4b807 | 2011-08-07 05:36:04 +0000 | [diff] [blame] | 64 | |
alokp@chromium.org | 40da4c5 | 2012-04-12 05:23:19 +0000 | [diff] [blame] | 65 | Token() : type(0), flags(0) { } |
| 66 | |
alokp@chromium.org | 19d7aa6 | 2012-05-31 17:34:05 +0000 | [diff] [blame] | 67 | void reset(); |
| 68 | bool equals(const Token& other) const; |
alokp@chromium.org | 04d7d22 | 2012-05-16 19:24:07 +0000 | [diff] [blame] | 69 | |
alokp@chromium.org | 19d7aa6 | 2012-05-31 17:34:05 +0000 | [diff] [blame] | 70 | // Returns true if this is the first token on line. |
| 71 | // It disregards any leading whitespace. |
| 72 | bool atStartOfLine() const { return (flags & AT_START_OF_LINE) != 0; } |
| 73 | void setAtStartOfLine(bool start); |
alokp@chromium.org | 40da4c5 | 2012-04-12 05:23:19 +0000 | [diff] [blame] | 74 | |
| 75 | bool hasLeadingSpace() const { return (flags & HAS_LEADING_SPACE) != 0; } |
alokp@chromium.org | 19d7aa6 | 2012-05-31 17:34:05 +0000 | [diff] [blame] | 76 | void setHasLeadingSpace(bool space); |
alokp@chromium.org | 40da4c5 | 2012-04-12 05:23:19 +0000 | [diff] [blame] | 77 | |
alokp@chromium.org | 7fc38dd | 2012-06-14 18:23:23 +0000 | [diff] [blame] | 78 | bool expansionDisabled() const { return (flags & EXPANSION_DISABLED) != 0; } |
| 79 | void setExpansionDisabled(bool disable); |
| 80 | |
alokp@chromium.org | 2e81891 | 2012-06-29 21:26:03 +0000 | [diff] [blame] | 81 | // Converts text into numeric value for CONST_INT and CONST_FLOAT token. |
| 82 | // Returns false if the parsed value cannot fit into an int or float. |
| 83 | bool iValue(int* value) const; |
| 84 | bool uValue(unsigned int* value) const; |
| 85 | bool fValue(float* value) const; |
| 86 | |
alokp@chromium.org | 4b2a522 | 2012-04-03 17:19:42 +0000 | [diff] [blame] | 87 | int type; |
alokp@chromium.org | 7fc38dd | 2012-06-14 18:23:23 +0000 | [diff] [blame] | 88 | unsigned int flags; |
alokp@chromium.org | 2c958ee | 2012-05-17 20:35:42 +0000 | [diff] [blame] | 89 | SourceLocation location; |
alokp@chromium.org | 5b6a68e | 2012-06-28 20:29:13 +0000 | [diff] [blame] | 90 | std::string text; |
alokp@chromium.org | 4e4b807 | 2011-08-07 05:36:04 +0000 | [diff] [blame] | 91 | }; |
| 92 | |
alokp@chromium.org | 98d04ec | 2012-05-21 22:47:20 +0000 | [diff] [blame] | 93 | inline bool operator==(const Token& lhs, const Token& rhs) |
| 94 | { |
| 95 | return lhs.equals(rhs); |
| 96 | } |
| 97 | |
| 98 | inline bool operator!=(const Token& lhs, const Token& rhs) |
| 99 | { |
| 100 | return !lhs.equals(rhs); |
| 101 | } |
| 102 | |
alokp@chromium.org | b81c401 | 2011-08-21 06:53:11 +0000 | [diff] [blame] | 103 | extern std::ostream& operator<<(std::ostream& out, const Token& token); |
| 104 | |
alokp@chromium.org | 4e4b807 | 2011-08-07 05:36:04 +0000 | [diff] [blame] | 105 | } // namepsace pp |
| 106 | #endif // COMPILER_PREPROCESSOR_TOKEN_H_ |