|
1 |
| -// Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file |
| 1 | +// Copyright (c) 2025, the Dart project authors. Please see the AUTHORS file |
2 | 2 | // for details. All rights reserved. Use of this source code is governed by a
|
3 | 3 | // BSD-style license that can be found in the LICENSE file.
|
4 | 4 |
|
5 |
| -library _fe_analyzer_shared.scanner.keywords; |
| 5 | +import 'dart:typed_data'; |
6 | 6 |
|
7 |
| -import 'token.dart' as analyzer; |
| 7 | +import 'characters.dart'; |
| 8 | +import 'token.dart'; |
8 | 9 |
|
9 |
| -import 'characters.dart' show $a, $z, $A, $Z; |
| 10 | +extension type KeywordState._(int _offset) { |
| 11 | + static const int blockSize = 59; |
10 | 12 |
|
11 |
| -/** |
12 |
| - * Abstract state in a state machine for scanning keywords. |
13 |
| - */ |
14 |
| -abstract class KeywordState { |
15 |
| - KeywordState? next(int c); |
16 |
| - KeywordState? nextCapital(int c); |
| 13 | + @pragma("vm:prefer-inline") |
| 14 | + bool get isNull => _offset == 0; |
17 | 15 |
|
18 |
| - analyzer.Keyword? get keyword; |
19 |
| - |
20 |
| - static KeywordState? _KEYWORD_STATE; |
21 |
| - static KeywordState get KEYWORD_STATE { |
22 |
| - if (_KEYWORD_STATE == null) { |
23 |
| - List<String> strings = analyzer.Keyword.values |
24 |
| - .map((keyword) => keyword.lexeme) |
25 |
| - .toList(growable: false); |
26 |
| - strings.sort((a, b) => a.compareTo(b)); |
27 |
| - _KEYWORD_STATE = computeKeywordStateTable( |
28 |
| - /* start = */ 0, |
29 |
| - strings, |
30 |
| - /* offset = */ 0, |
31 |
| - strings.length, |
32 |
| - ); |
33 |
| - } |
34 |
| - return _KEYWORD_STATE!; |
| 16 | + @pragma("vm:prefer-inline") |
| 17 | + Keyword? get keyword { |
| 18 | + // The 0'th index at the offset. |
| 19 | + int keywordIndexPlusOne = KeywordStateHelper._table![_offset]; |
| 20 | + if (keywordIndexPlusOne == 0) return null; |
| 21 | + return Keyword.values[keywordIndexPlusOne - 1]; |
35 | 22 | }
|
36 | 23 |
|
37 |
| - static KeywordState computeKeywordStateTable( |
38 |
| - int start, List<String> strings, int offset, int length) { |
39 |
| - bool isLowercase = true; |
40 |
| - |
41 |
| - List<KeywordState?> table = |
42 |
| - new List<KeywordState?>.filled($z - $A + 1, /* fill = */ null); |
43 |
| - assert(length != 0); |
44 |
| - int chunk = 0; |
45 |
| - int chunkStart = -1; |
46 |
| - bool isLeaf = false; |
47 |
| - for (int i = offset; i < offset + length; i++) { |
48 |
| - if (strings[i].length == start) { |
49 |
| - isLeaf = true; |
50 |
| - } |
51 |
| - if (strings[i].length > start) { |
52 |
| - int c = strings[i].codeUnitAt(start); |
53 |
| - if ($A <= c && c <= $Z) { |
54 |
| - isLowercase = false; |
55 |
| - } |
56 |
| - if (chunk != c) { |
57 |
| - if (chunkStart != -1) { |
58 |
| - assert(table[chunk - $A] == null); |
59 |
| - table[chunk - $A] = computeKeywordStateTable( |
60 |
| - start + 1, strings, chunkStart, i - chunkStart); |
61 |
| - } |
62 |
| - chunkStart = i; |
63 |
| - chunk = c; |
64 |
| - } |
65 |
| - } |
66 |
| - } |
67 |
| - if (chunkStart != -1) { |
68 |
| - assert(table[chunk - $A] == null); |
69 |
| - table[chunk - $A] = computeKeywordStateTable( |
70 |
| - start + 1, strings, chunkStart, offset + length - chunkStart); |
71 |
| - } else { |
72 |
| - assert(length == 1); |
73 |
| - return new LeafKeywordState(strings[offset]); |
74 |
| - } |
75 |
| - String? syntax = isLeaf ? strings[offset] : null; |
76 |
| - if (isLowercase) { |
77 |
| - table = table.sublist($a - $A); |
78 |
| - return new LowerCaseArrayKeywordState(table, syntax); |
79 |
| - } else { |
80 |
| - return new UpperCaseArrayKeywordState(table, syntax); |
81 |
| - } |
| 24 | + @pragma("vm:prefer-inline") |
| 25 | + KeywordState next(int next) { |
| 26 | + // The entry for next starts with A at index offset + 1 because offset + 0 |
| 27 | + // is the (possible) keyword. |
| 28 | + return new KeywordState._( |
| 29 | + KeywordStateHelper._table![_offset + next - $A + 1]); |
82 | 30 | }
|
83 | 31 | }
|
84 | 32 |
|
85 |
| -/** |
86 |
| - * A state with multiple outgoing transitions. |
87 |
| - */ |
88 |
| -abstract class ArrayKeywordState implements KeywordState { |
89 |
| - final List<KeywordState?> table; |
90 |
| - @override |
91 |
| - final analyzer.Keyword? keyword; |
92 |
| - |
93 |
| - ArrayKeywordState(this.table, String? syntax) |
94 |
| - : keyword = ((syntax == null) ? null : analyzer.Keyword.keywords[syntax]); |
95 |
| - |
96 |
| - @override |
97 |
| - KeywordState? next(int c); |
98 |
| - |
99 |
| - @override |
100 |
| - KeywordState? nextCapital(int c); |
101 |
| - |
102 |
| - @override |
103 |
| - String toString() { |
104 |
| - StringBuffer sb = new StringBuffer(); |
105 |
| - sb.write("["); |
106 |
| - if (keyword != null) { |
107 |
| - sb.write("*"); |
108 |
| - sb.write(keyword); |
109 |
| - sb.write(" "); |
110 |
| - } |
111 |
| - List<KeywordState?> foo = table; |
112 |
| - for (int i = 0; i < foo.length; i++) { |
113 |
| - if (foo[i] != null) { |
114 |
| - sb.write("${new String.fromCharCodes([i + $a])}: " |
115 |
| - "${foo[i]}; "); |
| 33 | +final class KeywordStateHelper { |
| 34 | + static Uint16List? _table; |
| 35 | + static KeywordState get table { |
| 36 | + if (_table == null) { |
| 37 | + // This is a fixed calculation, though if creating more keywords this |
| 38 | + // number of (double) bytes might have to change. |
| 39 | + Uint16List table = _table = new Uint16List(297 * KeywordState.blockSize); |
| 40 | + int nextEmpty = 2 * KeywordState.blockSize; |
| 41 | + for (int i = 0; i < Keyword.values.length; i++) { |
| 42 | + Keyword keyword = Keyword.values[i]; |
| 43 | + String lexeme = keyword.lexeme; |
| 44 | + // At this point we're looking at the $blockSize bytes |
| 45 | + // $blockSize->(2 * $blockSize + 1). |
| 46 | + // The first blockSize bytes (0->($blockSize-1)) are all 0s, |
| 47 | + // being the "null leaf". |
| 48 | + int offset = KeywordState.blockSize; |
| 49 | + // For an offset, the 0'th byte is a link to the keyword |
| 50 | + // (+1, so 0 means no keyword) and the remaining 58 spots are table |
| 51 | + // entries for codeUnit - $A. |
| 52 | + for (int j = 0; j < lexeme.length; j++) { |
| 53 | + int charOffset = lexeme.codeUnitAt(j) - $A; |
| 54 | + int link = table[offset + 1 + charOffset]; |
| 55 | + if (link == 0) { |
| 56 | + // New one |
| 57 | + table[offset + 1 + charOffset] = nextEmpty; |
| 58 | + offset = nextEmpty; |
| 59 | + nextEmpty += KeywordState.blockSize; |
| 60 | + } else { |
| 61 | + // Existing one. |
| 62 | + offset = link; |
| 63 | + } |
| 64 | + } |
| 65 | + // this offsets position 0 points to the i+1'th keyword. |
| 66 | + table[offset + 0] = i + 1; |
116 | 67 | }
|
| 68 | + assert(nextEmpty == table.length); |
117 | 69 | }
|
118 |
| - sb.write("]"); |
119 |
| - return sb.toString(); |
120 |
| - } |
121 |
| -} |
122 |
| - |
123 |
| -class LowerCaseArrayKeywordState extends ArrayKeywordState { |
124 |
| - LowerCaseArrayKeywordState(List<KeywordState?> table, String? syntax) |
125 |
| - : super(table, syntax) { |
126 |
| - assert(table.length == $z - $a + 1); |
127 |
| - } |
128 |
| - |
129 |
| - @override |
130 |
| - KeywordState? next(int c) => table[c - $a]; |
131 |
| - |
132 |
| - @override |
133 |
| - KeywordState? nextCapital(int c) => null; |
134 |
| -} |
135 |
| - |
136 |
| -class UpperCaseArrayKeywordState extends ArrayKeywordState { |
137 |
| - UpperCaseArrayKeywordState(List<KeywordState?> table, String? syntax) |
138 |
| - : super(table, syntax) { |
139 |
| - assert(table.length == $z - $A + 1); |
| 70 | + return new KeywordState._(KeywordState.blockSize); |
140 | 71 | }
|
141 |
| - |
142 |
| - @override |
143 |
| - KeywordState? next(int c) => table[c - $A]; |
144 |
| - |
145 |
| - @override |
146 |
| - KeywordState? nextCapital(int c) => table[c - $A]; |
147 |
| -} |
148 |
| - |
149 |
| -/** |
150 |
| - * A state that has no outgoing transitions. |
151 |
| - */ |
152 |
| -class LeafKeywordState implements KeywordState { |
153 |
| - @override |
154 |
| - final analyzer.Keyword keyword; |
155 |
| - |
156 |
| - LeafKeywordState(String syntax) |
157 |
| - : keyword = analyzer.Keyword.keywords[syntax]!; |
158 |
| - |
159 |
| - @override |
160 |
| - KeywordState? next(int c) => null; |
161 |
| - @override |
162 |
| - KeywordState? nextCapital(int c) => null; |
163 |
| - |
164 |
| - @override |
165 |
| - String toString() => keyword.lexeme; |
166 | 72 | }
|
0 commit comments