-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathLexer.x
2849 lines (2482 loc) · 95.3 KB
/
Lexer.x
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
import io.TextPosition;
/**
* A lexical analyzer (tokenizer) for the Ecstasy language.
*/
class Lexer
implements Iterator<Token>
implements Markable {
// ----- constructors --------------------------------------------------------------------------
/**
* Construct an Ecstasy lexical analyzer ("tokenizer") that processes source code from a Reader.
*
* @param source the Ecstasy source code
* @param errs the ErrorList to log errors to
* @param synthesizeEof pass True to automatically append a
*/
construct(String source, ErrorList? errs = Null, Boolean synthesizeEof = False) {
construct Lexer(new Source(source), errs, synthesizeEof);
}
/**
* Construct an Ecstasy lexical analyzer ("tokenizer") that processes source code from a Reader.
*
* @param source the Ecstasy source code
* @param errs the ErrorList to log errors to
* @param synthesizeEof pass True to automatically append a
*/
construct(Source source, ErrorList? errs = Null, Boolean synthesizeEof = False) {
this.source = source;
this.reader = source.createReader();
this.errs = errs ?: new ErrorList(10);
this.synthesizeEof = synthesizeEof;
} finally {
eatWhitespace();
}
/**
* Internal use.
*
* @param parent a Lexer that this Lexer can delegate to if necessary
*/
protected construct(Lexer parent) {
source = parent.source;
errs = parent.errs;
reader = source.createReader();
}
// ----- properties ----------------------------------------------------------------------------
/**
* The [Source] being lexed.
*/
public/private Source source;
/**
* The underlying [Reader]. Note that the UTF-escape transformation occurs between this Reader
* and the lexer.
*/
protected/private Reader reader;
/**
* True once the stream of characters is exhausted.
*/
public Boolean eof.get() = reader.eof;
/**
* The number of characters past the EOF that the lexer has pretended to read.
*/
protected/private Int pastEOF;
/**
* The number of characters past the EOF that the lexer has pretended to read.
*/
protected/private Boolean synthesizeEof;
/**
* A special "end of file" token.
*/
public/private Token? eofToken;
/**
* Keeps track of whether whitespace was encountered.
*/
protected/private Boolean whitespace;
/**
* The ErrorList to log errors to.
*/
public/private ErrorList errs;
// ----- Iterator methods ----------------------------------------------------------------------
@Override
conditional Token next() {
if (eof) {
if (synthesizeEof && eofToken == Null) {
// once EOF has been reached, the lexer creates a synthetic (fake) token that
// represents the end-of-file condition; this is useful for matching/demand parsers
// that don't want to check for an end-of-file condition everywhere
TextPosition pos = reader.position;
Token eofToken = new Token(EndOfFile, pos, pos, Null, True, True);
this.eofToken = eofToken;
return True, eofToken;
}
return False;
}
Boolean spaceBefore = whitespace;
TextPosition posBefore = reader.position;
(Id id, Object value) = eatToken(posBefore);
TextPosition posAfter = reader.position;
Boolean spaceAfter = eatWhitespace();
return True, new Token(id, posBefore, posAfter, value, spaceBefore, spaceAfter);
}
// ----- Markable methods ----------------------------------------------------------------------
/**
* A restorable position within the Lexer (Literally, Lex-Mark.)
*/
protected static const Mark(TextPosition position, Int pastEOF, Boolean whitespace);
@Override
immutable Object mark() = new Mark(reader.position, pastEOF, whitespace);
@Override
void restore(immutable Object mark, Boolean unmark = False) {
assert mark.is(Mark);
reader.position = mark.position;
this.pastEOF = mark.pastEOF;
this.whitespace = mark.whitespace;
}
// ----- simulated Lexer -----------------------------------------------------------------------
/**
* Create a Lexer that pretends to lex the provided array of tokens.
*
* @param tokens the tokens to emit
*
* @return the new Lexer
*/
Lexer! createLexer(Token[] tokens) {
return new Lexer(this) {
Int index = 0;
@Override
conditional Token next() {
if (index >= tokens.size) {
return False;
}
return True, tokens[index++];
}
@Override
immutable Object mark() {
return index;
}
@Override
void restore(immutable Object mark, Boolean unmark = False) {
index = mark.as(Int);
}
};
}
// ----- internal ------------------------------------------------------------------------------
/**
* Lex a single token.
*
* @param before the position of the first character of the token
*
* @return id the token id
* @return value the token value (usually Null)
*/
protected (Id id, Object value) eatToken(TextPosition before) {
switch (Char next = nextChar()) {
case '{':
return LeftCurly, Null;
case '}':
return RightCurly, Null;
case '(':
return LeftParen, Null;
case ')':
return RightParen, Null;
case '[':
return LeftSquare, Null;
case ']':
return RightSquare, Null;
case ';':
return Semicolon, Null;
case ',':
return Comma, Null;
case '.':
switch (nextChar()) {
case '.':
switch (nextChar()) {
case '/':
return ParentDir, Null;
default:
rewind();
return DotDot, Null;
}
case '/':
return CurrentDir, Null;
case '0'..'9':
rewind(2);
return eatNumericLiteral(before);
default:
rewind();
return Dot, Null;
}
case '$':
switch (nextChar()) {
case '\"':
return eatTemplateLiteral(before);
case '|':
return eatMultilineTemplateLiteral(before);
case '/':
// it is a file name starting with "/"
rewind();
return StrFile, Null;
case '.':
switch (nextChar()) {
case '.':
switch (nextChar()) {
case '/':
// it is a file name starting with "../"
rewind(3);
return StrFile, Null;
default:
rewind(3);
return Identifier, "$";
}
case '/':
// it is a file name starting with "./"
rewind(2);
return StrFile, Null;
default:
rewind(2);
return Identifier, "$";
}
default:
rewind();
return Identifier, "$";
}
case '#':
switch (nextChar()) {
case '.':
case '/':
// it is a file name
rewind();
return BinFile, Null;
case '|':
// multi-line binary literal
return eatBinaryLiteral(before, True);
default:
rewind();
return eatBinaryLiteral(before, False);
}
case '@':
return At, Null;
case '?':
switch (nextChar()) {
case '=':
return NotNullAsn, Null;
case ':':
switch (nextChar()) {
case '=':
return ElvisAsn, Null;
default:
rewind();
return Elvis, Null;
}
default:
rewind();
return Condition, Null;
}
case ':':
switch (nextChar()) {
case '=':
return CondAsn, Null;
default:
rewind();
return Colon, Null;
}
case '+':
switch (nextChar()) {
case '+':
return Increment, Null;
case '=':
return AddAsn, Null;
default:
rewind();
return Add, Null;
}
case '-':
switch (nextChar()) {
case '-':
return Decrement, Null;
case '>':
return Lambda, Null;
case '=':
return SubAsn, Null;
default:
rewind();
return Sub, Null;
}
case '*':
switch (nextChar()) {
case '=':
return MulAsn, Null;
default:
rewind();
return Mul, Null;
}
case '/':
switch (nextChar()) {
case '/':
return eatSingleLineComment(before);
case '*':
return eatEnclosedComment(before);
case '=':
return DivAsn, Null;
case '%':
return DivRem, Null;
default:
rewind();
return Div, Null;
}
case '<':
switch (nextChar()) {
case '<':
switch (nextChar()) {
case '=':
return ShiftLeftAsn, Null;
default:
rewind();
return ShiftLeft, Null;
}
case '=':
switch (nextChar()) {
case '>':
return CompareOrder, Null;
default:
rewind();
return CompareLTEQ, Null;
}
case '-':
return AsnExpr, Null;
default:
rewind();
return CompareLT, Null;
}
case '>':
switch (nextChar()) {
case '>':
switch (nextChar()) {
case '>':
switch (nextChar()) {
case '=':
return ShiftAllAsn, Null;
default:
rewind();
return ShiftAll, Null;
}
case '=':
return ShiftRightAsn, Null;
default:
rewind();
return ShiftRight, Null;
}
case '=':
return CompareGTEQ, Null;
default:
rewind();
return CompareGT, Null;
}
case '&':
switch (nextChar()) {
case '&':
switch (nextChar()) {
case '=':
return BoolAndAsn, Null;
default:
rewind();
return BoolAnd, Null;
}
case '=':
return BitAndAsn, Null;
default:
rewind();
return BitAnd, Null;
}
case '|':
switch (nextChar()) {
case '|':
switch (nextChar()) {
case '=':
return BoolOrAsn, Null;
default:
rewind();
return BoolOr, Null;
}
case '=':
return BitOrAsn, Null;
default:
rewind();
return BitOr, Null;
}
case '=':
switch (nextChar()) {
case '=':
return CompareEQ, Null;
default:
rewind();
return Asn, Null;
}
case '%':
switch (nextChar()) {
case '=':
return ModuloAsn, Null;
default:
rewind();
return Modulo, Null;
}
case '!':
switch (nextChar()) {
case '=':
return CompareNE, Null;
default:
rewind();
return BoolNot, Null;
}
case '^':
switch (nextChar()) {
case '(':
return AsyncParen, Null;
case '^':
return BoolXor, Null;
case '=':
return BitXorAsn, Null;
default:
rewind();
return BitXor, Null;
}
case '~':
return BitNot, Null;
case '0'..'9':
rewind();
return eatNumericLiteral(before);
case '\'':
return eatCharLiteral(before);
case '\"':
return eatStringLiteral(before);
case '\\':
switch (nextChar()) {
case '|':
return eatMultilineLiteral(before);
default:
rewind();
break;
}
continue;
default:
if (!isIdentifierStart(next)) {
log(Error, IllegalChar, [next.quoted()], before, reader.position);
}
continue;
case 'A'..'Z':
case 'a'..'z':
case '_':
return eatIdentifierOrKeyword(before, next);
}
}
/**
* Eat a token that may be an identifier or keyword. The first character has already been eaten.
*
* @param before the position of the first character of the token
* @param first the first character of the token
*
* @return id the token id
* @return value the token value
*/
protected (Id id, Object value) eatIdentifierOrKeyword(TextPosition before, Char first) {
StringBuffer nameBuf = new StringBuffer();
Char next = first;
do {
nameBuf.add(next);
next = nextChar();
} while (isIdentifierPart(next));
rewind();
String name = nameBuf.toString();
if (name == Id.Todo.text) {
// the T0D0 keyword has two different lexical modes: an end-of-line comment mode, and
// a looks-like-a-function-call mode
if (next == '(') {
return Todo, Null;
} else {
(_, String text) = eatSingleLineComment(before);
return Todo, text;
}
}
if (next == ':') {
TextPosition colon = reader.position;
assert nextChar() == ':';
StringBuffer buf = new StringBuffer();
while (!eof) {
Char ch = nextChar();
if (isIdentifierPart(ch)) {
buf.add(ch);
} else {
rewind();
break;
}
}
String suffix = buf.toString();
// check for a possible keyword that has different suffixed variants
if (Id prefixId := Id.prefixes.get(name)) {
// check for a legal suffix, e.g. "this:private"
String full = name + ':' + suffix;
if (Id fullId := Id.allKeywords.get(full)) {
return fullId, Null;
}
reader.position = colon;
return prefixId, Null;
}
// check for suffix of private / protected / public / struct (etc.); these will get
// lexed in some subsequent call to next()
reader.position = colon;
if (!Id.keywords.contains(suffix)) {
// check for some specific literal type formats
switch (name) {
case "Bit":
IntLiteral value = eatIntLiteral(before, Bit.MinValue, Bit.MaxValue, True);
return LitBit, value.toBit();
case "Nibble":
IntLiteral value = eatIntLiteral(before, Nibble.MinValue, Nibble.MaxValue, True);
return LitNibble, value.toNibble();
case "Int":
IntLiteral value = eatIntLiteral(before, Int.MinValue, Int.MaxValue, True);
return LitInt, value.toInt();
case "Int8":
IntLiteral value = eatIntLiteral(before, Int8.MinValue, Int8.MaxValue, True);
return LitInt8, value.toInt8();
case "Int16":
IntLiteral value = eatIntLiteral(before, Int16.MinValue, Int16.MaxValue, True);
return LitInt16, value.toInt16();
case "Int32":
IntLiteral value = eatIntLiteral(before, Int32.MinValue, Int32.MaxValue, True);
return LitInt32, value.toInt32();
case "Int64":
IntLiteral value = eatIntLiteral(before, Int64.MinValue, Int64.MaxValue, True);
return LitInt64, value.toInt64();
case "Int128":
IntLiteral value = eatIntLiteral(before, Int128.MinValue, Int128.MaxValue, True);
return LitInt128, value.toInt128();
case "IntN":
IntLiteral value = eatIntLiteral(before, explicitlyInt=True);
return LitIntN, value.toIntN();
case "UInt":
IntLiteral value = eatIntLiteral(before, UInt.MinValue, UInt.MaxValue, True);
return LitUInt, value.toUInt();
case "UInt8":
IntLiteral value = eatIntLiteral(before, UInt8.MinValue, UInt8.MaxValue, True);
return LitUInt8, value.toUInt8();
case "UInt16":
IntLiteral value = eatIntLiteral(before, UInt16.MinValue, UInt16.MaxValue, True);
return LitUInt16, value.toUInt16();
case "UInt32":
IntLiteral value = eatIntLiteral(before, UInt32.MinValue, UInt32.MaxValue, True);
return LitUInt32, value.toUInt32();
case "UInt64":
IntLiteral value = eatIntLiteral(before, UInt64.MinValue, UInt64.MaxValue, True);
return LitUInt64, value.toUInt64();
case "UInt128":
IntLiteral value = eatIntLiteral(before, UInt128.MinValue, UInt128.MaxValue, True);
return LitUInt128, value.toUInt128();
case "UIntN":
IntLiteral value = eatIntLiteral(before, explicitlyInt=True);
return LitUIntN, value.toUIntN();
case "Dec":
(Id id, IntLiteral|FPLiteral value) = eatNumericLiteral(before);
return LitDec, value.toDec();
case "Dec32":
(Id id, IntLiteral|FPLiteral value) = eatNumericLiteral(before);
return LitDec32, value.toDec32();
case "Dec64":
(Id id, IntLiteral|FPLiteral value) = eatNumericLiteral(before);
return LitDec64, value.toDec64();
case "Dec128":
(Id id, IntLiteral|FPLiteral value) = eatNumericLiteral(before);
return LitDec128, value.toDec128();
case "DecN":
(Id id, IntLiteral|FPLiteral value) = eatNumericLiteral(before);
return LitDecN, value.toDecN();
case "Float8e4":
(Id id, IntLiteral|FPLiteral value) = eatNumericLiteral(before);
return LitFloat8e4, value.toFloat8e4();
case "Float8e5":
(Id id, IntLiteral|FPLiteral value) = eatNumericLiteral(before);
return LitFloat8e5, value.toFloat8e5();
case "BFloat16":
(Id id, IntLiteral|FPLiteral value) = eatNumericLiteral(before);
return LitBFloat16, value.toBFloat16();
case "Float16":
(Id id, IntLiteral|FPLiteral value) = eatNumericLiteral(before);
return LitFloat16, value.toFloat16();
case "Float32":
(Id id, IntLiteral|FPLiteral value) = eatNumericLiteral(before);
return LitFloat32, value.toFloat32();
case "Float64":
(Id id, IntLiteral|FPLiteral value) = eatNumericLiteral(before);
return LitFloat64, value.toFloat64();
case "Float128":
(Id id, IntLiteral|FPLiteral value) = eatNumericLiteral(before);
return LitFloat128, value.toFloat128();
case "FloatN":
(Id id, IntLiteral|FPLiteral value) = eatNumericLiteral(before);
return LitFloatN, value.toFloatN();
case "Date":
return eatDateLiteral(before);
case "TimeOfDay":
return eatTimeOfDayLiteral(before);
case "Time":
return eatTimeLiteral(before);
case "TimeZone":
return eatTimeZoneLiteral(before);
case "Duration":
return eatDurationLiteral(before);
case "Version":
case "v":
return eatVersionLiteral(before);
}
}
}
Id? keyword = Id.keywords[name];
return (keyword?, Null) : (Identifier, name);
}
/**
* Lex a date literal token, starting with the colon.
*
* @param before the position of the first character of the token
* @param embedded True indicates that this literal value is part of a larger time value
*
* @return id the token id
* @return value the token value
*/
protected (Id id, Date value) eatDateLiteral(TextPosition before, Boolean embedded = False) {
assert embedded || nextChar() == ':';
TextPosition start = embedded ? reader.position : before;
Int year = 0;
Int month = 0;
Int day = 0;
if (year := eatDigits(4)) {
Boolean sep = match('-');
if (month := eatDigits(2)) {
if (!sep || expect('-')) {
day := eatDigits(2);
}
}
}
if (year < 1582 || month < 1 || month > 12 || day < 1 || day > Date.daysInMonth(year, month)) {
TextPosition end = reader.position;
String date = reader[start ..< end];
log(Error, BadDate, [date], before, end);
return LitDate, new Date(1970, 1, 1);
}
if (!embedded) {
peekNotIdentifierOrNumber();
}
return LitDate, new Date(year, month, day);
}
/**
* Lex a time-of-day literal token, starting with the colon.
*
* @param before the position of the first character of the token
* @param embedded True indicates that this literal value is part of a larger time value
*
* @return id the token id
* @return value the token value
*/
protected (Id id, TimeOfDay value) eatTimeOfDayLiteral(TextPosition before, Boolean embedded = False) {
assert embedded || nextChar() == ':';
TextPosition start = embedded ? reader.position : before;
Int hours = 0;
Int minutes = 0;
Int seconds = 0;
Int picos = 0;
if (hours := eatDigits(2)) {
Boolean colon = match(':');
if (minutes := eatDigits(2)) {
if ((colon && match(':') || !colon && peekDigit()),
seconds := eatDigits(2),
match('.')) {
Int digits = 0;
while (Char ch := nextDigit()) {
if (++digits <= 12) {
picos = picos * 10 + (ch - '0');
}
}
if (digits == 0) {
// assume that the '.' is part of the next token
rewind();
} else {
// scale the integer value up to picos (trillionths)
while (++digits <= 12) {
picos *= 10;
}
}
}
}
}
if (!((0 <= hours <= 23 || hours == 24 && minutes == 0 && seconds == 0)
&& (0 <= minutes <= 59)
&& (0 <= seconds <= 59 || minutes == 59 && seconds == 60))) {
TextPosition end = reader.position;
String timeOfDay = reader[start ..< end];
log(Error, BadTimeOfDay, [timeOfDay], before, end);
return LitTimeOfDay, MIDNIGHT;
}
if (!embedded) {
peekNotIdentifierOrNumber();
}
return LitTimeOfDay, new TimeOfDay(hours, minutes, seconds, picos);
}
/**
* Lex a timezone literal token, starting with the colon.
*
* @param before the position of the first character of the token
* @param embedded True indicates that this literal value is part of a larger time value
*
* @return id the token id
* @return value the token value
*/
protected (Id id, TimeZone value) eatTimeZoneLiteral(TextPosition before, Boolean embedded = False) {
assert embedded || nextChar() == ':';
TextPosition start = embedded ? reader.position : before;
if (match('Z') || match('z')) {
peekNotIdentifierOrNumber();
return LitTimezone, UTC;
}
Int hour = 0;
Int minute = 0;
Boolean minus = False;
Boolean legit = False;
switch (nextChar()) {
case '-':
minus = True;
continue;
case '+':
if (hour := eatDigits(2)) {
if (match(':') || peekDigit()) {
if (minute := eatDigits(2)) {
legit = True;
peekNotIdentifierOrNumber();
}
} else {
legit = True;
peekNotIdentifierOrNumber();
}
}
break;
default:
rewind();
break;
}
if (!legit || hour > 16 || minute > 59) {
TextPosition end = reader.position;
String timezone = reader[start ..< end];
log(Error, BadTimezone, [timezone], before, end);
return LitTimezone, NoTZ;
}
Int offset = hour * TimeOfDay.PicosPerHour + minute * TimeOfDay.PicosPerMinute;
return LitTimezone, new TimeZone((minus ? -1 : +1) * offset.toInt64());
}
/**
* Lex a date/time literal token, starting with the colon.
*
* @param before the position of the first character of the token
*
* @return id the token id
* @return value the token value
*/
protected (Id id, Time value) eatTimeLiteral(TextPosition before) {
assert nextChar() == ':';
(_, Date date) = eatDateLiteral(before, True);
TimeOfDay timeOfDay = MIDNIGHT;
TimeZone timezone = NoTZ;
if (match('t') || expect('T')) {
(_, timeOfDay) = eatTimeOfDayLiteral(before, True);
switch (peekChar()) {
case 'Z', 'z':
case '+', '-':
(_, timezone) = eatTimeZoneLiteral(before, True);
break;
}
} else {
log(Error, BadTime, [reader[before ..< reader.position].toString()], before, reader.position);
}
return LitTime, new Time(date, timeOfDay, timezone);
}
/**
* Lex a time duration literal token, starting with the colon.
*
* @param before the position of the first character of the token
*
* @return id the token id
* @return value the token value
*/
protected (Id id, Duration value) eatDurationLiteral(TextPosition before) {
assert nextChar() == ':';
enum Stage(Boolean naked=False) {Init(True), Head(True), Day, Sep(True), Hour, Minute, Second, Fraction, Err}
Stage prevStage = Init;
Int128 picos = 0;
Boolean any = False;
Boolean err = False;
Loop: while (True) {
// read the number
static UInt MAX = MaxValue / 10 - 1;
UInt value = 0;
UInt digits = 0;
while (Char digit := nextDigit()) {
if (value >= MAX) {
err = True;
} else {
value = value * 10 + (digit - '0');
}
++digits;
}
// read the label
Char label = nextChar();
Stage stage;
switch (label) {
case 'P', 'p':
// the "P" just indicates a duration value
stage = Head;
break;
case 'D', 'd':
stage = Day;
picos += value * Duration.PicosPerDay;
break;
case 'T':
case 't':
stage = Sep;
break;
case 'H', 'h':
stage = Hour;
picos += value * Duration.PicosPerHour;
break;
case 'M', 'm':
stage = Minute;
picos += value * Duration.PicosPerMinute;
break;
case '.':
stage = Second;
picos += value * Duration.PicosPerSecond;
break;