summaryrefslogtreecommitdiff
path: root/util/qlalr/examples/glsl/glsl.g
blob: 669f334a201d15a70c42ede94aec69af215b6bf3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
----------------------------------------------------------------------------
--
-- Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
-- All rights reserved.
-- Contact: Nokia Corporation (qt-info@nokia.com)
--
-- This file is part of the QtCore module of the Qt Toolkit.
--
-- $QT_BEGIN_LICENSE:LGPL$
-- No Commercial Usage
-- This file contains pre-release code and may not be distributed.
-- You may use this file in accordance with the terms and conditions
-- contained in the Technology Preview License Agreement accompanying
-- this package.
--
-- GNU Lesser General Public License Usage
-- Alternatively, this file may be used under the terms of the GNU Lesser
-- General Public License version 2.1 as published by the Free Software
-- Foundation and appearing in the file LICENSE.LGPL included in the
-- packaging of this file.  Please review the following information to
-- ensure the GNU Lesser General Public License version 2.1 requirements
-- will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
--
-- In addition, as a special exception, Nokia gives you certain additional
-- rights.  These rights are described in the Nokia Qt LGPL Exception
-- version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
--
-- If you have questions regarding the use of this file, please contact
-- Nokia at qt-info@nokia.com.
--
--
--
--
--
--
--
--
-- $QT_END_LICENSE$
--
----------------------------------------------------------------------------

%parser GLSLParserTable
%merged_output glsl.cpp

%token ADD_ASSIGN
%token AMPERSAND
%token AND_ASSIGN
%token AND_OP
%token ATTRIBUTE
%token BANG
%token BOOL
%token BOOLCONSTANT
%token BREAK
%token BVEC2
%token BVEC3
%token BVEC4
%token CARET
%token COLON
%token COMMA
%token CONST
%token CONTINUE
%token DASH
%token DEC_OP
%token DISCARD
%token DIV_ASSIGN
%token DO
%token DOT
%token ELSE
%token EQUAL
%token EQ_OP
%token FLOAT
%token FLOATCONSTANT
%token FOR
%token GE_OP
%token IDENTIFIER
%token IF
%token IN
%token INC_OP
%token INOUT
%token INT
%token INTCONSTANT
%token IVEC2
%token IVEC3
%token IVEC4
%token LEFT_ANGLE
%token LEFT_ASSIGN
%token LEFT_BRACE
%token LEFT_BRACKET
%token LEFT_OP
%token LEFT_PAREN
%token LE_OP
%token MAT2
%token MAT3
%token MAT4
%token MOD_ASSIGN
%token MUL_ASSIGN
%token NE_OP
%token OR_ASSIGN
%token OR_OP
%token OUT
%token PERCENT
%token PLUS
%token QUESTION
%token RETURN
%token RIGHT_ANGLE
%token RIGHT_ASSIGN
%token RIGHT_BRACE
%token RIGHT_BRACKET
%token RIGHT_OP
%token RIGHT_PAREN
%token SAMPLER1D
%token SAMPLER1DSHADOW
%token SAMPLER2D
%token SAMPLER2DSHADOW
%token SAMPLER3D
%token SAMPLERCUBE
%token SEMICOLON
%token SLASH
%token STAR
%token STRUCT
%token SUB_ASSIGN
%token TILDE
%token TYPE_NAME
%token UNIFORM
%token VARYING
%token VEC2
%token VEC3
%token VEC4
%token VERTICAL_BAR
%token VOID
%token WHILE
%token XOR_ASSIGN
%token XOR_OP
%token ERROR
%token HIGH_PRECISION
%token MEDIUM_PRECISION
%token LOW_PRECISION
%start translation_unit


/:

#include <QtCore>

class GLSLParser: protected $table
{
public:
    union Value {
      int i;
      unsigned u;
      unsigned long ul;
      unsigned long long ull;
      long l;
      double d;
      float f;
      const QString *s;
      // ### more...
    };

public:
    GLSLParser();
    ~GLSLParser();

    bool parse();

protected:
    inline void reallocateStack();

    inline Value &sym(int index)
    { return sym_stack [tos + index - 1]; }

    int nextToken();

    bool isTypename(const QString *s) const
    {
      return types.contains(s);
    }

    inline const QString *intern(const QString &s)
    { return &*string_repository.insert(s); }

protected:
    int tos;
    int stack_size;
    Value *sym_stack;
    int *state_stack;
    Value yylval;
    QSet<QString> string_repository;
    QSet<const QString*> types;

    struct /*Context*/ {
      int line;
      const QString *function_name;
      QString fileName;

      void init()
      {
        line = 1;
        function_name = 0;
        fileName.clear();
      }
    } context;
};

inline void GLSLParser::reallocateStack()
{
    if (! stack_size)
        stack_size = 128;
    else
        stack_size <<= 1;

    sym_stack = reinterpret_cast<Value*> (qRealloc(sym_stack, stack_size * sizeof(Value)));
    state_stack = reinterpret_cast<int*> (qRealloc(state_stack, stack_size * sizeof(int)));
}

:/


/.

GLSLParser::GLSLParser():
    tos(0),
    stack_size(0),
    sym_stack(0),
    state_stack(0)
{
}

GLSLParser::~GLSLParser()
{
    if (stack_size) {
        qFree(sym_stack);
        qFree(state_stack);
    }
}

bool GLSLParser::parse()
{
  const int INITIAL_STATE = 0;

  int yytoken = -1;

  reallocateStack();

  context.init();
  tos = 0;
  state_stack[++tos] = INITIAL_STATE;

  while (true)
    {
      if (yytoken == -1 && - TERMINAL_COUNT != action_index [state_stack [tos]])
        yytoken = nextToken();

      int act = t_action (state_stack [tos], yytoken);

      if (act == ACCEPT_STATE) {
        return true;
      }

      else if (act > 0)
        {
          if (++tos == stack_size)
            reallocateStack();

          sym_stack [tos] = yylval;
          state_stack [tos] = act;
          yytoken = -1;
        }

      else if (act < 0)
        {
          int r = - act - 1;

          int ridx = rule_index [r];
          printf ("*** reduce using rule %d %s ::=", r + 1, spell[rule_info [ridx]]);
          ++ridx;
          for (int i = ridx; i < ridx + rhs [r]; ++i)
            {
              int symbol = rule_info [i];
              if (const char *name = spell [symbol])
                printf (" %s", name);
              else
                printf (" #%d", symbol);
            }
          printf ("\n");

          tos -= rhs [r];
          act = state_stack [tos++];

          switch (r) {
./


translation_unit ::= external_declaration ;
translation_unit ::= translation_unit external_declaration ;

variable_identifier ::= IDENTIFIER ;

primary_expression ::= variable_identifier ;
primary_expression ::= INTCONSTANT ;
primary_expression ::= FLOATCONSTANT ;
primary_expression ::= BOOLCONSTANT ;
primary_expression ::= LEFT_PAREN expression RIGHT_PAREN ;


postfix_expression ::= primary_expression ;
postfix_expression ::= postfix_expression LEFT_BRACKET integer_expression RIGHT_BRACKET ;
postfix_expression ::= function_call ;
postfix_expression ::= postfix_expression DOT IDENTIFIER ;
postfix_expression ::= postfix_expression DOT TYPE_NAME ;
postfix_expression ::= postfix_expression INC_OP ;
postfix_expression ::= postfix_expression DEC_OP ;


integer_expression ::= expression ;

function_call ::= function_call_generic ;

function_call_generic ::= function_call_header_with_parameters RIGHT_PAREN ;
function_call_generic ::= function_call_header_no_parameters RIGHT_PAREN ;

function_call_header_no_parameters ::= function_call_header VOID ;
function_call_header_no_parameters ::= function_call_header ;


function_call_header_with_parameters ::= function_call_header assignment_expression ;
function_call_header_with_parameters ::= function_call_header_with_parameters COMMA assignment_expression ;

function_call_header ::=  function_identifier LEFT_PAREN ;

function_identifier ::= constructor_identifier ;
function_identifier ::= IDENTIFIER ;


constructor_identifier ::= FLOAT ;
constructor_identifier ::= INT ;
constructor_identifier ::= BOOL ;
constructor_identifier ::= VEC2 ;
constructor_identifier ::= VEC3 ;
constructor_identifier ::= VEC4 ;
constructor_identifier ::= BVEC2 ;
constructor_identifier ::= BVEC3 ;
constructor_identifier ::= BVEC4 ;
constructor_identifier ::= IVEC2 ;
constructor_identifier ::= IVEC3 ;
constructor_identifier ::= IVEC4 ;
constructor_identifier ::= MAT2 ;
constructor_identifier ::= MAT3 ;
constructor_identifier ::= MAT4 ;
constructor_identifier ::= TYPE_NAME ;

unary_expression ::= postfix_expression ;
unary_expression ::= INC_OP unary_expression ;
unary_expression ::= DEC_OP unary_expression ;
unary_expression ::= unary_operator unary_expression ;

-- Grammar Note:  No traditional style type casts.

unary_operator ::= PLUS ;
unary_operator ::= DASH ;
unary_operator ::= BANG ;
unary_operator ::= TILDE  ;    -- reserved

-- Grammar Note:  No '*' or '&' unary ops. Pointers are not supported.

multiplicative_expression ::= unary_expression ;
multiplicative_expression ::= multiplicative_expression STAR unary_expression ;
multiplicative_expression ::= multiplicative_expression SLASH unary_expression ;
multiplicative_expression ::= multiplicative_expression PERCENT unary_expression ;          -- reserved


additive_expression ::= multiplicative_expression ;
additive_expression ::= additive_expression PLUS multiplicative_expression ;
additive_expression ::= additive_expression DASH multiplicative_expression ;

shift_expression ::= additive_expression ;
shift_expression ::= shift_expression LEFT_OP additive_expression ;  -- reserved
shift_expression ::= shift_expression RIGHT_OP additive_expression ;  -- reserved

relational_expression ::= shift_expression ;
relational_expression ::= relational_expression LEFT_ANGLE shift_expression ;
relational_expression ::= relational_expression RIGHT_ANGLE shift_expression ;
relational_expression ::= relational_expression LE_OP shift_expression ;
relational_expression ::= relational_expression GE_OP shift_expression ;

equality_expression ::= relational_expression ;
equality_expression ::= equality_expression EQ_OP relational_expression ;
equality_expression ::= equality_expression NE_OP relational_expression ;

and_expression ::= equality_expression ;
and_expression ::= and_expression AMPERSAND equality_expression ; -- reserved

exclusive_or_expression ::= and_expression ;
exclusive_or_expression ::= exclusive_or_expression CARET and_expression ; -- reserved

inclusive_or_expression ::= exclusive_or_expression ;
inclusive_or_expression ::= inclusive_or_expression VERTICAL_BAR exclusive_or_expression ; -- reserved

logical_and_expression ::= inclusive_or_expression ;
logical_and_expression ::= logical_and_expression AND_OP inclusive_or_expression ;

logical_xor_expression ::= logical_and_expression ;
logical_xor_expression ::= logical_xor_expression XOR_OP logical_and_expression ;

logical_or_expression ::= logical_xor_expression ;
logical_or_expression ::= logical_or_expression OR_OP logical_xor_expression ;

conditional_expression ::= logical_or_expression ;
conditional_expression ::= logical_or_expression QUESTION expression COLON conditional_expression ;

assignment_expression ::= conditional_expression ;
assignment_expression ::= unary_expression assignment_operator assignment_expression ;

assignment_operator ::= EQUAL ;
assignment_operator ::= MUL_ASSIGN ;
assignment_operator ::= DIV_ASSIGN ;
assignment_operator ::= MOD_ASSIGN ; -- reserved
assignment_operator ::= ADD_ASSIGN ;
assignment_operator ::= SUB_ASSIGN ;
assignment_operator ::= LEFT_ASSIGN ; -- reserved
assignment_operator ::= RIGHT_ASSIGN ; -- reserved
assignment_operator ::= AND_ASSIGN ; -- reserved
assignment_operator ::= XOR_ASSIGN ; -- reserved
assignment_operator ::= OR_ASSIGN ; -- reserved

expression ::= assignment_expression ;
expression ::= expression COMMA assignment_expression ;

constant_expression ::= conditional_expression ;

declaration ::= function_prototype SEMICOLON ;
declaration ::= init_declarator_list SEMICOLON ;

function_prototype ::= function_declarator RIGHT_PAREN ;

function_declarator ::= function_header ;
function_declarator ::= function_header_with_parameters ;

function_header_with_parameters ::= function_header parameter_declaration ;
function_header_with_parameters ::= function_header_with_parameters COMMA parameter_declaration ;

function_header ::= fully_specified_type IDENTIFIER LEFT_PAREN ;
/.
case $rule_number: {
  context.function_name = sym(2).s;
} break;
./

parameter_declarator ::= type_specifier IDENTIFIER ;
parameter_declarator ::= type_specifier IDENTIFIER LEFT_BRACKET constant_expression RIGHT_BRACKET ;

parameter_declaration ::= type_qualifier parameter_qualifier parameter_declarator ;
parameter_declaration ::= parameter_qualifier parameter_declarator ;
parameter_declaration ::= type_qualifier parameter_qualifier parameter_type_specifier ;
parameter_declaration ::= parameter_qualifier parameter_type_specifier ;

parameter_qualifier ::=  ;
parameter_qualifier ::= IN ;
parameter_qualifier ::= OUT ;
parameter_qualifier ::= INOUT ;

parameter_type_specifier ::= type_specifier ;
parameter_type_specifier ::= type_specifier LEFT_BRACKET constant_expression RIGHT_BRACKET ;

init_declarator_list ::= single_declaration ;
init_declarator_list ::= init_declarator_list COMMA IDENTIFIER ;
init_declarator_list ::= init_declarator_list COMMA IDENTIFIER LEFT_BRACKET RIGHT_BRACKET ;
init_declarator_list ::= init_declarator_list COMMA IDENTIFIER LEFT_BRACKET constant_expression ;
init_declarator_list ::= RIGHT_BRACKET ;
init_declarator_list ::= init_declarator_list COMMA IDENTIFIER EQUAL initializer ;

single_declaration ::= fully_specified_type ;
single_declaration ::= fully_specified_type IDENTIFIER ;
single_declaration ::= fully_specified_type IDENTIFIER LEFT_BRACKET RIGHT_BRACKET ;
single_declaration ::= fully_specified_type IDENTIFIER LEFT_BRACKET constant_expression RIGHT_BRACKET ;
single_declaration ::= fully_specified_type IDENTIFIER EQUAL initializer ;

-- Grammar Note:  No 'enum', or 'typedef'.

--fully_specified_type ::= type_specifier ;
--fully_specified_type ::= type_qualifier type_specifier ;

fully_specified_type ::= type_specifier ;
fully_specified_type ::= type_qualifier ;
fully_specified_type ::= fully_specified_type type_specifier ;
fully_specified_type ::= fully_specified_type type_qualifier ;

type_qualifier ::= CONST ;
type_qualifier ::= ATTRIBUTE ;           -- Vertex only.
type_qualifier ::= VARYING ;
type_qualifier ::= UNIFORM ;

type_specifier ::= type_specifier_no_prec ;
type_specifier ::= precision_qualifier type_specifier_no_prec ;

type_specifier_no_prec ::= VOID ;
type_specifier_no_prec ::= FLOAT ;
type_specifier_no_prec ::= INT ;
type_specifier_no_prec ::= BOOL ;
type_specifier_no_prec ::= VEC2 ;
type_specifier_no_prec ::= VEC3 ;
type_specifier_no_prec ::= VEC4 ;
type_specifier_no_prec ::= BVEC2 ;
type_specifier_no_prec ::= BVEC3 ;
type_specifier_no_prec ::= BVEC4 ;
type_specifier_no_prec ::= IVEC2 ;
type_specifier_no_prec ::= IVEC3 ;
type_specifier_no_prec ::= IVEC4 ;
type_specifier_no_prec ::= MAT2 ;
type_specifier_no_prec ::= MAT3 ;
type_specifier_no_prec ::= MAT4 ;
type_specifier_no_prec ::= SAMPLER1D ;
type_specifier_no_prec ::= SAMPLER2D ;
type_specifier_no_prec ::= SAMPLER3D ;
type_specifier_no_prec ::= SAMPLERCUBE ;
type_specifier_no_prec ::= SAMPLER1DSHADOW ;
type_specifier_no_prec ::= SAMPLER2DSHADOW ;
type_specifier_no_prec ::= struct_specifier ;
type_specifier_no_prec ::= TYPE_NAME ;

precision_qualifier ::= HIGH_PRECISION ;
precision_qualifier ::= MEDIUM_PRECISION ;
precision_qualifier ::= LOW_PRECISION ;

struct_specifier ::= STRUCT IDENTIFIER LEFT_BRACE struct_declaration_list RIGHT_BRACE ;
/.
case $rule_number: {
  types.insert(sym(2).s);
} break;
./

struct_specifier ::= STRUCT LEFT_BRACE struct_declaration_list RIGHT_BRACE ;

struct_declaration_list ::= struct_declaration ;
struct_declaration_list ::= struct_declaration_list struct_declaration ;

struct_declaration ::= type_specifier struct_declarator_list SEMICOLON ;

struct_declarator_list ::= struct_declarator ;
struct_declarator_list ::= struct_declarator_list COMMA struct_declarator ;

struct_declarator ::= IDENTIFIER ;
struct_declarator ::= IDENTIFIER LEFT_BRACKET constant_expression RIGHT_BRACKET ;

initializer ::= assignment_expression ;

declaration_statement ::= declaration ;

statement ::= compound_statement ;
statement ::= simple_statement ;

-- Grammar Note:  No labeled statements; 'goto' is not supported.

simple_statement ::= declaration_statement ;
simple_statement ::= expression_statement ;
simple_statement ::= selection_statement ;
simple_statement ::= iteration_statement ;
simple_statement ::= jump_statement ;

compound_statement ::= LEFT_BRACE RIGHT_BRACE ;
compound_statement ::= LEFT_BRACE statement_list RIGHT_BRACE ;

statement_no_new_scope ::= compound_statement_no_new_scope ;
statement_no_new_scope ::= simple_statement ;

compound_statement_no_new_scope ::= LEFT_BRACE RIGHT_BRACE ;
compound_statement_no_new_scope ::= LEFT_BRACE statement_list RIGHT_BRACE ;

statement_list ::= statement ;
statement_list ::= statement_list statement ;

expression_statement ::= SEMICOLON ;
expression_statement ::= expression SEMICOLON ;

selection_statement ::= IF LEFT_PAREN expression RIGHT_PAREN statement ELSE statement ;
selection_statement ::= IF LEFT_PAREN expression RIGHT_PAREN statement ;

-- Grammar Note:  No 'switch'. Switch statements not supported.

condition ::= expression ;
condition ::= fully_specified_type IDENTIFIER EQUAL initializer ;

iteration_statement ::= WHILE LEFT_PAREN condition RIGHT_PAREN statement_no_new_scope ;
iteration_statement ::= DO statement WHILE LEFT_PAREN expression RIGHT_PAREN SEMICOLON ;
iteration_statement ::= FOR LEFT_PAREN for_init_statement for_rest_statement RIGHT_PAREN statement_no_new_scope ;

for_init_statement ::= expression_statement ;
for_init_statement ::= declaration_statement ;

conditionopt ::=  ;
conditionopt ::=  condition ;

for_rest_statement ::= conditionopt SEMICOLON ;
for_rest_statement ::= conditionopt SEMICOLON expression ;

jump_statement ::= CONTINUE SEMICOLON ;
jump_statement ::= BREAK SEMICOLON ;
jump_statement ::= RETURN SEMICOLON ;
jump_statement ::= RETURN expression SEMICOLON ;
jump_statement ::= DISCARD SEMICOLON ;                -- Fragment shader only.

-- Grammar Note:  No 'goto'. Gotos are not supported.

external_declaration ::= function_definition ;
external_declaration ::= declaration ;

function_definition ::= function_prototype compound_statement_no_new_scope ;
/.
    case $rule_number: { // $rule_name
       qDebug() << "--> function" << *context.function_name;
    }  break;
./





/.
          } // switch

          state_stack [tos] = nt_action (act, lhs [r] - TERMINAL_COUNT);
        }

      else
        {
          // ### ERROR RECOVERY HERE
          break;
        }
    }

    fprintf (stderr, "%s:%d: Syntax Error\n", qPrintable(context.fileName), context.line);

    return false;
}

#include "glsl-lex.incl"


/////////////////////////////
// entry point
/////////////////////////////
int main()
{
#if 0 // dump the GLSL grammar
    for (int r = 0; r < GLSLParserTable::RULE_COUNT; ++r)
      {
        int ridx = GLSLParserTable::rule_index [r];
        int rhs = GLSLParserTable::rhs [r];
        printf ("%3d) %s ::=", r + 1, GLSLParserTable::spell[GLSLParserTable::rule_info [ridx]]);
        ++ridx;
        for (int i = ridx; i < ridx + rhs; ++i)
          {
            int symbol = GLSLParserTable::rule_info [i];
            if (const char *name = GLSLParserTable::spell [symbol])
              printf (" %s", name);
            else
              printf (" #%d", symbol);
          }
        printf ("\n");
      }
#endif

    GLSLParser parser;

    if (parser.parse())
        qDebug() << "OK";
    else
        qDebug() << "KO";
}

./