Debian Codegraph @

src/tools/qlalr/examples/glsl/glsl.g

raw
1 -- Copyright (C) 2016 The Qt Company Ltd.
2 -- SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
3
4 %parser GLSLParserTable
5 %merged_output glsl.cpp
6
7 %token ADD_ASSIGN
8 %token AMPERSAND
9 %token AND_ASSIGN
10 %token AND_OP
11 %token ATTRIBUTE
12 %token BANG
13 %token BOOL
14 %token BOOLCONSTANT
15 %token BREAK
16 %token BVEC2
17 %token BVEC3
18 %token BVEC4
19 %token CARET
20 %token COLON
21 %token COMMA
22 %token CONST
23 %token CONTINUE
24 %token DASH
25 %token DEC_OP
26 %token DISCARD
27 %token DIV_ASSIGN
28 %token DO
29 %token DOT
30 %token ELSE
31 %token EQUAL
32 %token EQ_OP
33 %token FLOAT
34 %token FLOATCONSTANT
35 %token FOR
36 %token GE_OP
37 %token IDENTIFIER
38 %token IF
39 %token IN
40 %token INC_OP
41 %token INOUT
42 %token INT
43 %token INTCONSTANT
44 %token IVEC2
45 %token IVEC3
46 %token IVEC4
47 %token LEFT_ANGLE
48 %token LEFT_ASSIGN
49 %token LEFT_BRACE
50 %token LEFT_BRACKET
51 %token LEFT_OP
52 %token LEFT_PAREN
53 %token LE_OP
54 %token MAT2
55 %token MAT3
56 %token MAT4
57 %token MOD_ASSIGN
58 %token MUL_ASSIGN
59 %token NE_OP
60 %token OR_ASSIGN
61 %token OR_OP
62 %token OUT
63 %token PERCENT
64 %token PLUS
65 %token QUESTION
66 %token RETURN
67 %token RIGHT_ANGLE
68 %token RIGHT_ASSIGN
69 %token RIGHT_BRACE
70 %token RIGHT_BRACKET
71 %token RIGHT_OP
72 %token RIGHT_PAREN
73 %token SAMPLER1D
74 %token SAMPLER1DSHADOW
75 %token SAMPLER2D
76 %token SAMPLER2DSHADOW
77 %token SAMPLER3D
78 %token SAMPLERCUBE
79 %token SEMICOLON
80 %token SLASH
81 %token STAR
82 %token STRUCT
83 %token SUB_ASSIGN
84 %token TILDE
85 %token TYPE_NAME
86 %token UNIFORM
87 %token VARYING
88 %token VEC2
89 %token VEC3
90 %token VEC4
91 %token VERTICAL_BAR
92 %token VOID
93 %token WHILE
94 %token XOR_ASSIGN
95 %token XOR_OP
96 %token ERROR
97 %token HIGH_PRECISION
98 %token MEDIUM_PRECISION
99 %token LOW_PRECISION
100 %start translation_unit
101
102
103 /:
104
105 #include <QtCore>
106
107 class GLSLParser: protected $table
108 {
109 public:
110 union Value {
111 int i;
112 unsigned u;
113 unsigned long ul;
114 unsigned long long ull;
115 long l;
116 double d;
117 float f;
118 const QString *s;
119 // ### more...
120 };
121
122 public:
123 GLSLParser();
124 ~GLSLParser();
125
126 bool parse();
127
128 protected:
129 inline void reallocateStack();
130
131 inline Value &sym(int index)
132 { return sym_stack [tos + index - 1]; }
133
134 int nextToken();
135
136 bool isTypename(const QString *s) const
137 {
138 return types.contains(s);
139 }
140
141 inline const QString *intern(const QString &s)
142 { return &*string_repository.insert(s); }
143
144 protected:
145 int tos;
146 int stack_size;
147 Value *sym_stack;
148 int *state_stack;
149 Value yylval;
150 QSet<QString> string_repository;
151 QSet<const QString*> types;
152
153 struct /*Context*/ {
154 int line;
155 const QString *function_name;
156 QString fileName;
157
158 void init()
159 {
160 line = 1;
161 function_name = 0;
162 fileName.clear();
163 }
164 } context;
165 };
166
167 inline void GLSLParser::reallocateStack()
168 {
169 if (! stack_size)
170 stack_size = 128;
171 else
172 stack_size <<= 1;
173
174 sym_stack = reinterpret_cast<Value*> (realloc(sym_stack, stack_size * sizeof(Value)));
175 state_stack = reinterpret_cast<int*> (realloc(state_stack, stack_size * sizeof(int)));
176 }
177
178 :/
179
180
181 /.
182
183 GLSLParser::GLSLParser():
184 tos(0),
185 stack_size(0),
186 sym_stack(0),
187 state_stack(0)
188 {
189 }
190
191 GLSLParser::~GLSLParser()
192 {
193 if (stack_size) {
194 free(sym_stack);
195 free(state_stack);
196 }
197 }
198
199 bool GLSLParser::parse()
200 {
201 const int INITIAL_STATE = 0;
202
203 int yytoken = -1;
204
205 reallocateStack();
206
207 context.init();
208 tos = 0;
209 state_stack[++tos] = INITIAL_STATE;
210
211 while (true)
212 {
213 if (yytoken == -1 && - TERMINAL_COUNT != action_index [state_stack [tos]])
214 yytoken = nextToken();
215
216 int act = t_action (state_stack [tos], yytoken);
217
218 if (act == ACCEPT_STATE) {
219 return true;
220 }
221
222 else if (act > 0)
223 {
224 if (++tos == stack_size)
225 reallocateStack();
226
227 sym_stack [tos] = yylval;
228 state_stack [tos] = act;
229 yytoken = -1;
230 }
231
232 else if (act < 0)
233 {
234 int r = - act - 1;
235
236 int ridx = rule_index [r];
237 printf ("--* reduce using rule %d %s ::=", r + 1, spell[rule_info [ridx]]);
238 ++ridx;
239 for (int i = ridx; i < ridx + rhs [r]; ++i)
240 {
241 int symbol = rule_info [i];
242 if (const char *name = spell [symbol])
243 printf (" %s", name);
244 else
245 printf (" #%d", symbol);
246 }
247 printf ("\n");
248
249 tos -= rhs [r];
250 act = state_stack [tos++];
251
252 switch (r) {
253 ./
254
255
256 translation_unit ::= external_declaration ;
257 translation_unit ::= translation_unit external_declaration ;
258
259 variable_identifier ::= IDENTIFIER ;
260
261 primary_expression ::= variable_identifier ;
262 primary_expression ::= INTCONSTANT ;
263 primary_expression ::= FLOATCONSTANT ;
264 primary_expression ::= BOOLCONSTANT ;
265 primary_expression ::= LEFT_PAREN expression RIGHT_PAREN ;
266
267
268 postfix_expression ::= primary_expression ;
269 postfix_expression ::= postfix_expression LEFT_BRACKET integer_expression RIGHT_BRACKET ;
270 postfix_expression ::= function_call ;
271 postfix_expression ::= postfix_expression DOT IDENTIFIER ;
272 postfix_expression ::= postfix_expression DOT TYPE_NAME ;
273 postfix_expression ::= postfix_expression INC_OP ;
274 postfix_expression ::= postfix_expression DEC_OP ;
275
276
277 integer_expression ::= expression ;
278
279 function_call ::= function_call_generic ;
280
281 function_call_generic ::= function_call_header_with_parameters RIGHT_PAREN ;
282 function_call_generic ::= function_call_header_no_parameters RIGHT_PAREN ;
283
284 function_call_header_no_parameters ::= function_call_header VOID ;
285 function_call_header_no_parameters ::= function_call_header ;
286
287
288 function_call_header_with_parameters ::= function_call_header assignment_expression ;
289 function_call_header_with_parameters ::= function_call_header_with_parameters COMMA assignment_expression ;
290
291 function_call_header ::= function_identifier LEFT_PAREN ;
292
293 function_identifier ::= constructor_identifier ;
294 function_identifier ::= IDENTIFIER ;
295
296
297 constructor_identifier ::= FLOAT ;
298 constructor_identifier ::= INT ;
299 constructor_identifier ::= BOOL ;
300 constructor_identifier ::= VEC2 ;
301 constructor_identifier ::= VEC3 ;
302 constructor_identifier ::= VEC4 ;
303 constructor_identifier ::= BVEC2 ;
304 constructor_identifier ::= BVEC3 ;
305 constructor_identifier ::= BVEC4 ;
306 constructor_identifier ::= IVEC2 ;
307 constructor_identifier ::= IVEC3 ;
308 constructor_identifier ::= IVEC4 ;
309 constructor_identifier ::= MAT2 ;
310 constructor_identifier ::= MAT3 ;
311 constructor_identifier ::= MAT4 ;
312 constructor_identifier ::= TYPE_NAME ;
313
314 unary_expression ::= postfix_expression ;
315 unary_expression ::= INC_OP unary_expression ;
316 unary_expression ::= DEC_OP unary_expression ;
317 unary_expression ::= unary_operator unary_expression ;
318
319 -- Grammar Note: No traditional style type casts.
320
321 unary_operator ::= PLUS ;
322 unary_operator ::= DASH ;
323 unary_operator ::= BANG ;
324 unary_operator ::= TILDE ; -- reserved
325
326 -- Grammar Note: No '*' or '&' unary ops. Pointers are not supported.
327
328 multiplicative_expression ::= unary_expression ;
329 multiplicative_expression ::= multiplicative_expression STAR unary_expression ;
330 multiplicative_expression ::= multiplicative_expression SLASH unary_expression ;
331 multiplicative_expression ::= multiplicative_expression PERCENT unary_expression ; -- reserved
332
333
334 additive_expression ::= multiplicative_expression ;
335 additive_expression ::= additive_expression PLUS multiplicative_expression ;
336 additive_expression ::= additive_expression DASH multiplicative_expression ;
337
338 shift_expression ::= additive_expression ;
339 shift_expression ::= shift_expression LEFT_OP additive_expression ; -- reserved
340 shift_expression ::= shift_expression RIGHT_OP additive_expression ; -- reserved
341
342 relational_expression ::= shift_expression ;
343 relational_expression ::= relational_expression LEFT_ANGLE shift_expression ;
344 relational_expression ::= relational_expression RIGHT_ANGLE shift_expression ;
345 relational_expression ::= relational_expression LE_OP shift_expression ;
346 relational_expression ::= relational_expression GE_OP shift_expression ;
347
348 equality_expression ::= relational_expression ;
349 equality_expression ::= equality_expression EQ_OP relational_expression ;
350 equality_expression ::= equality_expression NE_OP relational_expression ;
351
352 and_expression ::= equality_expression ;
353 and_expression ::= and_expression AMPERSAND equality_expression ; -- reserved
354
355 exclusive_or_expression ::= and_expression ;
356 exclusive_or_expression ::= exclusive_or_expression CARET and_expression ; -- reserved
357
358 inclusive_or_expression ::= exclusive_or_expression ;
359 inclusive_or_expression ::= inclusive_or_expression VERTICAL_BAR exclusive_or_expression ; -- reserved
360
361 logical_and_expression ::= inclusive_or_expression ;
362 logical_and_expression ::= logical_and_expression AND_OP inclusive_or_expression ;
363
364 logical_xor_expression ::= logical_and_expression ;
365 logical_xor_expression ::= logical_xor_expression XOR_OP logical_and_expression ;
366
367 logical_or_expression ::= logical_xor_expression ;
368 logical_or_expression ::= logical_or_expression OR_OP logical_xor_expression ;
369
370 conditional_expression ::= logical_or_expression ;
371 conditional_expression ::= logical_or_expression QUESTION expression COLON conditional_expression ;
372
373 assignment_expression ::= conditional_expression ;
374 assignment_expression ::= unary_expression assignment_operator assignment_expression ;
375
376 assignment_operator ::= EQUAL ;
377 assignment_operator ::= MUL_ASSIGN ;
378 assignment_operator ::= DIV_ASSIGN ;
379 assignment_operator ::= MOD_ASSIGN ; -- reserved
380 assignment_operator ::= ADD_ASSIGN ;
381 assignment_operator ::= SUB_ASSIGN ;
382 assignment_operator ::= LEFT_ASSIGN ; -- reserved
383 assignment_operator ::= RIGHT_ASSIGN ; -- reserved
384 assignment_operator ::= AND_ASSIGN ; -- reserved
385 assignment_operator ::= XOR_ASSIGN ; -- reserved
386 assignment_operator ::= OR_ASSIGN ; -- reserved
387
388 expression ::= assignment_expression ;
389 expression ::= expression COMMA assignment_expression ;
390
391 constant_expression ::= conditional_expression ;
392
393 declaration ::= function_prototype SEMICOLON ;
394 declaration ::= init_declarator_list SEMICOLON ;
395
396 function_prototype ::= function_declarator RIGHT_PAREN ;
397
398 function_declarator ::= function_header ;
399 function_declarator ::= function_header_with_parameters ;
400
401 function_header_with_parameters ::= function_header parameter_declaration ;
402 function_header_with_parameters ::= function_header_with_parameters COMMA parameter_declaration ;
403
404 function_header ::= fully_specified_type IDENTIFIER LEFT_PAREN ;
405 /.
406 case $rule_number: {
407 context.function_name = sym(2).s;
408 } break;
409 ./
410
411 parameter_declarator ::= type_specifier IDENTIFIER ;
412 parameter_declarator ::= type_specifier IDENTIFIER LEFT_BRACKET constant_expression RIGHT_BRACKET ;
413
414 parameter_declaration ::= type_qualifier parameter_qualifier parameter_declarator ;
415 parameter_declaration ::= parameter_qualifier parameter_declarator ;
416 parameter_declaration ::= type_qualifier parameter_qualifier parameter_type_specifier ;
417 parameter_declaration ::= parameter_qualifier parameter_type_specifier ;
418
419 parameter_qualifier ::= ;
420 parameter_qualifier ::= IN ;
421 parameter_qualifier ::= OUT ;
422 parameter_qualifier ::= INOUT ;
423
424 parameter_type_specifier ::= type_specifier ;
425 parameter_type_specifier ::= type_specifier LEFT_BRACKET constant_expression RIGHT_BRACKET ;
426
427 init_declarator_list ::= single_declaration ;
428 init_declarator_list ::= init_declarator_list COMMA IDENTIFIER ;
429 init_declarator_list ::= init_declarator_list COMMA IDENTIFIER LEFT_BRACKET RIGHT_BRACKET ;
430 init_declarator_list ::= init_declarator_list COMMA IDENTIFIER LEFT_BRACKET constant_expression ;
431 init_declarator_list ::= RIGHT_BRACKET ;
432 init_declarator_list ::= init_declarator_list COMMA IDENTIFIER EQUAL initializer ;
433
434 single_declaration ::= fully_specified_type ;
435 single_declaration ::= fully_specified_type IDENTIFIER ;
436 single_declaration ::= fully_specified_type IDENTIFIER LEFT_BRACKET RIGHT_BRACKET ;
437 single_declaration ::= fully_specified_type IDENTIFIER LEFT_BRACKET constant_expression RIGHT_BRACKET ;
438 single_declaration ::= fully_specified_type IDENTIFIER EQUAL initializer ;
439
440 -- Grammar Note: No 'enum', or 'typedef'.
441
442 --fully_specified_type ::= type_specifier ;
443 --fully_specified_type ::= type_qualifier type_specifier ;
444
445 fully_specified_type ::= type_specifier ;
446 fully_specified_type ::= type_qualifier ;
447 fully_specified_type ::= fully_specified_type type_specifier ;
448 fully_specified_type ::= fully_specified_type type_qualifier ;
449
450 type_qualifier ::= CONST ;
451 type_qualifier ::= ATTRIBUTE ; -- Vertex only.
452 type_qualifier ::= VARYING ;
453 type_qualifier ::= UNIFORM ;
454
455 type_specifier ::= type_specifier_no_prec ;
456 type_specifier ::= precision_qualifier type_specifier_no_prec ;
457
458 type_specifier_no_prec ::= VOID ;
459 type_specifier_no_prec ::= FLOAT ;
460 type_specifier_no_prec ::= INT ;
461 type_specifier_no_prec ::= BOOL ;
462 type_specifier_no_prec ::= VEC2 ;
463 type_specifier_no_prec ::= VEC3 ;
464 type_specifier_no_prec ::= VEC4 ;
465 type_specifier_no_prec ::= BVEC2 ;
466 type_specifier_no_prec ::= BVEC3 ;
467 type_specifier_no_prec ::= BVEC4 ;
468 type_specifier_no_prec ::= IVEC2 ;
469 type_specifier_no_prec ::= IVEC3 ;
470 type_specifier_no_prec ::= IVEC4 ;
471 type_specifier_no_prec ::= MAT2 ;
472 type_specifier_no_prec ::= MAT3 ;
473 type_specifier_no_prec ::= MAT4 ;
474 type_specifier_no_prec ::= SAMPLER1D ;
475 type_specifier_no_prec ::= SAMPLER2D ;
476 type_specifier_no_prec ::= SAMPLER3D ;
477 type_specifier_no_prec ::= SAMPLERCUBE ;
478 type_specifier_no_prec ::= SAMPLER1DSHADOW ;
479 type_specifier_no_prec ::= SAMPLER2DSHADOW ;
480 type_specifier_no_prec ::= struct_specifier ;
481 type_specifier_no_prec ::= TYPE_NAME ;
482
483 precision_qualifier ::= HIGH_PRECISION ;
484 precision_qualifier ::= MEDIUM_PRECISION ;
485 precision_qualifier ::= LOW_PRECISION ;
486
487 struct_specifier ::= STRUCT IDENTIFIER LEFT_BRACE struct_declaration_list RIGHT_BRACE ;
488 /.
489 case $rule_number: {
490 types.insert(sym(2).s);
491 } break;
492 ./
493
494 struct_specifier ::= STRUCT LEFT_BRACE struct_declaration_list RIGHT_BRACE ;
495
496 struct_declaration_list ::= struct_declaration ;
497 struct_declaration_list ::= struct_declaration_list struct_declaration ;
498
499 struct_declaration ::= type_specifier struct_declarator_list SEMICOLON ;
500
501 struct_declarator_list ::= struct_declarator ;
502 struct_declarator_list ::= struct_declarator_list COMMA struct_declarator ;
503
504 struct_declarator ::= IDENTIFIER ;
505 struct_declarator ::= IDENTIFIER LEFT_BRACKET constant_expression RIGHT_BRACKET ;
506
507 initializer ::= assignment_expression ;
508
509 declaration_statement ::= declaration ;
510
511 statement ::= compound_statement ;
512 statement ::= simple_statement ;
513
514 -- Grammar Note: No labeled statements; 'goto' is not supported.
515
516 simple_statement ::= declaration_statement ;
517 simple_statement ::= expression_statement ;
518 simple_statement ::= selection_statement ;
519 simple_statement ::= iteration_statement ;
520 simple_statement ::= jump_statement ;
521
522 compound_statement ::= LEFT_BRACE RIGHT_BRACE ;
523 compound_statement ::= LEFT_BRACE statement_list RIGHT_BRACE ;
524
525 statement_no_new_scope ::= compound_statement_no_new_scope ;
526 statement_no_new_scope ::= simple_statement ;
527
528 compound_statement_no_new_scope ::= LEFT_BRACE RIGHT_BRACE ;
529 compound_statement_no_new_scope ::= LEFT_BRACE statement_list RIGHT_BRACE ;
530
531 statement_list ::= statement ;
532 statement_list ::= statement_list statement ;
533
534 expression_statement ::= SEMICOLON ;
535 expression_statement ::= expression SEMICOLON ;
536
537 selection_statement ::= IF LEFT_PAREN expression RIGHT_PAREN statement ELSE statement ;
538 selection_statement ::= IF LEFT_PAREN expression RIGHT_PAREN statement ;
539
540 -- Grammar Note: No 'switch'. Switch statements not supported.
541
542 condition ::= expression ;
543 condition ::= fully_specified_type IDENTIFIER EQUAL initializer ;
544
545 iteration_statement ::= WHILE LEFT_PAREN condition RIGHT_PAREN statement_no_new_scope ;
546 iteration_statement ::= DO statement WHILE LEFT_PAREN expression RIGHT_PAREN SEMICOLON ;
547 iteration_statement ::= FOR LEFT_PAREN for_init_statement for_rest_statement RIGHT_PAREN statement_no_new_scope ;
548
549 for_init_statement ::= expression_statement ;
550 for_init_statement ::= declaration_statement ;
551
552 conditionopt ::= ;
553 conditionopt ::= condition ;
554
555 for_rest_statement ::= conditionopt SEMICOLON ;
556 for_rest_statement ::= conditionopt SEMICOLON expression ;
557
558 jump_statement ::= CONTINUE SEMICOLON ;
559 jump_statement ::= BREAK SEMICOLON ;
560 jump_statement ::= RETURN SEMICOLON ;
561 jump_statement ::= RETURN expression SEMICOLON ;
562 jump_statement ::= DISCARD SEMICOLON ; -- Fragment shader only.
563
564 -- Grammar Note: No 'goto'. Gotos are not supported.
565
566 external_declaration ::= function_definition ;
567 external_declaration ::= declaration ;
568
569 function_definition ::= function_prototype compound_statement_no_new_scope ;
570 /.
571 case $rule_number: { // $rule_name
572 qDebug() << "--> function" << *context.function_name;
573 } break;
574 ./
575
576
577
578
579
580 /.
581 } // switch
582
583 state_stack [tos] = nt_action (act, lhs [r] - TERMINAL_COUNT);
584 }
585
586 else
587 {
588 // ### ERROR RECOVERY HERE
589 break;
590 }
591 }
592
593 fprintf (stderr, "%s:%d: Syntax Error\n", qPrintable(context.fileName), context.line);
594
595 return false;
596 }
597
598 #include "glsl-lex.incl"
599
600
601 /////////////////////////////
602 // entry point
603 /////////////////////////////
604 int main()
605 {
606 #if 0 // dump the GLSL grammar
607 for (int r = 0; r < GLSLParserTable::RULE_COUNT; ++r)
608 {
609 int ridx = GLSLParserTable::rule_index [r];
610 int rhs = GLSLParserTable::rhs [r];
611 printf ("%3d) %s ::=", r + 1, GLSLParserTable::spell[GLSLParserTable::rule_info [ridx]]);
612 ++ridx;
613 for (int i = ridx; i < ridx + rhs; ++i)
614 {
615 int symbol = GLSLParserTable::rule_info [i];
616 if (const char *name = GLSLParserTable::spell [symbol])
617 printf (" %s", name);
618 else
619 printf (" #%d", symbol);
620 }
621 printf ("\n");
622 }
623 #endif
624
625 GLSLParser parser;
626
627 if (parser.parse())
628 qDebug() << "OK";
629 else
630 qDebug() << "KO";
631 }
632
633 ./