-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
Copy pathtoken.dart
1677 lines (1359 loc) · 46.8 KB
/
token.dart
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 (c) 2016, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
/**
* Defines the tokens that are produced by the scanner, used by the parser, and
* referenced from the [AST structure](ast.dart).
*/
import 'dart:collection';
import 'package:front_end/src/base/syntactic_entity.dart';
import 'package:front_end/src/fasta/scanner/token_constants.dart';
import 'package:front_end/src/scanner/string_utilities.dart';
const int NO_PRECEDENCE = 0;
const int ASSIGNMENT_PRECEDENCE = 1;
const int CASCADE_PRECEDENCE = 2;
const int CONDITIONAL_PRECEDENCE = 3;
const int IF_NULL_PRECEDENCE = 4;
const int LOGICAL_OR_PRECEDENCE = 5;
const int LOGICAL_AND_PRECEDENCE = 6;
const int EQUALITY_PRECEDENCE = 7;
const int RELATIONAL_PRECEDENCE = 8;
const int BITWISE_OR_PRECEDENCE = 9;
const int BITWISE_XOR_PRECEDENCE = 10;
const int BITWISE_AND_PRECEDENCE = 11;
const int SHIFT_PRECEDENCE = 12;
const int ADDITIVE_PRECEDENCE = 13;
const int MULTIPLICATIVE_PRECEDENCE = 14;
const int PREFIX_PRECEDENCE = 15;
const int POSTFIX_PRECEDENCE = 16;
/**
* The opening half of a grouping pair of tokens. This is used for curly
* brackets ('{'), parentheses ('('), and square brackets ('[').
*/
class BeginToken extends SimpleToken {
/**
* The token that corresponds to this token.
*/
Token endToken;
/**
* Initialize a newly created token to have the given [type] at the given
* [offset].
*/
BeginToken(TokenType type, int offset, [CommentToken precedingComment])
: super(type, offset, precedingComment) {
assert(type == TokenType.LT ||
type == TokenType.OPEN_CURLY_BRACKET ||
type == TokenType.OPEN_PAREN ||
type == TokenType.OPEN_SQUARE_BRACKET ||
type == TokenType.STRING_INTERPOLATION_EXPRESSION);
}
@override
Token copy() => new BeginToken(type, offset, copyComments(precedingComments));
@override
Token get endGroup => endToken;
/**
* Set the token that corresponds to this token.
*/
set endGroup(Token token) {
endToken = token;
}
}
/**
* A token representing a comment.
*/
class CommentToken extends StringToken {
/**
* The token that contains this comment.
*/
SimpleToken parent;
/**
* Initialize a newly created token to represent a token of the given [type]
* with the given [value] at the given [offset].
*/
CommentToken(TokenType type, String value, int offset)
: super(type, value, offset);
@override
CommentToken copy() => new CommentToken(type, _value, offset);
/**
* Remove this comment token from the list.
*
* This is used when we decide to interpret the comment as syntax.
*/
void remove() {
if (previous != null) {
previous.setNextWithoutSettingPrevious(next);
next?.previous = previous;
} else {
assert(parent.precedingComments == this);
parent.precedingComments = next;
}
}
}
/**
* A documentation comment token.
*/
class DocumentationCommentToken extends CommentToken {
/**
* The references embedded within the documentation comment.
* This list will be empty unless this is a documentation comment that has
* references embedded within it.
*/
final List<Token> references = <Token>[];
/**
* Initialize a newly created token to represent a token of the given [type]
* with the given [value] at the given [offset].
*/
DocumentationCommentToken(TokenType type, String value, int offset)
: super(type, value, offset);
@override
CommentToken copy() {
DocumentationCommentToken copy =
new DocumentationCommentToken(type, _value, offset);
references.forEach((ref) => copy.references.add(ref.copy()));
return copy;
}
}
/**
* The keywords in the Dart programming language.
*
* Clients may not extend, implement or mix-in this class.
*/
class Keyword extends TokenType {
static const Keyword ABSTRACT =
const Keyword("abstract", "ABSTRACT", isBuiltIn: true, isModifier: true);
static const Keyword AS = const Keyword("as", "AS",
precedence: RELATIONAL_PRECEDENCE, isBuiltIn: true);
static const Keyword ASSERT = const Keyword("assert", "ASSERT");
static const Keyword ASYNC = const Keyword("async", "ASYNC", isPseudo: true);
static const Keyword AWAIT = const Keyword("await", "AWAIT", isPseudo: true);
static const Keyword BREAK = const Keyword("break", "BREAK");
static const Keyword CASE = const Keyword("case", "CASE");
static const Keyword CATCH = const Keyword("catch", "CATCH");
static const Keyword CLASS =
const Keyword("class", "CLASS", isTopLevelKeyword: true);
static const Keyword CONST =
const Keyword("const", "CONST", isModifier: true);
static const Keyword CONTINUE = const Keyword("continue", "CONTINUE");
static const Keyword COVARIANT = const Keyword("covariant", "COVARIANT",
isBuiltIn: true, isModifier: true);
static const Keyword DEFAULT = const Keyword("default", "DEFAULT");
static const Keyword DEFERRED =
const Keyword("deferred", "DEFERRED", isBuiltIn: true);
static const Keyword DO = const Keyword("do", "DO");
static const Keyword DYNAMIC =
const Keyword("dynamic", "DYNAMIC", isBuiltIn: true);
static const Keyword ELSE = const Keyword("else", "ELSE");
static const Keyword ENUM =
const Keyword("enum", "ENUM", isTopLevelKeyword: true);
static const Keyword EXPORT = const Keyword("export", "EXPORT",
isBuiltIn: true, isTopLevelKeyword: true);
static const Keyword EXTENDS = const Keyword("extends", "EXTENDS");
static const Keyword EXTERNAL =
const Keyword("external", "EXTERNAL", isBuiltIn: true, isModifier: true);
static const Keyword FACTORY =
const Keyword("factory", "FACTORY", isBuiltIn: true);
static const Keyword FALSE = const Keyword("false", "FALSE");
static const Keyword FINAL =
const Keyword("final", "FINAL", isModifier: true);
static const Keyword FINALLY = const Keyword("finally", "FINALLY");
static const Keyword FOR = const Keyword("for", "FOR");
static const Keyword FUNCTION =
const Keyword("Function", "FUNCTION", isPseudo: true);
static const Keyword GET = const Keyword("get", "GET", isBuiltIn: true);
static const Keyword HIDE = const Keyword("hide", "HIDE", isPseudo: true);
static const Keyword IF = const Keyword("if", "IF");
static const Keyword IMPLEMENTS =
const Keyword("implements", "IMPLEMENTS", isBuiltIn: true);
static const Keyword IMPORT = const Keyword("import", "IMPORT",
isBuiltIn: true, isTopLevelKeyword: true);
static const Keyword IN = const Keyword("in", "IN");
static const Keyword IS =
const Keyword("is", "IS", precedence: RELATIONAL_PRECEDENCE);
static const Keyword LIBRARY = const Keyword("library", "LIBRARY",
isBuiltIn: true, isTopLevelKeyword: true);
static const Keyword NATIVE =
const Keyword("native", "NATIVE", isPseudo: true);
static const Keyword NEW = const Keyword("new", "NEW");
static const Keyword NULL = const Keyword("null", "NULL");
static const Keyword OF = const Keyword("of", "OF", isPseudo: true);
static const Keyword ON = const Keyword("on", "ON", isPseudo: true);
static const Keyword OPERATOR =
const Keyword("operator", "OPERATOR", isBuiltIn: true);
static const Keyword PART =
const Keyword("part", "PART", isBuiltIn: true, isTopLevelKeyword: true);
static const Keyword PATCH = const Keyword("patch", "PATCH", isPseudo: true);
static const Keyword RETHROW = const Keyword("rethrow", "RETHROW");
static const Keyword RETURN = const Keyword("return", "RETURN");
static const Keyword SET = const Keyword("set", "SET", isBuiltIn: true);
static const Keyword SHOW = const Keyword("show", "SHOW", isPseudo: true);
static const Keyword SOURCE =
const Keyword("source", "SOURCE", isPseudo: true);
static const Keyword STATIC =
const Keyword("static", "STATIC", isBuiltIn: true, isModifier: true);
static const Keyword SUPER = const Keyword("super", "SUPER");
static const Keyword SWITCH = const Keyword("switch", "SWITCH");
static const Keyword SYNC = const Keyword("sync", "SYNC", isPseudo: true);
static const Keyword THIS = const Keyword("this", "THIS");
static const Keyword THROW = const Keyword("throw", "THROW");
static const Keyword TRUE = const Keyword("true", "TRUE");
static const Keyword TRY = const Keyword("try", "TRY");
static const Keyword TYPEDEF = const Keyword("typedef", "TYPEDEF",
isBuiltIn: true, isTopLevelKeyword: true);
static const Keyword VAR = const Keyword("var", "VAR", isModifier: true);
static const Keyword VOID = const Keyword("void", "VOID");
static const Keyword WHILE = const Keyword("while", "WHILE");
static const Keyword WITH = const Keyword("with", "WITH");
static const Keyword YIELD = const Keyword("yield", "YIELD", isPseudo: true);
static const List<Keyword> values = const <Keyword>[
ABSTRACT,
AS,
ASSERT,
ASYNC,
AWAIT,
BREAK,
CASE,
CATCH,
CLASS,
CONST,
CONTINUE,
COVARIANT,
DEFAULT,
DEFERRED,
DO,
DYNAMIC,
ELSE,
ENUM,
EXPORT,
EXTENDS,
EXTERNAL,
FACTORY,
FALSE,
FINAL,
FINALLY,
FOR,
FUNCTION,
GET,
HIDE,
IF,
IMPLEMENTS,
IMPORT,
IN,
IS,
LIBRARY,
NATIVE,
NEW,
NULL,
OF,
ON,
OPERATOR,
PART,
PATCH,
RETHROW,
RETURN,
SET,
SHOW,
SOURCE,
STATIC,
SUPER,
SWITCH,
SYNC,
THIS,
THROW,
TRUE,
TRY,
TYPEDEF,
VAR,
VOID,
WHILE,
WITH,
YIELD,
];
/**
* A table mapping the lexemes of keywords to the corresponding keyword.
*/
static final Map<String, Keyword> keywords = _createKeywordMap();
/**
* A flag indicating whether the keyword is "built-in" identifier.
*/
@override
final bool isBuiltIn;
@override
final bool isPseudo;
/**
* Initialize a newly created keyword.
*/
const Keyword(String lexeme, String name,
{this.isBuiltIn: false,
bool isModifier: false,
this.isPseudo: false,
bool isTopLevelKeyword: false,
int precedence: NO_PRECEDENCE})
: super(lexeme, name, precedence, KEYWORD_TOKEN,
isModifier: isModifier, isTopLevelKeyword: isTopLevelKeyword);
bool get isBuiltInOrPseudo => isBuiltIn || isPseudo;
/**
* A flag indicating whether the keyword is "built-in" identifier.
* This method exists for backward compatibility and will be removed.
* Use [isBuiltIn] instead.
*/
@deprecated
bool get isPseudoKeyword => isBuiltIn; // TODO (danrubel): remove this
/**
* The name of the keyword type.
*/
String get name => lexeme.toUpperCase();
/**
* The lexeme for the keyword.
*
* Deprecated - use [lexeme] instead.
*/
@deprecated
String get syntax => lexeme;
@override
String toString() => name;
/**
* Create a table mapping the lexemes of keywords to the corresponding keyword
* and return the table that was created.
*/
static Map<String, Keyword> _createKeywordMap() {
LinkedHashMap<String, Keyword> result =
new LinkedHashMap<String, Keyword>();
for (Keyword keyword in values) {
result[keyword.lexeme] = keyword;
}
return result;
}
}
/**
* A token representing a keyword in the language.
*/
class KeywordToken extends SimpleToken {
@override
final Keyword keyword;
/**
* Initialize a newly created token to represent the given [keyword] at the
* given [offset].
*/
KeywordToken(this.keyword, int offset, [CommentToken precedingComment])
: super(keyword, offset, precedingComment);
@override
Token copy() =>
new KeywordToken(keyword, offset, copyComments(precedingComments));
@override
bool get isIdentifier => keyword.isPseudo || keyword.isBuiltIn;
@override
bool get isKeyword => true;
@override
bool get isKeywordOrIdentifier => true;
@override
Object value() => keyword;
}
/**
* A token that was scanned from the input. Each token knows which tokens
* precede and follow it, acting as a link in a doubly linked list of tokens.
*/
class SimpleToken implements Token {
/**
* The type of the token.
*/
@override
final TokenType type;
/**
* The offset from the beginning of the file to the first character in the
* token.
*/
@override
int offset = 0;
/**
* The previous token in the token stream.
*/
@override
Token previous;
@override
Token next;
/**
* The first comment in the list of comments that precede this token.
*/
CommentToken _precedingComment;
/**
* Initialize a newly created token to have the given [type] and [offset].
*/
SimpleToken(this.type, this.offset, [this._precedingComment]) {
_setCommentParent(_precedingComment);
}
@override
int get charCount => length;
@override
int get charOffset => offset;
@override
int get charEnd => end;
@override
int get end => offset + length;
@override
Token get endGroup => null;
@override
bool get isEof => type == TokenType.EOF;
@override
bool get isIdentifier => false;
@override
bool get isKeyword => false;
@override
bool get isKeywordOrIdentifier => isIdentifier;
@override
bool get isModifier => type.isModifier;
@override
bool get isOperator => type.isOperator;
@override
bool get isSynthetic => length == 0;
@override
bool get isTopLevelKeyword => type.isTopLevelKeyword;
@override
bool get isUserDefinableOperator => type.isUserDefinableOperator;
@override
Keyword get keyword => null;
@override
int get kind => type.kind;
@override
int get length => lexeme.length;
@override
String get lexeme => type.lexeme;
@override
CommentToken get precedingComments => _precedingComment;
void set precedingComments(CommentToken comment) {
_precedingComment = comment;
_setCommentParent(_precedingComment);
}
@override
String get stringValue => type.stringValue;
@override
Token copy() =>
new SimpleToken(type, offset, copyComments(precedingComments));
@override
Token copyComments(Token token) {
if (token == null) {
return null;
}
Token head = token.copy();
Token tail = head;
token = token.next;
while (token != null) {
tail = tail.setNext(token.copy());
token = token.next;
}
return head;
}
@override
bool matchesAny(List<TokenType> types) {
for (TokenType type in types) {
if (this.type == type) {
return true;
}
}
return false;
}
@override
Token setNext(Token token) {
next = token;
token.previous = this;
return token;
}
@override
Token setNextWithoutSettingPrevious(Token token) {
next = token;
return token;
}
@override
String toString() => lexeme;
@override
Object value() => lexeme;
/**
* Sets the `parent` property to `this` for the given [comment] and all the
* next tokens.
*/
void _setCommentParent(CommentToken comment) {
while (comment != null) {
comment.parent = this;
comment = comment.next;
}
}
}
/**
* A token whose value is independent of it's type.
*/
class StringToken extends SimpleToken {
/**
* The lexeme represented by this token.
*/
String _value;
/**
* Initialize a newly created token to represent a token of the given [type]
* with the given [value] at the given [offset].
*/
StringToken(TokenType type, String value, int offset,
[CommentToken precedingComment])
: super(type, offset, precedingComment) {
this._value = StringUtilities.intern(value);
}
@override
bool get isIdentifier => identical(kind, IDENTIFIER_TOKEN);
@override
String get lexeme => _value;
@override
Token copy() =>
new StringToken(type, _value, offset, copyComments(precedingComments));
@override
String value() => _value;
}
/**
* A synthetic begin token.
*/
class SyntheticBeginToken extends BeginToken {
/**
* Initialize a newly created token to have the given [type] at the given
* [offset].
*/
SyntheticBeginToken(TokenType type, int offset,
[CommentToken precedingComment])
: super(type, offset, precedingComment);
@override
Token copy() =>
new SyntheticBeginToken(type, offset, copyComments(precedingComments));
@override
bool get isSynthetic => true;
@override
int get length => 0;
}
/**
* A synthetic keyword token.
*/
class SyntheticKeywordToken extends KeywordToken {
/**
* Initialize a newly created token to represent the given [keyword] at the
* given [offset].
*/
SyntheticKeywordToken(Keyword keyword, int offset) : super(keyword, offset);
@override
int get length => 0;
@override
Token copy() => new SyntheticKeywordToken(keyword, offset);
}
/**
* A token whose value is independent of it's type.
*/
class SyntheticStringToken extends StringToken {
final int _length;
/**
* Initialize a newly created token to represent a token of the given [type]
* with the given [value] at the given [offset]. If the [length] is
* not specified, then it defaults to the length of [value].
*/
SyntheticStringToken(TokenType type, String value, int offset, [this._length])
: super(type, value, offset);
@override
bool get isSynthetic => true;
@override
int get length => _length ?? super.length;
@override
Token copy() => new SyntheticStringToken(type, _value, offset, _length);
}
/**
* A synthetic token.
*/
class SyntheticToken extends SimpleToken {
SyntheticToken(TokenType type, int offset) : super(type, offset);
@override
int get length => 0;
@override
Token copy() => new SyntheticToken(type, offset);
}
/**
* A token that was scanned from the input. Each token knows which tokens
* precede and follow it, acting as a link in a doubly linked list of tokens.
*
* Clients may not extend, implement or mix-in this class.
*/
abstract class Token implements SyntacticEntity {
/**
* Initialize a newly created token to have the given [type] and [offset].
*/
factory Token(TokenType type, int offset, [CommentToken preceedingComment]) =
SimpleToken;
/**
* Initialize a newly created end-of-file token to have the given [offset].
*/
factory Token.eof(int offset, [CommentToken precedingComments]) {
Token eof = new SimpleToken(TokenType.EOF, offset, precedingComments);
// EOF points to itself so there's always infinite look-ahead.
eof.previous = eof;
eof.next = eof;
return eof;
}
/**
* The number of characters parsed by this token.
*/
int get charCount;
/**
* The character offset of the start of this token within the source text.
*/
int get charOffset;
/**
* The character offset of the end of this token within the source text.
*/
int get charEnd;
@override
int get end;
/**
* The token that corresponds to this token, or `null` if this token is not
* the first of a pair of matching tokens (such as parentheses).
*/
Token get endGroup => null;
/**
* Return `true` if this token represents an end of file.
*/
bool get isEof;
/**
* True if this token is an identifier. Some keywords allowed as identifiers,
* see implementation in [KeywordToken].
*/
bool get isIdentifier;
/**
* True if this token is a keyword. Some keywords allowed as identifiers,
* see implementation in [KeywordToken].
*/
bool get isKeyword;
/**
* True if this token is a keyword or an identifier.
*/
bool get isKeywordOrIdentifier;
/**
* Return `true` if this token is a modifier such as `abstract` or `const`.
*/
bool get isModifier;
/**
* Return `true` if this token represents an operator.
*/
bool get isOperator;
/**
* Return `true` if this token is a synthetic token. A synthetic token is a
* token that was introduced by the parser in order to recover from an error
* in the code.
*/
bool get isSynthetic;
/**
* Return `true` if this token is a keyword starting a top level declaration
* such as `class`, `enum`, `import`, etc.
*/
bool get isTopLevelKeyword;
/**
* Return `true` if this token represents an operator that can be defined by
* users.
*/
bool get isUserDefinableOperator;
/**
* Return the keyword, if a keyword token, or `null` otherwise.
*/
Keyword get keyword;
/**
* The kind enum of this token as determined by its [type].
*/
int get kind;
@override
int get length;
/**
* Return the lexeme that represents this token.
*
* For [StringToken]s the [lexeme] includes the quotes, explicit escapes, etc.
*/
String get lexeme;
/**
* Return the next token in the token stream.
*/
Token get next;
/**
* Return the next token in the token stream.
*/
void set next(Token next);
@override
int get offset;
/**
* Set the offset from the beginning of the file to the first character in
* the token to the given [offset].
*/
void set offset(int offset);
/**
* Return the first comment in the list of comments that precede this token,
* or `null` if there are no comments preceding this token. Additional
* comments can be reached by following the token stream using [next] until
* `null` is returned.
*
* For example, if the original contents were `/* one */ /* two */ id`, then
* the first preceding comment token will have a lexeme of `/* one */` and
* the next comment token will have a lexeme of `/* two */`.
*/
Token get precedingComments;
/**
* Return the previous token in the token stream.
*/
Token get previous;
/**
* Set the previous token in the token stream to the given [token].
*/
void set previous(Token token);
/**
* For symbol and keyword tokens, returns the string value represented by this
* token. For [StringToken]s this method returns [:null:].
*
* For [SymbolToken]s and [KeywordToken]s, the string value is a compile-time
* constant originating in the [TokenType] or in the [Keyword] instance.
* This allows testing for keywords and symbols using [:identical:], e.g.,
* [:identical('class', token.value):].
*
* Note that returning [:null:] for string tokens is important to identify
* symbols and keywords, we cannot use [lexeme] instead. The string literal
* "$a($b"
* produces ..., SymbolToken($), StringToken(a), StringToken((), ...
*
* After parsing the identifier 'a', the parser tests for a function
* declaration using [:identical(next.stringValue, '('):], which (rightfully)
* returns false because stringValue returns [:null:].
*/
String get stringValue;
/**
* Return the type of the token.
*/
TokenType get type;
/**
* Return a newly created token that is a copy of this tokens
* including any [preceedingComment] tokens,
* but that is not a part of any token stream.
*/
Token copy();
/**
* Copy a linked list of comment tokens identical to the given comment tokens.
*/
Token copyComments(Token token);
/**
* Return `true` if this token has any one of the given [types].
*/
bool matchesAny(List<TokenType> types);
/**
* Set the next token in the token stream to the given [token]. This has the
* side-effect of setting this token to be the previous token for the given
* token. Return the token that was passed in.
*/
Token setNext(Token token);
/**
* Set the next token in the token stream to the given token without changing
* which token is the previous token for the given token. Return the token
* that was passed in.
*/
Token setNextWithoutSettingPrevious(Token token);
/**
* Returns a textual representation of this token to be used for debugging
* purposes. The resulting string might contain information about the
* structure of the token, for example 'StringToken(foo)' for the identifier
* token 'foo'.
*
* Use [lexeme] for the text actually parsed by the token.
*/
@override
String toString();
/**
* Return the value of this token. For keyword tokens, this is the keyword
* associated with the token, for other tokens it is the lexeme associated
* with the token.
*/
Object value();
/**
* Compare the given [tokens] to find the token that appears first in the
* source being parsed. That is, return the left-most of all of the tokens.
* The list must be non-`null`, but the elements of the list are allowed to be
* `null`. Return the token with the smallest offset, or `null` if the list is
* empty or if all of the elements of the list are `null`.
*/
static Token lexicallyFirst(List<Token> tokens) {
Token first = null;
int offset = -1;
int length = tokens.length;
for (int i = 0; i < length; i++) {
Token token = tokens[i];
if (token != null && (offset < 0 || token.offset < offset)) {
first = token;
offset = token.offset;
}
}
return first;
}
}
/**
* The classes (or groups) of tokens with a similar use.
*/
class TokenClass {
/**
* A value used to indicate that the token type is not part of any specific
* class of token.
*/
static const TokenClass NO_CLASS = const TokenClass('NO_CLASS');
/**
* A value used to indicate that the token type is an additive operator.
*/
static const TokenClass ADDITIVE_OPERATOR =
const TokenClass('ADDITIVE_OPERATOR', 13);
/**
* A value used to indicate that the token type is an assignment operator.
*/
static const TokenClass ASSIGNMENT_OPERATOR =
const TokenClass('ASSIGNMENT_OPERATOR', 1);
/**
* A value used to indicate that the token type is a bitwise-and operator.