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