OpenVDB: Tokens.h Source File

OpenVDB 8.1.0

Tokens.h
Go to the documentation of this file.
1 // Copyright Contributors to the OpenVDB Project
2 // SPDX-License-Identifier: MPL-2.0
3
11
12 #ifndef OPENVDB_AX_AST_TOKENS_HAS_BEEN_INCLUDED
13 #define OPENVDB_AX_AST_TOKENS_HAS_BEEN_INCLUDED
14
15 #include "../Exceptions.h"
16
17 #include <openvdb/version.h>
18 #include <openvdb/Types.h>
19
20 #include <stdexcept>
21
22 namespace openvdb {
23 OPENVDB_USE_VERSION_NAMESPACE
24 namespace OPENVDB_VERSION_NAME {
25
26 namespace ax {
27 namespace ast {
28
29 namespace tokens {
30
31 enum CoreType
32 {
33 BOOL = 0,
34 CHAR,
35 INT16,
36 INT32,
37 INT64,
38 FLOAT,
39 DOUBLE,
40 //
41 VEC2I,
42 VEC2F,
43 VEC2D,
44 //
45 VEC3I,
46 VEC3F,
47 VEC3D,
48 //
49 VEC4I,
50 VEC4F,
51 VEC4D,
52 //
53 MAT3F,
54 MAT3D,
55 //
56 MAT4F,
57 MAT4D,
58 //
59 QUATF,
60 QUATD,
61 //
62 STRING,
63 UNKNOWN
64 };
65
66 inline CoreType tokenFromTypeString(const std::string& type)
67 {
68 if (type[0] == 'v') {
69 if (type == "vec2i") return VEC2I;
70 if (type == "vec2f") return VEC2F;
71 if (type == "vec2d") return VEC2D;
72 if (type == "vec3i") return VEC3I;
73 if (type == "vec3f") return VEC3F;
74 if (type == "vec3d") return VEC3D;
75 if (type == "vec4i") return VEC4I;
76 if (type == "vec4f") return VEC4F;
77 if (type == "vec4d") return VEC4D;
78 }
79 else if (type[0] == 'm') {
80 if (type == "mat3f") return MAT3F;
81 if (type == "mat3d") return MAT3D;
82 if (type == "mat4f") return MAT4F;
83 if (type == "mat4d") return MAT4D;
84 }
85 else if (type[0] == 'q') {
86 if (type == "quatf") return QUATF;
87 if (type == "quatd") return QUATD;
88 }
89 else if (type[0] == 'i') {
90 if (type == "int16") return INT16;
91 if (type == "int") return INT32;
92 if (type == "int32") return INT32;
93 if (type == "int64") return INT64;
94 }
95 else if (type == "bool") return BOOL;
96 else if (type == "char") return CHAR;
97 else if (type == "float") return FLOAT;
98 else if (type == "double") return DOUBLE;
99 else if (type == "string") return STRING;
100
101 // also handle vdb types that have different type strings to our tokens
102 // @todo These should probably be separated out. The executables currently
103 // use this function to guarantee conversion
104 if (type[0] == 'v') {
105 if (type == "vec2s") return VEC2F;
106 if (type == "vec3s") return VEC3F;
107 if (type == "vec4s") return VEC4F;
108 }
109 else if (type[0] == 'm') {
110 if (type == "mat3s") return MAT3F;
111 if (type == "mat4s") return MAT4F;
112 }
113 else if (type == "quats") return QUATF;
114
115 return UNKNOWN;
116 }
117
118 inline std::string typeStringFromToken(const CoreType type)
119 {
120 switch (type) {
121 case BOOL : return "bool";
122 case CHAR : return "char";
123 case INT16 : return "int16";
124 case INT32 : return "int32";
125 case INT64 : return "int64";
126 case FLOAT : return "float";
127 case DOUBLE : return "double";
128 case VEC2I : return "vec2i";
129 case VEC2F : return "vec2f";
130 case VEC2D : return "vec2d";
131 case VEC3I : return "vec3i";
132 case VEC3F : return "vec3f";
133 case VEC3D : return "vec3d";
134 case VEC4I : return "vec4i";
135 case VEC4F : return "vec4f";
136 case VEC4D : return "vec4d";
137 case MAT3F : return "mat3f";
138 case MAT3D : return "mat3d";
139 case MAT4F : return "mat4f";
140 case MAT4D : return "mat4d";
141 case QUATF : return "quatf";
142 case QUATD : return "quatd";
143 case STRING : return "string";
144 case UNKNOWN :
145 default :
146 return "unknown";
147 }
148 }
149
150 enum OperatorToken
151 {
155 PLUS = 0,
156 MINUS,
157 MULTIPLY,
158 DIVIDE,
159 MODULO,
163 AND,
164 OR,
165 NOT,
169 EQUALSEQUALS,
170 NOTEQUALS,
171 MORETHAN,
172 LESSTHAN,
173 MORETHANOREQUAL,
174 LESSTHANOREQUAL,
178 SHIFTLEFT,
179 SHIFTRIGHT,
180 BITAND,
181 BITOR,
182 BITXOR,
183 BITNOT,
187 EQUALS,
188 PLUSEQUALS,
189 MINUSEQUALS,
190 MULTIPLYEQUALS,
191 DIVIDEEQUALS,
192 MODULOEQUALS,
193 SHIFTLEFTEQUALS,
194 SHIFTRIGHTEQUALS,
195 BITANDEQUALS,
196 BITXOREQUALS,
197 BITOREQUALS
198 };
199
200 enum OperatorType
201 {
202 ARITHMETIC = 0,
203 LOGICAL,
204 RELATIONAL,
205 BITWISE,
206 ASSIGNMENT,
207 UNKNOWN_OPERATOR
208 };
209
210 inline OperatorType operatorType(const OperatorToken token)
211 {
212 const size_t idx = static_cast<size_t>(token);
213 if (idx <= static_cast<size_t>(MODULO)) return ARITHMETIC;
214 if (idx <= static_cast<size_t>(NOT)) return LOGICAL;
215 if (idx <= static_cast<size_t>(LESSTHANOREQUAL)) return RELATIONAL;
216 if (idx <= static_cast<size_t>(BITNOT)) return BITWISE;
217 if (idx <= static_cast<size_t>(BITOREQUALS)) return ASSIGNMENT;
218 return UNKNOWN_OPERATOR;
219 }
220
221 inline OperatorToken operatorTokenFromName(const std::string& name)
222 {
223 if (name == "+") return PLUS;
224 if (name == "-") return MINUS;
225 if (name == "*") return MULTIPLY;
226 if (name == "/") return DIVIDE;
227 if (name == "%") return MODULO;
228 if (name == "&&") return AND;
229 if (name == "||") return OR;
230 if (name == "!") return NOT;
231 if (name == "==") return EQUALSEQUALS;
232 if (name == "!=") return NOTEQUALS;
233 if (name == ">") return MORETHAN;
234 if (name == "<") return LESSTHAN;
235 if (name == ">=") return MORETHANOREQUAL;
236 if (name == "<=") return LESSTHANOREQUAL;
237 if (name == "<<") return SHIFTLEFT;
238 if (name == ">>") return SHIFTRIGHT;
239 if (name == "&") return BITAND;
240 if (name == "|") return BITOR;
241 if (name == "^") return BITXOR;
242 if (name == "~") return BITNOT;
243 if (name == "=") return EQUALS;
244 if (name == "+=") return PLUSEQUALS;
245 if (name == "-=") return MINUSEQUALS;
246 if (name == "*=") return MULTIPLYEQUALS;
247 if (name == "/=") return DIVIDEEQUALS;
248 if (name == "%=") return MODULOEQUALS;
249 if (name == "<<=") return SHIFTLEFTEQUALS;
250 if (name == ">>=") return SHIFTRIGHTEQUALS;
251 if (name == "&=") return BITANDEQUALS;
252 if (name == "^=") return BITXOREQUALS;
253 if (name == "|=") return BITOREQUALS;
254 OPENVDB_THROW(AXTokenError, "Unsupported op \"" + name + "\"");
255 }
256
257 inline std::string operatorNameFromToken(const OperatorToken token)
258 {
259 switch (token) {
260 case PLUS : return "+";
261 case MINUS : return "-";
262 case MULTIPLY : return "*";
263 case DIVIDE : return "/";
264 case MODULO : return "%";
265 case AND : return "&&";
266 case OR : return "||";
267 case NOT : return "!";
268 case EQUALSEQUALS : return "==";
269 case NOTEQUALS : return "!=";
270 case MORETHAN : return ">";
271 case LESSTHAN : return "<";
272 case MORETHANOREQUAL : return ">=";
273 case LESSTHANOREQUAL : return "<=";
274 case SHIFTLEFT : return "<<";
275 case SHIFTRIGHT : return ">>";
276 case BITAND : return "&";
277 case BITOR : return "|";
278 case BITXOR : return "^";
279 case BITNOT : return "~";
280 case EQUALS : return "=";
281 case PLUSEQUALS : return "+=";
282 case MINUSEQUALS : return "-=";
283 case MULTIPLYEQUALS : return "*=";
284 case DIVIDEEQUALS : return "/=";
285 case MODULOEQUALS : return "%=";
286 case SHIFTLEFTEQUALS : return "<<=";
287 case SHIFTRIGHTEQUALS : return ">>=";
288 case BITANDEQUALS : return "&=";
289 case BITXOREQUALS : return "^=";
290 case BITOREQUALS : return "|=";
291 default :
292 OPENVDB_THROW(AXTokenError, "Unsupported op");
293 }
294 }
295
296 enum LoopToken
297 {
298 FOR = 0,
299 DO,
300 WHILE
301 };
302
303 inline std::string loopNameFromToken(const LoopToken loop)
304 {
305 switch (loop) {
306 case FOR : return "for";
307 case DO : return "do";
308 case WHILE : return "while";
309 default :
310 OPENVDB_THROW(AXTokenError, "Unsupported loop");
311 }
312 }
313
314 enum KeywordToken
315 {
316 RETURN = 0,
317 BREAK,
318 CONTINUE
319 };
320
321 inline std::string keywordNameFromToken(const KeywordToken keyw)
322 {
323 switch (keyw) {
324 case RETURN : return "return";
325 case BREAK : return "break";
326 case CONTINUE : return "continue";
327 default :
328 OPENVDB_THROW(AXTokenError, "Unsupported keyword");
329 }
330 }
331
332
333 } // namespace tokens
334
335 } // namespace ast
336 } // namespace ax
337 } // namespace OPENVDB_VERSION_NAME
338 } // namespace openvdb
339
340 #endif // OPENVDB_AX_AST_TOKENS_HAS_BEEN_INCLUDED
341
NOT
@ NOT
Definition: axparser.h:145
VEC3D
@ VEC3D
Definition: axparser.h:84
BITOR
@ BITOR
Definition: axparser.h:128
DIVIDE
@ DIVIDE
Definition: axparser.h:142
LESSTHANOREQUAL
@ LESSTHANOREQUAL
Definition: axparser.h:136
SHIFTRIGHT
@ SHIFTRIGHT
Definition: axparser.h:138
MAT3F
@ MAT3F
Definition: axparser.h:93
MAT3D
@ MAT3D
Definition: axparser.h:94
MAT4D
@ MAT4D
Definition: axparser.h:96
WHILE
@ WHILE
Definition: axparser.h:65
DOUBLE
@ DOUBLE
Definition: axparser.h:74
BITNOT
@ BITNOT
Definition: axparser.h:146
MORETHANOREQUAL
@ MORETHANOREQUAL
Definition: axparser.h:135
INT64
@ INT64
Definition: axparser.h:77
MINUSEQUALS
@ MINUSEQUALS
Definition: axparser.h:117
SHIFTLEFTEQUALS
@ SHIFTLEFTEQUALS
Definition: axparser.h:124
CONTINUE
@ CONTINUE
Definition: axparser.h:68
BITOREQUALS
@ BITOREQUALS
Definition: axparser.h:123
RETURN
@ RETURN
Definition: axparser.h:66
BITXOREQUALS
@ BITXOREQUALS
Definition: axparser.h:122
VEC3F
@ VEC3F
Definition: axparser.h:83
MODULOEQUALS
@ MODULOEQUALS
Definition: axparser.h:120
PLUSEQUALS
@ PLUSEQUALS
Definition: axparser.h:116
EQUALSEQUALS
@ EQUALSEQUALS
Definition: axparser.h:131
VEC2I
@ VEC2I
Definition: axparser.h:79
BITXOR
@ BITXOR
Definition: axparser.h:129
BITAND
@ BITAND
Definition: axparser.h:130
MAT4F
@ MAT4F
Definition: axparser.h:95
AND
@ AND
Definition: axparser.h:127
PLUS
@ PLUS
Definition: axparser.h:139
LESSTHAN
@ LESSTHAN
Definition: axparser.h:134
VEC4F
@ VEC4F
Definition: axparser.h:86
DIVIDEEQUALS
@ DIVIDEEQUALS
Definition: axparser.h:119
EQUALS
@ EQUALS
Definition: axparser.h:115
BREAK
@ BREAK
Definition: axparser.h:67
OR
@ OR
Definition: axparser.h:126
VEC4I
@ VEC4I
Definition: axparser.h:85
FLOAT
@ FLOAT
Definition: axparser.h:75
MODULO
@ MODULO
Definition: axparser.h:143
FOR
@ FOR
Definition: axparser.h:63
MORETHAN
@ MORETHAN
Definition: axparser.h:133
VEC2D
@ VEC2D
Definition: axparser.h:81
VEC3I
@ VEC3I
Definition: axparser.h:82
MULTIPLYEQUALS
@ MULTIPLYEQUALS
Definition: axparser.h:118
DO
@ DO
Definition: axparser.h:64
VEC4D
@ VEC4D
Definition: axparser.h:87
INT32
@ INT32
Definition: axparser.h:76
BITANDEQUALS
@ BITANDEQUALS
Definition: axparser.h:121
NOTEQUALS
@ NOTEQUALS
Definition: axparser.h:132
SHIFTRIGHTEQUALS
@ SHIFTRIGHTEQUALS
Definition: axparser.h:125
MULTIPLY
@ MULTIPLY
Definition: axparser.h:141
BOOL
@ BOOL
Definition: axparser.h:78
SHIFTLEFT
@ SHIFTLEFT
Definition: axparser.h:137
VEC2F
@ VEC2F
Definition: axparser.h:80
STRING
@ STRING
Definition: axparser.h:73
MINUS
@ MINUS
Definition: axparser.h:140
openvdb::v8_1::AXTokenError
Definition: ax/openvdb_ax/Exceptions.h:34
openvdb::v8_1::ax::ast::tokens::OperatorToken
OperatorToken
Definition: Tokens.h:151
openvdb::v8_1::ax::ast::tokens::keywordNameFromToken
std::string keywordNameFromToken(const KeywordToken keyw)
Definition: Tokens.h:321
openvdb::v8_1::ax::ast::tokens::operatorType
OperatorType operatorType(const OperatorToken token)
Definition: Tokens.h:210
openvdb::v8_1::ax::ast::tokens::typeStringFromToken
std::string typeStringFromToken(const CoreType type)
Definition: Tokens.h:118
openvdb::v8_1::ax::ast::tokens::loopNameFromToken
std::string loopNameFromToken(const LoopToken loop)
Definition: Tokens.h:303
openvdb::v8_1::ax::ast::tokens::KeywordToken
KeywordToken
Definition: Tokens.h:315
openvdb::v8_1::ax::ast::tokens::OperatorType
OperatorType
Definition: Tokens.h:201
openvdb::v8_1::ax::ast::tokens::ASSIGNMENT
@ ASSIGNMENT
Definition: Tokens.h:206
openvdb::v8_1::ax::ast::tokens::LOGICAL
@ LOGICAL
Definition: Tokens.h:203
openvdb::v8_1::ax::ast::tokens::BITWISE
@ BITWISE
Definition: Tokens.h:205
openvdb::v8_1::ax::ast::tokens::ARITHMETIC
@ ARITHMETIC
Definition: Tokens.h:202
openvdb::v8_1::ax::ast::tokens::RELATIONAL
@ RELATIONAL
Definition: Tokens.h:204
openvdb::v8_1::ax::ast::tokens::UNKNOWN_OPERATOR
@ UNKNOWN_OPERATOR
Definition: Tokens.h:207
openvdb::v8_1::ax::ast::tokens::LoopToken
LoopToken
Definition: Tokens.h:297
openvdb::v8_1::ax::ast::tokens::operatorNameFromToken
std::string operatorNameFromToken(const OperatorToken token)
Definition: Tokens.h:257
openvdb::v8_1::ax::ast::tokens::tokenFromTypeString
CoreType tokenFromTypeString(const std::string &type)
Definition: Tokens.h:66
openvdb::v8_1::ax::ast::tokens::CoreType
CoreType
Definition: Tokens.h:32
openvdb::v8_1::ax::ast::tokens::CHAR
@ CHAR
Definition: Tokens.h:34
openvdb::v8_1::ax::ast::tokens::UNKNOWN
@ UNKNOWN
Definition: Tokens.h:63
openvdb::v8_1::ax::ast::tokens::INT16
@ INT16
Definition: Tokens.h:35
openvdb::v8_1::ax::ast::tokens::QUATF
@ QUATF
Definition: Tokens.h:59
openvdb::v8_1::ax::ast::tokens::QUATD
@ QUATD
Definition: Tokens.h:60
openvdb::v8_1::ax::ast::tokens::operatorTokenFromName
OperatorToken operatorTokenFromName(const std::string &name)
Definition: Tokens.h:221
openvdb
Definition: openvdb/Exceptions.h:13
OPENVDB_THROW
#define OPENVDB_THROW(exception, message)
Definition: openvdb/Exceptions.h:74
Types.h
OPENVDB_VERSION_NAME
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
Definition: version.h.in:116
OPENVDB_USE_VERSION_NAMESPACE
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h.in:178