Chromium Code Reviews| 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 fasta.scanner.keywords; | 5 library fasta.scanner.keywords; |
| 6 | 6 |
| 7 import '../../scanner/token.dart' as analyzer; | |
| 8 | |
| 7 import 'characters.dart' show $a, $z, $A, $Z; | 9 import 'characters.dart' show $a, $z, $A, $Z; |
| 8 | 10 |
| 9 import 'precedence.dart' show PrecedenceInfo; | 11 import 'precedence.dart' show PrecedenceInfo; |
| 10 | 12 |
| 11 import 'precedence.dart' show AS_INFO, IS_INFO, KEYWORD_INFO; | 13 import 'precedence.dart' show AS_INFO, IS_INFO, KEYWORD_INFO; |
| 12 | 14 |
| 13 /** | 15 /** |
| 14 * A keyword in the Dart programming language. | 16 * A keyword in the Dart programming language. |
| 15 */ | 17 */ |
| 16 class Keyword { | 18 class Keyword implements analyzer.Keyword { |
| 19 static const ASSERT = const Keyword("assert"); | |
|
ahe
2017/03/14 14:28:34
Why do we need to declare these constants?
danrubel
2017/03/14 15:23:48
I want analyzer.Keyword consts to be identical to
| |
| 20 static const BREAK = const Keyword("break"); | |
| 21 static const CASE = const Keyword("case"); | |
| 22 static const CATCH = const Keyword("catch"); | |
| 23 static const CLASS = const Keyword("class"); | |
| 24 static const CONST = const Keyword("const"); | |
| 25 static const CONTINUE = const Keyword("continue"); | |
| 26 static const DEFAULT = const Keyword("default"); | |
| 27 static const DO = const Keyword("do"); | |
| 28 static const ELSE = const Keyword("else"); | |
| 29 static const ENUM = const Keyword("enum"); | |
| 30 static const EXTENDS = const Keyword("extends"); | |
| 31 static const FALSE = const Keyword("false"); | |
| 32 static const FINAL = const Keyword("final"); | |
| 33 static const FINALLY = const Keyword("finally"); | |
| 34 static const FOR = const Keyword("for"); | |
| 35 static const IF = const Keyword("if"); | |
| 36 static const IN = const Keyword("in"); | |
| 37 static const NEW = const Keyword("new"); | |
| 38 static const NULL = const Keyword("null"); | |
| 39 static const RETHROW = const Keyword("rethrow"); | |
| 40 static const RETURN = const Keyword("return"); | |
| 41 static const SUPER = const Keyword("super"); | |
| 42 static const SWITCH = const Keyword("switch"); | |
| 43 static const THIS = const Keyword("this"); | |
| 44 static const THROW = const Keyword("throw"); | |
| 45 static const TRUE = const Keyword("true"); | |
| 46 static const TRY = const Keyword("try"); | |
| 47 static const VAR = const Keyword("var"); | |
| 48 static const VOID = const Keyword("void"); | |
| 49 static const WHILE = const Keyword("while"); | |
| 50 static const WITH = const Keyword("with"); | |
| 51 | |
| 52 // TODO(ahe): Don't think this is a reserved word. | |
| 53 // See: http://dartbug.com/5579 | |
| 54 static const IS = const Keyword("is", info: IS_INFO); | |
| 55 | |
| 56 static const ABSTRACT = const Keyword("abstract", isBuiltIn: true); | |
| 57 static const AS = const Keyword("as", info: AS_INFO, isBuiltIn: true); | |
| 58 static const COVARIANT = const Keyword("covariant", isBuiltIn: true); | |
| 59 static const DYNAMIC = const Keyword("dynamic", isBuiltIn: true); | |
| 60 static const EXPORT = const Keyword("export", isBuiltIn: true); | |
| 61 static const EXTERNAL = const Keyword("external", isBuiltIn: true); | |
| 62 static const FACTORY = const Keyword("factory", isBuiltIn: true); | |
| 63 static const GET = const Keyword("get", isBuiltIn: true); | |
| 64 static const IMPLEMENTS = const Keyword("implements", isBuiltIn: true); | |
| 65 static const IMPORT = const Keyword("import", isBuiltIn: true); | |
| 66 static const LIBRARY = const Keyword("library", isBuiltIn: true); | |
| 67 static const OPERATOR = const Keyword("operator", isBuiltIn: true); | |
| 68 static const PART = const Keyword("part", isBuiltIn: true); | |
| 69 static const SET = const Keyword("set", isBuiltIn: true); | |
| 70 static const STATIC = const Keyword("static", isBuiltIn: true); | |
| 71 static const TYPEDEF = const Keyword("typedef", isBuiltIn: true); | |
| 72 | |
| 73 static const ASYNC = const Keyword("async", isPseudo: true); | |
| 74 static const AWAIT = const Keyword("await", isPseudo: true); | |
| 75 static const DEFERRED = const Keyword("deferred", isPseudo: true); | |
| 76 static const FUNCTION = const Keyword("Function", isPseudo: true); | |
| 77 static const HIDE = const Keyword("hide", isPseudo: true); | |
| 78 static const NATIVE = const Keyword("native", isPseudo: true); | |
| 79 static const OF = const Keyword("of", isPseudo: true); | |
| 80 static const ON = const Keyword("on", isPseudo: true); | |
| 81 static const PATCH = const Keyword("patch", isPseudo: true); | |
| 82 static const SHOW = const Keyword("show", isPseudo: true); | |
| 83 static const SOURCE = const Keyword("source", isPseudo: true); | |
| 84 static const SYNC = const Keyword("sync", isPseudo: true); | |
| 85 static const YIELD = const Keyword("yield", isPseudo: true); | |
| 86 | |
| 17 static const List<Keyword> values = const <Keyword>[ | 87 static const List<Keyword> values = const <Keyword>[ |
| 18 const Keyword("assert"), | 88 ASSERT, |
| 19 const Keyword("break"), | 89 BREAK, |
| 20 const Keyword("case"), | 90 CASE, |
| 21 const Keyword("catch"), | 91 CATCH, |
| 22 const Keyword("class"), | 92 CLASS, |
| 23 const Keyword("const"), | 93 CONST, |
| 24 const Keyword("continue"), | 94 CONTINUE, |
| 25 const Keyword("default"), | 95 DEFAULT, |
| 26 const Keyword("do"), | 96 DO, |
| 27 const Keyword("else"), | 97 ELSE, |
| 28 const Keyword("enum"), | 98 ENUM, |
| 29 const Keyword("extends"), | 99 EXTENDS, |
| 30 const Keyword("false"), | 100 FALSE, |
| 31 const Keyword("final"), | 101 FINAL, |
| 32 const Keyword("finally"), | 102 FINALLY, |
| 33 const Keyword("for"), | 103 FOR, |
| 34 const Keyword("if"), | 104 IF, |
| 35 const Keyword("in"), | 105 IN, |
| 36 const Keyword("new"), | 106 NEW, |
| 37 const Keyword("null"), | 107 NULL, |
| 38 const Keyword("rethrow"), | 108 RETHROW, |
| 39 const Keyword("return"), | 109 RETURN, |
| 40 const Keyword("super"), | 110 SUPER, |
| 41 const Keyword("switch"), | 111 SWITCH, |
| 42 const Keyword("this"), | 112 THIS, |
| 43 const Keyword("throw"), | 113 THROW, |
| 44 const Keyword("true"), | 114 TRUE, |
| 45 const Keyword("try"), | 115 TRY, |
| 46 const Keyword("var"), | 116 VAR, |
| 47 const Keyword("void"), | 117 VOID, |
| 48 const Keyword("while"), | 118 WHILE, |
| 49 const Keyword("with"), | 119 WITH, |
| 50 | 120 // ==== |
| 51 // TODO(ahe): Don't think this is a reserved word. | 121 IS, |
| 52 // See: http://dartbug.com/5579 | 122 // ==== Built In |
| 53 const Keyword("is", info: IS_INFO), | 123 ABSTRACT, |
| 54 | 124 AS, |
| 55 const Keyword("abstract", isBuiltIn: true), | 125 COVARIANT, |
| 56 const Keyword("as", info: AS_INFO, isBuiltIn: true), | 126 DYNAMIC, |
| 57 const Keyword("covariant", isBuiltIn: true), | 127 EXPORT, |
| 58 const Keyword("dynamic", isBuiltIn: true), | 128 EXTERNAL, |
| 59 const Keyword("export", isBuiltIn: true), | 129 FACTORY, |
| 60 const Keyword("external", isBuiltIn: true), | 130 GET, |
| 61 const Keyword("factory", isBuiltIn: true), | 131 IMPLEMENTS, |
| 62 const Keyword("get", isBuiltIn: true), | 132 IMPORT, |
| 63 const Keyword("implements", isBuiltIn: true), | 133 LIBRARY, |
| 64 const Keyword("import", isBuiltIn: true), | 134 OPERATOR, |
| 65 const Keyword("library", isBuiltIn: true), | 135 PART, |
| 66 const Keyword("operator", isBuiltIn: true), | 136 SET, |
| 67 const Keyword("part", isBuiltIn: true), | 137 STATIC, |
| 68 const Keyword("set", isBuiltIn: true), | 138 TYPEDEF, |
| 69 const Keyword("static", isBuiltIn: true), | 139 // ==== Pseudo |
| 70 const Keyword("typedef", isBuiltIn: true), | 140 ASYNC, |
| 71 | 141 AWAIT, |
| 72 const Keyword("async", isPseudo: true), | 142 DEFERRED, |
| 73 const Keyword("await", isPseudo: true), | 143 FUNCTION, |
| 74 const Keyword("deferred", isPseudo: true), | 144 HIDE, |
| 75 const Keyword("Function", isPseudo: true), | 145 NATIVE, |
| 76 const Keyword("hide", isPseudo: true), | 146 OF, |
| 77 const Keyword("native", isPseudo: true), | 147 ON, |
| 78 const Keyword("of", isPseudo: true), | 148 PATCH, |
| 79 const Keyword("on", isPseudo: true), | 149 SHOW, |
| 80 const Keyword("patch", isPseudo: true), | 150 SOURCE, |
| 81 const Keyword("show", isPseudo: true), | 151 SYNC, |
| 82 const Keyword("source", isPseudo: true), | 152 YIELD, |
| 83 const Keyword("sync", isPseudo: true), | |
| 84 const Keyword("yield", isPseudo: true), | |
| 85 ]; | 153 ]; |
| 86 | 154 |
| 87 final String syntax; | 155 final String syntax; |
| 88 final bool isPseudo; | 156 final bool isPseudo; |
| 89 final bool isBuiltIn; | 157 final bool isBuiltIn; |
| 90 final PrecedenceInfo info; | 158 final PrecedenceInfo info; |
| 91 | 159 |
| 92 static Map<String, Keyword> _keywords; | 160 static Map<String, Keyword> _keywords; |
| 93 static Map<String, Keyword> get keywords { | 161 static Map<String, Keyword> get keywords { |
| 94 if (_keywords == null) { | 162 if (_keywords == null) { |
| 95 _keywords = computeKeywordMap(); | 163 _keywords = computeKeywordMap(); |
| 96 } | 164 } |
| 97 return _keywords; | 165 return _keywords; |
| 98 } | 166 } |
| 99 | 167 |
| 100 const Keyword(this.syntax, | 168 const Keyword(this.syntax, |
| 101 {this.isPseudo: false, this.isBuiltIn: false, this.info: KEYWORD_INFO}); | 169 {this.isPseudo: false, this.isBuiltIn: false, this.info: KEYWORD_INFO}); |
| 102 | 170 |
| 103 static Map<String, Keyword> computeKeywordMap() { | 171 static Map<String, Keyword> computeKeywordMap() { |
| 104 Map<String, Keyword> result = new Map<String, Keyword>(); | 172 Map<String, Keyword> result = new Map<String, Keyword>(); |
| 105 for (Keyword keyword in values) { | 173 for (Keyword keyword in values) { |
| 106 result[keyword.syntax] = keyword; | 174 result[keyword.syntax] = keyword; |
| 107 } | 175 } |
| 108 return result; | 176 return result; |
| 109 } | 177 } |
| 110 | 178 |
| 111 String toString() => syntax; | 179 String toString() => syntax; |
| 180 | |
| 181 //=================== analyzer.Token compatibility ================== | |
| 182 | |
| 183 @override | |
| 184 bool get isPseudoKeyword { | |
| 185 // The term "pseudo-keyword" doesn't exist in the spec, and | |
| 186 // Analyzer and Fasta have different notions of what it means. | |
| 187 // Analyzer's notion of "pseudo-keyword" corresponds with Fasta's | |
| 188 // notion of "built-in keyword". | |
|
Paul Berry
2017/03/14 13:01:46
Let's expand this comment to mention https://githu
danrubel
2017/03/14 15:23:48
Good idea. Done.
| |
| 189 return isBuiltIn || this == DEFERRED; | |
| 190 } | |
| 191 | |
| 192 @override | |
| 193 String get name => syntax.toUpperCase(); | |
| 112 } | 194 } |
| 113 | 195 |
| 114 /** | 196 /** |
| 115 * Abstract state in a state machine for scanning keywords. | 197 * Abstract state in a state machine for scanning keywords. |
| 116 */ | 198 */ |
| 117 abstract class KeywordState { | 199 abstract class KeywordState { |
| 118 KeywordState next(int c); | 200 KeywordState next(int c); |
| 119 KeywordState nextCapital(int c); | 201 KeywordState nextCapital(int c); |
| 120 | 202 |
| 121 Keyword get keyword; | 203 Keyword get keyword; |
| (...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 242 class LeafKeywordState implements KeywordState { | 324 class LeafKeywordState implements KeywordState { |
| 243 final Keyword keyword; | 325 final Keyword keyword; |
| 244 | 326 |
| 245 LeafKeywordState(String syntax) : keyword = Keyword.keywords[syntax]; | 327 LeafKeywordState(String syntax) : keyword = Keyword.keywords[syntax]; |
| 246 | 328 |
| 247 KeywordState next(int c) => null; | 329 KeywordState next(int c) => null; |
| 248 KeywordState nextCapital(int c) => null; | 330 KeywordState nextCapital(int c) => null; |
| 249 | 331 |
| 250 String toString() => keyword.syntax; | 332 String toString() => keyword.syntax; |
| 251 } | 333 } |
| OLD | NEW |