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