| 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 | |
| 72 final String syntax; | |
| 73 final bool isPseudo; | |
| 74 final bool isBuiltIn; | |
| 75 final PrecedenceInfo info; | |
| 76 | |
| 77 static Map<String, Keyword> _keywords; | |
| 78 static Map<String, Keyword> get keywords { | |
| 79 if (_keywords == null) { | |
| 80 _keywords = computeKeywordMap(); | |
| 81 } | |
| 82 return _keywords; | |
| 83 } | |
| 84 | |
| 85 const Keyword(this.syntax, | |
| 86 {this.isPseudo: false, | |
| 87 this.isBuiltIn: false, | |
| 88 this.info: KEYWORD_INFO}); | |
| 89 | |
| 90 static Map<String, Keyword> computeKeywordMap() { | |
| 91 Map<String, Keyword> result = new Map<String, Keyword>(); | |
| 92 for (Keyword keyword in values) { | |
| 93 result[keyword.syntax] = keyword; | |
| 94 } | |
| 95 return result; | |
| 96 } | |
| 97 | |
| 98 String toString() => syntax; | |
| 99 } | |
| 100 | |
| 101 /** | |
| 102 * Abstract state in a state machine for scanning keywords. | |
| 103 */ | |
| 104 abstract class KeywordState { | |
| 105 KeywordState(this.keyword); | |
| 106 | |
| 107 KeywordState next(int c); | |
| 108 final Keyword keyword; | |
| 109 | |
| 110 static KeywordState _KEYWORD_STATE; | |
| 111 static KeywordState get KEYWORD_STATE { | |
| 112 if (_KEYWORD_STATE == null) { | |
| 113 List<String> strings = | |
| 114 new List<String>(Keyword.values.length); | |
| 115 for (int i = 0; i < Keyword.values.length; i++) { | |
| 116 strings[i] = Keyword.values[i].syntax; | |
| 117 } | |
| 118 strings.sort((a,b) => a.compareTo(b)); | |
| 119 _KEYWORD_STATE = computeKeywordStateTable(0, strings, 0, strings.length); | |
| 120 } | |
| 121 return _KEYWORD_STATE; | |
| 122 } | |
| 123 | |
| 124 static KeywordState computeKeywordStateTable(int start, List<String> strings, | |
| 125 int offset, int length) { | |
| 126 List<KeywordState> result = new List<KeywordState>(26); | |
| 127 assert(length != 0); | |
| 128 int chunk = 0; | |
| 129 int chunkStart = -1; | |
| 130 bool isLeaf = false; | |
| 131 for (int i = offset; i < offset + length; i++) { | |
| 132 if (strings[i].length == start) { | |
| 133 isLeaf = true; | |
| 134 } | |
| 135 if (strings[i].length > start) { | |
| 136 int c = strings[i].codeUnitAt(start); | |
| 137 if (chunk != c) { | |
| 138 if (chunkStart != -1) { | |
| 139 assert(result[chunk - $a] == null); | |
| 140 result[chunk - $a] = computeKeywordStateTable(start + 1, strings, | |
| 141 chunkStart, | |
| 142 i - chunkStart); | |
| 143 } | |
| 144 chunkStart = i; | |
| 145 chunk = c; | |
| 146 } | |
| 147 } | |
| 148 } | |
| 149 if (chunkStart != -1) { | |
| 150 assert(result[chunk - $a] == null); | |
| 151 result[chunk - $a] = | |
| 152 computeKeywordStateTable(start + 1, strings, chunkStart, | |
| 153 offset + length - chunkStart); | |
| 154 } else { | |
| 155 assert(length == 1); | |
| 156 return new LeafKeywordState(strings[offset]); | |
| 157 } | |
| 158 if (isLeaf) { | |
| 159 return new ArrayKeywordState(result, strings[offset]); | |
| 160 } else { | |
| 161 return new ArrayKeywordState(result, null); | |
| 162 } | |
| 163 } | |
| 164 } | |
| 165 | |
| 166 /** | |
| 167 * A state with multiple outgoing transitions. | |
| 168 */ | |
| 169 class ArrayKeywordState extends KeywordState { | |
| 170 final List<KeywordState> table; | |
| 171 | |
| 172 ArrayKeywordState(List<KeywordState> this.table, String syntax) | |
| 173 : super((syntax == null) ? null : Keyword.keywords[syntax]); | |
| 174 | |
| 175 KeywordState next(int c) => table[c - $a]; | |
| 176 | |
| 177 String toString() { | |
| 178 StringBuffer sb = new StringBuffer(); | |
| 179 sb.write("["); | |
| 180 if (keyword != null) { | |
| 181 sb.write("*"); | |
| 182 sb.write(keyword); | |
| 183 sb.write(" "); | |
| 184 } | |
| 185 List<KeywordState> foo = table; | |
| 186 for (int i = 0; i < foo.length; i++) { | |
| 187 if (foo[i] != null) { | |
| 188 sb.write("${new String.fromCharCodes([i + $a])}: ${foo[i]}; "); | |
| 189 } | |
| 190 } | |
| 191 sb.write("]"); | |
| 192 return sb.toString(); | |
| 193 } | |
| 194 } | |
| 195 | |
| 196 /** | |
| 197 * A state that has no outgoing transitions. | |
| 198 */ | |
| 199 class LeafKeywordState extends KeywordState { | |
| 200 LeafKeywordState(String syntax) : super(Keyword.keywords[syntax]); | |
| 201 | |
| 202 KeywordState next(int c) => null; | |
| 203 | |
| 204 String toString() => keyword.syntax; | |
| 205 } | |
| OLD | NEW |