Skip to content

Commit b699852

Browse files
kallentucommit-bot@chromium.org
authored andcommitted
Add 'out'/'inout' keywords for variance.
Under the `variance` flag, `out` and `inout` will be treated as keywords. Change-Id: I73ba6871a9f0d410a7b3818833a1f50a5f75fba6 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/117203 Commit-Queue: Kallen Tu <[email protected]> Reviewed-by: Leaf Petersen <[email protected]>
1 parent a1fdfb8 commit b699852

File tree

5 files changed

+45
-1
lines changed

5 files changed

+45
-1
lines changed

pkg/front_end/lib/src/fasta/scanner/abstract_scanner.dart

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,10 @@ abstract class AbstractScanner implements Scanner {
7373
/// and https://github.com/dart-lang/language/issues/60
7474
bool _enableTripleShift = false;
7575

76+
/// Experimental flag for enabling variance.
77+
/// See https://github.com/dart-lang/language/issues/524
78+
bool _enableVariance = false;
79+
7680
/**
7781
* The string offset for the next token that will be created.
7882
*
@@ -139,6 +143,7 @@ abstract class AbstractScanner implements Scanner {
139143
_enableExtensionMethods = config.enableExtensionMethods;
140144
_enableNonNullable = config.enableNonNullable;
141145
_enableTripleShift = config.enableTripleShift;
146+
_enableVariance = config.enableVariance;
142147
}
143148
}
144149

@@ -1483,6 +1488,10 @@ abstract class AbstractScanner implements Scanner {
14831488
(state.keyword == Keyword.LATE || state.keyword == Keyword.REQUIRED)) {
14841489
return tokenizeIdentifier(next, start, allowDollar);
14851490
}
1491+
if (!_enableVariance &&
1492+
(state.keyword == Keyword.OUT || state.keyword == Keyword.INOUT)) {
1493+
return tokenizeIdentifier(next, start, allowDollar);
1494+
}
14861495
if (($A <= next && next <= $Z) ||
14871496
($0 <= next && next <= $9) ||
14881497
identical(next, $_) ||
@@ -1907,13 +1916,19 @@ class ScannerConfiguration {
19071916
/// and https://github.com/dart-lang/language/issues/60
19081917
final bool enableTripleShift;
19091918

1919+
/// Experimental flag for enabling variance.
1920+
/// See https://github.com/dart-lang/language/issues/524
1921+
final bool enableVariance;
1922+
19101923
const ScannerConfiguration({
19111924
bool enableExtensionMethods,
19121925
bool enableNonNullable,
19131926
bool enableTripleShift,
1927+
bool enableVariance,
19141928
}) : this.enableExtensionMethods = enableExtensionMethods ?? false,
19151929
this.enableNonNullable = enableNonNullable ?? false,
1916-
this.enableTripleShift = enableTripleShift ?? false;
1930+
this.enableTripleShift = enableTripleShift ?? false,
1931+
this.enableVariance = enableVariance ?? false;
19171932
}
19181933

19191934
bool _isIdentifierChar(int next, bool allowDollar) {

pkg/front_end/lib/src/scanner/token.dart

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,8 @@ class Keyword extends TokenType {
209209

210210
static const Keyword IN = const Keyword("in", "IN");
211211

212+
static const Keyword INOUT = const Keyword("inout", "INOUT", isPseudo: true);
213+
212214
static const Keyword INTERFACE =
213215
const Keyword("interface", "INTERFACE", isBuiltIn: true);
214216

@@ -237,6 +239,8 @@ class Keyword extends TokenType {
237239
static const Keyword OPERATOR =
238240
const Keyword("operator", "OPERATOR", isBuiltIn: true);
239241

242+
static const Keyword OUT = const Keyword("out", "OUT", isPseudo: true);
243+
240244
static const Keyword PART =
241245
const Keyword("part", "PART", isBuiltIn: true, isTopLevelKeyword: true);
242246

@@ -321,6 +325,7 @@ class Keyword extends TokenType {
321325
IMPLEMENTS,
322326
IMPORT,
323327
IN,
328+
INOUT,
324329
INTERFACE,
325330
IS,
326331
LATE,
@@ -332,6 +337,7 @@ class Keyword extends TokenType {
332337
OF,
333338
ON,
334339
OPERATOR,
340+
OUT,
335341
PART,
336342
PATCH,
337343
REQUIRED,

pkg/front_end/test/scanner_test.dart

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -544,6 +544,16 @@ abstract class ScannerTestBase {
544544
_assertKeywordToken("in");
545545
}
546546

547+
void test_keyword_inout() {
548+
_assertKeywordToken("inout",
549+
configuration: ScannerConfiguration(enableVariance: true));
550+
}
551+
552+
void test_keyword_inout_old() {
553+
_assertNotKeywordToken("inout",
554+
configuration: ScannerConfiguration(enableVariance: false));
555+
}
556+
547557
void test_keyword_is() {
548558
_assertKeywordToken("is");
549559
}
@@ -590,6 +600,16 @@ abstract class ScannerTestBase {
590600
_assertKeywordToken("operator");
591601
}
592602

603+
void test_keyword_out() {
604+
_assertKeywordToken("out",
605+
configuration: ScannerConfiguration(enableVariance: true));
606+
}
607+
608+
void test_keyword_out_old() {
609+
_assertNotKeywordToken("out",
610+
configuration: ScannerConfiguration(enableVariance: false));
611+
}
612+
593613
void test_keyword_part() {
594614
_assertKeywordToken("part");
595615
}

pkg/front_end/test/spell_checking_list_common.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1354,6 +1354,7 @@ inlined
13541354
inlining
13551355
inner
13561356
innermost
1357+
inout
13571358
input
13581359
inputs
13591360
insert

pkg/front_end/test/token_test.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,9 +171,11 @@ class Foo {
171171
Keyword.AWAIT,
172172
Keyword.FUNCTION,
173173
Keyword.HIDE,
174+
Keyword.INOUT,
174175
Keyword.NATIVE,
175176
Keyword.OF,
176177
Keyword.ON,
178+
Keyword.OUT,
177179
Keyword.PATCH,
178180
Keyword.SHOW,
179181
Keyword.SOURCE,

0 commit comments

Comments
 (0)