-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathbslex.c
1670 lines (1562 loc) · 39.1 KB
/
bslex.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
/*
* Copyright 2022 Rochus Keller <mailto:[email protected]>
*
* This file is part of the BUSY build system.
*
* The following is the license that applies to this copy of the
* application. For a license to use the application under conditions
* other than those described here, please email to [email protected].
*
* GNU General Public License Usage
* This file may be used under the terms of the GNU General Public
* License (GPL) versions 2.0 or 3.0 as published by the Free Software
* Foundation and appearing in the file LICENSE.GPL included in
* the packaging of this file. Please review the following information
* to ensure GNU General Public Licensing requirements will be met:
* http://www.fsf.org/licensing/licenses/info/GPLv2.html and
* http://www.gnu.org/copyleft/gpl.html.
*/
#include "bslex.h"
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <ctype.h>
#include <string.h>
#include <stdarg.h>
#include "bsunicode.h"
#include "bshost.h"
typedef struct BSTokenQueue {
BSToken tok;
struct BSTokenQueue* next;
} BSTokenQueue;
typedef struct BSLexer
{
const char* source;
const uchar* str;
const uchar* pos;
const uchar* end;
uint ch;
BSRowCol loc;
BSTokenQueue* first;
BSTokenQueue* last;
uchar n; // number of bytes read for cur ch
uchar ownsStr;
uchar muted;
uchar alltokens; // dont aggregate strings and deliver comment delimiters
BSLogger logger;
void* loggerData;
} BSLexer;
void bs_defaultLogger(BSLogLevel l, void* data,const char* file, BSRowCol loc, const char* format, va_list args)
{
FILE* out;
if( l == BS_Info || l == BS_Message || l == BS_Debug )
out = stdout;
else
out = stderr;
const char* tag = "";
switch( l )
{
default:
break;
case BS_Debug:
tag = "DGB:";
break;
case BS_Warning:
tag = "WRN:";
break;
case BS_Error:
tag = "ERR:";
break;
case BS_Critical:
tag = "PANIC:";
break;
}
if( file && loc.row )
fprintf(out,"%s:%d:%d:%s ", file, loc.row, loc.col, tag);
else if( file )
fprintf(out,"%s:%s ", file, tag);
else if( *tag != 0 )
fprintf(out,"%s ", tag);
vfprintf(out, format, args);
fprintf(out,"\n");
fflush(out);
}
static void error1( BSLexer* l, const char* format, ...)
{
if( l->muted )
return;
va_list args;
va_start(args, format);
l->logger(BS_Error, l->loggerData,l->source, l->loc, format, args);
va_end(args);
}
static void error2( BSLexer* l, const char* format, ...)
{
if( l->muted )
return;
BSRowCol loc;
loc.row = 0;
va_list args;
va_start(args, format);
l->logger(BS_Error, l->loggerData, 0, loc, format, args);
va_end(args);
}
static void readchar(BSLexer* l)
{
if( l->n )
{
l->pos += l->n;
l->loc.col++;
}
if( l->pos >= l->end )
{
l->n = 0;
l->ch = 0;
}else
{
l->ch = unicode_decode_utf8(l->pos, &l->n);
if( l->n == 0 || l->n > (l->end - l->pos) )
{
if( !l->muted )
error2(l, "file has invalid utf-8 format: %s\n", l->source);
l->ch = 0;
l->pos = l->end;
}
}
}
static char peekchar(BSLexer* l)
{
if( l->pos + 1 < l->end )
return l->pos[1];
else
return 0;
}
BSLexer* bslex_open(const char* filepath, const char* sourceName)
{
FILE* f = bs_fopen(filepath,"r");
if( f == NULL )
{
fprintf(stderr, "cannot open file for reading %s\n", filepath);
return 0;
}
fseek(f, 0L, SEEK_END);
const int sz = ftell(f);
if( sz < 0 )
{
fprintf(stderr, "cannot determine file size %s\n", filepath);
return 0;
}
rewind(f);
uchar* str = (uchar*) malloc(sz+1);
if( str == NULL )
{
fprintf(stderr, "not enough memory to read file %s\n", filepath);
return 0;
}
if( fread( str, 1, sz, f ) != (size_t)sz )
{
free(str);
fprintf(stderr, "cannot read file %s\n", filepath);
return 0;
}
str[sz] = 0;
BSLexer* l = (BSLexer*) calloc(1,sizeof(BSLexer));
if( l == NULL )
{
free(str);
fprintf(stderr, "not enough memory to process file %s\n", filepath);
return 0;
}
l->logger = bs_defaultLogger;
if( sourceName == 0 )
l->source = filepath;
else
l->source = sourceName;
l->str = str;
l->pos = str;
l->end = str + sz;
l->loc.row = 1;
l->loc.col = 0;
l->ownsStr = 1;
if( sz >= 3 && str[0] == 0xEF && str[1] == 0xBB && str[2] == 0xBF)
l->pos += 3; // remove BOM
readchar(l);
return l;
}
BSLexer* bslex_openFromString(const char* str, int len, const char* sourceName)
{
BSLexer* l = (BSLexer*) calloc(1,sizeof(BSLexer));
if( l == NULL )
{
fprintf(stderr, "not enough memory create BSLexer\n");
return 0;
}
const int sz = len;
l->source = sourceName;
l->str = (const uchar*)str;
l->pos = l->str;
l->end = l->str + sz;
l->loc.row = 1;
l->loc.col = 0;
l->ownsStr = 0;
l->logger = bs_defaultLogger;
if( sz >= 3 && l->str[0] == 0xEF && l->str[1] == 0xBB && l->str[2] == 0xBF)
l->pos += 3; // remove BOM
readchar(l);
return l;
}
void bslex_free(BSLexer* l)
{
if( l == NULL )
return;
assert(l);
assert(l->str);
while( l->first )
{
BSTokenQueue* tmp = l->first;
l->first = l->first->next;
free(tmp);
}
if( l->ownsStr )
free((uchar*)l->str);
free(l);
}
static char at( const char* str, int i ){
return ( i >= 0 ? str[i] : 0 );
}
static BSTokType tokenTypeFromString( const char* str, int* pos );
static void skipWhiteSpace(BSLexer* l)
{
while( l->pos < l->end && ( isspace(l->ch) || unicode_isspace(l->ch) ) )
{
if( l->ch == '\n' )
{
l->loc.row++;
l->loc.col = 0;
}
readchar(l);
}
}
static BSToken path(BSLexer* l, BSToken t, int quoted )
{
// path ::= [ '../' { '../' } | './' | '//' ] fsname { '/' fsname } | '..' | '.'
// fsname := fschar { fschar }
// fschar ::= any unicode char besides '/', '\', '?', '*', '|', '"', '<', '>', ',', ';', '='
// fschar can include ':' directly after '//' and a single letter, like "//c:/"
// fschar can include ' ' if path is surrounded by '\''
// path may not include '/../' or '/./' nor any pair of '..'
// when we arrive here we're at the start or somewhere within the first fsname
t.tok = Tok_path;
if( quoted )
{
assert( t.val == (const char*)l->pos && *t.val == '\'' );
readchar(l);
if( l->ch == '.' )
{
readchar(l);
if( l->ch == '.' )
{
readchar(l);
if( l->ch != '/' && l->ch != '\'' )
{
error1(l, "expecting '..' or '../path'\n" );
t.tok = Tok_Invalid;
return t;
}
}else if( l->ch != '/' && l->ch != '\'' )
{
error1(l, "expecting '.' or './path'\n" );
t.tok = Tok_Invalid;
return t;
}
}else if( l->ch == '/' )
{
readchar(l);
if( l->ch != '/' )
{
error1(l, "expecting '//' in the root of an absolute path\n" );
t.tok = Tok_Invalid;
return t;
}
}else if( l->ch == '\'' )
{
error1(l, "empty paths not allowed\n" );
t.tok = Tok_Invalid;
return t;
}
}
int dotdotStart = strncmp( quoted ? t.val+1: t.val,"../",3) == 0;
const char* lastSlash = 0;
const char* i;
for( i = (const char*)l->pos; i >= t.val; i-- )
{
if( *i == '/' )
{
lastSlash = i;
break;
}
}
const char* lastDot = 0;
while( l->pos < l->end )
{
const char ch = l->ch;
if( quoted && l->ch == '\\' && l->pos[1] == '\'' ) // only one escape is supported: \'
readchar(l);
else if( bs_forbidden_fschar(l->ch) )
{
error1( l, "cannot use '%c' in a path\n", l->ch );
t.tok = Tok_Invalid;
return t;
}else if( l->ch == ':' )
{
const char* start = quoted ? t.val + 1 : t.val;
const int diff = (const char*)l->pos - start;
if( !( diff == 3
&& start[0] == '/'
&& start[1] == '/'
&& isalpha(start[2])
&& ( l->pos[1] == '/' || isspace(l->pos[1]) || ( quoted && l->pos[1] == '\'' ) ) ) )
{
error1( l, "':' can only be used in the root of an absolute path like //c:\n" );
t.tok = Tok_Invalid;
return t;
}
}else if( l->ch == '/' )
{
if( lastSlash )
{
const int diff = (const char*)l->pos - lastSlash;
switch(diff)
{
case 1:
error1(l, "'//' only allowed at the beginning of an absolute path\n");
t.tok = Tok_Invalid;
return t;
case 2:
if( strncmp(lastSlash,"/./",3) == 0 )
{
error1(l, "'/./' not allowed in a path\n" );
t.tok = Tok_Invalid;
return t;
}
break;
case 3:
if( strncmp(lastSlash,"/../",4) == 0 )
{
if( !dotdotStart )
{
error1(l, "'/../' not allowed in a path\n" );
t.tok = Tok_Invalid;
return t;
}
}
break;
}
}
lastSlash = (const char*)l->pos;
}else if( l->ch == '.' )
{
if( !dotdotStart && lastDot && ((const char*)l->pos - lastDot) == 1 )
{
error1(l, "pairs of '..' not allowed in a path\n" );
t.tok = Tok_Invalid;
return t;
}
lastDot = (const char*)l->pos;
}else if( !quoted && ( isspace(l->ch) || unicode_isspace(l->ch) ) )
break;
else if( quoted && l->ch == '\'' )
{
readchar(l);
break;
}else
dotdotStart = 0;
readchar(l);
}
if( lastSlash && (const char*)l->pos - lastSlash == ( quoted ? 2 : 1 ) &&
(const char*)l->pos - t.val > ( quoted ? 3 : 2 ) )
{
error1(l, "trailing '/' not allowed\n" );
t.tok = Tok_Invalid;
return t;
}
t.len = (const char*)l->pos - t.val;
return t;
}
static BSToken ident(BSLexer* l, BSToken t)
{
do
{
readchar(l);
}while( unicode_isletter(l->ch) || l->ch == '_' || unicode_isdigit(l->ch) );
if( 0 ) // no, since ident.ident, ident-ident or ident/ident has a meaning: l->ch == '.' || l->ch == '/' || l->ch == '-' )
{
// it's a relative path not starting with "./", i.e. looks like an ident until here
return path(l,t, 0);
}
t.tok = Tok_ident;
t.len = (const char*)l->pos - t.val;
int pos = 0;
const BSTokType kw = tokenTypeFromString(t.val,&pos);
if( kw != Tok_Invalid && pos == t.len )
t.tok = kw;
return t;
}
static BSToken symbol(BSLexer* l, BSToken t)
{
do
{
readchar(l);
}while( unicode_isletter(l->ch) || l->ch == '_' || unicode_isdigit(l->ch) );
t.tok = Tok_symbol;
t.len = (const char*)l->pos - t.val;
return t;
}
static int ishexdigit(int ch)
{
return isdigit(ch) || ( ch >= 'A' && ch <= 'F' ) || ( ch >= 'a' && ch <= 'f' );
}
static BSToken number(BSLexer* l,BSToken t)
{
// integer ::= // digit {digit}
// hex ::= '0x' hexDigit {hexDigit}
// real ::= // digit {digit} '.' {digit} [ScaleFactor]
// ScaleFactor- ::= // 'e' ['+' | '-'] digit {digit}
readchar(l);
if( *t.val == '0' && ( l->ch == 'x' || l->ch == 'X' ) )
{
// read a hex integer
readchar(l);
while( ishexdigit(l->ch) )
readchar(l);
t.tok = Tok_integer;
t.len = (const char*)l->pos - t.val;
return t;
}
while( unicode_isdigit(l->ch) )
readchar(l);
if( l->ch == '.' || l->ch == 'e' || l->ch == 'E' )
{
if( l->ch == '.' )
{
readchar(l);
if( !unicode_isdigit(l->ch) )
error1(l, "expecting a digit after '.'\n" );
while( unicode_isdigit(l->ch) )
readchar(l);
}
if( l->ch == 'e' || l->ch == 'E' )
{
readchar(l);
if( l->ch == '+' || l->ch == '-' )
readchar(l);
if( !unicode_isdigit(l->ch) )
error1(l, "expecting a digit after exponent\n" );
while( unicode_isdigit(l->ch) )
readchar(l);
}
t.tok = Tok_real;
t.len = (const char*)l->pos - t.val;
return t;
}else
{
t.tok = Tok_integer;
t.len = (const char*)l->pos - t.val;
return t;
}
}
static void queueToken(BSLexer* l, BSToken t)
{
BSTokenQueue* tmp = l->first;
int i = 0;
// walk through the queue starting from first until first invalid or eof
while( tmp && tmp->tok.tok != Tok_Invalid )
{
tmp = tmp->next;
i++;
}
// here we have either a tmp == 0 or an invalid tmp
if( tmp == 0 )
{
tmp = (BSTokenQueue*)malloc(sizeof(BSTokenQueue));
if( tmp == 0 )
{
error2(l, "not enough memory to read file %s\n", l->source);
exit(1);
}
tmp->tok = t;
tmp->next = 0;
if( l->first == 0 )
{
l->first = l->last = tmp;
}else
{
l->last->next = tmp;
l->last = tmp;
}
}else if( tmp->tok.tok == Tok_Invalid )
{
tmp->tok = t;
}
}
static BSToken string(BSLexer* l,BSToken t)
{
while( l->pos < l->end )
{
readchar(l);
if( l->ch == '\n' )
{
l->loc.row++;
l->loc.col = 0;
}else if( l->ch == '\\' )
{
if( l->pos[1] == '"' || l->pos[1] == '\\' ) // valid escapes are \\ and \"
readchar(l);
}else if( l->ch == '"' )
break;
}
if( l->ch != '"' && !l->muted )
{
error1(l,"non-terminated string\n" );
t.tok = Tok_Invalid;
return t;
}// else
readchar(l);
t.tok = Tok_string;
t.len = (const char*)l->pos - t.val;
return t;
}
static BSToken comment(BSLexer* l,BSToken t)
{
// '/*' already read
int level = 1;
while( l->pos < l->end )
{
readchar(l);
switch( l->ch )
{
case '*':
if( peekchar(l) == '/' )
{
readchar(l);
level--;
}
break;
case '/':
if( peekchar(l) == '*' )
{
readchar(l);
level++;
}
break;
case '\n':
l->loc.row++;
l->loc.col = 0;
break;
}
#if 0
if( l->ch == '*' )
{
readchar(l);
if( l->ch == '/' )
level--;
}else if( l->ch == '/' )
{
readchar(l);
if( l->ch == '*' )
level++;
}
if( l->ch == '\n' )
{
l->loc.row++;
l->loc.col = 0;
}
#endif
if( level == 0 )
break;
}
readchar(l);
if( level != 0 && !l->muted )
{
error1(l,"non-terminated comment\n" );
t.tok = Tok_Invalid;
return t;
}
t.tok = Tok_Lcmt;
t.len = (const char*)l->pos - t.val;
return t;
}
static BSToken next(BSLexer* l)
{
assert(l != NULL);
skipWhiteSpace(l);
BSToken t;
memset(&t,0,sizeof(BSToken));
t.tok = Tok_Eof;
t.val = (const char*)l->pos;
t.loc = l->loc;
t.source = l->source;
if( l->pos >= l->end ) // reached eof
return t;
if( unicode_isletter(l->ch) || l->ch == '_' )
return ident(l,t);
if( unicode_isdigit(l->ch) )
return number(l,t);
switch( l->ch )
{
case '\'':
t = path(l, t, 1 );
break;
case '.':
readchar(l);
switch(l->ch)
{
case '.':
readchar(l);
if( l->ch == '/' )
{
readchar(l);
t = path(l, t, 0 );
}else if( isspace(l->ch) )
{
t.tok = Tok_path;
t.len = 2;
}else
{
error1(l, "expecting '/' after '..'\n" );
t.tok = Tok_Invalid;
}
break;
case '/':
readchar(l);
t = path(l, t, 0 );
break;
default:
t.tok = isspace(l->ch) ? Tok_path : Tok_Dot;
t.len = 1;
break;
}
break;
case '/':
readchar(l);
if( l->ch == '/' )
{
readchar(l);
t = path(l, t, 0 );
}else if( l->ch == '*')
{
t = comment(l, t);
if( !l->alltokens )
t = next(l);
}else
{
t.tok = Tok_Slash;
t.len = 1;
}
break;
case '#': // line comment, read until end of line
while(l->pos < l->end && l->ch != '\n')
readchar(l);
if( l->alltokens )
{
t.tok = Tok_Hash;
t.len = (const char*)l->pos - t.val;
}else
t = next(l);
break;
case '"':
return string(l,t);
case '`':
case '$': // TODO: we currently accept both
return symbol(l, t);
default:
{
int pos = 0;
t.tok = tokenTypeFromString(t.val,&pos);
if( t.tok == Tok_Invalid )
{
assert( pos == 0 );
pos++;
if( t.val )
error1(l, "unexpected symbol: %c\n", *t.val );
else
error1(l, "unexpected symbol\n" );
}
t.len = pos;
while( pos-- > 0 )
readchar(l);
}
break;
}
return t;
}
BSToken bslex_next(BSLexer* l)
{
if( l->first && l->first->tok.tok != Tok_Invalid )
{
BSToken res = l->first->tok;
l->first->tok.tok = Tok_Invalid;
// move the read queue slot to the end of the queue
if( l->first != l->last )
{
BSTokenQueue* tmp = l->first;
l->first = tmp->next;
l->last->next = tmp;
l->last = tmp;
tmp->next = 0; // necessary because of deleteQueue
}
return res;
}else
return next(l);
}
BSToken bslex_peek(BSLexer* l, int off )
{
assert( off > 0 );
off--;
BSTokenQueue* tmp = l->first;
int i = 0;
// walk through the queue starting from first until off reached or first invalid or eof
while( tmp && tmp->tok.tok != Tok_Invalid && i != off )
{
tmp = tmp->next;
i++;
}
// in case no result for off yet continue through the queue bei either reusing invalid slots or adding new slots to the end
while( i != off || tmp == 0 || tmp->tok.tok == Tok_Invalid )
{
if( tmp == 0 )
{
// passed last slot, add another one and set it to next() token
tmp = (BSTokenQueue*)malloc(sizeof(BSTokenQueue));
if( tmp == 0 )
{
error2(l, "not enough memory to read file %s\n", l->source);
exit(1);
}
tmp->tok = next(l);
tmp->next = 0;
if( l->first == 0 )
{
l->first = l->last = tmp;
}else
{
l->last->next = tmp;
l->last = tmp;
}
}else if( tmp->tok.tok == Tok_Invalid )
{
// hit invalid slot; set it to next() token and continue
tmp->tok = next(l);
if( tmp->tok.tok == Tok_Invalid )
return tmp->tok;
}
if( i != off )
{
tmp = tmp->next;
i++;
}
}
assert( i == off && tmp != 0 );
return tmp->tok;
}
void bslex_dump(const BSToken* t)
{
char buf[16];
int i;
for(i = 0; i < (t->len < 15 ? t->len : 15); i++ )
{
if( isspace(t->val[i]) )
buf[i] = ' ';
else
buf[i] = t->val[i];
}
buf[i] = 0;
printf("Tok %d:%d:%d %s\n", t->tok, t->loc.row, t->loc.col, buf );
fflush(stdout);
}
const char*bslex_filepath(BSLexer* lex)
{
return lex->source;
}
typedef struct BSTemplArg
{
BSTokChain* what;
const char* name;
struct BSTemplArg* next;
} BSTemplArg;
typedef struct BSHiLexLevel
{
BSLexer* lex;
BSRowCol orig; // the place where the string in lex is located in the file
BSToken ref;
BSTemplArg* args;
}BSHiLexLevel;
typedef struct BSHiLexMemSlot
{
struct BSHiLexMemSlot* next;
char str[sizeof(void*)];
} BSHiLexMemSlot;
typedef struct BSHiLex
{
#define BS_MAX_LEVEL 20
BSHiLexLevel lex[BS_MAX_LEVEL];
unsigned level : 5;
BSTokenQueue* first;
BSTokenQueue* last;
BSToken cur;
BSHiLexMemSlot* mem;
} BSHiLex;
BSHiLex*bslex_createhilex(const char* filepath, const char* sourceName)
{
BSLexer* l = bslex_open(filepath,sourceName);
if( l == 0 )
return 0;
BSHiLex* hl = (BSHiLex*)calloc(1,sizeof(BSHiLex));
if( hl == 0 )
{
bslex_free(l);
fprintf(stderr, "not enough memory to create lexer for %s\n", filepath);
return 0;
}
hl->lex[0].lex = l;
return hl;
}
static void freeLevel(BSHiLexLevel* l)
{
while( l->args )
{
BSTemplArg* tmp = l->args;
while(tmp->what)
{
BSTokChain* tmp2 = tmp->what;
tmp->what = tmp->what->next;
free(tmp2);
}
l->args = l->args->next;
free(tmp);
}
bslex_free(l->lex);
}
void bslex_freehilex(BSHiLex* l)
{
if( l == NULL )
return;
int i;
for( i = 0; i <= l->level; i++ )
{
freeLevel(&l->lex[i]);
}
while( l->mem )
{
BSHiLexMemSlot* tmp = l->mem;
l->mem = l->mem->next;
free(tmp);
}
free(l);
}
static int isequal( const char* lhs, const char* rhs, int len)
{
int i = 0;
while( *lhs && i < len )
{
if( *lhs != rhs[i] )
return 0;
i++;
lhs++;
}
return i == len;
}
static char* chainToStr(BSHiLex* l, BSTokChain* ts)
{
assert(ts);
BSTokChain* p = ts;
int len = 0;
while(p)
{
len += p->tok.len + 1; // +1 for space
p = p->next;
}
char* res = bslex_allocstr(l,len+1);
if( res == 0 )
{
error2(l->lex[0].lex,"%s:%d:%d: not enough memory to make string of token chain\n",
ts->tok.source, ts->tok.loc.row, ts->tok.loc.col );
exit(1);
}
p = ts;
char* s = res;
while(p)
{
strncpy(s,p->tok.val,p->tok.len);
s += p->tok.len;
*s = ' ';
s++;
p = p->next;
}
*s = 0;
return res;
}
static BSToken hnext(BSHiLex* l)
{
BSHiLexLevel* lev = &l->lex[l->level];
BSToken t = bslex_next(lev->lex);
if( t.tok == Tok_Eof )
{
if( l->level > 0 )
{
freeLevel(lev);
l->level--;
lev = &l->lex[l->level];
t = bslex_next(lev->lex);
}
}
if( t.tok == Tok_ident && lev->args )
{
// if ident check for arg stream
BSTemplArg* a = lev->args;
while(a)
{