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