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 
10 #include <ostream>
11 #include <string>
12 
13 #include "compiler/preprocessor/SourceLocation.h"
14 
15 namespace pp
16 {
17 
18 struct Token
19 {
20     enum Type
21     {
22         // Calling this ERROR causes a conflict with wingdi.h
23         GOT_ERROR = -1,
24         LAST  = 0,  // EOF.
25 
26         IDENTIFIER = 258,
27 
28         CONST_INT,
29         CONST_FLOAT,
30 
31         OP_INC,
32         OP_DEC,
33         OP_LEFT,
34         OP_RIGHT,
35         OP_LE,
36         OP_GE,
37         OP_EQ,
38         OP_NE,
39         OP_AND,
40         OP_XOR,
41         OP_OR,
42         OP_ADD_ASSIGN,
43         OP_SUB_ASSIGN,
44         OP_MUL_ASSIGN,
45         OP_DIV_ASSIGN,
46         OP_MOD_ASSIGN,
47         OP_LEFT_ASSIGN,
48         OP_RIGHT_ASSIGN,
49         OP_AND_ASSIGN,
50         OP_XOR_ASSIGN,
51         OP_OR_ASSIGN,
52 
53         // Preprocessing token types.
54         // These types are used by the preprocessor internally.
55         // Preprocessor clients must not depend or check for them.
56         PP_HASH,
57         PP_NUMBER,
58         PP_OTHER
59     };
60     enum Flags
61     {
62         AT_START_OF_LINE   = 1 << 0,
63         HAS_LEADING_SPACE  = 1 << 1,
64         EXPANSION_DISABLED = 1 << 2
65     };
66 
TokenToken67     Token() : type(0), flags(0) {}
68 
69     void reset();
70     bool equals(const Token &other) const;
71 
72     // Returns true if this is the first token on line.
73     // It disregards any leading whitespace.
atStartOfLineToken74     bool atStartOfLine() const { return (flags & AT_START_OF_LINE) != 0; }
75     void setAtStartOfLine(bool start);
76 
hasLeadingSpaceToken77     bool hasLeadingSpace() const { return (flags & HAS_LEADING_SPACE) != 0; }
78     void setHasLeadingSpace(bool space);
79 
expansionDisabledToken80     bool expansionDisabled() const { return (flags & EXPANSION_DISABLED) != 0; }
81     void setExpansionDisabled(bool disable);
82 
83     // Converts text into numeric value for CONST_INT and CONST_FLOAT token.
84     // Returns false if the parsed value cannot fit into an int or float.
85     bool iValue(int *value) const;
86     bool uValue(unsigned int *value) const;
87     bool fValue(float *value) const;
88 
89     int type;
90     unsigned int flags;
91     SourceLocation location;
92     std::string text;
93 };
94 
95 inline bool operator==(const Token &lhs, const Token &rhs)
96 {
97     return lhs.equals(rhs);
98 }
99 
100 inline bool operator!=(const Token &lhs, const Token &rhs)
101 {
102     return !lhs.equals(rhs);
103 }
104 
105 std::ostream &operator<<(std::ostream &out, const Token &token);
106 
107 }  // namepsace pp
108 
109 #endif  // COMPILER_PREPROCESSOR_TOKEN_H_
110