-
Notifications
You must be signed in to change notification settings - Fork 0
/
mllexgen.c
1194 lines (988 loc) · 34 KB
/
mllexgen.c
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
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* The default lexical-scanner template for Moonlime. Terms under which the
* generated code may be distributed, modified, etc. are provided by the
* lexer-writer below. */
/* A lexer for Moonlime lexers.
* Copyright © 2012 Zachary Catlin. See LICENSE for terms. */
#define ML_ML_LEXER_H
#ifndef ML_UTILS_H
#include "utils.h"
#endif
#ifndef ML_REGEX_H
#include "regex.h"
#endif
typedef enum {
D_NONE,
D_TOP,
D_HEADER,
D_STATE,
D_INITSTATE,
D_PREFIX,
D_USTATE_TYPE
} directive_kind;
struct pattern_entry {
regex_t *rx;
len_string *code;
lstr_list_t *states;
struct pattern_entry *next;
};
typedef struct pattern_entry pat_entry_t;
typedef struct {
directive_kind dir; /* Current type of directive being parsed */
int c_nest_depth; /* Current brace-nesting depth in C code */
int regex_nest_depth; /* Current parenthesis-nesting depth in regex */
len_string *code; /* The current chunk of C code */
regex_t *curr_rx; /* Current regular-expression fragment being worked on */
regex_t *rx_stack; /* Stack of regular-expression fragments -- only
* types R_CONCAT, R_OPTION, and R_PAREN should be on
* the stack! */
lstr_list_t *curr_st; /* List of start states for current fragment */
pat_entry_t *phead; /* First element in the list of regular expression/
* code action pairs */
pat_entry_t *ptail; /* Final element in the list so far */
size_t npats; /* Number of elements in the list */
len_string *header; /* Code to appear both in any generated header file and
* the top of the generated lexer */
len_string *top; /* Code to appear at the top of the lexer, after the
* header */
lstr_list_t *states; /* The list of states */
len_string *initstate; /* Initial start state */
len_string *prefix; /* A prefix to use for names in the generated lexer. */
FILE *verb; /* An optional file to print verbose information */
len_string *ustate_type; /* The type of the (optional) user state object */
} lexer_lexer_state;
void init_lexer_lexer_state(lexer_lexer_state *st);
#ifndef ML_STDIO_H
#define ML_STDIO_H
#include <stdio.h>
#endif
#ifndef ML_STDLIB_H
#define ML_STDLIB_H
#include <stdlib.h>
#endif
#ifndef ML_STRING_H
#define ML_STRING_H
#include <string.h>
#endif
void init_lexer_lexer_state(lexer_lexer_state *st)
{
if(st == NULL)
return;
st->dir = D_NONE;
st->code = st->header = st->top = st->initstate = st->prefix = NULL;
st->curr_rx = st->rx_stack = NULL;
st->phead = st->ptail = NULL;
st->states = st->curr_st = NULL;
st->regex_nest_depth = st->c_nest_depth = 0;
st->npats = 0;
st->verb = NULL;
st->ustate_type = NULL;
}
static void add_simple_regex_impl(lexer_lexer_state *st, regex_t *rx,
const char *fname, int line)
{
regex_t *new_rx;
if(st == NULL || rx == NULL) {
fprintf(stderr, "%s:%d: NULL argument to add_simple_regex\n",
fname, line);
exit(1);
}
if(st->curr_rx == NULL) {
st->curr_rx = rx;
return;
}
if(st->rx_stack == NULL) {
new_rx = mk_concat_rx(0);
add_enc_rx_impl(new_rx, st->curr_rx, fname, line);
st->rx_stack = new_rx;
st->curr_rx = rx;
return;
}
switch(st->rx_stack->type) {
case R_CONCAT:
add_enc_rx_impl(st->rx_stack, st->curr_rx, fname, line);
break;
case R_OPTION:
case R_PAREN:
new_rx = mk_concat_rx(0);
add_enc_rx_impl(new_rx, st->curr_rx, fname, line);
new_rx->next = st->rx_stack;
st->rx_stack = new_rx;
break;
default:
fprintf(stderr, "%s:%d: Bad type %d on the regex stack\n",
fname, line, st->rx_stack->type);
exit(1);
}
st->curr_rx = rx;
}
#define add_simple_regex(st, rx) add_simple_regex_impl((st), (rx), \
__FILE__, __LINE__)
static char unescape_rx_escape(const char *buf)
{
char c;
switch(buf[1]) {
case 'x':
c = (hex_digits[0xff & buf[2]] << 4) | hex_digits[0xff & buf[3]];
break;
case 'n':
c = '\n';
break;
case 't':
c = '\t';
break;
default:
c = buf[1];
}
return c;
}
/* Prepends the text denoted by yytext and yylen to the list starting at lst
* if it's not already listed; returns the start of the new version of the
* list. */
static lstr_list_t * add_to_list_impl(const char *yytext, size_t yylen,
lstr_list_t *lst, const char *fname,
int line_num)
{
len_string *s = lstring_dupbuf(yylen, yytext);
lstr_list_t *p = lst;
while(p != NULL) {
if(lstr_eq(s, p->s)) {
free(s);
return lst;
}
p = p->next;
}
p = mod_2(1, lstr_list_t, fname, line_num);
p->s = s;
p->next = lst;
return p;
}
#define add_to_list(yytext, yylen, lst) \
add_to_list_impl((yytext), (yylen), (lst), __FILE__, __LINE__)
#ifdef LEXER_DBG
static const char * directive_name(directive_kind dir)
{
switch(dir) {
case D_NONE:
return "[NONE]";
case D_TOP:
return "%top";
case D_HEADER:
return "%header";
case D_STATE:
return "%state";
case D_INITSTATE:
return "%initstate";
case D_PREFIX:
return "%prefix";
case D_USTATE_TYPE:
return "%userdata";
}
return NULL;
}
#endif
#define vfprintf if(yydata->verb) fprintf
#define vfputs(s) if(yydata->verb) fputs((s), yydata->verb)
#define LEN ((int) yylen)
#include <stdlib.h>
typedef struct {
int done_num;
int trans_start;
int trans_end;
} yyml_fa;
typedef struct {
unsigned char transset[32];
int dest_state;
} yyml_trans;
typedef struct yy_Moonlime_state {
int is_in_error;
int curr_state; /* state of the DFA */
int curr_start_state; /* which DFA to use... */
int last_done_num;
int last_done_len;
size_t string_len;
size_t curr_buf_size;
void * (*alloc)(size_t);
void (*unalloc)(void *);
char *buf;
char start_buf[64];
} yyml_state;
typedef struct yy_Moonlime_state Moonlime_state;
static yyml_fa yy_x[] = {
{0, 0, 1},
{25, 1, 1},
{0, 1, 3},
{26, 3, 3},
{24, 3, 4},
{0, 4, 6},
{20, 6, 6},
{0, 6, 13},
{23, 13, 13},
{23, 13, 13},
{0, 13, 16},
{0, 16, 17},
{0, 17, 20},
{0, 20, 21},
{23, 21, 23},
{0, 23, 25},
{0, 25, 28},
{0, 28, 30},
{0, 30, 32},
{21, 32, 32},
{22, 32, 32},
{0, 32, 36},
{10, 36, 36},
{10, 36, 36},
{0, 36, 38},
{9, 38, 38},
{0, 38, 39},
{0, 39, 40},
{11, 40, 40},
{0, 40, 51},
{27, 51, 51},
{26, 51, 51},
{12, 51, 51},
{13, 51, 51},
{16, 51, 51},
{7, 51, 51},
{27, 51, 53},
{0, 53, 55},
{0, 55, 58},
{0, 58, 60},
{1, 60, 60},
{0, 60, 62},
{8, 62, 63},
{8, 63, 63},
{0, 63, 65},
{14, 65, 65},
{0, 65, 66},
{0, 66, 67},
{19, 67, 69},
{0, 69, 70},
{0, 70, 72},
{18, 72, 72},
{0, 72, 75},
{0, 75, 77},
{17, 77, 77},
{15, 77, 77},
{0, 77, 81},
{4, 81, 82},
{4, 82, 83},
{5, 83, 83},
{6, 83, 83},
{0, 83, 93},
{27, 93, 94},
{2, 94, 95},
{3, 95, 95}
};
static yyml_trans yy_y[] = {
{ {0,6,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, 1 },
{ {0,0,0,0,0,0,0,0,254,255,255,135,254,255,255,7,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, 4 },
{ {0,6,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, 3 },
{ {0,0,0,0,0,0,255,3,254,255,255,135,254,255,255,7,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, 4 },
{ {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,8,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, 6 },
{ {0,6,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, 3 },
{ {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,32,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, 20 },
{ {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,8,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, 19 },
{ {0,0,0,0,0,128,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, 14 },
{ {0,0,0,0,128,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, 12 },
{ {0,0,0,0,4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, 10 },
{ {0,6,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, 9 },
{ {255,249,255,255,122,127,255,255,255,255,255,255,255,255,255,215,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255}, 8 },
{ {0,0,0,0,0,0,0,0,0,0,0,16,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, 11 },
{ {0,0,0,0,4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, 8 },
{ {255,255,255,255,251,255,255,255,255,255,255,239,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255}, 10 },
{ {255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255}, 10 },
{ {0,0,0,0,0,0,0,0,0,0,0,16,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, 13 },
{ {0,0,0,0,128,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, 8 },
{ {255,255,255,255,127,255,255,255,255,255,255,239,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255}, 12 },
{ {255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255}, 12 },
{ {0,0,0,0,0,128,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, 18 },
{ {0,0,0,0,0,4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, 15 },
{ {0,0,0,0,0,4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, 16 },
{ {255,255,255,255,255,251,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255}, 15 },
{ {0,0,0,0,0,128,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, 8 },
{ {0,0,0,0,0,4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, 17 },
{ {255,255,255,255,255,123,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255}, 15 },
{ {0,0,0,0,0,4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, 17 },
{ {255,255,255,255,255,123,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255}, 15 },
{ {0,4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, 8 },
{ {255,251,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255}, 18 },
{ {0,0,0,0,0,0,0,0,0,0,0,32,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, 28 },
{ {0,0,0,0,0,0,0,0,0,0,0,16,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, 24 },
{ {0,6,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, 23 },
{ {255,249,255,255,254,255,255,255,255,255,255,207,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255}, 22 },
{ {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, 26 },
{ {255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,254,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255}, 25 },
{ {0,0,0,0,0,0,255,3,126,0,0,0,126,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, 27 },
{ {0,0,0,0,0,0,255,3,126,0,0,0,126,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, 25 },
{ {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,16,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, 55 },
{ {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,8,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, 48 },
{ {0,0,0,0,0,0,0,0,0,0,0,16,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, 44 },
{ {0,0,0,0,0,0,0,0,0,0,0,8,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, 42 },
{ {0,0,0,0,0,128,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, 36 },
{ {0,0,0,0,0,64,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, 35 },
{ {0,0,0,0,0,12,0,128,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, 34 },
{ {0,0,0,0,0,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, 33 },
{ {0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, 32 },
{ {0,6,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, 31 },
{ {255,249,255,255,254,48,255,111,255,255,255,199,255,255,255,199,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255}, 30 },
{ {0,0,0,0,0,128,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, 41 },
{ {0,0,0,0,0,4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, 37 },
{ {0,0,0,0,0,4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, 38 },
{ {255,255,255,255,255,251,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255}, 37 },
{ {0,0,0,0,0,128,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, 40 },
{ {0,0,0,0,0,4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, 39 },
{ {255,255,255,255,255,123,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255}, 37 },
{ {0,0,0,0,0,4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, 39 },
{ {255,255,255,255,255,123,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255}, 37 },
{ {0,4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, 40 },
{ {255,251,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255}, 41 },
{ {0,0,0,0,0,0,0,0,0,0,0,64,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, 43 },
{ {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, 46 },
{ {255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,254,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255}, 45 },
{ {0,0,0,0,0,0,255,3,126,0,0,0,126,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, 47 },
{ {0,0,0,0,0,0,255,3,126,0,0,0,126,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, 45 },
{ {0,0,0,0,0,0,255,3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, 52 },
{ {0,0,0,0,0,16,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, 49 },
{ {0,0,0,0,0,0,255,3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, 50 },
{ {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,32,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, 51 },
{ {0,0,0,0,0,0,255,3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, 50 },
{ {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,32,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, 54 },
{ {0,0,0,0,0,0,255,3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, 52 },
{ {0,0,0,0,0,16,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, 53 },
{ {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,32,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, 54 },
{ {0,0,0,0,0,0,255,3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, 50 },
{ {0,0,0,0,0,0,0,64,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, 60 },
{ {0,0,0,0,0,16,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, 59 },
{ {0,6,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, 58 },
{ {255,249,255,255,254,239,255,191,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255}, 57 },
{ {255,255,255,255,255,239,255,191,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255}, 57 },
{ {255,255,255,255,255,239,255,191,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255}, 57 },
{ {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,16,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, 55 },
{ {0,0,0,0,0,0,0,0,0,0,0,16,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, 44 },
{ {0,0,0,0,0,0,0,0,0,0,0,8,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, 42 },
{ {0,0,0,0,0,0,0,16,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, 64 },
{ {0,0,0,0,0,128,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, 36 },
{ {0,0,0,0,0,64,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, 35 },
{ {0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, 32 },
{ {0,0,0,0,32,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, 62 },
{ {0,6,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, 31 },
{ {255,249,255,255,222,48,255,111,255,255,255,199,255,255,255,199,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255}, 30 },
{ {0,0,0,0,0,0,0,0,0,0,0,0,254,255,255,7,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, 63 },
{ {0,0,0,0,0,0,0,0,0,0,0,0,254,255,255,7,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, 63 }
};
static int yy_init_states[] = {
0,
2,
5,
7,
21,
29,
56,
61
};
#define YY_STATE_NON_WHSP_IS_ERROR 0
#define YY_STATE_PRE_C_TOKEN 1
#define YY_STATE_PRE_C_CODE 2
#define YY_STATE_C_CODE 3
#define YY_STATE_IN_CHARCLASS 4
#define YY_STATE_IN_REGEX 5
#define YY_STATE_IN_SELECTOR 6
#define YY_STATE_MAIN 7
#define YY_MAXSTATE 7
#define YY_INITSTATE YY_STATE_MAIN
Moonlime_state * MoonlimeInit( void * (*alloc)(size_t),
void (*unalloc)(void *) )
{
yyml_state *ms;
if(alloc == NULL || unalloc == NULL)
return NULL;
ms = alloc(sizeof(yyml_state));
if(ms == NULL)
return NULL;
ms->is_in_error = 0;
ms->curr_state = yy_init_states[YY_INITSTATE];
ms->curr_start_state = YY_INITSTATE;
ms->last_done_num = 0;
ms->last_done_len = 0;
ms->string_len = 0;
ms->curr_buf_size = 64;
ms->buf = ms->start_buf;
ms->alloc = alloc;
ms->unalloc = unalloc;
return ms;
}
void MoonlimeDestroy( Moonlime_state *lexer )
{
yyml_state *ms = lexer;
if(ms == NULL)
return;
if(ms->buf != NULL && ms->buf != ms->start_buf) {
ms->unalloc(ms->buf);
}
ms->unalloc(ms);
}
static void yymoonlime_action(int done_num, const char *yytext, size_t yylen,
int *yy_start_state , lexer_lexer_state * yydata);
static int yyrun_char(yyml_state *ms, char c, int add_to_buf, int len)
{
int curr_trans, end_trans, c_idx, c_mask, next_state, i;
char *new_buf;
curr_trans = yy_x[ms->curr_state].trans_start;
end_trans = yy_x[ms->curr_state].trans_end;
c_idx = ((unsigned char) c) >> 3;
c_mask = 1 << (c & 7);
if(add_to_buf) {
if(ms->string_len >= ms->curr_buf_size - 1) {
if((new_buf = ms->alloc(ms->curr_buf_size * 2)) == NULL) {
ms->is_in_error = 1;
return 0;
}
for(i = 0; i < ms->string_len; ++i)
new_buf[i] = ms->buf[i];
ms->curr_buf_size *= 2;
if(ms->buf != ms->start_buf)
ms->unalloc(ms->buf);
ms->buf = new_buf;
}
ms->buf[ms->string_len++] = c;
}
while(curr_trans < end_trans) {
if(yy_y[curr_trans].transset[c_idx] & c_mask) {
ms->curr_state = next_state = yy_y[curr_trans].dest_state;
if(yy_x[next_state].done_num) {
ms->last_done_num = yy_x[next_state].done_num;
ms->last_done_len = len;
}
return 1;
}
++curr_trans;
}
return 0;
}
static void yyreset_state(yyml_state *ms)
{
int i;
for(i = ms->last_done_len; i < ms->string_len; ++i)
ms->buf[i - ms->last_done_len] = ms->buf[i];
ms->string_len -= ms->last_done_len;
ms->last_done_len = ms->last_done_num = 0;
ms->curr_state = yy_init_states[ms->curr_start_state];
}
int MoonlimeRead( Moonlime_state *lexer, char *input, size_t len , lexer_lexer_state * data )
{
int done_relexing, i;
char *end = input + len;
yyml_state *ms = lexer;
if(ms == NULL || ms->is_in_error)
return 0;
if(len == 0) { /* Signifies EOF */
if(ms->string_len == 0)
return 1;
if(ms->last_done_num == 0) {
ms->is_in_error = 1;
return 0;
}
yymoonlime_action(ms->last_done_num, ms->buf, ms->last_done_len,
&(ms->curr_start_state) , data);
yyreset_state(ms);
while(ms->string_len > 0) {
for(i = 0; i < ms->string_len; ++i) {
if(!yyrun_char(ms, ms->buf[i], 0, i+1) ||
i == ms->string_len - 1) {
if(ms->is_in_error || ms->last_done_num == 0) {
ms->is_in_error = 1;
return 0;
}
yymoonlime_action(ms->last_done_num, ms->buf,
ms->last_done_len,
&(ms->curr_start_state) , data);
yyreset_state(ms);
break;
}
}
}
return 1;
}
while(input < end) {
if(!yyrun_char(ms, *input, 1, ms->string_len + 1)) { /* past pattern */
if(ms->is_in_error)
return 0;
if(ms->last_done_num == 0) { /* no pattern matches buf */
ms->is_in_error = 1;
return 0;
}
yymoonlime_action(ms->last_done_num, ms->buf, ms->last_done_len,
&(ms->curr_start_state) , data);
yyreset_state(ms);
/* Re-lex remaining part of the buffer */
done_relexing = 0;
while(ms->string_len > 0 && !done_relexing) {
i = 0;
while(i < ms->string_len) {
if(!yyrun_char(ms, ms->buf[i], 0, i+1)) {
if(!ms->is_in_error && ms->last_done_num == 0)
ms->is_in_error = 1;
if(ms->is_in_error)
return 0;
yymoonlime_action(ms->last_done_num, ms->buf,
ms->last_done_len,
&(ms->curr_start_state) , data);
yyreset_state(ms);
i = 0;
continue;
}
++i;
}
if(i == ms->string_len)
done_relexing = 1;
}
}
++input;
}
return 1;
}
#define YYSTART(x) do { *yy_start_state = YY_STATE_ ## x ; } while(0)
static void yymoonlime_action(int done_num, const char *yytext, size_t yylen,
int *yy_start_state , lexer_lexer_state * yydata)
{
switch(done_num) {
case 1: {
;
} break;
case 2: {
if(yylen == 4 && !strncmp(yytext, "%top", yylen)) {
yydata->dir = D_TOP;
YYSTART(PRE_C_CODE);
} else if(yylen == 7 && !strncmp(yytext, "%header", yylen)) {
yydata->dir = D_HEADER;
YYSTART(PRE_C_CODE);
} else if(yylen == 6 && !strncmp(yytext, "%state", yylen)) {
yydata->dir = D_STATE;
YYSTART(PRE_C_TOKEN);
} else if(yylen == 10 && !strncmp(yytext, "%initstate", yylen)) {
yydata->dir = D_INITSTATE;
YYSTART(PRE_C_TOKEN);
} else if(yylen == 7 && !strncmp(yytext, "%prefix", yylen)) {
yydata->dir = D_PREFIX;
YYSTART(PRE_C_TOKEN);
} else if(yylen == 9 && !strncmp(yytext, "%userdata", yylen)) {
yydata->dir = D_USTATE_TYPE;
YYSTART(PRE_C_CODE);
} else {
fprintf(stderr, "Unknown directive %.*s!\n", (int) yylen, yytext);
exit(1);
}
} break;
case 3: {
YYSTART(IN_SELECTOR);
} break;
case 4: {
int i, still_valid = 1;
/* These ifs are only guaranteed to work reliably
* on ASCII-like character encodings */
if((yytext[0] < 'a' || yytext[0] > 'z') &&
(yytext[0] < 'A' || yytext[0] > 'Z') &&
(yytext[0] != '_'))
still_valid = 0;
for(i = 1; i < yylen && still_valid; i++) {
if((yytext[i] < 'a' || yytext[i] > 'z') &&
(yytext[i] < 'A' || yytext[i] > 'Z') &&
(yytext[i] < '0' || yytext[i] > '9') &&
(yytext[i] != '_'))
still_valid = 0;
}
if(!still_valid) {
fprintf(stderr, "Invalid start-state selector: %.*s\n", LEN, yytext);
exit(1);
}
yydata->curr_st = add_to_list(yytext, yylen, yydata->curr_st);
#ifdef LEXER_DBG
vfprintf(yydata->verb, "Start-state selector \"%.*s\"\n", LEN, yytext);
#endif
} break;
case 5: {
;
} break;
case 6: {
YYSTART(IN_REGEX);
} break;
case 7: {
#ifdef LEXER_DBG
vfputs("Any\n");
#endif
add_simple_regex(yydata, mk_any_rx());
YYSTART(IN_REGEX);
} break;
case 8: {
#ifdef LEXER_DBG
vfprintf(yydata->verb, "Character class%s:\n",
(yylen > 1) ? " (inverted)" : "");
#endif
add_simple_regex(yydata, mk_char_class_rx(yylen > 1));
YYSTART(IN_CHARCLASS);
} break;
case 9: {
#ifdef LEXER_DBG
vfprintf(yydata->verb,
yytext[1] != 'x' ? " \'\\%c\'\n" : " \'\\%c%c%c\'\n",
yytext[1], yytext[2], yytext[3]);
#endif
add_to_char_class(yydata->curr_rx, unescape_rx_escape(yytext));
} break;
case 10: {
#ifdef LEXER_DBG
vfprintf(yydata->verb, " \'%c\'\n", yytext[0]);
#endif
add_to_char_class(yydata->curr_rx, yytext[0]);
} break;
case 11: {
#ifdef LEXER_DBG
vfprintf(yydata->verb, "End character class\n");
#endif
YYSTART(IN_REGEX);
} break;
case 12: {
regex_t *new_rx, *paren;
++yydata->regex_nest_depth;
#ifdef LEXER_DBG
vfputs("(\n");
#endif
if(yydata->curr_rx != NULL) {
if(yydata->rx_stack == NULL) {
new_rx = mk_concat_rx(0);
add_enc_rx(new_rx, yydata->curr_rx);
yydata->rx_stack = new_rx;
} else switch(yydata->rx_stack->type) {
case R_CONCAT:
case R_OPTION:
add_enc_rx(yydata->rx_stack, yydata->curr_rx);
break;
case R_PAREN:
new_rx = mk_concat_rx(0);
add_enc_rx(new_rx, yydata->curr_rx);
new_rx->next = yydata->rx_stack;
yydata->rx_stack = new_rx;
break;
default:
fprintf(stderr, "Invalid stack state %d\n",
yydata->rx_stack->type);
exit(1);
}
}
paren = mk_paren_rx();
paren->next = yydata->rx_stack;
yydata->rx_stack = paren;
yydata->curr_rx = NULL;
YYSTART(IN_REGEX);
} break;
case 13: {
regex_t *re, *top;
if(--yydata->regex_nest_depth < 0) {
fputs("Improper parentheses nesting!\n", stderr);
exit(1);
}
re = yydata->curr_rx;
top = yydata->rx_stack;
if(top != NULL)
yydata->rx_stack = top->next;
while(top != NULL && top->type != R_PAREN) {
if(re != NULL)
add_enc_rx(top, re);
else if(top->type == R_OPTION)
add_enc_rx(top, mk_zero_rx());
re = top;
top = yydata->rx_stack;
if(top != NULL)
yydata->rx_stack = top->next;
}
if(top == NULL) {
fputs("Close-paren without open-paren\n", stderr);
exit(1);
} else
free_regex_tree(top);
yydata->curr_rx = re;
} break;
case 14: {
#ifdef LEXER_DBG
vfprintf(yydata->verb,
(yytext[1] != 'x') ? "Char \'\\%c\'\n" : "Char \'\\%c%c%c\'\n",
yytext[1], yytext[2], yytext[3]);
#endif
add_simple_regex(yydata, mk_char_rx(unescape_rx_escape(yytext)));
YYSTART(IN_REGEX);
} break;
case 15: {
regex_t *re, *top, *next;
#ifdef LEXER_DBG
printf("|\n");
#endif
re = yydata->curr_rx;
if(re == NULL)
re = mk_zero_rx();
top = yydata->rx_stack;
if(top != NULL)
yydata->rx_stack = top->next;
if(top == NULL) {
top = mk_option_rx();
add_enc_rx(top, re);
} else if(top->type == R_OPTION) {
add_enc_rx(top, re);
} else if(top->type == R_CONCAT) {
if(re->type != R_ZERO) {
add_enc_rx(top, re);
} else
free_regex_tree(re);
next = yydata->rx_stack;
if(next != NULL)
yydata->rx_stack = next->next;
if(next != NULL && next->type == R_OPTION) {
add_enc_rx(next, top);
top = next;
} else {
if(next != NULL) {
yydata->rx_stack = next;
}
next = mk_option_rx();
add_enc_rx(next, top);
top = next;
}
} else { /* top->type == R_PAREN */
if(top != NULL)
yydata->rx_stack = top;
top = mk_option_rx();
add_enc_rx(top, re);
}
top->next = yydata->rx_stack;
yydata->rx_stack = top;
yydata->curr_rx = NULL;
YYSTART(IN_REGEX);
} break;
case 16: {
#ifdef LEXER_DBG
vfprintf(yydata->verb, "Repetition: %.*s\n", LEN, yytext);
#endif
if(yydata->curr_rx == NULL) {
fputs("Tried to apply repetition to empty regex\n", stderr);
exit(1);
}
switch(yytext[0]) {
case '?':
yydata->curr_rx = mk_maybe_rx(yydata->curr_rx);
break;
case '*':
yydata->curr_rx = mk_star_rx(yydata->curr_rx);
break;
case '+':
yydata->curr_rx = mk_plus_rx(yydata->curr_rx);
}
} break;
case 17: {
int n;
#ifdef LEXER_DBG
vfputs("rep1\n");
#endif
if(yydata->curr_rx == NULL) {
fputs("Tried to apply repetition to empty regex\n", stderr);
exit(1);
}
n = (int) strtol(yytext+1, NULL, 10);
if(yytext[yylen-2] != ',')
yydata->curr_rx = mk_num_rx(yydata->curr_rx, n, n);
else
yydata->curr_rx = mk_num_rx(yydata->curr_rx, n, -1);
} break;
case 18: {
int n, m;
char *ptr = (char *)(yytext+1);
#ifdef LEXER_DBG
vfputs("rep2\n");
#endif
if(yydata->curr_rx == NULL) {
fputs("Tried to apply repetition to empty regex\n", stderr);
exit(1);
}
if(yytext[1] == ',') {
n = -1;
} else
n = (int) strtol(ptr, &ptr, 10);
m = (int) strtol(ptr+1, NULL, 10);
yydata->curr_rx = mk_num_rx(yydata->curr_rx, n, m);
} break;
case 19: {
regex_t *re, *next;
#ifdef LEXER_DBG
vfputs("Start action code\n");
#endif
if(yydata->regex_nest_depth > 0) {
fputs("Code improperly contained inside parentheses!\n", stderr);
exit(1);
}
if(yydata->curr_rx == NULL && yydata->rx_stack == NULL) {
fputs("A code action without a regex!\n", stderr);
exit(1);
}
if(yydata->curr_rx != NULL) {
re = yydata->curr_rx;
next = yydata->rx_stack;
if(next != NULL)
yydata->rx_stack = next->next;
} else {
re = yydata->rx_stack;
if(re->type == R_OPTION)
add_enc_rx(re, mk_zero_rx());
next = re->next;
if(next != NULL)
yydata->rx_stack = next->next;
else
yydata->rx_stack = NULL;