-
Notifications
You must be signed in to change notification settings - Fork 4
/
lexer.l
971 lines (866 loc) · 26.2 KB
/
lexer.l
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
/* -*- c -*-
* Copyright (c) 1993-2012 David Gay and Gustav Hållberg
* All rights reserved.
*
* Permission to use, copy, modify, and distribute this software for any
* purpose, without fee, and without written agreement is hereby granted,
* provided that the above copyright notice and the following two paragraphs
* appear in all copies of this software.
*
* IN NO EVENT SHALL DAVID GAY OR GUSTAV HALLBERG BE LIABLE TO ANY PARTY FOR
* DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
* OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF DAVID GAY OR
* GUSTAV HALLBERG HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* DAVID GAY AND GUSTAV HALLBERG SPECIFICALLY DISCLAIM ANY WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS ON AN
* "AS IS" BASIS, AND DAVID GAY AND GUSTAV HALLBERG HAVE NO OBLIGATION TO
* PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
*/
%top{
#include "mudlle-config.h"
}
%{
#include <ctype.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include "calloc.h"
#include "charset.h"
#include "global.h"
#include "lexer.h"
#include "mparser.h"
#include "mvalues.h"
#include "strbuf.h"
#include "tree.h"
#include "utils.h"
# include "parser.tab.h"
/* We never wrap into another file */
#define YY_SKIP_YYWRAP
#define yywrap() (1)
#define YY_USE_PROTOS
#define YY_USER_ACTION do { \
yylloc.last_column += yyleng; \
yylloc.first_line = yylloc.last_line; \
} while (0);
#define LOCATION_STEP() do { \
yylloc.first_column = yylloc.last_column; \
} while (0)
#define LOCATION_NEWLINE() do { \
yylloc.last_column = yylloc.first_column = 1; \
yylloc.last_line += 1; \
} while (0)
typedef size_t (*reader_fn)(void *ptr, size_t size, size_t nmemb,
FILE *stream);
static size_t string_read(void *ptr, size_t size, size_t nmemb, FILE *stream);
#undef YY_INPUT
#define YY_INPUT(buf, result, max_size) do { \
(result) = lstate.reader((buf), 1, (max_size), yyin); \
} while (0)
static const char *base_name(int base);
static YY_BUFFER_STATE mbuf;
struct lexer_state {
reader_fn reader;
bool allow_comma_expr, force_constant;
YYLTYPE loc;
struct {
const char *str;
size_t len;
const char *const *strs;
} s;
};
static struct lexer_state lstate;
struct lkeyword {
const char *name;
int value;
};
static const struct lkeyword keywords[] = {
{ "fn", FUNCTION },
{ "if", KW_IF },
{ "else", ELSE },
{ "while", WHILE },
{ "exit", LOOP_EXIT },
{ "loop", LOOP },
{ "for", FOR },
{ "match", MATCH },
{ "match!", FORCE_MATCH },
{ "library", LIBRARY },
{ "module", MODULE },
{ "requires", REQUIRES },
{ "reads", READS },
{ "writes", WRITES },
{ "static", STATIC },
{ "defines", DEFINES }
};
#define NKEYWORDS VLENGTH(keywords)
#define UNTERMINATED_STRING() do { \
compile_error(&cstring.loc, "unterminated %s constant", \
cstring.is_char_const ? "character" : "string"); \
yyterminate(); \
} while (0)
static struct {
struct strbuf sb;
struct loc loc;
bool is_char_const;
} cstring = { .sb = SBNULL };
static struct {
YYLTYPE start;
int level;
} ccomment;
static inline int hexval(int c)
{
if (isdigit(c))
return c - '0';
return toupper(c) - 'A' + 10;
}
#define lexer_error(...) compile_error(LLOC_LOC(yylloc), __VA_ARGS__)
#define END_ESC_CHAR() do { \
if (cstring.is_char_const) \
{ \
BEGIN(INITIAL); \
assert(sb_len(&cstring.sb) == 1); \
yylval.integer = (unsigned char)sb_str(&cstring.sb)[0]; \
return INTEGER; \
} \
BEGIN(STRING_CONST); \
} while (0)
#define END_FLOAT() do { \
if (!mudlle_strtofloat(sb_str(&cstring.sb), \
sb_len(&cstring.sb), \
&yylval.mudlle_float)) \
{ \
compile_error(&cstring.loc, "illegal floating-point number"); \
yyterminate(); \
} \
BEGIN(INITIAL); \
return FLOAT; \
} while (0)
static bool check_int(const char **str, size_t len, int *base,
const char *type, bool *is_float,
const struct loc *sloc);
%}
%option noinput
%option nounput
%option noyymore
DIGIT [0-9]
NONZERO [1-9]
OCTDIGIT [0-7]
EXP [eE][+-]?{DIGIT}+
DECIM \.{DIGIT}+
HEXDIGIT [0-9a-fA-F]
GLOBAL_PREFIX :
SYMBOL_NAME [a-zA-Z][a-zA-Z0-9$_:?!]{0,1024}
I_BIN_TAIL [bB][01]{0,1024}
I_OCT_TAIL {OCTDIGIT}{0,1024}
I_HEX_TAIL [xX]{HEXDIGIT}{0,1024}
I_DEC {NONZERO}{DIGIT}{0,1024}
INT ({I_DEC}|0({I_HEX_TAIL}|{I_BIN_TAIL}|{I_OCT_TAIL}))
/* used by read_constant() */
%x STRING_CONST
%x SC_ESC
%x SC_NAME
%x DEC_PERIOD
%x HEX_PERIOD
%x FLOAT_EXP
%x LINE_COMMENT
%x COMMENT
%%
%{
LOCATION_STEP();
if (lstate.force_constant)
{
lstate.force_constant = false;
return FORCE_CONSTANT;
}
%}
\n { LOCATION_NEWLINE(); }
[ \t\r]+ { LOCATION_STEP(); }
"/"[/\*]? {
if (yyleng == 1) return '/';
if (yytext[1] == '*')
{
ccomment.start = yylloc;
ccomment.level = 1;
BEGIN(COMMENT);
}
else
BEGIN(LINE_COMMENT);
}
<LINE_COMMENT><<eof>> { BEGIN(INITIAL); }
<LINE_COMMENT>\n { LOCATION_NEWLINE(); BEGIN(INITIAL); }
<LINE_COMMENT>.* { }
<COMMENT>[^/\*\n] { }
<COMMENT>\n { LOCATION_NEWLINE(); }
<COMMENT><<eof>> {
compile_error(LLOC_LOC(ccomment.start), "unterminated comment");
yyterminate();
}
<COMMENT>\*\/? {
if (yyleng == 2 && --ccomment.level == 0)
BEGIN(INITIAL);
}
<COMMENT>\/\*? {
if (yyleng == 2)
++ccomment.level;
}
"&&" { return SC_AND; }
"||" { return SC_OR; }
"^^" { return XOR; }
"!" { return '!'; }
"[" { return '['; }
"]" { return ']'; }
"|" { return '|'; }
";" { return ';'; }
"+" { return '+'; }
"-" { return '-'; }
"*" { return '*'; }
"%" { return '%'; }
"(" { return '('; }
")" { return ')'; }
"{" { return '{'; }
"}" { return '}'; }
"&" { return '&'; }
"~" { return '~'; }
"^" { return '^'; }
"+=" { yylval.bop = b_add; return ASSIGN_ADD; }
"&=" { yylval.bop = b_bitand; return ASSIGN_BIT_AND; }
"|=" { yylval.bop = b_bitor; return ASSIGN_BIT_OR; }
"^=" { yylval.bop = b_bitxor; return ASSIGN_BIT_XOR; }
"/=" { yylval.bop = b_divide; return ASSIGN_DIV; }
"*=" { yylval.bop = b_multiply; return ASSIGN_MUL; }
"%=" { yylval.bop = b_remainder; return ASSIGN_REM; }
"&&=" { yylval.bop = b_sc_and; return ASSIGN_SC_AND; }
"||=" { yylval.bop = b_sc_or; return ASSIGN_SC_OR; }
"<<=" { yylval.bop = b_shift_left; return ASSIGN_SHL; }
">>=" { yylval.bop = b_shift_right; return ASSIGN_SHR; }
"-=" { yylval.bop = b_subtract; return ASSIGN_SUB; }
"^^=" { yylval.bop = b_xor; return ASSIGN_XOR; }
"=" { return ASSIGN; }
"==" { return EQ; }
">=" { return GE; }
">" { return GT; }
"<=" { return LE; }
"<" { return LT; }
"!=" { return NE; }
"<<" { return SHIFT_LEFT; }
">>" { return SHIFT_RIGHT; }
"--" { return DECREMENT; }
"++" { return INCREMENT; }
"@" { return '@'; }
"'" { return QUOTE; }
"=>" { return PATTERN_MATCH; }
"_" { return SINK; }
"."{1,3} {
if (yyleng == 1) return '.';
if (yyleng == 3) return ELLIPSIS;
lexer_error("invalid '..' operator");
yyterminate();
}
,[ \n\t\r]? {
yylval.tcomma.space_suffix = (yytext[1] != 0);
if (yytext[1] == '\n')
LOCATION_NEWLINE();
return COMMA;
}
\?([0-9A-Za-z_][a-zA-Z0-9$_:?!]?|.)? {
cstring.is_char_const = true;
if (yyleng == 1)
UNTERMINATED_STRING();
if (yyleng == 3)
{
lexer_error("invalid character constant");
yyterminate();
}
unsigned char c = yytext[1];
if (c == '\\')
{
sb_empty(&cstring.sb);
BEGIN(SC_ESC);
}
else if (!IS_8PRINT(c))
{
int esc = 0;
switch (c)
{
#define _E(escchr, chr) case escchr: esc = chr; break
FOR_CHAR_ESCAPES(_E, SEP_SEMI);
#undef _E
}
if (esc)
lexer_error("invalid character constant: use '?\\%c'", esc);
else
lexer_error("invalid character constant: use '?\\%03o'", c);
yyterminate();
}
else if (IS_8SPACE(c) || strchr("(){}[]\"", c) != NULL)
{
lexer_error("'?%c' must be written '?\\%c'", c, c);
yyterminate();
}
else
{
yylval.integer = c;
return INTEGER;
}
}
{INT}[0-9A-Za-z_.]? {
const char *s = yytext;
int base;
bool is_float;
if (!check_int(&s, yyleng, &base, "integer", &is_float,
LLOC_LOC(yylloc)))
yyterminate();
if (!is_float)
{
if (!mudlle_strtolong(s, yytext + yyleng - s, &yylval.integer,
base, true))
{
lexer_error("%sinteger constant out of range",
base_name(base));
yyterminate();
}
return INTEGER;
}
sb_empty(&cstring.sb);
sb_addmem(&cstring.sb, yytext, yyleng);
cstring.loc = *LLOC_LOC(yylloc);
int last = yytext[yyleng - 1];
if (last == 'p' || last == 'P')
{
assert(base == 16);
BEGIN(FLOAT_EXP);
}
else if (last == 'e' || last == 'E')
{
assert(base == 10);
BEGIN(FLOAT_EXP);
}
else
{
assert(last == '.');
if (base == 10)
BEGIN(DEC_PERIOD);
else if (base == 16)
BEGIN(HEX_PERIOD);
else
abort();
}
}
<DEC_PERIOD>{DIGIT}+[A-Za-z_]? {
sb_addmem(&cstring.sb, yytext, yyleng);
int last = yytext[yyleng - 1];
if (last == 'e' || last == 'E')
{
BEGIN(FLOAT_EXP);
}
else if (!isdigit(last))
{
cstring.loc.col += sb_len(&cstring.sb) - 1;
compile_error(
&cstring.loc,
"invalid suffix in floating-point constant: %c",
last);
yyterminate();
}
else
END_FLOAT();
}
<DEC_PERIOD>[^0-9] {
cstring.loc.col += sb_len(&cstring.sb) - 1;
compile_error(
&cstring.loc,
"expected digit after decimal period");
yyterminate();
}
<DEC_PERIOD><<eof>> {
cstring.loc.col += sb_len(&cstring.sb) - 1;
compile_error(
&cstring.loc,
"expected digit after decimal period");
yyterminate();
}
<HEX_PERIOD>{HEXDIGIT}+[A-Za-z_]? {
sb_addmem(&cstring.sb, yytext, yyleng);
int last = yytext[yyleng - 1];
if (last == 'p' || last == 'P')
{
BEGIN(FLOAT_EXP);
}
else
{
cstring.loc.col += sb_len(&cstring.sb) - 1;
compile_error(
&cstring.loc,
"expected exponent in hexadecimal floating-point constant");
yyterminate();
}
}
<HEX_PERIOD><<eof>> {
cstring.loc.col += sb_len(&cstring.sb);
compile_error(
&cstring.loc,
"expected hexadecimal digit after period");
yyterminate();
}
<FLOAT_EXP>[+-]?{DIGIT}{0,1024}[A-Za-z_]? {
sb_addmem(&cstring.sb, yytext, yyleng);
if (sb_len(&cstring.sb) > 1024)
{
cstring.loc.col += 1024;
compile_error(
&cstring.loc,
"too many characters in floating-point constant");
yyterminate();
}
int last = yytext[yyleng - 1];
if (last == '+' || last == '-')
{
cstring.loc.col += sb_len(&cstring.sb);
compile_error(
&cstring.loc,
"expected exponent in floating-point constant");
yyterminate();
}
if (!isdigit(last))
{
cstring.loc.col += sb_len(&cstring.sb);
compile_error(
&cstring.loc,
"invalid suffix in floating-point constant: %c", last);
yyterminate();
}
END_FLOAT();
}
<FLOAT_EXP><<eof>> {
cstring.loc.col += sb_len(&cstring.sb);
compile_error(
&cstring.loc,
"expected exponent in floating-point constant");
yyterminate();
}
\#(arith|[bB](\[|-?{INT}?[0-9A-Za-z_]?))? {
if (yyleng == 1)
{
yylloc.first_column += yyleng;
lexer_error("# must be followed by \"b\" (bigint) or \"arith\"");
yyterminate();
}
if (toupper(yytext[1]) != 'B')
return ARITH;
if (!yytext[2])
{
yylloc.first_column += yyleng;
lexer_error("#%c must be followed by a bigint constant",
yytext[1]);
yyterminate();
}
const char *s = yytext + 2;
bool neg = *s == '-';
if (neg) ++s;
int base;
struct loc loc = *LLOC_LOC(yylloc);
loc.col += s - yytext;
if (!check_int(&s, yytext + yyleng - s, &base, "bigint", NULL, &loc))
yyterminate();
size_t sz = yytext + yyleng - s + 1;
yylval.bigint = allocate(parser_memory, sizeof *yylval.bigint + sz);
strcpy(yylval.bigint->str, s);
yylval.bigint->base = base;
yylval.bigint->neg = neg;
return BIGINT;
}
\" {
sb_empty(&cstring.sb);
cstring.loc = *LLOC_LOC(yylloc);
cstring.is_char_const = false;
BEGIN(STRING_CONST);
}
<STRING_CONST>\" {
BEGIN(INITIAL);
size_t len = sb_len(&cstring.sb);
yylval.string.len = len;
yylval.string.str = allocate(parser_memory, len);
memcpy(yylval.string.str, sb_str(&cstring.sb), len);
if (len > 512)
sb_free(&cstring.sb);
return STRING;
}
<STRING_CONST>[^\n\\\"]+ {
sb_addmem(&cstring.sb, yytext, yyleng);
if (sb_len(&cstring.sb) > MAX_STRING_SIZE)
{
lexer_error("string length exceeds %ld characters",
(long)MAX_STRING_SIZE);
yyterminate();
}
}
<STRING_CONST,SC_NAME>\n { UNTERMINATED_STRING(); }
<STRING_CONST,SC_NAME,SC_ESC><<eof>> { UNTERMINATED_STRING(); }
<STRING_CONST>\\ { BEGIN(SC_ESC); }
<SC_ESC>\n {
if (cstring.is_char_const)
UNTERMINATED_STRING();
END_ESC_CHAR();
LOCATION_NEWLINE();
}
<SC_ESC>N\{? {
if (yytext[1] != '{')
{
lexer_error("\\N must be followed by left curly bracket ({)");
yyterminate();
}
BEGIN(SC_NAME);
}
<SC_NAME>[- A-Z]{0,1024}[^\n]? {
int last = yytext[yyleng - 1];
if (last != '}')
{
yylloc.first_column = yylloc.last_column - 1;
if (yyleng > 1024)
{
lexer_error("too long character name");
yyterminate();
}
else if (isupper(last) || last == '-' || last == ' ')
{
++yylloc.first_column;
lexer_error("unterminated character name");
}
else if (isprint(last) && !isspace(last))
lexer_error("invalid character in character name: %c", last);
else
lexer_error("invalid%s character in character name: \\x%02x",
isspace(last) ? " space" : "", last);
yyterminate();
}
int c = lookup_named_character(yytext, yyleng - 1);
if (c < 0)
{
lexer_error("unknown named character");
yyterminate();
}
sb_addc(&cstring.sb, c);
END_ESC_CHAR();
}
<SC_ESC>{OCTDIGIT}{1,3}[a-zA-Z0-9$_:?!]? {
int last = yytext[yyleng - 1];
/* check for invalid next character for octal constants */
if ((last < '0' || last > '7') || yyleng > 3)
{
if (cstring.is_char_const)
{
yylloc.first_column = yylloc.last_column;
lexer_error("invalid character constant");
yyterminate();
}
--yyleng;
}
else
last = 0;
int val = 0;
for (int i = 0; i < yyleng; ++i)
val = val * 8 + yytext[i] - '0';
if (val > 255)
{
lexer_error("invalid octal character: \\%03o", val);
yyterminate();
}
sb_addc(&cstring.sb, val);
if (last)
sb_addc(&cstring.sb, last);
END_ESC_CHAR();
}
<SC_ESC>x{HEXDIGIT}{0,2}[a-zA-Z0-9$_:?!] {
if (yyleng < 3 || !isxdigit(yytext[2])
|| (yyleng == 4 && cstring.is_char_const))
{
lexer_error("invalid hexadecimal character constant");
yyterminate();
}
sb_addc(&cstring.sb, hexval(yytext[1]) * 16 + hexval(yytext[2]));
if (yyleng == 4)
sb_addc(&cstring.sb, yytext[3]);
END_ESC_CHAR();
}
<SC_ESC>([A-Za-z_][a-zA-Z0-9$_:?!]?|.) {
if (yyleng == 2 && cstring.is_char_const)
{
lexer_error("invalid character constant");
yyterminate();
}
int c = yytext[0];
switch (c)
{
#define _E(escchr, chr) case chr: c = escchr; break
FOR_CHAR_ESCAPES(_E, SEP_SEMI);
#undef _E
}
sb_addc(&cstring.sb, c);
if (yyleng == 2)
sb_addc(&cstring.sb, yytext[1]);
END_ESC_CHAR();
}
{GLOBAL_PREFIX}?{SYMBOL_NAME} {
for (int i = 0; i < NKEYWORDS; i++)
if (strcasecmp(yytext, keywords[i].name) == 0)
return keywords[i].value;
size_t len = yyleng;
bool is_global = strncmp(yytext, GLOBAL_ENV_PREFIX,
strlen(GLOBAL_ENV_PREFIX)) == 0;
if (is_global)
len -= strlen(GLOBAL_ENV_PREFIX);
if (len > MAX_VARIABLE_LENGTH)
{
lexer_error("symbol name exceeds %d characters",
MAX_VARIABLE_LENGTH);
yyterminate();
}
size_t size = yyleng + 1;
char *sym = allocate(parser_memory, size);
memcpy(sym, yytext, size);
yylval.symbol = sym;
return is_global ? GLOBAL_SYMBOL : SYMBOL;
}
. {
unsigned char c = yytext[0];
if (isprint(c))
lexer_error("bad character %c (%#02x)", c, c);
else
lexer_error("bad character %#02x", c);
yyterminate();
}
%%
static const char *base_name(int base)
{
switch (base)
{
case 2: return "binary ";
case 8: return "octal ";
case 10: return "decimal ";
case 16: return "hexadecimal ";
}
return "";
}
/* set *base to detected base; set *str to start of digit sequence;
return true if valid */
static bool check_int(const char **str, size_t len, int *base,
const char *type, bool *is_float,
const struct loc *sloc)
{
if (is_float)
*is_float = false;
if (len > 1024)
{
struct loc loc = *sloc;
loc.col += 1024;
compile_error(&loc, "too many characters in numeric constant");
return false;
}
const char *s = *str, *end = s + len;
int b = 0;
const char *lastp;
if (s < end && *s == '0')
{
++s;
if (s == end)
{
*base = 8;
return true;
}
switch (*s)
{
case 'b': case 'B':
*str = ++s;
b = 2;
break;
case 'x': case 'X':
*str = ++s;
b = 16;
if (end > s)
{
lastp = end - 1;
int last = *lastp;
if (last == '.' || last == 'p' || last == 'P')
{
if (is_float == NULL)
goto invalid_last;
*base = 16;
*is_float = true;
return true;
}
}
break;
default:
b = 8;
const char *non_octal = NULL;
do
{
lastp = s++;
int last = *lastp;
if (last == '8' || last == '9')
{
/* handle decimal floating-point constants with leading
zero */
if (is_float == NULL)
goto invalid_last;
non_octal = lastp;
/* only decimal digits can occur before the last character,
so we skip to it right away */
s = end;
lastp = s - 1;
}
}
while (s < end);
int last = *lastp;
if (last == '.' || last == 'e' || last == 'E')
{
if (is_float == NULL)
goto invalid_last;
assert(s == end);
*base = 10;
*is_float = true;
return true;
}
if (non_octal)
{
lastp = non_octal;
goto invalid_last;
}
if (last < '0' || last > '7')
goto invalid_last;
*base = 8;
return true;
}
}
if (s == end)
goto incomplete;
if (b == 0)
{
lastp = s;
if (!isdigit(*lastp))
goto invalid_last;
b = 10;
}
*base = b;
lastp = end - 1;
if (b == 10 && (*lastp == '.' || *lastp == 'e' || *lastp == 'E'))
{
if (is_float == NULL)
goto invalid_last;
*is_float = true;
return true;
}
if (b == 16
? !isxdigit(*lastp)
: *lastp < '0' || *lastp >= '0' + b)
goto invalid_last;
return true;
struct loc loc;
invalid_last:
loc = *sloc;
loc.col += lastp + len - end;
compile_error(&loc, "invalid %s in %s%s constant: %c",
(isdigit(*lastp)
? "digit"
: b == 0 ? "leading character" : "character"),
base_name(b), type, *lastp);
return false;
incomplete:
loc = *sloc;
loc.col += len;
compile_error(&loc, "incomplete %s%s constant", base_name(b), type);
return false;
}
static size_t string_read(void *ptr, size_t size, size_t nmemb, FILE *stream)
{
char *dst = ptr;
size_t max_size = size * nmemb;
size_t total = 0;
for (;;)
{
if (lstate.s.str == NULL)
return total;
size_t cnt = (lstate.s.len < max_size
? lstate.s.len
: max_size);
memcpy(dst, lstate.s.str, cnt);
max_size -= cnt;
lstate.s.str += cnt;
lstate.s.len -= cnt;
total += cnt;
if (max_size == 0)
return total;
dst += cnt;
lstate.s.str = *++lstate.s.strs;
lstate.s.len = lstate.s.str ? strlen(lstate.s.str) : 0;
}
}
bool allow_comma_expression(void)
{
return lstate.allow_comma_expr;
}
void save_reader_state(struct reader_state *state)
{
*state = (struct reader_state){
.data = malloc(sizeof *state->data)
};
*state->data = lstate;
state->data->loc = yylloc;
lstate = (struct lexer_state){ 0 };
yylloc = (YYLTYPE){ 0 };
}
void restore_reader_state(struct reader_state *state)
{
yylloc = state->data->loc;
lstate = *state->data;
free(state->data);
*state = (struct reader_state){ 0 };
}
static void init_reader(FILE *f, const struct filename *fname,
bool force_const, reader_fn reader)
{
assert(lstate.reader == NULL);
assert(reader != NULL);
assert(fname != NULL);
if (mbuf == NULL)
{
mbuf = yy_create_buffer(f, YY_BUF_SIZE);
yy_switch_to_buffer(mbuf);
}
else
yyrestart(f);
lstate = (struct lexer_state){
.reader = reader,
.force_constant = force_const,
.allow_comma_expr = !force_const
};
yylloc = (YYLTYPE){
.first_column = 1,
.first_line = 1,
.last_column = 1,
.last_line = 1,
.fname = fname
};
}
void read_from_file(FILE *f, const struct filename *fname)
{
init_reader(f, fname, false, fread);
BEGIN(INITIAL);
}
void read_from_strings(const char *const *strs, const struct filename *fname,
bool force_const)
{
assert(fname != NULL);
init_reader(NULL, fname, force_const, string_read);
lstate.s.strs = strs;
lstate.s.str = *strs;
lstate.s.len = strlen(lstate.s.str);
BEGIN(INITIAL);
}
const struct loc *lexer_location(void)
{
static struct loc loc;
loc = *LLOC_LOC(yylloc);
return &loc;
}