forked from rebolsource/r3
/
l-scan.c
1704 lines (1484 loc) · 49.2 KB
/
l-scan.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
/***********************************************************************
**
** REBOL [R3] Language Interpreter and Run-time Environment
**
** Copyright 2012 REBOL Technologies
** REBOL is a trademark of REBOL Technologies
**
** Licensed under the Apache License, Version 2.0 (the "License");
** you may not use this file except in compliance with the License.
** You may obtain a copy of the License at
**
** http://www.apache.org/licenses/LICENSE-2.0
**
** Unless required by applicable law or agreed to in writing, software
** distributed under the License is distributed on an "AS IS" BASIS,
** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
** See the License for the specific language governing permissions and
** limitations under the License.
**
************************************************************************
**
** Module: l-scan.c
** Summary: lexical analyzer for source to binary translation
** Section: lexical
** Author: Carl Sassenrath
** Notes:
** WARNING WARNING WARNING
** This is highly tuned code that should only be modified by experts
** who fully understand its design. It is very easy to create odd
** side effects so please be careful and extensively test all changes!
**
***********************************************************************/
#include "sys-core.h"
#include "sys-scan.h"
// In UTF8 C0, C1, F5, and FF are invalid.
#ifdef USE_UNICODE
#define LEX_UTFE LEX_DEFAULT
#else
#define LEX_UTFE LEX_WORD
#endif
/***********************************************************************
**
*/ const REBYTE Lex_Map[256] =
/*
** Maps each character to its lexical attributes, using
** a frequency optimized encoding.
**
** UTF8: The values C0, C1, F5 to FF never appear.
**
***********************************************************************/
{
/* 00 EOF */ LEX_DELIMIT|LEX_DELIMIT_END_FILE,
/* 01 */ LEX_DEFAULT,
/* 02 */ LEX_DEFAULT,
/* 03 */ LEX_DEFAULT,
/* 04 */ LEX_DEFAULT,
/* 05 */ LEX_DEFAULT,
/* 06 */ LEX_DEFAULT,
/* 07 */ LEX_DEFAULT,
/* 08 BS */ LEX_DEFAULT,
/* 09 TAB */ LEX_DEFAULT,
/* 0A LF */ LEX_DELIMIT|LEX_DELIMIT_LINEFEED,
/* 0B */ LEX_DEFAULT,
/* 0C PG */ LEX_DEFAULT,
/* 0D CR */ LEX_DELIMIT|LEX_DELIMIT_RETURN,
/* 0E */ LEX_DEFAULT,
/* 0F */ LEX_DEFAULT,
/* 10 */ LEX_DEFAULT,
/* 11 */ LEX_DEFAULT,
/* 12 */ LEX_DEFAULT,
/* 13 */ LEX_DEFAULT,
/* 14 */ LEX_DEFAULT,
/* 15 */ LEX_DEFAULT,
/* 16 */ LEX_DEFAULT,
/* 17 */ LEX_DEFAULT,
/* 18 */ LEX_DEFAULT,
/* 19 */ LEX_DEFAULT,
/* 1A */ LEX_DEFAULT,
/* 1B */ LEX_DEFAULT,
/* 1C */ LEX_DEFAULT,
/* 1D */ LEX_DEFAULT,
/* 1E */ LEX_DEFAULT,
/* 1F */ LEX_DEFAULT,
/* 20 */ LEX_DELIMIT|LEX_DELIMIT_SPACE,
/* 21 ! */ LEX_WORD,
/* 22 " */ LEX_DELIMIT|LEX_DELIMIT_QUOTE,
/* 23 # */ LEX_SPECIAL|LEX_SPECIAL_POUND,
/* 24 $ */ LEX_SPECIAL|LEX_SPECIAL_DOLLAR,
/* 25 % */ LEX_SPECIAL|LEX_SPECIAL_PERCENT,
/* 26 & */ LEX_WORD,
/* 27 ' */ LEX_SPECIAL|LEX_SPECIAL_TICK,
/* 28 ( */ LEX_DELIMIT|LEX_DELIMIT_LEFT_PAREN,
/* 29 ) */ LEX_DELIMIT|LEX_DELIMIT_RIGHT_PAREN,
/* 2A * */ LEX_WORD,
/* 2B + */ LEX_SPECIAL|LEX_SPECIAL_PLUS,
/* 2C , */ LEX_SPECIAL|LEX_SPECIAL_COMMA,
/* 2D - */ LEX_SPECIAL|LEX_SPECIAL_MINUS,
/* 2E . */ LEX_SPECIAL|LEX_SPECIAL_PERIOD,
/* 2F / */ LEX_DELIMIT|LEX_DELIMIT_SLASH,
/* 30 0 */ LEX_NUMBER|0,
/* 31 1 */ LEX_NUMBER|1,
/* 32 2 */ LEX_NUMBER|2,
/* 33 3 */ LEX_NUMBER|3,
/* 34 4 */ LEX_NUMBER|4,
/* 35 5 */ LEX_NUMBER|5,
/* 36 6 */ LEX_NUMBER|6,
/* 37 7 */ LEX_NUMBER|7,
/* 38 8 */ LEX_NUMBER|8,
/* 39 9 */ LEX_NUMBER|9,
/* 3A : */ LEX_SPECIAL|LEX_SPECIAL_COLON,
/* 3B ; */ LEX_DELIMIT|LEX_DELIMIT_SEMICOLON,
/* 3C < */ LEX_SPECIAL|LEX_SPECIAL_LESSER,
/* 3D = */ LEX_WORD,
/* 3E > */ LEX_SPECIAL|LEX_SPECIAL_GREATER,
/* 3F ? */ LEX_WORD,
/* 40 @ */ LEX_SPECIAL|LEX_SPECIAL_AT,
/* 41 A */ LEX_WORD|10,
/* 42 B */ LEX_WORD|11,
/* 43 C */ LEX_WORD|12,
/* 44 D */ LEX_WORD|13,
/* 45 E */ LEX_WORD|14,
/* 46 F */ LEX_WORD|15,
/* 47 G */ LEX_WORD,
/* 48 H */ LEX_WORD,
/* 49 I */ LEX_WORD,
/* 4A J */ LEX_WORD,
/* 4B K */ LEX_WORD,
/* 4C L */ LEX_WORD,
/* 4D M */ LEX_WORD,
/* 4E N */ LEX_WORD,
/* 4F O */ LEX_WORD,
/* 50 P */ LEX_WORD,
/* 51 Q */ LEX_WORD,
/* 52 R */ LEX_WORD,
/* 53 S */ LEX_WORD,
/* 54 T */ LEX_WORD,
/* 55 U */ LEX_WORD,
/* 56 V */ LEX_WORD,
/* 57 W */ LEX_WORD,
/* 58 X */ LEX_WORD,
/* 59 Y */ LEX_WORD,
/* 5A Z */ LEX_WORD,
/* 5B [ */ LEX_DELIMIT|LEX_DELIMIT_LEFT_BRACKET,
/* 5C \ */ LEX_SPECIAL|LEX_SPECIAL_BACKSLASH,
/* 5D ] */ LEX_DELIMIT|LEX_DELIMIT_RIGHT_BRACKET,
/* 5E ^ */ LEX_WORD,
/* 5F _ */ LEX_WORD,
/* 60 ` */ LEX_WORD,
/* 61 a */ LEX_WORD|10,
/* 62 b */ LEX_WORD|11,
/* 63 c */ LEX_WORD|12,
/* 64 d */ LEX_WORD|13,
/* 65 e */ LEX_WORD|14,
/* 66 f */ LEX_WORD|15,
/* 67 g */ LEX_WORD,
/* 68 h */ LEX_WORD,
/* 69 i */ LEX_WORD,
/* 6A j */ LEX_WORD,
/* 6B k */ LEX_WORD,
/* 6C l */ LEX_WORD,
/* 6D m */ LEX_WORD,
/* 6E n */ LEX_WORD,
/* 6F o */ LEX_WORD,
/* 70 p */ LEX_WORD,
/* 71 q */ LEX_WORD,
/* 72 r */ LEX_WORD,
/* 73 s */ LEX_WORD,
/* 74 t */ LEX_WORD,
/* 75 u */ LEX_WORD,
/* 76 v */ LEX_WORD,
/* 77 w */ LEX_WORD,
/* 78 x */ LEX_WORD,
/* 79 y */ LEX_WORD,
/* 7A z */ LEX_WORD,
/* 7B { */ LEX_DELIMIT|LEX_DELIMIT_LEFT_BRACE,
/* 7C | */ LEX_WORD,
/* 7D } */ LEX_DELIMIT|LEX_DELIMIT_RIGHT_BRACE,
/* 7E ~ */ LEX_WORD, //LEX_SPECIAL|LEX_SPECIAL_TILDE,
/* 7F DEL */ LEX_DEFAULT,
/* Odd Control Chars */
LEX_WORD,LEX_WORD,LEX_WORD,LEX_WORD, /* 80 */
LEX_WORD,LEX_WORD,LEX_WORD,LEX_WORD,
LEX_WORD,LEX_WORD,LEX_WORD,LEX_WORD,
LEX_WORD,LEX_WORD,LEX_WORD,LEX_WORD,
LEX_WORD,LEX_WORD,LEX_WORD,LEX_WORD,
LEX_WORD,LEX_WORD,LEX_WORD,LEX_WORD,
LEX_WORD,LEX_WORD,LEX_WORD,LEX_WORD,
LEX_WORD,LEX_WORD,LEX_WORD,LEX_WORD,
/* Alternate Chars */
#ifdef USE_UNICODE
LEX_WORD,LEX_WORD,LEX_WORD,LEX_WORD,
#else
LEX_DEFAULT,LEX_WORD,LEX_WORD,LEX_WORD, /* A0 (a space) */
#endif
LEX_WORD,LEX_WORD,LEX_WORD,LEX_WORD,
LEX_WORD,LEX_WORD,LEX_WORD,LEX_WORD,
LEX_WORD,LEX_WORD,LEX_WORD,LEX_WORD,
LEX_WORD,LEX_WORD,LEX_WORD,LEX_WORD,
LEX_WORD,LEX_WORD,LEX_WORD,LEX_WORD,
LEX_WORD,LEX_WORD,LEX_WORD,LEX_WORD,
LEX_WORD,LEX_WORD,LEX_WORD,LEX_WORD,
// C0, C1
LEX_UTFE,LEX_UTFE,LEX_WORD,LEX_WORD,
LEX_WORD,LEX_WORD,LEX_WORD,LEX_WORD,
LEX_WORD,LEX_WORD,LEX_WORD,LEX_WORD,
LEX_WORD,LEX_WORD,LEX_WORD,LEX_WORD,
LEX_WORD,LEX_WORD,LEX_WORD,LEX_WORD,
LEX_WORD,LEX_WORD,LEX_WORD,LEX_WORD,
LEX_WORD,LEX_WORD,LEX_WORD,LEX_WORD,
LEX_WORD,LEX_WORD,LEX_WORD,LEX_WORD,
LEX_WORD,LEX_WORD,LEX_WORD,LEX_WORD,
LEX_WORD,LEX_WORD,LEX_WORD,LEX_WORD,
LEX_WORD,LEX_WORD,LEX_WORD,LEX_WORD,
LEX_WORD,LEX_WORD,LEX_WORD,LEX_WORD,
LEX_WORD,LEX_WORD,LEX_WORD,LEX_WORD,
LEX_WORD,LEX_UTFE,LEX_WORD,LEX_WORD,
LEX_WORD,LEX_WORD,LEX_WORD,LEX_WORD,
LEX_WORD,LEX_WORD,LEX_WORD,LEX_UTFE
};
#ifdef LOWER_CASE_BYTE
/***********************************************************************
**
*/ const REBYTE Upper_Case[256] =
/*
** Maps each character to its upper case value. Done this
** way for speed. Note the odd cases in last block.
**
***********************************************************************/
{
0, 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, 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,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,138,155,156,141,142,159, /* some up/low cases mod 16 (not mod 32) */
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,
192,193,194,195,196,197,198,199,200,201,202,203,204,205,206,207,
208,209,210,211,212,213,214,247,216,217,218,219,220,221,222,159
};
/***********************************************************************
**
*/ const REBYTE Lower_Case[256] =
/*
** Maps each character to its lower case value. Done this
** way for speed. Note the odd cases in last block.
**
***********************************************************************/
{
0, 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, 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, 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,154,139,140,157,158,143,
144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,255, /* some up/low cases mod 16 (not mod 32) */
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,
224,225,226,227,228,229,230,231,232,233,234,235,236,237,238,239,
240,241,242,243,244,245,246,215,248,249,250,251,252,253,254,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
};
#endif
/***********************************************************************
**
*/ static REBINT Scan_Char(REBYTE **bp)
/*
** Scan a char, handling ^A, ^/, ^(null), ^(1234)
**
** Returns the numeric value for char, or -1 for errors.
**
** Advances the cp to just past the last position.
**
** test: to-integer load to-binary mold to-char 1234
**
***********************************************************************/
{
REBINT n;
REBYTE *cp;
REBYTE c;
REBYTE lex;
c = **bp;
// Handle unicoded char:
if (c >= 0x80) {
n = Decode_UTF8_Char(bp, 0); // zero on error
(*bp)++; // skip char
return n;
}
(*bp)++;
if (c != '^') return c;
// Must be ^ escaped char:
c = **bp;
(*bp)++;
switch (c) {
case 0:
n = 0;
break;
case '/':
n = LF;
break;
case '^':
n = c;
break;
case '-':
n = TAB;
break;
case '!':
n = '\036'; // record separator
break;
case '(': // ^(tab) ^(1234)
// Check for hex integers ^(1234):
cp = *bp; // restart location
n = 0;
while ((lex = Lex_Map[*cp]) > LEX_WORD) {
c = lex & LEX_VALUE;
if (!c && lex < LEX_NUMBER) break;
n = (n << 4) + c;
cp++;
}
if ((cp - *bp) > 4) return -1;
if (*cp == ')') {
cp++;
*bp = cp;
return n;
}
// Check for identifiers:
for (n = 0; n < ESC_MAX; n++) {
if (NZ(cp = Match_Bytes(*bp, (REBYTE*)(Esc_Names[n])))) {
if (cp && *cp == ')') {
*bp = cp + 1;
return Esc_Codes[n];
}
}
}
return -1;
default:
n = UP_CASE(c);
if (n >= '@' && n <= '_') n -= '@';
else if (n == '~') n = 0x7f; // special for DEL
else n = c; // includes: ^{ ^} ^"
}
return n;
}
/***********************************************************************
**
*/ REBYTE *Scan_Quote(REBYTE *src, SCAN_STATE *scan_state)
/*
** Scan a quoted string, handling all the escape characters.
**
** The result will be put into the temporary MOLD_BUF unistring.
**
***********************************************************************/
{
REBINT nest = 0;
REBUNI term;
REBINT chr;
REBCNT lines = 0;
REBSER *buf = BUF_MOLD;
RESET_TAIL(buf);
term = (*src++ == '{') ? '}' : '"'; // pick termination
while (*src != term || nest > 0) {
chr = *src;
switch (chr) {
case 0:
return 0; // Scan_state shows error location.
case '^':
chr = Scan_Char(&src);
if (chr == -1) return 0;
src--;
break;
case '{':
if (term != '"') nest++;
break;
case '}':
if (term != '"' && nest > 0) nest--;
break;
case CR:
if (src[1] == LF) src++;
// fall thru
case LF:
if (term == '"') return 0;
lines++;
chr = LF;
break;
default:
if (chr >= 0x80) {
chr = Decode_UTF8_Char(&src, 0); // zero on error
if (chr == 0) return 0;
}
}
src++;
if (SERIES_LEN(buf) >= SERIES_REST(buf)) Extend_Series(buf, 1);
*UNI_SKIP(buf, buf->tail) = chr;
buf->tail ++;
}
src++; // Skip ending quote or brace.
if (scan_state) scan_state->line_count += lines;
UNI_TERM(buf);
return src;
}
/***********************************************************************
**
*/ REBYTE *Scan_Item(REBYTE *src, REBYTE *end, REBUNI term, REBYTE *invalid)
/*
** Scan as UTF8 an item like a file or URL.
**
** Returns continuation point or zero for error.
**
** Put result into the MOLD_BUF as uni-chars.
**
***********************************************************************/
{
REBUNI c;
REBSER *buf;
buf = BUF_MOLD;
RESET_TAIL(buf);
while (src < end && *src != term) {
c = *src;
// End of stream?
if (c == 0) break;
// If no term, then any white will terminate:
if (!term && IS_WHITE(c)) break;
// Ctrl chars are invalid:
if (c < ' ') return 0; // invalid char
if (c == '\\') c = '/';
// Accept %xx encoded char:
else if (c == '%') {
if (!Scan_Hex2(src+1, &c, FALSE)) return 0;
src += 2;
}
// Accept ^X encoded char:
else if (c == '^') {
if (src+1 == end) return 0; // nothing follows ^
c = Scan_Char(&src);
if (!term && IS_WHITE(c)) break;
src--;
}
// Accept UTF8 encoded char:
else if (c >= 0x80) {
c = Decode_UTF8_Char(&src, 0); // zero on error
if (c == 0) return 0;
}
// Is char as literal valid? (e.g. () [] etc.)
else if (invalid && strchr(invalid, c)) return 0;
src++;
*UNI_SKIP(buf, buf->tail) = c; // not affected by Extend_Series
if (++(buf->tail) >= SERIES_REST(buf)) Extend_Series(buf, 1);
}
if (*src && *src == term) src++;
UNI_TERM(buf);
return src;
}
/***********************************************************************
**
*/ static REBYTE *Skip_Tag(REBYTE *cp)
/*
** Skip the entire contents of a tag, including quoted strings.
** The argument points to the opening '<'. Zero is returned on
** errors.
**
***********************************************************************/
{
if (*cp == '<') cp++;
while (*cp && *cp != '>') {
if (*cp == '"') {
cp++;
while (*cp && *cp != '"') cp++;
if (!*cp) return 0;
}
cp++;
}
if (*cp) return cp+1;
return 0;
}
/***********************************************************************
**
*/ static void Scan_Error(REBCNT errnum, SCAN_STATE *ss, REBCNT tkn, REBYTE *arg, REBCNT size, REBVAL *relax)
/*
** Scanner error handler
**
***********************************************************************/
{
ERROR_OBJ *error;
REBSER *errs;
REBYTE *name;
REBYTE *cp;
REBYTE *bp;
REBSER *ser;
REBCNT len = 0;
ss->errors++;
if (PG_Boot_Strs)
name = BOOT_STR(RS_SCAN,tkn);
else
name = (REBYTE*)"boot";
cp = ss->head_line;
while (IS_LEX_SPACE(*cp)) cp++; // skip indentation
bp = cp;
while (NOT_NEWLINE(*cp)) cp++, len++;
//DISABLE_GC;
errs = Make_Error(errnum, 0, 0, 0);
error = (ERROR_OBJ *)FRM_VALUES(errs);
ser = Make_Binary(len + 16);
Append_Bytes(ser, "(line ");
Append_Int(ser, ss->line_count);
Append_Bytes(ser, ") ");
Append_Series(ser, (REBYTE*)bp, len);
Set_String(&error->nearest, ser);
Set_String(&error->arg1, Copy_Bytes(name, -1));
Set_String(&error->arg2, Copy_Bytes(arg, size));
if (relax) {
SET_ERROR(relax, errnum, errs);
//ENABLE_GC;
return;
}
Throw_Error(errs); // ENABLE_GC implied
}
/***********************************************************************
**
*/ static REBCNT Prescan(SCAN_STATE *scan_state)
/*
** The general idea of this function is to break up a string
** into tokens, with sensitivity to common token frequencies.
** That is, find DELIMITERS, simple WORDS, and simple NUMBERS
** rapidly. For everything else, find the substring and note
** the special characters that it contains. All scans start
** by skipping whitespace and are concluded by a delimiter.
** A delimiter is returned only when nothing was found before
** it (i.e. not part of other lexical tokens).
**
** Returns a word with bit flags indicating special chars
** that were found during the scan (other than the first
** char, which is not part of the flags).
** Both the beginning and ending positions are updated.
**
***********************************************************************/
{
REBYTE *cp = scan_state->begin; /* char scan pointer */
REBCNT flags = 0; /* lexical flags */
while (IS_LEX_SPACE(*cp)) cp++; /* skip white space */
scan_state->begin = cp; /* start of lexical symbol */
while (1) {
switch (GET_LEX_CLASS(*cp)) {
case LEX_CLASS_DELIMIT:
if (cp == scan_state->begin) cp++; /* returning delimiter */
scan_state->end = cp;
return flags;
case LEX_CLASS_SPECIAL: /* Flag all but first special char: */
if (cp != scan_state->begin) SET_LEX_FLAG(flags, GET_LEX_VALUE(*cp));
cp++;
break;
case LEX_CLASS_WORD:
SET_LEX_FLAG(flags, LEX_SPECIAL_WORD); /* flags word char (for nums) */
while (IS_LEX_AT_LEAST_WORD(*cp)) cp++; /* word or number */
break;
case LEX_CLASS_NUMBER:
while (IS_LEX_AT_LEAST_NUMBER(*cp)) cp++;
break;
}
}
}
/***********************************************************************
**
*/ static REBINT Scan_Token(SCAN_STATE *scan_state)
/*
** Scan the next lexical object and determine its datatype.
** Skip all leading whitespace and conclude on a delimiter.
**
** Returns the value type (VT) identifying the token.
** Negative value types indicate an error in that type.
** Both the beginning and ending positions are updated.
**
** Note: this function does not need to find errors in types
** that are to be scanned and converted. It only needs to
** recognize that the value should be of that type. For words
** however, since no further scanning is done, they must be
** checked for errors here. Same is true for delimiters.
**
***********************************************************************/
{
REBCNT flags;
REBYTE *cp;
REBINT type;
flags = Prescan(scan_state);
cp = scan_state->begin;
switch (GET_LEX_CLASS(*cp)) {
case LEX_CLASS_DELIMIT:
switch (GET_LEX_VALUE(*cp)) {
case LEX_DELIMIT_SPACE: /* white space (pre-processed above) */
case LEX_DELIMIT_SEMICOLON: /* ; begin comment */
while (NOT_NEWLINE(*cp)) cp++;
if (!*cp) cp--; /* avoid passing EOF */
if (*cp == LF) goto line_feed;
/* fall thru */
case LEX_DELIMIT_RETURN: /* CR */
if (cp[1] == LF) cp++;
/* fall thru */
case LEX_DELIMIT_LINEFEED: /* LF */
line_feed:
scan_state->line_count++;
scan_state->end = cp+1;
return TOKEN_LINE;
case LEX_DELIMIT_LEFT_BRACKET: /* [ begin block */
return TOKEN_BLOCK;
case LEX_DELIMIT_RIGHT_BRACKET: /* ] end block */
return TOKEN_BLOCK_END;
case LEX_DELIMIT_LEFT_PAREN: /* ( begin paren */
return TOKEN_PAREN;
case LEX_DELIMIT_RIGHT_PAREN: /* ) end paren */
return TOKEN_PAREN_END;
case LEX_DELIMIT_QUOTE: /* " quote */
cp = Scan_Quote(cp, scan_state); // stores result string in BUF_MOLD
goto check_str;
case LEX_DELIMIT_LEFT_BRACE: /* { begin quote */
cp = Scan_Quote(cp, scan_state); // stores result string in BUF_MOLD
check_str:
if (cp) {
scan_state->end = cp;
return TOKEN_STRING;
} else { /* try to recover at next new line... */
for (cp = (scan_state->begin)+1; NOT_NEWLINE(*cp); cp++);
scan_state->end = cp;
return -TOKEN_STRING;
}
case LEX_DELIMIT_RIGHT_BRACE: /* } end quote !!! handle better (missing) */
return -TOKEN_STRING;
case LEX_DELIMIT_SLASH: /* probably / or / * */
while (*cp && *cp == '/') cp++;
if (IS_LEX_AT_LEAST_WORD(*cp) || *cp=='+' || *cp=='-' || *cp=='.') {
// ///refine not allowed
if (scan_state->begin + 1 != cp) {
scan_state->end = cp;
return -TOKEN_REFINE;
}
scan_state->begin = cp;
flags = Prescan(scan_state);
scan_state->begin--;
type = TOKEN_REFINE;
// Fast easy case:
if (ONLY_LEX_FLAG(flags, LEX_SPECIAL_WORD)) return type;
goto scanword;
}
if (cp[0] == '<' || cp[0] == '>') {
scan_state->end = cp+1;
return -TOKEN_REFINE;
}
scan_state->end = cp;
return TOKEN_WORD;
case LEX_DELIMIT_END_FILE: /* end of file */
scan_state->end--;
return TOKEN_EOF;
case LEX_DELIMIT_UTF8_ERROR:
default:
return -TOKEN_WORD; /* just in case */
}
case LEX_CLASS_SPECIAL:
if (HAS_LEX_FLAG(flags, LEX_SPECIAL_AT) && *cp != '<') return TOKEN_EMAIL;
next_ls:
switch (GET_LEX_VALUE(*cp)) {
case LEX_SPECIAL_AT:
return -TOKEN_EMAIL;
case LEX_SPECIAL_PERCENT: /* %filename */
cp = scan_state->end;
if (*cp == '"') {
cp = Scan_Quote(cp, scan_state); // stores result string in BUF_MOLD
if (!cp) return -TOKEN_FILE;
scan_state->end = cp;
return TOKEN_FILE;
}
while (*cp == '/') { /* deal with path delimiter */
cp++;
while (IS_LEX_AT_LEAST_SPECIAL(*cp)) cp++;
}
scan_state->end = cp;
return TOKEN_FILE;
case LEX_SPECIAL_COLON: /* :word :12 (time) */
if (IS_LEX_NUMBER(cp[1])) return TOKEN_TIME;
if (ONLY_LEX_FLAG(flags, LEX_SPECIAL_WORD)) return TOKEN_GET; /* common case */
if (cp[1] == '\'') return -TOKEN_WORD;
// Various special cases of < << <> >> > >= <=
if (cp[1] == '<' || cp[1] == '>') {
cp++;
if (cp[1] == '<' || cp[1] == '>' || cp[1] == '=') cp++;
if (!IS_LEX_DELIMIT(cp[1])) return -TOKEN_GET;
scan_state->end = cp+1;
return TOKEN_GET;
}
type = TOKEN_GET;
cp++; /* skip ':' */
goto scanword;
case LEX_SPECIAL_TICK:
if (IS_LEX_NUMBER(cp[1])) return -TOKEN_LIT; // no '2nd
if (cp[1] == ':') return -TOKEN_LIT; // no ':X
if (ONLY_LEX_FLAG(flags, LEX_SPECIAL_WORD)) return TOKEN_LIT; /* common case */
if (!IS_LEX_WORD(cp[1])) {
// Various special cases of < << <> >> > >= <=
if ((cp[1] == '-' || cp[1] == '+') && IS_LEX_NUMBER(cp[2])) return -TOKEN_WORD;
if (cp[1] == '<' || cp[1] == '>') {
cp++;
if (cp[1] == '<' || cp[1] == '>' || cp[1] == '=') cp++;
if (!IS_LEX_DELIMIT(cp[1])) return -TOKEN_LIT;
scan_state->end = cp+1;
return TOKEN_LIT;
}
}
if (cp[1] == '\'') return -TOKEN_WORD;
type = TOKEN_LIT;
goto scanword;
case LEX_SPECIAL_COMMA: /* ,123 */
case LEX_SPECIAL_PERIOD: /* .123 .123.456.789 */
SET_LEX_FLAG(flags, (GET_LEX_VALUE(*cp)));
if (IS_LEX_NUMBER(cp[1])) goto num;
if (GET_LEX_VALUE(*cp) != LEX_SPECIAL_PERIOD) return -TOKEN_WORD;
type = TOKEN_WORD;
goto scanword;
case LEX_SPECIAL_GREATER:
if (IS_LEX_DELIMIT(cp[1])) return TOKEN_WORD; // RAMBO 3903
if (cp[1] == '>') {
if (IS_LEX_DELIMIT(cp[2])) return TOKEN_WORD;
return -TOKEN_WORD;
}
case LEX_SPECIAL_LESSER:
if (IS_LEX_ANY_SPACE(cp[1]) || cp[1] == ']' || cp[1] == 0) return TOKEN_WORD; // CES.9121 Was LEX_DELIMIT - changed for </tag>
if ((cp[0] == '<' && cp[1] == '<') || cp[1] == '=' || cp[1] == '>') {
if (IS_LEX_DELIMIT(cp[2])) return TOKEN_WORD;
return -TOKEN_WORD;
}
if (GET_LEX_VALUE(*cp) == LEX_SPECIAL_GREATER) return -TOKEN_WORD;
cp = Skip_Tag(cp);
if (!cp) return -TOKEN_TAG;
scan_state->end = cp;
return TOKEN_TAG;
case LEX_SPECIAL_PLUS: /* +123 +123.45 +$123 */
case LEX_SPECIAL_MINUS: /* -123 -123.45 -$123 */
if (HAS_LEX_FLAG(flags, LEX_SPECIAL_AT)) return TOKEN_EMAIL;
if (HAS_LEX_FLAG(flags, LEX_SPECIAL_DOLLAR)) return TOKEN_MONEY;
if (HAS_LEX_FLAG(flags, LEX_SPECIAL_COLON)) {
cp = Skip_To_Char(cp, scan_state->end, ':');
if (cp && (cp+1) != scan_state->end) return TOKEN_TIME; /* 12:34 */
cp = scan_state->begin;
if (cp[1] == ':') { // +: -:
type = TOKEN_WORD;
goto scanword;
}
}
cp++;
if (IS_LEX_AT_LEAST_NUMBER(*cp)) goto num;
if (IS_LEX_SPECIAL(*cp)) {
if ((GET_LEX_VALUE(*cp)) >= LEX_SPECIAL_PERIOD) goto next_ls;
/* if (*cp == '#') goto hex; */
if (*cp == '+' || *cp == '-') {
type = TOKEN_WORD;
goto scanword;
}
return -TOKEN_WORD;
}
type = TOKEN_WORD;
goto scanword;
case LEX_SPECIAL_POUND:
pound:
cp++;
/* hex:
if (HAS_LEX_FLAGS(flags, ~(LEX_FLAG(LEX_SPECIAL_POUND) | LEX_FLAG(LEX_SPECIAL_PERIOD)
| LEX_FLAG(LEX_SPECIAL_TICK) | LEX_FLAG(LEX_SPECIAL_WORD)))) return -TOKEN_INTEGER;
*/
/* if (HAS_LEX_FLAG(flags, LEX_SPECIAL_PERIOD)) return TOKEN_BYTES; */
if (*cp == '[') {
scan_state->end = ++cp;
return TOKEN_CONSTRUCT;
}
if (*cp == '"') { /* CHAR #"C" */
cp++;
type = Scan_Char(&cp);
if (type >= 0 && *cp == '"') {
scan_state->end = cp+1;
return TOKEN_CHAR;
} else { /* try to recover at next new line... */
for (cp = (scan_state->begin)+1; NOT_NEWLINE(*cp); cp++);
scan_state->end = cp;
return -TOKEN_CHAR;
}
}
if (*cp == '{') { /* BINARY #{12343132023902902302938290382} */
scan_state->end = scan_state->begin; /* save start */
scan_state->begin = cp;
cp = Scan_Quote(cp, scan_state); // stores result string in BUF_MOLD !!??
scan_state->begin = scan_state->end; /* restore start */
if (cp) {
scan_state->end = cp;
return TOKEN_BINARY;
} else { /* try to recover at next new line... */
for (cp = (scan_state->begin)+1; NOT_NEWLINE(*cp); cp++);
scan_state->end = cp;
return -TOKEN_BINARY;
}
}
if (cp-1 == scan_state->begin) return TOKEN_ISSUE;
else return -TOKEN_INTEGER;
case LEX_SPECIAL_DOLLAR:
if (HAS_LEX_FLAG(flags, LEX_SPECIAL_AT)) return TOKEN_EMAIL;
return TOKEN_MONEY;
default:
return -TOKEN_WORD;
}
case LEX_CLASS_WORD:
if (ONLY_LEX_FLAG(flags, LEX_SPECIAL_WORD)) return TOKEN_WORD;
type = TOKEN_WORD;
goto scanword;
case LEX_CLASS_NUMBER: /* order of tests is important */
num:
if (!flags) return TOKEN_INTEGER; /* simple integer */
if (HAS_LEX_FLAG(flags, LEX_SPECIAL_AT)) return TOKEN_EMAIL;
if (HAS_LEX_FLAG(flags, LEX_SPECIAL_POUND)) {
if (cp == scan_state->begin) { // no +2 +16 +64 allowed
if (
(cp[0] == '6' && cp[1] == '4' && cp[2] == '#' && cp[3] == '{')
|| (cp[0] == '1' && cp[1] == '6' && cp[2] == '#' && cp[3] == '{') // rare
) {cp += 2; goto pound;}
if (cp[0] == '2' && cp[1] == '#' && cp[2] == '{')
{cp++; goto pound;} // very rare
}
return -TOKEN_INTEGER;
}
if (HAS_LEX_FLAG(flags, LEX_SPECIAL_COLON)) return TOKEN_TIME; /* 12:34 */
if (HAS_LEX_FLAG(flags, LEX_SPECIAL_PERIOD)) { /* 1.2 1.2.3 1,200.3 1.200,3 1.E-2 */
if (Skip_To_Char(cp, scan_state->end, 'x')) return TOKEN_PAIR;
cp = Skip_To_Char(cp, scan_state->end, '.');
if (!(HAS_LEX_FLAG(flags, LEX_SPECIAL_COMMA)) && /* no comma in bytes */
Skip_To_Char(cp+1, scan_state->end, '.')) return TOKEN_TUPLE;
return TOKEN_DECIMAL;
}
if (HAS_LEX_FLAG(flags, LEX_SPECIAL_COMMA)) {
if (Skip_To_Char(cp, scan_state->end, 'x')) return TOKEN_PAIR;
return TOKEN_DECIMAL; /* 1,23 */
}
if (HAS_LEX_FLAG(flags, LEX_SPECIAL_POUND)) { /* -#123 2#1010 */
if (HAS_LEX_FLAGS(flags, ~(LEX_FLAG(LEX_SPECIAL_POUND) | LEX_FLAG(LEX_SPECIAL_PERIOD) | LEX_FLAG(LEX_SPECIAL_TICK)))) return -TOKEN_INTEGER;
if (HAS_LEX_FLAG(flags, LEX_SPECIAL_PERIOD)) return TOKEN_TUPLE;
return TOKEN_INTEGER;
}
/* Note: cannot detect dates of the form 1/2/1998 because they
** may appear within a path, where they are not actually dates!
** Special parsing is required at the next level up. */
for (;cp != scan_state->end; cp++) { /* what do we hit first? 1-AUG-97 or 123E-4 */
if (*cp == '-') return TOKEN_DATE; /* 1-2-97 1-jan-97 */
if (*cp == 'x' || *cp == 'X') return TOKEN_PAIR; // 320x200
if (*cp == 'E' || *cp == 'e') {
if (Skip_To_Char(cp, scan_state->end, 'x')) return TOKEN_PAIR;
return TOKEN_DECIMAL; /* 123E4 */
}
if (*cp == '%') return TOKEN_PERCENT;
}
/*cp = scan_state->begin;*/
if (HAS_LEX_FLAG(flags, LEX_SPECIAL_TICK)) return TOKEN_INTEGER; /* 1'200 */
return -TOKEN_INTEGER;
default:
return -TOKEN_WORD;
}
#if ndef // unreachable code
/* avoid '123 :123 from scanning as a word.... */