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