1 #ifndef _RSGTOKEN_HPP
2 #define _RSGTOKEN_HPP
3 /*-------------------------------------------------------------------------
4 * drawElements Quality Program Random Shader Generator
5 * ----------------------------------------------------
6 *
7 * Copyright 2014 The Android Open Source Project
8 *
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
12 *
13 * http://www.apache.org/licenses/LICENSE-2.0
14 *
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 *
21 *//*!
22 * \file
23 * \brief Token class.
24 *//*--------------------------------------------------------------------*/
25
26 #include "rsgDefs.hpp"
27
28 #include <vector>
29
30 namespace rsg
31 {
32
33 class Token
34 {
35 public:
36 enum Type
37 {
38 IDENTIFIER,
39 STRUCT,
40 INVARIANT,
41 PRECISION,
42 VOID,
43 BREAK,
44 CONTINUE,
45 DO,
46 WHILE,
47 ELSE,
48 FOR,
49 IF,
50 DISCARD,
51 RETURN,
52 INC_OP,
53 DEC_OP,
54 LEFT_PAREN,
55 RIGHT_PAREN,
56 LEFT_BRACKET, // [
57 RIGHT_BRACKET, // ]
58 LEFT_BRACE, // {
59 RIGHT_BRACE, // }
60 DOT,
61 COMMA,
62 COLON,
63 SEMICOLON,
64 MINUS,
65 PLUS,
66 MUL,
67 DIV,
68 MOD,
69 QUESTION,
70 BOOL,
71 BVEC2,
72 BVEC3,
73 BVEC4,
74 INT,
75 IVEC2,
76 IVEC3,
77 IVEC4,
78 FLOAT,
79 VEC2,
80 VEC3,
81 VEC4,
82 MAT2,
83 MAT3,
84 MAT4,
85 SAMPLER2D,
86 SAMPLERCUBE,
87 FLOAT_LITERAL,
88 INT_LITERAL,
89 BOOL_LITERAL,
90 EQUAL,
91 MUL_ASSIGN,
92 DIV_ASSIGN,
93 ADD_ASSIGN,
94 SUB_ASSIGN,
95 CMP_LT,
96 CMP_GT,
97 CMP_LE,
98 CMP_GE,
99 CMP_EQ,
100 CMP_NE,
101 LOGICAL_AND,
102 LOGICAL_OR,
103 LOGICAL_NOT,
104 LOGICAL_XOR,
105 ATTRIBUTE,
106 UNIFORM,
107 VARYING,
108 CONST,
109 FLAT,
110 HIGH_PRECISION,
111 MEDIUM_PRECISION,
112 LOW_PRECISION,
113 IN,
114 OUT,
115 INOUT,
116 LAYOUT,
117 LOCATION,
118
119 // Formatting only
120 INDENT_INC,
121 INDENT_DEC,
122 NEWLINE,
123
124 TYPE_LAST
125 };
126
127 Token(void);
128 Token(Type type);
129 Token(const char *identifier);
130 Token(float value);
131 Token(int value);
132 Token(bool value);
133 Token(const Token &other);
134
135 ~Token(void);
136
operator ==(Type type) const137 inline bool operator==(Type type) const
138 {
139 return m_type == type;
140 }
operator !=(Type type) const141 inline bool operator!=(Type type) const
142 {
143 return m_type != type;
144 }
145
146 bool operator==(const Token &other) const;
147 bool operator!=(const Token &other) const;
148
149 Token &operator=(const Token &other);
150
getType(void) const151 inline Type getType(void) const
152 {
153 return m_type;
154 }
155
156 const char *getIdentifier(void) const;
157 float getFloat(void) const;
158 int getInt(void) const;
159 bool getBool(void) const;
160
161 private:
162 Type m_type;
163 union
164 {
165 char *identifier;
166 float floatValue;
167 int intValue;
168 bool boolValue;
169 } m_arg;
170 };
171
Token(void)172 inline Token::Token(void) : m_type(TYPE_LAST)
173 {
174 m_arg.identifier = DE_NULL;
175 }
176
Token(Type type)177 inline Token::Token(Type type) : m_type(type)
178 {
179 DE_ASSERT(type != IDENTIFIER);
180 }
181
Token(float value)182 inline Token::Token(float value) : m_type(FLOAT_LITERAL)
183 {
184 m_arg.floatValue = value;
185 }
186
Token(int value)187 inline Token::Token(int value) : m_type(INT_LITERAL)
188 {
189 m_arg.intValue = value;
190 }
191
Token(bool value)192 inline Token::Token(bool value) : m_type(BOOL_LITERAL)
193 {
194 m_arg.boolValue = value;
195 }
196
operator ==(const Token & other) const197 inline bool Token::operator==(const Token &other) const
198 {
199 return !(*this != other);
200 }
201
getIdentifier(void) const202 inline const char *Token::getIdentifier(void) const
203 {
204 DE_ASSERT(m_type == IDENTIFIER);
205 return m_arg.identifier;
206 }
207
getFloat(void) const208 inline float Token::getFloat(void) const
209 {
210 DE_ASSERT(m_type == FLOAT_LITERAL);
211 return m_arg.floatValue;
212 }
213
getInt(void) const214 inline int Token::getInt(void) const
215 {
216 DE_ASSERT(m_type == INT_LITERAL);
217 return m_arg.intValue;
218 }
219
getBool(void) const220 inline bool Token::getBool(void) const
221 {
222 DE_ASSERT(m_type == BOOL_LITERAL);
223 return m_arg.boolValue;
224 }
225
226 class TokenStream
227 {
228 public:
229 TokenStream(void);
230 ~TokenStream(void);
231
getSize(void) const232 int getSize(void) const
233 {
234 return (int)m_numTokens;
235 }
operator [](int ndx) const236 const Token &operator[](int ndx) const
237 {
238 return m_tokens[ndx];
239 }
240
241 TokenStream &operator<<(const Token &token);
242
243 private:
244 enum
245 {
246 ALLOC_SIZE = 64
247 };
248
249 std::vector<Token> m_tokens;
250 size_t m_numTokens;
251 };
252
operator <<(const Token & token)253 inline TokenStream &TokenStream::operator<<(const Token &token)
254 {
255 if (m_tokens.size() == m_numTokens)
256 m_tokens.resize(m_numTokens + ALLOC_SIZE);
257
258 m_tokens[m_numTokens] = token;
259 m_numTokens += 1;
260
261 return *this;
262 }
263
264 } // namespace rsg
265
266 #endif // _RSGTOKEN_HPP
267