-
Notifications
You must be signed in to change notification settings - Fork 3
/
parser.y
374 lines (322 loc) · 11.5 KB
/
parser.y
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
/*
Example using Flex and Bison with Qt.
Copyright (C) 2013 Gonzalo Exequiel Pedone
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with This program. If not, see <http://www.gnu.org/licenses/>.
Email : hipersayan DOT x AT gmail DOT com
Web-Site: http://github.com/hipersayanX/FlexBisonQt
*/
%{
#include <QtGui>
extern int yylex(void);
void yyerror(const char *s);
%}
// Here we can define some custom variable types.
// The custom types must be of static size.
%union {
QVariant *QVariant_t;
}
// Define the types for terminal expressions.
%token <QVariant_t> TOK_INTIGER
%token <QVariant_t> TOK_FLOAT
%token <QVariant_t> TOK_BOOLEAN
%token <QVariant_t> TOK_STRING
// Define the tokens for the symbols.
%token TOK_LEFTPAREN
%token TOK_RIGHTPAREN
%token TOK_LEFTCURLYBRACKET
%token TOK_RIGHTCURLYBRACKET
%token TOK_LEFTBRACKET
%token TOK_RIGHTBRACKET
%token TOK_COMMA
%token TOK_COLON
// Define the tokens for the keywords.
%token TOK_SIZE
%token TOK_SIZEF
%token TOK_POINT
%token TOK_POINTF
%token TOK_RECT
%token TOK_RECTF
%token TOK_LINE
%token TOK_LINEF
%token TOK_DATE
%token TOK_TIME
%token TOK_DATETIME
%token TOK_COLOR
%token TOK_BYTES
%token TOK_URL
// Define the types for non-terminal expressions.
%type <QVariant_t> variant
%type <QVariant_t> size
%type <QVariant_t> sizeF
%type <QVariant_t> point
%type <QVariant_t> pointF
%type <QVariant_t> rect
%type <QVariant_t> rectF
%type <QVariant_t> line
%type <QVariant_t> lineF
%type <QVariant_t> date
%type <QVariant_t> time
%type <QVariant_t> dateTime
%type <QVariant_t> color
%type <QVariant_t> bytes
%type <QVariant_t> url
%type <QVariant_t> number
%type <QVariant_t> variantListItems
%type <QVariant_t> variantList
%type <QVariant_t> variantMapPair
%type <QVariant_t> variantMapItems
%type <QVariant_t> variantMap
// Prevents memory leak in non-terminal expressions.
%destructor {delete $$;} variant
%destructor {delete $$;} size
%destructor {delete $$;} sizeF
%destructor {delete $$;} point
%destructor {delete $$;} pointF
%destructor {delete $$;} rect
%destructor {delete $$;} rectF
%destructor {delete $$;} line
%destructor {delete $$;} lineF
%destructor {delete $$;} date
%destructor {delete $$;} time
%destructor {delete $$;} dateTime
%destructor {delete $$;} color
%destructor {delete $$;} bytes
%destructor {delete $$;} url
%destructor {delete $$;} number
%destructor {delete $$;} variantListItems
%destructor {delete $$;} variantList
%destructor {delete $$;} variantMapPair
%destructor {delete $$;} variantMapItems
%destructor {delete $$;} variantMap
%%
/* Main expression to check. */
start: variant {qDebug() << *$1;};
variant: number
| TOK_BOOLEAN
| size
| sizeF
| point
| pointF
| rect
| rectF
| line
| lineF
| date
| time
| dateTime
| color
| bytes
| url
| TOK_STRING
| variantList
| variantMap
;
rect: TOK_RECT TOK_LEFTPAREN TOK_RIGHTPAREN {$$ = new QVariant(QRect());}
| TOK_RECT TOK_LEFTPAREN point TOK_COMMA point TOK_RIGHTPAREN {
// $$ is a reference to the result of the espression.
// $1, $2, $3, ..., $N are references to each expression.
$$ = new QVariant();
*$$ = QRect($3->toPoint(), $5->toPoint());
}
| TOK_RECT TOK_LEFTPAREN point TOK_COMMA size TOK_RIGHTPAREN {
$$ = new QVariant();
*$$ = QRect($3->toPoint(), $5->toSize());
}
| TOK_RECT TOK_LEFTPAREN number TOK_COMMA number TOK_COMMA number TOK_COMMA number TOK_RIGHTPAREN {
$$ = new QVariant();
*$$ = QRect($3->toFloat(), $5->toFloat(), $7->toFloat(), $9->toFloat());
}
;
rectF: TOK_RECTF TOK_LEFTPAREN TOK_RIGHTPAREN {$$ = new QVariant(QRectF());}
| TOK_RECTF TOK_LEFTPAREN pointF TOK_COMMA pointF TOK_RIGHTPAREN {
$$ = new QVariant();
*$$ = QRectF($3->toPointF(), $5->toPointF());
}
| TOK_RECTF TOK_LEFTPAREN pointF TOK_COMMA sizeF TOK_RIGHTPAREN {
$$ = new QVariant();
*$$ = QRectF($3->toPointF(), $5->toSizeF());
}
| TOK_RECTF TOK_LEFTPAREN rect TOK_RIGHTPAREN {
$$ = new QVariant();
*$$ = QRectF($3->toRect());
}
| TOK_RECTF TOK_LEFTPAREN number TOK_COMMA number TOK_COMMA number TOK_COMMA number TOK_RIGHTPAREN {
$$ = new QVariant();
*$$ = QRectF($3->toFloat(), $5->toFloat(), $7->toFloat(), $9->toFloat());
}
;
line: TOK_LINE TOK_LEFTPAREN TOK_RIGHTPAREN {$$ = new QVariant(QLine());}
| TOK_LINE TOK_LEFTPAREN point TOK_COMMA point TOK_RIGHTPAREN {
$$ = new QVariant();
*$$ = QLine($3->toPoint(), $5->toPoint());
}
| TOK_LINE TOK_LEFTPAREN number TOK_COMMA number TOK_COMMA number TOK_COMMA number TOK_RIGHTPAREN {
$$ = new QVariant();
*$$ = QLine($3->toFloat(), $5->toFloat(), $7->toFloat(), $9->toFloat());
}
;
lineF: TOK_LINEF TOK_LEFTPAREN TOK_RIGHTPAREN {$$ = new QVariant(QLineF());}
| TOK_LINEF TOK_LEFTPAREN pointF TOK_COMMA pointF TOK_RIGHTPAREN {
$$ = new QVariant();
*$$ = QLineF($3->toPointF(), $5->toPointF());
}
| TOK_LINEF TOK_LEFTPAREN line TOK_RIGHTPAREN {
$$ = new QVariant();
*$$ = QLineF($3->toLine());
}
| TOK_LINEF TOK_LEFTPAREN number TOK_COMMA number TOK_COMMA number TOK_COMMA number TOK_RIGHTPAREN {
$$ = new QVariant();
*$$ = QLineF($3->toFloat(), $5->toFloat(), $7->toFloat(), $9->toFloat());
}
;
point: TOK_POINT TOK_LEFTPAREN TOK_RIGHTPAREN {$$ = new QVariant(QPoint());}
| TOK_POINT TOK_LEFTPAREN number TOK_COMMA number TOK_RIGHTPAREN {
$$ = new QVariant();
*$$ = QPoint($3->toFloat(), $5->toFloat());
}
;
pointF: TOK_POINTF TOK_LEFTPAREN TOK_RIGHTPAREN {$$ = new QVariant(QPointF());}
| TOK_POINTF TOK_LEFTPAREN point TOK_RIGHTPAREN {
$$ = new QVariant();
*$$ = QPointF($3->toPoint());
}
| TOK_POINTF TOK_LEFTPAREN number TOK_COMMA number TOK_RIGHTPAREN {
$$ = new QVariant();
*$$ = QPointF($3->toFloat(), $5->toFloat());
}
;
size: TOK_SIZE TOK_LEFTPAREN TOK_RIGHTPAREN {$$ = new QVariant(QSize());}
| TOK_SIZE TOK_LEFTPAREN number TOK_COMMA number TOK_RIGHTPAREN {
$$ = new QVariant();
*$$ = QSize($3->toFloat(), $5->toFloat());
}
;
sizeF: TOK_SIZEF TOK_LEFTPAREN TOK_RIGHTPAREN {$$ = new QVariant(QSizeF());}
| TOK_SIZEF TOK_LEFTPAREN size TOK_RIGHTPAREN {
$$ = new QVariant();
*$$ = QSizeF($3->toSize());
}
| TOK_SIZEF TOK_LEFTPAREN number TOK_COMMA number TOK_RIGHTPAREN {
$$ = new QVariant();
*$$ = QSizeF($3->toFloat(), $5->toFloat());
}
;
dateTime: TOK_DATETIME TOK_LEFTPAREN TOK_RIGHTPAREN {$$ = new QVariant(QDateTime());}
| TOK_DATETIME TOK_LEFTPAREN date TOK_RIGHTPAREN {
$$ = new QVariant();
*$$ = QDateTime($3->toDate());
}
| TOK_DATETIME TOK_LEFTPAREN date TOK_COMMA time TOK_RIGHTPAREN {
$$ = new QVariant();
*$$ = QDateTime($3->toDate(), $5->toTime());
}
;
date: TOK_DATE TOK_LEFTPAREN TOK_RIGHTPAREN {$$ = new QVariant(QDate());}
| TOK_DATE TOK_LEFTPAREN number TOK_COMMA number TOK_COMMA number TOK_RIGHTPAREN {
$$ = new QVariant();
*$$ = QDate($3->toFloat(), $5->toFloat(), $7->toFloat());
}
;
time: TOK_TIME TOK_LEFTPAREN TOK_RIGHTPAREN {$$ = new QVariant(QTime());}
| TOK_TIME TOK_LEFTPAREN number TOK_COMMA number TOK_RIGHTPAREN {
$$ = new QVariant();
*$$ = QTime($3->toFloat(), $5->toFloat());
}
| TOK_TIME TOK_LEFTPAREN number TOK_COMMA number TOK_COMMA number TOK_RIGHTPAREN {
$$ = new QVariant();
*$$ = QTime($3->toFloat(), $5->toFloat(), $7->toFloat());
}
| TOK_TIME TOK_LEFTPAREN number TOK_COMMA number TOK_COMMA number TOK_COMMA number TOK_RIGHTPAREN {
$$ = new QVariant();
*$$ = QTime($3->toFloat(), $5->toFloat(), $7->toFloat(), $9->toFloat());
}
;
color: TOK_COLOR TOK_LEFTPAREN TOK_RIGHTPAREN {$$ = new QVariant(QColor());}
| TOK_COLOR TOK_LEFTPAREN TOK_STRING TOK_RIGHTPAREN {
$$ = new QVariant();
*$$ = QColor($3->toString());
}
| TOK_COLOR TOK_LEFTPAREN number TOK_COMMA number TOK_COMMA number TOK_RIGHTPAREN {
$$ = new QVariant();
*$$ = QColor($3->toFloat(), $5->toFloat(), $7->toFloat());
}
| TOK_COLOR TOK_LEFTPAREN number TOK_COMMA number TOK_COMMA number TOK_COMMA number TOK_RIGHTPAREN {
$$ = new QVariant();
*$$ = QColor($3->toFloat(), $5->toFloat(), $7->toFloat(), $9->toFloat());
}
;
bytes: TOK_BYTES TOK_STRING {
$$ = new QVariant();
*$$ = $2->toString().toUtf8();
}
;
url: TOK_URL TOK_LEFTPAREN TOK_RIGHTPAREN {$$ = new QVariant(QUrl());}
| TOK_URL TOK_LEFTPAREN TOK_STRING TOK_RIGHTPAREN {
$$ = new QVariant();
*$$ = QUrl($3->toString());
}
;
variantList: TOK_LEFTBRACKET TOK_RIGHTBRACKET {$$ = new QVariant(QVariantList());}
| TOK_LEFTBRACKET variantListItems TOK_RIGHTBRACKET {
$$ = new QVariant();
*$$ = $2->toList();
}
;
variantListItems: variant {
$$ = new QVariant();
QVariantList variantList;
variantList << *$1;
*$$ = variantList;
}
| variantListItems TOK_COMMA variant {
$$ = new QVariant();
QVariantList variantList($1->toList());
variantList << *$3;
*$$ = variantList;
}
;
variantMap: TOK_LEFTCURLYBRACKET TOK_RIGHTCURLYBRACKET {$$ = new QVariant(QVariantMap());}
| TOK_LEFTCURLYBRACKET variantMapItems TOK_RIGHTCURLYBRACKET {
$$ = new QVariant();
*$$ = $2->toMap();
}
;
variantMapItems: variantMapPair {
$$ = new QVariant();
QVariantMap variantMap;
QVariantList pair = $1->toList();
variantMap[pair[0].toString()] = pair[1];
*$$ = variantMap;
}
| variantMapItems TOK_COMMA variantMapPair {
$$ = new QVariant();
QVariantMap variantMap($1->toMap());
QVariantList pair = $3->toList();
variantMap[pair[0].toString()] = pair[1];
*$$ = variantMap;
}
;
variantMapPair: TOK_STRING TOK_COLON variant {
$$ = new QVariant();
QVariantList variantList;
variantList << $1->toString() << *$3;
*$$ = variantList;
}
;
number: TOK_INTIGER
| TOK_FLOAT
;
%%
void yyerror(const char *s)
{
qDebug() << "error:" << s;
}