OLD | NEW |
1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 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 | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 library dart2js.tokens.keywords; | 5 library dart2js.tokens.keywords; |
6 | 6 |
7 import '../util/characters.dart' show | 7 import '../util/characters.dart' as Characters show |
8 $a; | 8 $a; |
9 | 9 |
10 import 'token.dart' show | 10 import 'precedence.dart' show |
| 11 PrecedenceInfo; |
| 12 import 'precedence_constants.dart' as Precedence show |
11 AS_INFO, | 13 AS_INFO, |
12 IS_INFO, | 14 IS_INFO, |
13 KEYWORD_INFO, | 15 KEYWORD_INFO; |
14 PrecedenceInfo; | |
15 | 16 |
16 /** | 17 /** |
17 * A keyword in the Dart programming language. | 18 * A keyword in the Dart programming language. |
18 */ | 19 */ |
19 class Keyword { | 20 class Keyword { |
20 static const List<Keyword> values = const <Keyword> [ | 21 static const List<Keyword> values = const <Keyword> [ |
21 const Keyword("assert"), | 22 const Keyword("assert"), |
22 const Keyword("break"), | 23 const Keyword("break"), |
23 const Keyword("case"), | 24 const Keyword("case"), |
24 const Keyword("catch"), | 25 const Keyword("catch"), |
(...skipping 21 matching lines...) Expand all Loading... |
46 const Keyword("throw"), | 47 const Keyword("throw"), |
47 const Keyword("true"), | 48 const Keyword("true"), |
48 const Keyword("try"), | 49 const Keyword("try"), |
49 const Keyword("var"), | 50 const Keyword("var"), |
50 const Keyword("void"), | 51 const Keyword("void"), |
51 const Keyword("while"), | 52 const Keyword("while"), |
52 const Keyword("with"), | 53 const Keyword("with"), |
53 | 54 |
54 // TODO(ahe): Don't think this is a reserved word. | 55 // TODO(ahe): Don't think this is a reserved word. |
55 // See: http://dartbug.com/5579 | 56 // See: http://dartbug.com/5579 |
56 const Keyword("is", info: IS_INFO), | 57 const Keyword("is", info: Precedence.IS_INFO), |
57 | 58 |
58 const Keyword("abstract", isBuiltIn: true), | 59 const Keyword("abstract", isBuiltIn: true), |
59 const Keyword("as", info: AS_INFO, isBuiltIn: true), | 60 const Keyword("as", info: Precedence.AS_INFO, isBuiltIn: true), |
60 const Keyword("dynamic", isBuiltIn: true), | 61 const Keyword("dynamic", isBuiltIn: true), |
61 const Keyword("export", isBuiltIn: true), | 62 const Keyword("export", isBuiltIn: true), |
62 const Keyword("external", isBuiltIn: true), | 63 const Keyword("external", isBuiltIn: true), |
63 const Keyword("factory", isBuiltIn: true), | 64 const Keyword("factory", isBuiltIn: true), |
64 const Keyword("get", isBuiltIn: true), | 65 const Keyword("get", isBuiltIn: true), |
65 const Keyword("implements", isBuiltIn: true), | 66 const Keyword("implements", isBuiltIn: true), |
66 const Keyword("import", isBuiltIn: true), | 67 const Keyword("import", isBuiltIn: true), |
67 const Keyword("library", isBuiltIn: true), | 68 const Keyword("library", isBuiltIn: true), |
68 const Keyword("operator", isBuiltIn: true), | 69 const Keyword("operator", isBuiltIn: true), |
69 const Keyword("part", isBuiltIn: true), | 70 const Keyword("part", isBuiltIn: true), |
(...skipping 22 matching lines...) Expand all Loading... |
92 static Map<String, Keyword> get keywords { | 93 static Map<String, Keyword> get keywords { |
93 if (_keywords == null) { | 94 if (_keywords == null) { |
94 _keywords = computeKeywordMap(); | 95 _keywords = computeKeywordMap(); |
95 } | 96 } |
96 return _keywords; | 97 return _keywords; |
97 } | 98 } |
98 | 99 |
99 const Keyword(this.syntax, | 100 const Keyword(this.syntax, |
100 {this.isPseudo: false, | 101 {this.isPseudo: false, |
101 this.isBuiltIn: false, | 102 this.isBuiltIn: false, |
102 this.info: KEYWORD_INFO}); | 103 this.info: Precedence.KEYWORD_INFO}); |
103 | 104 |
104 static Map<String, Keyword> computeKeywordMap() { | 105 static Map<String, Keyword> computeKeywordMap() { |
105 Map<String, Keyword> result = new Map<String, Keyword>(); | 106 Map<String, Keyword> result = new Map<String, Keyword>(); |
106 for (Keyword keyword in values) { | 107 for (Keyword keyword in values) { |
107 result[keyword.syntax] = keyword; | 108 result[keyword.syntax] = keyword; |
108 } | 109 } |
109 return result; | 110 return result; |
110 } | 111 } |
111 | 112 |
112 String toString() => syntax; | 113 String toString() => syntax; |
(...skipping 30 matching lines...) Expand all Loading... |
143 int chunkStart = -1; | 144 int chunkStart = -1; |
144 bool isLeaf = false; | 145 bool isLeaf = false; |
145 for (int i = offset; i < offset + length; i++) { | 146 for (int i = offset; i < offset + length; i++) { |
146 if (strings[i].length == start) { | 147 if (strings[i].length == start) { |
147 isLeaf = true; | 148 isLeaf = true; |
148 } | 149 } |
149 if (strings[i].length > start) { | 150 if (strings[i].length > start) { |
150 int c = strings[i].codeUnitAt(start); | 151 int c = strings[i].codeUnitAt(start); |
151 if (chunk != c) { | 152 if (chunk != c) { |
152 if (chunkStart != -1) { | 153 if (chunkStart != -1) { |
153 assert(result[chunk - $a] == null); | 154 assert(result[chunk - Characters.$a] == null); |
154 result[chunk - $a] = computeKeywordStateTable(start + 1, strings, | 155 result[chunk - Characters.$a] = |
155 chunkStart, | 156 computeKeywordStateTable( |
156 i - chunkStart); | 157 start + 1, strings, chunkStart, i - chunkStart); |
157 } | 158 } |
158 chunkStart = i; | 159 chunkStart = i; |
159 chunk = c; | 160 chunk = c; |
160 } | 161 } |
161 } | 162 } |
162 } | 163 } |
163 if (chunkStart != -1) { | 164 if (chunkStart != -1) { |
164 assert(result[chunk - $a] == null); | 165 assert(result[chunk - Characters.$a] == null); |
165 result[chunk - $a] = | 166 result[chunk - Characters.$a] = |
166 computeKeywordStateTable(start + 1, strings, chunkStart, | 167 computeKeywordStateTable(start + 1, strings, chunkStart, |
167 offset + length - chunkStart); | 168 offset + length - chunkStart); |
168 } else { | 169 } else { |
169 assert(length == 1); | 170 assert(length == 1); |
170 return new LeafKeywordState(strings[offset]); | 171 return new LeafKeywordState(strings[offset]); |
171 } | 172 } |
172 if (isLeaf) { | 173 if (isLeaf) { |
173 return new ArrayKeywordState(result, strings[offset]); | 174 return new ArrayKeywordState(result, strings[offset]); |
174 } else { | 175 } else { |
175 return new ArrayKeywordState(result, null); | 176 return new ArrayKeywordState(result, null); |
176 } | 177 } |
177 } | 178 } |
178 } | 179 } |
179 | 180 |
180 /** | 181 /** |
181 * A state with multiple outgoing transitions. | 182 * A state with multiple outgoing transitions. |
182 */ | 183 */ |
183 class ArrayKeywordState extends KeywordState { | 184 class ArrayKeywordState extends KeywordState { |
184 final List<KeywordState> table; | 185 final List<KeywordState> table; |
185 | 186 |
186 ArrayKeywordState(List<KeywordState> this.table, String syntax) | 187 ArrayKeywordState(List<KeywordState> this.table, String syntax) |
187 : super((syntax == null) ? null : Keyword.keywords[syntax]); | 188 : super((syntax == null) ? null : Keyword.keywords[syntax]); |
188 | 189 |
189 KeywordState next(int c) => table[c - $a]; | 190 KeywordState next(int c) => table[c - Characters.$a]; |
190 | 191 |
191 String toString() { | 192 String toString() { |
192 StringBuffer sb = new StringBuffer(); | 193 StringBuffer sb = new StringBuffer(); |
193 sb.write("["); | 194 sb.write("["); |
194 if (keyword != null) { | 195 if (keyword != null) { |
195 sb.write("*"); | 196 sb.write("*"); |
196 sb.write(keyword); | 197 sb.write(keyword); |
197 sb.write(" "); | 198 sb.write(" "); |
198 } | 199 } |
199 List<KeywordState> foo = table; | 200 List<KeywordState> foo = table; |
200 for (int i = 0; i < foo.length; i++) { | 201 for (int i = 0; i < foo.length; i++) { |
201 if (foo[i] != null) { | 202 if (foo[i] != null) { |
202 sb.write("${new String.fromCharCodes([i + $a])}: ${foo[i]}; "); | 203 sb.write("${new String.fromCharCodes([i + Characters.$a])}: " |
| 204 "${foo[i]}; "); |
203 } | 205 } |
204 } | 206 } |
205 sb.write("]"); | 207 sb.write("]"); |
206 return sb.toString(); | 208 return sb.toString(); |
207 } | 209 } |
208 } | 210 } |
209 | 211 |
210 /** | 212 /** |
211 * A state that has no outgoing transitions. | 213 * A state that has no outgoing transitions. |
212 */ | 214 */ |
213 class LeafKeywordState extends KeywordState { | 215 class LeafKeywordState extends KeywordState { |
214 LeafKeywordState(String syntax) : super(Keyword.keywords[syntax]); | 216 LeafKeywordState(String syntax) : super(Keyword.keywords[syntax]); |
215 | 217 |
216 KeywordState next(int c) => null; | 218 KeywordState next(int c) => null; |
217 | 219 |
218 String toString() => keyword.syntax; | 220 String toString() => keyword.syntax; |
219 } | 221 } |
OLD | NEW |