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	{ return m_type == type;	}
operator !=(Type type) const138 	inline bool		operator!=		(Type type) const	{ return m_type != type;	}
139 
140 	bool			operator==		(const Token& other) const;
141 	bool			operator!=		(const Token& other) const;
142 
143 	Token&			operator=		(const Token& other);
144 
getType(void) const145 	inline Type		getType			(void) const		{ return m_type;			}
146 
147 	const char*		getIdentifier	(void) const;
148 	float			getFloat		(void) const;
149 	int				getInt			(void) const;
150 	bool			getBool			(void) const;
151 
152 private:
153 	Type			m_type;
154 	union
155 	{
156 		char*			identifier;
157 		float			floatValue;
158 		int				intValue;
159 		bool			boolValue;
160 	} m_arg;
161 };
162 
163 
Token(void)164 inline Token::Token (void)
165 	: m_type(TYPE_LAST)
166 {
167 	m_arg.identifier = DE_NULL;
168 }
169 
Token(Type type)170 inline Token::Token (Type type)
171 	: m_type(type)
172 {
173 	DE_ASSERT(type != IDENTIFIER);
174 }
175 
Token(float value)176 inline Token::Token (float value)
177 	: m_type(FLOAT_LITERAL)
178 {
179 	m_arg.floatValue = value;
180 }
181 
Token(int value)182 inline Token::Token (int value)
183 	: m_type(INT_LITERAL)
184 {
185 	m_arg.intValue = value;
186 }
187 
Token(bool value)188 inline Token::Token (bool value)
189 	: m_type(BOOL_LITERAL)
190 {
191 	m_arg.boolValue = value;
192 }
193 
operator ==(const Token & other) const194 inline bool Token::operator== (const Token& other) const
195 {
196 	return !(*this != other);
197 }
198 
getIdentifier(void) const199 inline const char* Token::getIdentifier (void) const
200 {
201 	DE_ASSERT(m_type == IDENTIFIER);
202 	return m_arg.identifier;
203 }
204 
getFloat(void) const205 inline float Token::getFloat (void) const
206 {
207 	DE_ASSERT(m_type == FLOAT_LITERAL);
208 	return m_arg.floatValue;
209 }
210 
getInt(void) const211 inline int Token::getInt (void) const
212 {
213 	DE_ASSERT(m_type == INT_LITERAL);
214 	return m_arg.intValue;
215 }
216 
getBool(void) const217 inline bool Token::getBool (void) const
218 {
219 	DE_ASSERT(m_type == BOOL_LITERAL);
220 	return m_arg.boolValue;
221 }
222 
223 class TokenStream
224 {
225 public:
226 							TokenStream		(void);
227 							~TokenStream	(void);
228 
getSize(void) const229 	int						getSize			(void) const	{ return (int)m_numTokens;	}
operator [](int ndx) const230 	const Token&			operator[]		(int ndx) const	{ return m_tokens[ndx];		}
231 
232 	TokenStream&			operator<<		(const Token& token);
233 
234 private:
235 	enum
236 	{
237 		ALLOC_SIZE = 64
238 	};
239 
240 	std::vector<Token>		m_tokens;
241 	size_t					m_numTokens;
242 };
243 
operator <<(const Token & token)244 inline TokenStream& TokenStream::operator<< (const Token& token)
245 {
246 	if (m_tokens.size() == m_numTokens)
247 		m_tokens.resize(m_numTokens+ALLOC_SIZE);
248 
249 	m_tokens[m_numTokens]	 = token;
250 	m_numTokens				+= 1;
251 
252 	return *this;
253 }
254 
255 } // rsg
256 
257 #endif // _RSGTOKEN_HPP
258