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 static final RegExp validationPattern = | 18 // Reserved words are not allowed as identifiers. |
19 new RegExp(r'^(?:[a-zA-Z$][a-zA-Z$0-9_]*\.)*(?:[a-zA-Z$][a-zA-Z$0-9_]*=?|' | 19 static const String reservedWordRE = |
20 r'-|' | 20 r'(?:assert|break|c(?:a(?:se|tch)|lass|on(?:st|tinue))|d(?:efault|o)|' |
21 r'unary-|' | 21 r'e(?:lse|num|xtends)|f(?:alse|inal(?:ly)?|or)|i[fns]|n(?:ew|ull)|' |
22 r'\[\]=|' | 22 r'ret(?:hrow|urn)|s(?:uper|witch)|t(?:h(?:is|row)|r(?:ue|y))|' |
23 r'~|' | 23 r'v(?:ar|oid)|w(?:hile|ith))'; |
24 r'==|' | 24 // Mathces a public identifier (identifier not starting with '_'). |
25 r'\[\]|' | 25 static const String publicIdentifierRE = |
26 r'\*|' | 26 r'(?!' '$reservedWordRE' r'\b(?!\$))[a-zA-Z$][\w$]*'; |
27 r'/|' | 27 // Matches the names of declarable operators. |
28 r'%|' | 28 static const String operatorRE = |
29 r'~/|' | 29 r'(?:[\-+*/%&|^]|\[\]=?|==|~/?|<[<=]?|>[>=]?|unary-)'; |
30 r'\+|' | 30 |
31 r'<<|' | 31 |
ahe
2014/02/21 12:52:09
Extra line.
| |
32 r'>>|' | 32 // Grammar: |
33 r'>=|' | 33 // symbol ::= qualifiedName | <empty> |
34 r'>|' | 34 // qualifiedName ::= publicIdentifier '.' qualifiedName | name |
35 r'<=|' | 35 // name ::= publicIdentifier |
36 r'<|' | 36 // | publicIdentifier '=' |
37 r'&|' | 37 // | operator |
38 r'\^|' | 38 // where publicIdentifier is any valid identifier (not a reserved word) |
39 r'\|' | 39 // that isn't private (doesn't start with '_'). |
40 r')$'); | 40 // |
41 // Railroad diagram of the accepted grammar: | |
42 // | |
43 // /----------------\ | |
44 // | | | |
45 // | /-[.]-/ /-[=]-\ | |
46 // \ / / \ | |
47 // -------[id]-------------------------> | |
48 // \ / | |
49 // \------[operator]---/ | |
50 // \ / | |
51 // \------------/ | |
52 // | |
53 | |
54 // Validates non-empty symbol (empty symbol is handled before using this). | |
55 static final RegExp validationPattern = new RegExp( | |
56 '^(?:$operatorRE\$|$publicIdentifierRE(?:=?\$|[.](?!\$)))+?\$'); | |
41 | 57 |
42 external const Symbol(String name); | 58 external const Symbol(String name); |
43 | 59 |
44 /** | 60 /** |
45 * Platform-private method used by the mirror system to create | 61 * Platform-private method used by the mirror system to create |
46 * otherwise invalid names. | 62 * otherwise invalid names. |
47 */ | 63 */ |
48 const Symbol.unvalidated(this._name); | 64 const Symbol.unvalidated(this._name); |
49 | 65 |
50 // This is called by dart2js. | 66 // This is called by dart2js. |
51 Symbol.validated(String name) | 67 Symbol.validated(String name) |
52 : this._name = validate(name); | 68 : this._name = validate(name); |
53 | 69 |
54 bool operator ==(other) => other is Symbol && _name == other._name; | 70 bool operator ==(other) => other is Symbol && _name == other._name; |
55 | 71 |
56 int get hashCode { | 72 int get hashCode { |
57 const arbitraryPrime = 664597; | 73 const arbitraryPrime = 664597; |
58 return 0x1fffffff & (arbitraryPrime * _name.hashCode); | 74 return 0x1fffffff & (arbitraryPrime * _name.hashCode); |
59 } | 75 } |
60 | 76 |
61 toString() => 'Symbol("$_name")'; | 77 toString() => 'Symbol("$_name")'; |
62 | 78 |
63 /// Platform-private accessor which cannot be called from user libraries. | 79 /// Platform-private accessor which cannot be called from user libraries. |
64 static String getName(Symbol symbol) => symbol._name; | 80 static String getName(Symbol symbol) => symbol._name; |
65 | 81 |
66 static String validate(String name) { | 82 static String validate(String name) { |
67 if (name.isEmpty) return name; | 83 if (name.isEmpty || validationPattern.hasMatch(name)) return name; |
68 if (name.startsWith('_')) { | 84 if (name.startsWith('_')) { |
85 // There may be other private parts in a qualified name than the first | |
86 // one, but this is a common case that deserves a specific error | |
87 // message. | |
69 throw new ArgumentError('"$name" is a private identifier'); | 88 throw new ArgumentError('"$name" is a private identifier'); |
70 } | 89 } |
71 if (!validationPattern.hasMatch(name)) { | 90 throw new ArgumentError( |
72 throw new ArgumentError( | 91 '"$name" is not a valid (qualified) symbol name'); |
73 '"$name" is not an identifier or an empty String'); | |
74 } | |
75 return name; | |
76 } | 92 } |
77 } | 93 } |
OLD | NEW |