Improve enum assertions generation.
[qtgstreamer:qtgstreamer.git] / codegen / parser.y
1 /*
2     Copyright (C) 2010  George Kiagiadakis <kiagiadakis.george@gmail.com>
3
4     This library is free software; you can redistribute it and/or modify
5     it under the terms of the GNU Lesser General Public License as published
6     by the Free Software Foundation; either version 2.1 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU Lesser General Public License
15     along with this program.  If not, see <http://www.gnu.org/licenses/>.
16 */
17 %{
18 #include "yystype.h"
19 #include "generator.h"
20
21 int yylex(CodeGen *codegen);
22 void yyerror(CodeGen *codegen, const char *msg);
23
24 %}
25
26 %parse-param {CodeGen *codegen}
27 %lex-param {CodeGen *codegen}
28
29 %start header
30
31 %token REGISTER_TYPE_BEGIN
32        SCOPE_RESOLUTION_OPERATOR
33        REGISTER_TYPE_END
34        INSTRUCTIONS_BEGIN
35        INSTRUCTIONS_ASSIGN_OPERATOR
36        INSTRUCTIONS_SEPARATOR
37        INSTRUCTIONS_END
38        ENUM_KEYWORD
39        LEFT_BRACE
40        RIGHT_BRACE
41        COMMA
42        SEMICOLON
43
44 %token <Id> IDENTIFIER
45 %destructor { delete $$; } IDENTIFIER
46
47 %type <IdList> enum_list
48 %destructor { delete $$; } enum_list
49
50 %type <Id> optional_enum_id
51 %destructor { delete $$; } optional_enum_id
52
53 %type <HashMap> optional_instructions
54 %destructor { delete $$; } optional_instructions
55
56 %type <HashMap> instruction_list
57 %destructor { delete $$; } instruction_list
58
59 %type <Pair> instruction
60 %destructor { delete $$; } instruction
61
62 %%
63
64 header: enum_def_list type_registrations_list;
65
66
67 enum_def_list: enum_def_list enum_def | enum_def | /*empty*/;
68
69 enum_def:
70     ENUM_KEYWORD IDENTIFIER LEFT_BRACE optional_instructions enum_list RIGHT_BRACE SEMICOLON
71     {
72         codegen->addEnum(*$5, *$4);
73         delete $2;
74         delete $4;
75         delete $5;
76     };
77
78 enum_list:
79     enum_list COMMA IDENTIFIER
80     {
81         $$ = $1;
82         $$->append(*$3);
83         delete $3;
84     }
85     | IDENTIFIER
86     {
87         $$ = new QList<QByteArray>();
88         $$->append(*$1);
89         delete $1;
90     };
91
92
93 type_registrations_list: type_registrations_list type_registration | type_registration | /*empty*/;
94
95 type_registration:
96     REGISTER_TYPE_BEGIN IDENTIFIER SCOPE_RESOLUTION_OPERATOR IDENTIFIER optional_enum_id REGISTER_TYPE_END optional_instructions
97     {
98         codegen->addTypeRegistration(*$2, *$4, *$5, *$7);
99         delete $2;
100         delete $4;
101         delete $5;
102         delete $7;
103     };
104
105 optional_enum_id:
106     SCOPE_RESOLUTION_OPERATOR IDENTIFIER
107     {
108         $$ = $2;
109     }
110     | /*empty*/
111     {
112         $$ = new QByteArray();
113     };
114
115
116 optional_instructions:
117     INSTRUCTIONS_BEGIN instruction_list INSTRUCTIONS_END
118     {
119         $$ = $2;
120     }
121     | /*empty*/
122     {
123         $$ = new QHash<QByteArray, QByteArray>();
124     };
125
126 instruction_list:
127     instruction_list INSTRUCTIONS_SEPARATOR instruction
128     {
129         $$ = $1;
130         $$->insert($3->first, $3->second);
131         delete $3;
132     }
133     | instruction
134     {
135         $$ = new QHash<QByteArray, QByteArray>();
136         $$->insert($1->first, $1->second);
137         delete $1;
138     };
139
140 instruction:
141     IDENTIFIER INSTRUCTIONS_ASSIGN_OPERATOR IDENTIFIER
142     {
143         $$ = new QPair<QByteArray, QByteArray>();
144         $$->first = *$1;
145         $$->second = *$3;
146         delete $1;
147         delete $3;
148     }
149     | IDENTIFIER
150     {
151         $$ = new QPair<QByteArray, QByteArray>();
152         $$->first = *$1;
153         delete $1;
154     };
155
156
157 %%
158
159 void yyerror(CodeGen *codegen, const char *msg)
160 {
161     codegen->fatalError(msg);
162 }