| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // BSD-style license that can be found in the LICENSE file. | |
| 4 | |
| 5 part of dart._internal; | |
| 6 | |
| 7 /** | |
| 8 * Implementation of [core.Symbol]. This class uses the same name as | |
| 9 * a core class so a user can't tell the difference. | |
| 10 * | |
| 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 | |
| 13 * [getName]. | |
| 14 */ | |
| 15 class Symbol implements core.Symbol { | |
| 16 final String _name; | |
| 17 | |
| 18 /** | |
| 19 * Source of RegExp matching Dart reserved words. | |
| 20 * | |
| 21 * Reserved words are not allowed as identifiers. | |
| 22 */ | |
| 23 static const String reservedWordRE = | |
| 24 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)|' | |
| 26 r'ret(?:hrow|urn)|s(?:uper|witch)|t(?:h(?:is|row)|r(?:ue|y))|' | |
| 27 r'v(?:ar|oid)|w(?:hile|ith))'; | |
| 28 /** | |
| 29 * Source of RegExp matching any public identifier. | |
| 30 * | |
| 31 * A public identifier is a valid identifier (not a reserved word) | |
| 32 * that doesn't start with '_'. | |
| 33 */ | |
| 34 static const String publicIdentifierRE = | |
| 35 r'(?!' '$reservedWordRE' r'\b(?!\$))[a-zA-Z$][\w$]*'; | |
| 36 /** | |
| 37 * Source of RegExp matching any identifier. | |
| 38 * | |
| 39 * It matches identifiers but not reserved words. The identifiers | |
| 40 * may start with '_'. | |
| 41 */ | |
| 42 static const String identifierRE = | |
| 43 r'(?!' '$reservedWordRE' r'\b(?!\$))[a-zA-Z$_][\w$]*'; | |
| 44 /** | |
| 45 * Source of RegExp matching a declarable operator names. | |
| 46 * | |
| 47 * The operators that can be declared using `operator` declarations are | |
| 48 * also the only ones allowed as symbols. The name of the oeprators is | |
| 49 * the same as the operator itself except for unary minus, where the name | |
| 50 * is "unary-". | |
| 51 */ | |
| 52 static const String operatorRE = | |
| 53 r'(?:[\-+*/%&|^]|\[\]=?|==|~/?|<[<=]?|>[>=]?|unary-)'; | |
| 54 | |
| 55 // Grammar if symbols: | |
| 56 // symbol ::= qualifiedName | <empty> | |
| 57 // qualifiedName ::= publicIdentifier '.' qualifiedName | name | |
| 58 // name ::= publicIdentifier | |
| 59 // | publicIdentifier '=' | |
| 60 // | operator | |
| 61 // where publicIdentifier is any valid identifier (not a reserved word) | |
| 62 // that isn't private (doesn't start with '_'). | |
| 63 // | |
| 64 // Railroad diagram of the accepted grammar: | |
| 65 // | |
| 66 // /----------------\ | |
| 67 // | | | |
| 68 // | /-[.]-/ /-[=]-\ | |
| 69 // \ / / \ | |
| 70 // -------[id]-------------------------> | |
| 71 // \ / | |
| 72 // \------[operator]---/ | |
| 73 // \ / | |
| 74 // \------------/ | |
| 75 // | |
| 76 | |
| 77 /** | |
| 78 * RegExp that validates a non-empty non-private symbol. | |
| 79 * | |
| 80 * The empty symbol is handled before this regexp is used, and is not | |
| 81 * accepted. | |
| 82 */ | |
| 83 static final RegExp publicSymbolPattern = new RegExp( | |
| 84 '^(?:$operatorRE\$|$publicIdentifierRE(?:=?\$|[.](?!\$)))+?\$'); | |
| 85 | |
| 86 // The grammar of symbols that may be private is the same as for public | |
| 87 // symbols, except that "publicIdentifier" is replaced by "identifier", | |
| 88 // which matches any identifier. | |
| 89 | |
| 90 /** | |
| 91 * RegExp that validates a non-empty symbol. | |
| 92 * | |
| 93 * Private symbols are accepted. | |
| 94 * | |
| 95 * The empty symbol is handled before this regexp is used, and is not | |
| 96 * accepted. | |
| 97 */ | |
| 98 static final RegExp symbolPattern = new RegExp( | |
| 99 '^(?:$operatorRE\$|$identifierRE(?:=?\$|[.](?!\$)))+?\$'); | |
| 100 | |
| 101 const Symbol(String name) | |
| 102 : this._name = name; | |
| 103 | |
| 104 /** | |
| 105 * Platform-private method used by the mirror system to create | |
| 106 * otherwise invalid names. | |
| 107 */ | |
| 108 const Symbol.unvalidated(this._name); | |
| 109 | |
| 110 // This is called by dart2js. | |
| 111 Symbol.validated(String name) | |
| 112 : this._name = validatePublicSymbol(name); | |
| 113 | |
| 114 bool operator ==(Object other) => other is Symbol && _name == other._name; | |
| 115 | |
| 116 int get hashCode { | |
| 117 const arbitraryPrime = 664597; | |
| 118 return 0x1fffffff & (arbitraryPrime * _name.hashCode); | |
| 119 } | |
| 120 | |
| 121 toString() => 'Symbol("$_name")'; | |
| 122 | |
| 123 /// Platform-private accessor which cannot be called from user libraries. | |
| 124 static String getName(Symbol symbol) => symbol._name; | |
| 125 | |
| 126 static String validatePublicSymbol(String name) { | |
| 127 if (name.isEmpty || publicSymbolPattern.hasMatch(name)) return name; | |
| 128 if (name.startsWith('_')) { | |
| 129 // There may be other private parts in a qualified name than the first | |
| 130 // one, but this is a common case that deserves a specific error | |
| 131 // message. | |
| 132 throw new ArgumentError('"$name" is a private identifier'); | |
| 133 } | |
| 134 throw new ArgumentError( | |
| 135 '"$name" is not a valid (qualified) symbol name'); | |
| 136 } | |
| 137 | |
| 138 /** | |
| 139 * Checks whether name is a valid symbol name. | |
| 140 * | |
| 141 * This test allows both private and non-private symbols. | |
| 142 */ | |
| 143 static bool isValidSymbol(String name) { | |
| 144 return (name.isEmpty || symbolPattern.hasMatch(name)); | |
| 145 } | |
| 146 } | |
| OLD | NEW |