OLD | NEW |
| (Empty) |
1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | |
2 // for details. All rights reserved. Use of this source code is governed by a | |
3 // BSD-style license that can be found in the LICENSE file. | |
4 | |
5 library dart2js.tokens.keywords; | |
6 | |
7 import '../util/characters.dart' show | |
8 $a; | |
9 | |
10 import 'token.dart' show | |
11 AS_INFO, | |
12 IS_INFO, | |
13 KEYWORD_INFO, | |
14 PrecedenceInfo; | |
15 | |
16 /** | |
17 * A keyword in the Dart programming language. | |
18 */ | |
19 class Keyword { | |
20 static const List<Keyword> values = const <Keyword> [ | |
21 const Keyword("assert"), | |
22 const Keyword("break"), | |
23 const Keyword("case"), | |
24 const Keyword("catch"), | |
25 const Keyword("class"), | |
26 const Keyword("const"), | |
27 const Keyword("continue"), | |
28 const Keyword("default"), | |
29 const Keyword("do"), | |
30 const Keyword("else"), | |
31 const Keyword("enum"), | |
32 const Keyword("extends"), | |
33 const Keyword("false"), | |
34 const Keyword("final"), | |
35 const Keyword("finally"), | |
36 const Keyword("for"), | |
37 const Keyword("if"), | |
38 const Keyword("in"), | |
39 const Keyword("new"), | |
40 const Keyword("null"), | |
41 const Keyword("rethrow"), | |
42 const Keyword("return"), | |
43 const Keyword("super"), | |
44 const Keyword("switch"), | |
45 const Keyword("this"), | |
46 const Keyword("throw"), | |
47 const Keyword("true"), | |
48 const Keyword("try"), | |
49 const Keyword("var"), | |
50 const Keyword("void"), | |
51 const Keyword("while"), | |
52 const Keyword("with"), | |
53 | |
54 // TODO(ahe): Don't think this is a reserved word. | |
55 // See: http://dartbug.com/5579 | |
56 const Keyword("is", info: IS_INFO), | |
57 | |
58 const Keyword("abstract", isBuiltIn: true), | |
59 const Keyword("as", info: AS_INFO, isBuiltIn: true), | |
60 const Keyword("dynamic", isBuiltIn: true), | |
61 const Keyword("export", isBuiltIn: true), | |
62 const Keyword("external", isBuiltIn: true), | |
63 const Keyword("factory", isBuiltIn: true), | |
64 const Keyword("get", isBuiltIn: true), | |
65 const Keyword("implements", isBuiltIn: true), | |
66 const Keyword("import", isBuiltIn: true), | |
67 const Keyword("library", isBuiltIn: true), | |
68 const Keyword("operator", isBuiltIn: true), | |
69 const Keyword("part", isBuiltIn: true), | |
70 const Keyword("set", isBuiltIn: true), | |
71 const Keyword("static", isBuiltIn: true), | |
72 const Keyword("typedef", isBuiltIn: true), | |
73 | |
74 const Keyword("hide", isPseudo: true), | |
75 const Keyword("native", isPseudo: true), | |
76 const Keyword("of", isPseudo: true), | |
77 const Keyword("on", isPseudo: true), | |
78 const Keyword("show", isPseudo: true), | |
79 const Keyword("source", isPseudo: true), | |
80 const Keyword("deferred", isPseudo: true), | |
81 const Keyword("async", isPseudo: true), | |
82 const Keyword("sync", isPseudo: true), | |
83 const Keyword("await", isPseudo: true), | |
84 const Keyword("yield", isPseudo: true)]; | |
85 | |
86 final String syntax; | |
87 final bool isPseudo; | |
88 final bool isBuiltIn; | |
89 final PrecedenceInfo info; | |
90 | |
91 static Map<String, Keyword> _keywords; | |
92 static Map<String, Keyword> get keywords { | |
93 if (_keywords == null) { | |
94 _keywords = computeKeywordMap(); | |
95 } | |
96 return _keywords; | |
97 } | |
98 | |
99 const Keyword(this.syntax, | |
100 {this.isPseudo: false, | |
101 this.isBuiltIn: false, | |
102 this.info: KEYWORD_INFO}); | |
103 | |
104 static Map<String, Keyword> computeKeywordMap() { | |
105 Map<String, Keyword> result = new Map<String, Keyword>(); | |
106 for (Keyword keyword in values) { | |
107 result[keyword.syntax] = keyword; | |
108 } | |
109 return result; | |
110 } | |
111 | |
112 String toString() => syntax; | |
113 } | |
114 | |
115 /** | |
116 * Abstract state in a state machine for scanning keywords. | |
117 */ | |
118 abstract class KeywordState { | |
119 KeywordState(this.keyword); | |
120 | |
121 KeywordState next(int c); | |
122 final Keyword keyword; | |
123 | |
124 static KeywordState _KEYWORD_STATE; | |
125 static KeywordState get KEYWORD_STATE { | |
126 if (_KEYWORD_STATE == null) { | |
127 List<String> strings = | |
128 new List<String>(Keyword.values.length); | |
129 for (int i = 0; i < Keyword.values.length; i++) { | |
130 strings[i] = Keyword.values[i].syntax; | |
131 } | |
132 strings.sort((a,b) => a.compareTo(b)); | |
133 _KEYWORD_STATE = computeKeywordStateTable(0, strings, 0, strings.length); | |
134 } | |
135 return _KEYWORD_STATE; | |
136 } | |
137 | |
138 static KeywordState computeKeywordStateTable(int start, List<String> strings, | |
139 int offset, int length) { | |
140 List<KeywordState> result = new List<KeywordState>(26); | |
141 assert(length != 0); | |
142 int chunk = 0; | |
143 int chunkStart = -1; | |
144 bool isLeaf = false; | |
145 for (int i = offset; i < offset + length; i++) { | |
146 if (strings[i].length == start) { | |
147 isLeaf = true; | |
148 } | |
149 if (strings[i].length > start) { | |
150 int c = strings[i].codeUnitAt(start); | |
151 if (chunk != c) { | |
152 if (chunkStart != -1) { | |
153 assert(result[chunk - $a] == null); | |
154 result[chunk - $a] = computeKeywordStateTable(start + 1, strings, | |
155 chunkStart, | |
156 i - chunkStart); | |
157 } | |
158 chunkStart = i; | |
159 chunk = c; | |
160 } | |
161 } | |
162 } | |
163 if (chunkStart != -1) { | |
164 assert(result[chunk - $a] == null); | |
165 result[chunk - $a] = | |
166 computeKeywordStateTable(start + 1, strings, chunkStart, | |
167 offset + length - chunkStart); | |
168 } else { | |
169 assert(length == 1); | |
170 return new LeafKeywordState(strings[offset]); | |
171 } | |
172 if (isLeaf) { | |
173 return new ArrayKeywordState(result, strings[offset]); | |
174 } else { | |
175 return new ArrayKeywordState(result, null); | |
176 } | |
177 } | |
178 } | |
179 | |
180 /** | |
181 * A state with multiple outgoing transitions. | |
182 */ | |
183 class ArrayKeywordState extends KeywordState { | |
184 final List<KeywordState> table; | |
185 | |
186 ArrayKeywordState(List<KeywordState> this.table, String syntax) | |
187 : super((syntax == null) ? null : Keyword.keywords[syntax]); | |
188 | |
189 KeywordState next(int c) => table[c - $a]; | |
190 | |
191 String toString() { | |
192 StringBuffer sb = new StringBuffer(); | |
193 sb.write("["); | |
194 if (keyword != null) { | |
195 sb.write("*"); | |
196 sb.write(keyword); | |
197 sb.write(" "); | |
198 } | |
199 List<KeywordState> foo = table; | |
200 for (int i = 0; i < foo.length; i++) { | |
201 if (foo[i] != null) { | |
202 sb.write("${new String.fromCharCodes([i + $a])}: ${foo[i]}; "); | |
203 } | |
204 } | |
205 sb.write("]"); | |
206 return sb.toString(); | |
207 } | |
208 } | |
209 | |
210 /** | |
211 * A state that has no outgoing transitions. | |
212 */ | |
213 class LeafKeywordState extends KeywordState { | |
214 LeafKeywordState(String syntax) : super(Keyword.keywords[syntax]); | |
215 | |
216 KeywordState next(int c) => null; | |
217 | |
218 String toString() => keyword.syntax; | |
219 } | |
OLD | NEW |