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