OLD | NEW |
---|---|
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, 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 part of dart._internal; | 5 part of dart._internal; |
6 | 6 |
7 /** | 7 /** |
8 * Implementation of [core.Symbol]. This class uses the same name as | 8 * Implementation of [core.Symbol]. This class uses the same name as |
9 * a core class so a user can't tell the difference. | 9 * a core class so a user can't tell the difference. |
10 * | 10 * |
11 * The purpose of this class is to hide [_name] from user code, but | 11 * The purpose of this class is to hide [_name] from user code, but |
12 * make it accessible to Dart platform code via the static method | 12 * make it accessible to Dart platform code via the static method |
13 * [getName]. | 13 * [getName]. |
14 */ | 14 */ |
15 class Symbol implements core.Symbol { | 15 class Symbol implements core.Symbol { |
16 final String _name; | 16 final String _name; |
17 | 17 |
18 // Used internally by DDC to map ES6 symbols to Dart. | |
19 final dynamic _nativeSymbol; | |
20 | |
18 /** | 21 /** |
19 * Source of RegExp matching Dart reserved words. | 22 * Source of RegExp matching Dart reserved words. |
20 * | 23 * |
21 * Reserved words are not allowed as identifiers. | 24 * Reserved words are not allowed as identifiers. |
22 */ | 25 */ |
23 static const String reservedWordRE = | 26 static const String reservedWordRE = |
24 r'(?:assert|break|c(?:a(?:se|tch)|lass|on(?:st|tinue))|d(?:efault|o)|' | 27 r'(?:assert|break|c(?:a(?:se|tch)|lass|on(?:st|tinue))|d(?:efault|o)|' |
25 r'e(?:lse|num|xtends)|f(?:alse|inal(?:ly)?|or)|i[fns]|n(?:ew|ull)|' | 28 r'e(?:lse|num|xtends)|f(?:alse|inal(?:ly)?|or)|i[fns]|n(?:ew|ull)|' |
26 r'ret(?:hrow|urn)|s(?:uper|witch)|t(?:h(?:is|row)|r(?:ue|y))|' | 29 r'ret(?:hrow|urn)|s(?:uper|witch)|t(?:h(?:is|row)|r(?:ue|y))|' |
27 r'v(?:ar|oid)|w(?:hile|ith))'; | 30 r'v(?:ar|oid)|w(?:hile|ith))'; |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
92 * | 95 * |
93 * Private symbols are accepted. | 96 * Private symbols are accepted. |
94 * | 97 * |
95 * The empty symbol is handled before this regexp is used, and is not | 98 * The empty symbol is handled before this regexp is used, and is not |
96 * accepted. | 99 * accepted. |
97 */ | 100 */ |
98 static final RegExp symbolPattern = new RegExp( | 101 static final RegExp symbolPattern = new RegExp( |
99 '^(?:$operatorRE\$|$identifierRE(?:=?\$|[.](?!\$)))+?\$'); | 102 '^(?:$operatorRE\$|$identifierRE(?:=?\$|[.](?!\$)))+?\$'); |
100 | 103 |
101 external const Symbol(String name); | 104 external const Symbol(String name); |
105 | |
106 external const Symbol.es6(String name, nativeSymbol); | |
102 | 107 |
103 /** | 108 /** |
104 * Platform-private method used by the mirror system to create | 109 * Platform-private method used by the mirror system to create |
105 * otherwise invalid names. | 110 * otherwise invalid names. |
106 */ | 111 */ |
107 const Symbol.unvalidated(this._name); | 112 const Symbol.unvalidated(this._name) : this._nativeSymbol = null; |
108 | 113 |
109 // This is called by dart2js. | 114 // This is called by dart2js. |
110 Symbol.validated(String name) | 115 Symbol.validated(String name) |
111 : this._name = validatePublicSymbol(name); | 116 : this._name = validatePublicSymbol(name), this._nativeSymbol = null; |
112 | 117 |
113 bool operator ==(Object other) => other is Symbol && _name == other._name; | 118 bool operator ==(Object other) => other is Symbol && _name == other._name |
119 && _nativeSymbol == other._nativeSymbol; | |
Bob Nystrom
2016/11/29 21:28:13
Run dartfmt?
vsm
2016/11/29 21:41:05
Done.
| |
114 | 120 |
115 external int get hashCode; | 121 external int get hashCode; |
116 | 122 |
117 toString() => 'Symbol("$_name")'; | 123 toString() => 'Symbol("$_name")'; |
118 | 124 |
119 /// Platform-private accessor which cannot be called from user libraries. | 125 /// Platform-private accessor which cannot be called from user libraries. |
120 static String getName(Symbol symbol) => symbol._name; | 126 static String getName(Symbol symbol) => symbol._name; |
121 | 127 |
128 static dynamic getNativeSymbol(Symbol symbol) => symbol._nativeSymbol; | |
129 | |
122 static String validatePublicSymbol(String name) { | 130 static String validatePublicSymbol(String name) { |
123 if (name.isEmpty || publicSymbolPattern.hasMatch(name)) return name; | 131 if (name.isEmpty || publicSymbolPattern.hasMatch(name)) return name; |
124 if (name.startsWith('_')) { | 132 if (name.startsWith('_')) { |
125 // There may be other private parts in a qualified name than the first | 133 // There may be other private parts in a qualified name than the first |
126 // one, but this is a common case that deserves a specific error | 134 // one, but this is a common case that deserves a specific error |
127 // message. | 135 // message. |
128 throw new ArgumentError('"$name" is a private identifier'); | 136 throw new ArgumentError('"$name" is a private identifier'); |
129 } | 137 } |
130 throw new ArgumentError( | 138 throw new ArgumentError( |
131 '"$name" is not a valid (qualified) symbol name'); | 139 '"$name" is not a valid (qualified) symbol name'); |
132 } | 140 } |
133 | 141 |
134 /** | 142 /** |
135 * Checks whether name is a valid symbol name. | 143 * Checks whether name is a valid symbol name. |
136 * | 144 * |
137 * This test allows both private and non-private symbols. | 145 * This test allows both private and non-private symbols. |
138 */ | 146 */ |
139 static bool isValidSymbol(String name) { | 147 static bool isValidSymbol(String name) { |
140 return (name.isEmpty || symbolPattern.hasMatch(name)); | 148 return (name.isEmpty || symbolPattern.hasMatch(name)); |
141 } | 149 } |
142 } | 150 } |
OLD | NEW |