1 // Copyright 2016 The SwiftShader Authors. All Rights Reserved. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 #ifndef COMPILER_PREPROCESSOR_TOKEN_H_ 16 #define COMPILER_PREPROCESSOR_TOKEN_H_ 17 18 #include <ostream> 19 #include <string> 20 21 #include "SourceLocation.h" 22 23 namespace pp 24 { 25 26 struct Token 27 { 28 enum Type 29 { 30 LAST = 0, // EOF. 31 32 IDENTIFIER = 258, 33 34 CONST_INT, 35 CONST_FLOAT, 36 37 OP_INC, 38 OP_DEC, 39 OP_LEFT, 40 OP_RIGHT, 41 OP_LE, 42 OP_GE, 43 OP_EQ, 44 OP_NE, 45 OP_AND, 46 OP_XOR, 47 OP_OR, 48 OP_ADD_ASSIGN, 49 OP_SUB_ASSIGN, 50 OP_MUL_ASSIGN, 51 OP_DIV_ASSIGN, 52 OP_MOD_ASSIGN, 53 OP_LEFT_ASSIGN, 54 OP_RIGHT_ASSIGN, 55 OP_AND_ASSIGN, 56 OP_XOR_ASSIGN, 57 OP_OR_ASSIGN, 58 59 // Preprocessing token types. 60 // These types are used by the preprocessor internally. 61 // Preprocessor clients must not depend or check for them. 62 PP_HASH, 63 PP_NUMBER, 64 PP_OTHER 65 }; 66 enum Flags 67 { 68 AT_START_OF_LINE = 1 << 0, 69 HAS_LEADING_SPACE = 1 << 1, 70 EXPANSION_DISABLED = 1 << 2 71 }; 72 TokenToken73 Token() : type(0), flags(0) { } 74 75 void reset(); 76 bool equals(const Token& other) const; 77 78 // Returns true if this is the first token on line. 79 // It disregards any leading whitespace. atStartOfLineToken80 bool atStartOfLine() const { return (flags & AT_START_OF_LINE) != 0; } 81 void setAtStartOfLine(bool start); 82 hasLeadingSpaceToken83 bool hasLeadingSpace() const { return (flags & HAS_LEADING_SPACE) != 0; } 84 void setHasLeadingSpace(bool space); 85 expansionDisabledToken86 bool expansionDisabled() const { return (flags & EXPANSION_DISABLED) != 0; } 87 void setExpansionDisabled(bool disable); 88 89 // Converts text into numeric value for CONST_INT and CONST_FLOAT token. 90 // Returns false if the parsed value cannot fit into an int or float. 91 bool iValue(int* value) const; 92 bool uValue(unsigned int* value) const; 93 bool fValue(float* value) const; 94 95 int type; 96 unsigned int flags; 97 SourceLocation location; 98 std::string text; 99 }; 100 101 inline bool operator==(const Token& lhs, const Token& rhs) 102 { 103 return lhs.equals(rhs); 104 } 105 106 inline bool operator!=(const Token& lhs, const Token& rhs) 107 { 108 return !lhs.equals(rhs); 109 } 110 111 extern std::ostream& operator<<(std::ostream& out, const Token& token); 112 113 } // namepsace pp 114 #endif // COMPILER_PREPROCESSOR_TOKEN_H_ 115