Debian Codegraph @

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

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