git stults online reflex / master rx-parse.y
master

Tree @master (Download .tar.gz)

rx-parse.y @masterraw · history · blame

  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
%{
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include "rx-limits.h"
#include "rx-parse.h"
    
extern int yylex();

/* this is re-declared to save some warnings */
void yyerror(YYLTYPE *, void *, const char *);
%}

%union {
    bool  boolean;
    int   token;
    int   integer;
    float floating;
    char *string;
}

%code requires {
/* this is required by the lexer, too, so we need to declare it in the
 * header of the *generated file* */
char *str_reverse(const char *str);
}

/* terminal tokens that are generic bison tokens -- ints ie */
%token<token> BOOL_TYPE INT_TYPE FLOAT_TYPE STR_TYPE LIST_TYPE STRUCT_TYPE XFORM_TYPE
%token<token> LEFT_ARROW RIGHT_ARROW LEFT_BIG_ARROW RIGHT_BIG_ARROW
%token<token> ETC QUALIFY RIGHT_PAREN LEFT_PAREN RIGHT_RW LEFT_RW
     
%token <boolean> BOOL
%token <integer> RIGHT_INT LEFT_INT
%token <floating> RIGHT_FLOAT LEFT_FLOAT
%token <string> ID STR UNCONVERTED_INT UNCONVERTED_FLOAT

 /* TODO what to do about ! here? and what to do about _ generic operator? does
  * the spec need to provide a way for the caller to set the precedence? */
%right '='
%left  '<' '>'  RIGHT_NE LEFT_NE RIGHT_LEQ LEFT_LEQ RIGHT_GEQ LEFT_GEQ EQ
%left  '|' '^' '&'
%left  '+' '-'
%left  '*' '/'
%left  '%'
%left  NEG

/* pure reentrant parser */
%define api.pure full

/* enable locations -- this adds another argument (a YYLTYPE*) to yylex and
 * associated calls to capture the lexer poition */
%define locations

%define parse.error verbose

/* directs bison to add this as an argument to the yyparse function it
 * generates and to call yylex with this argument, also. flex's reentrent
 * option in the lexer code will add this argument to the generated yylex
 * function, so that it can be called at all. these two methods don't
 * coordinate at code generation time. */
%param { void *scanner }

/* "unit" is the first non-terminal rule in the grammar -- this is redundant
 * but let's be explicit. */
%start unit

%%

/* note that this goes through all the permutations here so as to remove
 * ambiguity and some conflicts */
unit:
RULE(main)
| RULE(toplevel_stmnts)
| RULE(toplevel_stmnts main)
| RULE(main toplevel_stmnts)
| RULE(toplevel_stmnts main toplevel_stmnts)
;

main:
RULE(right_main_decl)
| RULE(left_main_decl)
;

/* these are statements that can be made within the unit scope of the
 * program. with the exception of statis declarations, they can *only* be
 * declared at the top level. */
toplevel_stmnts:
RULE(toplevel_stmnts toplevel_stmnt)
| RULE(toplevel_stmnt)
;

toplevel_stmnt:
RULE(right_namespace_xform) /* package import */
| RULE(left_namespace_xform) /* package import */
| RULE(right_xform_decl)
| RULE(left_xform_decl)
| RULE(right_static_decl)
| RULE(left_static_decl)
; 

/* these are statements (lines, more or less) that cannot be declared directly
 * within the program unit scope -- all scopes are directed, because they must
 * be defined within a scoping language construct and that provides a
 * direction */
right_scoped_stmnts:
RULE(right_scoped_stmnts right_scoped_stmnt)
| RULE(right_scoped_stmnt)
;

left_scoped_stmnts:
RULE(left_scoped_stmnts left_scoped_stmnt)
| RULE(left_scoped_stmnt)
;

right_scoped_stmnt:
RULE(right_static_decl)
| RULE(right_decl)
| RULE(right_free_io)
;

left_scoped_stmnt:
RULE(left_static_decl)
| RULE(left_decl)
| RULE(left_free_io)
;

/* io that can accept free expressions; a non free version used to exist but
 * now nothing uses it so I removd it */
right_free_io:
RULE(right_free_expr RIGHT_RW)
| RULE(nested_id LEFT_RW)
;

left_free_io:
RULE(LEFT_RW left_free_expr)
| RULE(RIGHT_RW nested_id)
;

right_main_decl:
RULE('=' right_xform)
;

left_main_decl:
RULE(left_free_expr LEFT_ARROW left_pattern '=')
| RULE(LEFT_BIG_ARROW left_free_expr LEFT_ARROW left_pattern '=')
| RULE(left_namespace_xform left_free_expr LEFT_ARROW left_pattern '=')
| RULE(left_free_io LEFT_ARROW left_pattern '=')
| RULE(LEFT_BIG_ARROW left_free_io LEFT_ARROW left_pattern '=')
| RULE(left_namespace_xform left_free_io LEFT_ARROW left_pattern '=')
| RULE(LEFT_ARROW left_pattern '=' left_xform_multiline)
| RULE(LEFT_BIG_ARROW LEFT_ARROW left_pattern '=' left_xform_multiline)
| RULE(left_namespace_xform LEFT_ARROW left_pattern '=' left_xform_multiline)
;

right_static_decl:
RULE(id '=' right_static_expr)
;

left_static_decl:
RULE(left_static_expr '=' id)
;

right_decl:
RULE(id '=' right_free_expr)
;

left_decl:
RULE(left_free_expr '=' id)
;

/*
foo pattern -> oneline
foo pattern -> multiline
foo pattern -> oneline => [baz [: bar]]
foo pattern -> => [baz [: bar]] multiline
*/
right_xform_decl:
RULE(id '=' right_xform)
;

right_xform:
RULE(right_pattern RIGHT_ARROW right_free_expr)
| RULE(right_pattern RIGHT_ARROW right_free_expr RIGHT_BIG_ARROW)
| RULE(right_pattern RIGHT_ARROW right_free_expr right_namespace_xform)
| RULE(right_pattern RIGHT_ARROW right_free_io)
| RULE(right_pattern RIGHT_ARROW right_free_io RIGHT_BIG_ARROW)
| RULE(right_pattern RIGHT_ARROW right_free_io right_namespace_xform)
| RULE(right_pattern RIGHT_ARROW right_xform_multiline)
| RULE(right_pattern RIGHT_ARROW RIGHT_BIG_ARROW right_xform_multiline)
| RULE(right_pattern RIGHT_ARROW right_namespace_xform right_xform_multiline)
;

left_xform_decl:
RULE(left_free_expr LEFT_ARROW left_pattern '=' id)
| RULE(LEFT_BIG_ARROW left_free_expr LEFT_ARROW left_pattern '=' id)
| RULE(left_namespace_xform left_free_expr LEFT_ARROW left_pattern '=' id)
| RULE(left_free_io LEFT_ARROW left_pattern '=' id)
| RULE(LEFT_BIG_ARROW left_free_io LEFT_ARROW left_pattern '=' id)
| RULE(left_namespace_xform left_free_io LEFT_ARROW left_pattern '=' id)
| RULE(LEFT_ARROW left_pattern '=' id left_xform_multiline)
| RULE(LEFT_BIG_ARROW LEFT_ARROW left_pattern '=' id left_xform_multiline)
| RULE(left_namespace_xform LEFT_ARROW left_pattern '=' id left_xform_multiline)
;

right_xform_multiline:
RULE(right_xform_body_switch)
| RULE(right_xform_body_freeform)
;

left_xform_multiline:
RULE(left_xform_body_switch)
| RULE(left_xform_body_freeform)
;

/*
[| expr -> expr]
.
.      -> expr
*/
right_xform_body_switch:
RULE(right_xform_body_cases right_xform_body_default_case)
;

right_xform_body_cases:
RULE(right_xform_body_cases right_xform_body_case)
| RULE(right_xform_body_case)
;

right_xform_body_case:
RULE(right_free_expr RIGHT_ARROW right_free_expr)
| RULE(right_free_expr RIGHT_ARROW right_free_io)
;

right_xform_body_default_case:
RULE(RIGHT_ARROW right_free_expr)
| RULE(RIGHT_ARROW right_free_io)
;

left_xform_body_switch:
RULE(left_xform_body_cases left_xform_body_default_case)
;

left_xform_body_cases:
RULE(left_xform_body_cases left_xform_body_case)
| RULE(left_xform_body_case)
;

left_xform_body_case:
RULE(left_free_expr LEFT_ARROW left_free_expr)
| RULE(left_free_io LEFT_ARROW left_free_expr)
;

left_xform_body_default_case:
RULE(left_free_expr LEFT_ARROW)
| RULE(left_free_io LEFT_ARROW)
;

right_xform_body_freeform:
RULE(right_scoped_stmnts RIGHT_ARROW right_free_expr)
| RULE(right_scoped_stmnts RIGHT_ARROW right_free_io)
;

left_xform_body_freeform:
RULE(left_scoped_stmnts left_free_expr LEFT_ARROW)
| RULE(left_scoped_stmnts left_free_io LEFT_ARROW)
;

right_namespace_xform:
RULE(RIGHT_BIG_ARROW id)
| RULE(RIGHT_BIG_ARROW id QUALIFY id)
;

left_namespace_xform:
RULE(id LEFT_BIG_ARROW)
| RULE(id QUALIFY id LEFT_BIG_ARROW)
;

right_pattern:
RULE(right_pattern right_free_typed_expr)
| RULE(right_free_typed_expr)
;

left_pattern:
RULE(left_pattern left_free_typed_expr)
| RULE(left_free_typed_expr)
;

/* TODO some sugar for list syntax and etc? also need to be able to handle
 * generic operator... maybe that should be removed as a thing, because then
 * what happens with precedence in the pattern matching? */
right_free_typed_id:
RULE(id)
| RULE(id type)
| RULE(id label)
| RULE(id type label)
| RULE(ETC)
| RULE(right_etc_pattern)
;

left_free_typed_id:
RULE(id)
| RULE(type id)
| RULE(label id)
| RULE(label type id)
| RULE(ETC)
| RULE(left_etc_pattern)
;

right_etc_pattern:
RULE(ETC label)
| RULE(id ETC label)
| RULE(id ETC label type)
| RULE(id ETC       type)
| RULE(ETC label id)
| RULE(ETC label id type)
| RULE(ETC       id)
| RULE(ETC       id type)
| RULE(ETC label id ETC       bidir_type)
| RULE(ETC label id ETC label bidir_type)
| RULE(ETC       id ETC label bidir_type)
| RULE(ETC       id ETC       bidir_type)
;

left_etc_pattern:
RULE(label ETC)
| RULE(     label ETC id)
| RULE(type       ETC id)
| RULE(type label ETC id)
| RULE(     id       ETC)
| RULE(     id label ETC)
| RULE(type id label ETC)
| RULE(bidir_type       ETC id       ETC)
| RULE(bidir_type label ETC id       ETC)
| RULE(bidir_type       ETC id label ETC)
| RULE(bidir_type label ETC id label ETC)
;

etc_concat:
RULE('<' etc_concat '>')
| RULE(ETC id)
| RULE(id ETC)
| RULE(ETC)
;

generic_concat:
RULE('<' concat_list '>')
;

concat_list:
RULE(concat_list id)
| RULE(id)
;

label:
RULE('<' id '>')
;

types:
RULE(types type)
| RULE(type)
;

type:
RULE(unidir_type)
| RULE(bidir_type)
;

unidir_type:
RULE(BOOL_TYPE)
| RULE(INT_TYPE)
| RULE(STR_TYPE)
| RULE(LIST_TYPE)
| RULE(STRUCT_TYPE)
| RULE('[' type ']') /* direct list type */
| RULE('{' types '}') /* direct struct type */
| RULE(XFORM_TYPE)
;

bidir_type:
RULE(FLOAT_TYPE)
;

/* free expressions are expressions that can be used in the pattern of an xform
 * declaration. they cannot use typing symbols, labels, or generic operations,
 * but can use ETC concatenation */
right_free_expr:
RULE(LEFT_PAREN right_free_expr RIGHT_PAREN)
| RULE(right_static_expr)
| RULE(right_xform_apply)
| RULE(etc_concat)
| RULE(generic_concat)
;

left_free_expr:
RULE(LEFT_PAREN left_free_expr RIGHT_PAREN)
| RULE(left_static_expr)
| RULE(left_xform_apply)
| RULE(etc_concat)
| RULE(generic_concat)
;

/* free expressions are expressions that can be used in the pattern of an xform
 * declaration. they are directed, cannot include function applications, and
 * have some special structure around identifiers, and can include typing
 * information. */
right_free_typed_expr:
RULE(LEFT_PAREN right_free_typed_expr RIGHT_PAREN)
| RULE(right_static_expr)
| RULE(right_free_typed_id)
;

left_free_typed_expr:
RULE(LEFT_PAREN left_free_typed_expr RIGHT_PAREN)
| RULE(left_static_expr)
| RULE(left_free_typed_id)
;

/* (non-static, non-free) expressions are those that can also include static
 * declarations. TODO what to do about ! unary operator in all of the below?
 * ... ? maybe need a special boolean expr so that this can be applied to that
 * as a unary */
right_expr:
RULE(LEFT_PAREN right_expr RIGHT_PAREN)
| RULE(right_static_expr)
| RULE(right_xform_apply)
;

left_expr:
RULE(LEFT_PAREN left_expr RIGHT_PAREN)
| RULE(left_static_expr)
| RULE(left_xform_apply)
;

/* static expressions are ones that don't require function applications to
 * evaluate. NOTE probably need to explicitly specify boolean comparisons so
 * that we can correctly apply left vs right GEQ vs LEQ */
right_static_expr:
RULE(LEFT_PAREN right_static_expr RIGHT_PAREN)
| RULE(right_static_expr binary_op right_static_expr)
| RULE(right_static_expr right_boolean_op right_static_expr)
| RULE(unary_op right_static_expr %prec NEG)
| RULE(nested_id)
| RULE(right_literal)
| RULE(literal)
;

left_static_expr:
RULE(LEFT_PAREN left_static_expr RIGHT_PAREN)
| RULE(left_static_expr binary_op left_static_expr)
| RULE(left_static_expr left_boolean_op left_static_expr)
| RULE(left_static_expr unary_op %prec NEG)
| RULE(nested_id)
| RULE(left_literal)
| RULE(literal)
;

unary_op:
RULE('+')
| RULE('-')
| RULE('!')
;

binary_op:
RULE('+')
| RULE('-')
| RULE('*')
| RULE('/')
| RULE('%')
;

right_boolean_op:
RULE(RIGHT_GEQ)
| RULE(RIGHT_LEQ)
| RULE(RIGHT_NE)
| RULE(boolean_op)
;

left_boolean_op:
RULE(LEFT_GEQ)
| RULE(LEFT_LEQ)
| RULE(LEFT_NE)
| RULE(boolean_op)
;

boolean_op:
RULE('|')
| RULE('&')
| RULE('^')
| RULE('!')
| RULE('>')
| RULE('<')
| RULE(EQ)
;

right_xform_apply:
RULE(nested_id right_xform_arglist)
;

left_xform_apply:
RULE(left_xform_arglist nested_id)
;

right_xform_arglist:
RULE(right_xform_arglist right_expr)
| RULE(right_expr)
;

left_xform_arglist:
RULE(left_xform_arglist left_expr)
| RULE(left_expr)
;

id:
RULE(ID) { printf("id --> %s\n", $1); free($1); $1 = NULL; }
;

nested_id:
RULE(nested_id QUALIFY id)
| RULE(id)
;

literal:
RULE(BOOL)
;

/* TODO list and struct decls should't be here because they can contain non-static exprs */
right_literal:
RULE(STR) { fprintf(stderr, "%s\n", $1); free($1); $1 = NULL; }
| RULE(RIGHT_INT)
| RULE(RIGHT_FLOAT)
| RULE(UNCONVERTED_INT) { fprintf(stderr, "%s -> %d\n", $1, atoi($1)); free($1); $1 = NULL; }
| RULE(UNCONVERTED_FLOAT) { fprintf(stderr, "%s -> %f\n", $1, atof($1)); free($1); $1 = NULL; }
| RULE(right_list)
| RULE(right_struct)
;

left_literal:
RULE(STR) { fprintf(stderr, "%s\n", $1); free($1); $1 = NULL; }
| RULE(LEFT_INT) { fprintf(stderr, "%d\n", $1); }
| RULE(LEFT_FLOAT) { fprintf(stderr, "%f\n", $1); }
| RULE(UNCONVERTED_INT) { fprintf(stderr, "%s -> %d\n", $1, atoi(str_reverse($1))); free($1); $1 = NULL; }
| RULE(UNCONVERTED_FLOAT) { fprintf(stderr, "%s -> %f\n", $1, atof(str_reverse($1))); free($1); $1 = NULL; }
| RULE(left_list)
| RULE(left_struct)
;

/* TODO fix this silly symmetry that's actually an asymmetry */
right_list:
RULE('[' right_struct_initlist ']')
;

left_list:
RULE('[' left_struct_initlist ']')
;

/* TODO fix this asymmetry */
right_struct:
RULE('{' right_struct_initlist '}')
;

left_struct:
RULE('{' left_struct_initlist '}')
;

right_struct_initlist:
RULE(right_struct_initlist right_expr)
| RULE(right_expr)
;

/* TODO optional endls here -- also need to ensure that the order in which
 * these are parsed is from top to bottom that's true for everything */
left_struct_initlist:
RULE(left_struct_initlist left_expr)
| RULE(left_expr)
;

%%


/* stupid simple string reverse via copy -- we're using this on lexer state and
 * we don't want to mess around with the contents of its internal buffers. this is
 * only meant to be used on numerics, which we can reverse bytewise. do not use
 * this to reverse unicode. */
char *str_reverse(const char *str)
{
    char * rstr = NULL;
    size_t i;
    size_t len = strnlen(str, STR_MAX);
    rstr = calloc(len + 1, sizeof(char));
    for (i = 0; i < len; ++i)
	rstr[i] = str[len - i - 1];
    return rstr;
    
}

void yyerror(YYLTYPE *loc, void *scanner, const char *msg)
{
    fprintf(stderr, "%d %d:%d %d:%d %s\n", yyget_lineno(scanner), loc->first_line,
	    loc->first_column, loc->last_line, loc->last_column, msg);
    fprintf(stderr, "%s\n", yyget_text(scanner));
}