| 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 // Reserved words are not allowed as identifiers. | 18 /** |
| 19 * Source of RegExp matching Dart reserved words. |
| 20 * |
| 21 * Reserved words are not allowed as identifiers. |
| 22 */ |
| 19 static const String reservedWordRE = | 23 static const String reservedWordRE = |
| 20 r'(?:assert|break|c(?:a(?:se|tch)|lass|on(?:st|tinue))|d(?:efault|o)|' | 24 r'(?:assert|break|c(?:a(?:se|tch)|lass|on(?:st|tinue))|d(?:efault|o)|' |
| 21 r'e(?:lse|num|xtends)|f(?:alse|inal(?:ly)?|or)|i[fns]|n(?:ew|ull)|' | 25 r'e(?:lse|num|xtends)|f(?:alse|inal(?:ly)?|or)|i[fns]|n(?:ew|ull)|' |
| 22 r'ret(?:hrow|urn)|s(?:uper|witch)|t(?:h(?:is|row)|r(?:ue|y))|' | 26 r'ret(?:hrow|urn)|s(?:uper|witch)|t(?:h(?:is|row)|r(?:ue|y))|' |
| 23 r'v(?:ar|oid)|w(?:hile|ith))'; | 27 r'v(?:ar|oid)|w(?:hile|ith))'; |
| 24 // Mathces a public identifier (identifier not starting with '_'). | 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 */ |
| 25 static const String publicIdentifierRE = | 34 static const String publicIdentifierRE = |
| 26 r'(?!' '$reservedWordRE' r'\b(?!\$))[a-zA-Z$][\w$]*'; | 35 r'(?!' '$reservedWordRE' r'\b(?!\$))[a-zA-Z$][\w$]*'; |
| 27 // Matches the names of declarable operators. | 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 */ |
| 28 static const String operatorRE = | 52 static const String operatorRE = |
| 29 r'(?:[\-+*/%&|^]|\[\]=?|==|~/?|<[<=]?|>[>=]?|unary-)'; | 53 r'(?:[\-+*/%&|^]|\[\]=?|==|~/?|<[<=]?|>[>=]?|unary-)'; |
| 30 | 54 |
| 31 // Grammar: | 55 // Grammar if symbols: |
| 32 // symbol ::= qualifiedName | <empty> | 56 // symbol ::= qualifiedName | <empty> |
| 33 // qualifiedName ::= publicIdentifier '.' qualifiedName | name | 57 // qualifiedName ::= publicIdentifier '.' qualifiedName | name |
| 34 // name ::= publicIdentifier | 58 // name ::= publicIdentifier |
| 35 // | publicIdentifier '=' | 59 // | publicIdentifier '=' |
| 36 // | operator | 60 // | operator |
| 37 // where publicIdentifier is any valid identifier (not a reserved word) | 61 // where publicIdentifier is any valid identifier (not a reserved word) |
| 38 // that isn't private (doesn't start with '_'). | 62 // that isn't private (doesn't start with '_'). |
| 39 // | 63 // |
| 40 // Railroad diagram of the accepted grammar: | 64 // Railroad diagram of the accepted grammar: |
| 41 // | 65 // |
| 42 // /----------------\ | 66 // /----------------\ |
| 43 // | | | 67 // | | |
| 44 // | /-[.]-/ /-[=]-\ | 68 // | /-[.]-/ /-[=]-\ |
| 45 // \ / / \ | 69 // \ / / \ |
| 46 // -------[id]-------------------------> | 70 // -------[id]-------------------------> |
| 47 // \ / | 71 // \ / |
| 48 // \------[operator]---/ | 72 // \------[operator]---/ |
| 49 // \ / | 73 // \ / |
| 50 // \------------/ | 74 // \------------/ |
| 51 // | 75 // |
| 52 | 76 |
| 53 // Validates non-empty symbol (empty symbol is handled before using this). | 77 /** |
| 54 static final RegExp validationPattern = new RegExp( | 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( |
| 55 '^(?:$operatorRE\$|$publicIdentifierRE(?:=?\$|[.](?!\$)))+?\$'); | 84 '^(?:$operatorRE\$|$publicIdentifierRE(?:=?\$|[.](?!\$)))+?\$'); |
| 56 | 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 symbolPatternPattern = new RegExp( |
| 99 '^(?:$operatorRE\$|$identifierRE(?:=?\$|[.](?!\$)))+?\$'); |
| 100 |
| 57 external const Symbol(String name); | 101 external const Symbol(String name); |
| 58 | 102 |
| 59 /** | 103 /** |
| 60 * Platform-private method used by the mirror system to create | 104 * Platform-private method used by the mirror system to create |
| 61 * otherwise invalid names. | 105 * otherwise invalid names. |
| 62 */ | 106 */ |
| 63 const Symbol.unvalidated(this._name); | 107 const Symbol.unvalidated(this._name); |
| 64 | 108 |
| 65 // This is called by dart2js. | 109 // This is called by dart2js. |
| 66 Symbol.validated(String name) | 110 Symbol.validated(String name) |
| 67 : this._name = validate(name); | 111 : this._name = validate(name); |
| 68 | 112 |
| 69 bool operator ==(other) => other is Symbol && _name == other._name; | 113 bool operator ==(other) => other is Symbol && _name == other._name; |
| 70 | 114 |
| 71 int get hashCode { | 115 int get hashCode { |
| 72 const arbitraryPrime = 664597; | 116 const arbitraryPrime = 664597; |
| 73 return 0x1fffffff & (arbitraryPrime * _name.hashCode); | 117 return 0x1fffffff & (arbitraryPrime * _name.hashCode); |
| 74 } | 118 } |
| 75 | 119 |
| 76 toString() => 'Symbol("$_name")'; | 120 toString() => 'Symbol("$_name")'; |
| 77 | 121 |
| 78 /// Platform-private accessor which cannot be called from user libraries. | 122 /// Platform-private accessor which cannot be called from user libraries. |
| 79 static String getName(Symbol symbol) => symbol._name; | 123 static String getName(Symbol symbol) => symbol._name; |
| 80 | 124 |
| 81 static String validate(String name) { | 125 static String validate(String name) { |
| 82 if (name.isEmpty || validationPattern.hasMatch(name)) return name; | 126 if (name.isEmpty || publicSymbolPattern.hasMatch(name)) return name; |
| 83 if (name.startsWith('_')) { | 127 if (name.startsWith('_')) { |
| 84 // There may be other private parts in a qualified name than the first | 128 // There may be other private parts in a qualified name than the first |
| 85 // one, but this is a common case that deserves a specific error | 129 // one, but this is a common case that deserves a specific error |
| 130 // message. |
| 131 throw new ArgumentError('"$name" is a private identifier'); |
| 132 } |
| 133 throw new ArgumentError( |
| 134 '"$name" is not a valid (qualified) symbol name'); |
| 135 } |
| 136 |
| 137 static String validatePrivate(String name) { |
| 138 if (name.isEmpty || symbolPattern.hasMatch(name)) return name; |
| 139 if (name.startsWith('_')) { |
| 140 // There may be other private parts in a qualified name than the first |
| 141 // one, but this is a common case that deserves a specific error |
| 86 // message. | 142 // message. |
| 87 throw new ArgumentError('"$name" is a private identifier'); | 143 throw new ArgumentError('"$name" is a private identifier'); |
| 88 } | 144 } |
| 89 throw new ArgumentError( | 145 throw new ArgumentError( |
| 90 '"$name" is not a valid (qualified) symbol name'); | 146 '"$name" is not a valid (qualified) symbol name'); |
| 91 } | 147 } |
| 92 } | 148 } |
| OLD | NEW |