Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(168)

Side by Side Diff: sdk/lib/internal/symbol.dart

Issue 177483002: Add validation of private symbols to the dart2js mirrors. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 6 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 // Reserved words are not allowed as identifiers.
ahe 2014/02/24 10:43:23 How about turning these comments into proper docum
Lasse Reichstein Nielsen 2014/03/03 12:58:44 Done.
19 static const String reservedWordRE = 19 static const String reservedWordRE =
20 r'(?:assert|break|c(?:a(?:se|tch)|lass|on(?:st|tinue))|d(?:efault|o)|' 20 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)|' 21 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))|' 22 r'ret(?:hrow|urn)|s(?:uper|witch)|t(?:h(?:is|row)|r(?:ue|y))|'
23 r'v(?:ar|oid)|w(?:hile|ith))'; 23 r'v(?:ar|oid)|w(?:hile|ith))';
24 // Mathces a public identifier (identifier not starting with '_'). 24 // Matches a public identifier (identifier not starting with '_').
25 static const String publicIdentifierRE = 25 static const String publicIdentifierRE =
26 r'(?!' '$reservedWordRE' r'\b(?!\$))[a-zA-Z$][\w$]*'; 26 r'(?!' '$reservedWordRE' r'\b(?!\$))[a-zA-Z$][\w$]*';
27 // Matches a public or private identifier.
ahe 2014/02/24 10:43:23 I suggest: /// Matches an identifier (public or p
Lasse Reichstein Nielsen 2014/03/03 12:58:44 rewritten.
28 static const String identifierRE =
29 r'(?!' '$reservedWordRE' r'\b(?!\$))[a-zA-Z$_][\w$]*';
27 // Matches the names of declarable operators. 30 // Matches the names of declarable operators.
28 static const String operatorRE = 31 static const String operatorRE =
29 r'(?:[\-+*/%&|^]|\[\]=?|==|~/?|<[<=]?|>[>=]?|unary-)'; 32 r'(?:[\-+*/%&|^]|\[\]=?|==|~/?|<[<=]?|>[>=]?|unary-)';
30 33
31 // Grammar: 34 // Grammar:
32 // symbol ::= qualifiedName | <empty> 35 // symbol ::= qualifiedName | <empty>
33 // qualifiedName ::= publicIdentifier '.' qualifiedName | name 36 // qualifiedName ::= publicIdentifier '.' qualifiedName | name
34 // name ::= publicIdentifier 37 // name ::= publicIdentifier
35 // | publicIdentifier '=' 38 // | publicIdentifier '='
36 // | operator 39 // | operator
(...skipping 10 matching lines...) Expand all
47 // \ / 50 // \ /
48 // \------[operator]---/ 51 // \------[operator]---/
49 // \ / 52 // \ /
50 // \------------/ 53 // \------------/
51 // 54 //
52 55
53 // Validates non-empty symbol (empty symbol is handled before using this). 56 // Validates non-empty symbol (empty symbol is handled before using this).
54 static final RegExp validationPattern = new RegExp( 57 static final RegExp validationPattern = new RegExp(
55 '^(?:$operatorRE\$|$publicIdentifierRE(?:=?\$|[.](?!\$)))+?\$'); 58 '^(?:$operatorRE\$|$publicIdentifierRE(?:=?\$|[.](?!\$)))+?\$');
56 59
60 static final RegExp privateValidationPattern = new RegExp(
ahe 2014/02/24 10:43:23 This name is slightly confusing: this doesn't vali
Lasse Reichstein Nielsen 2014/03/03 12:58:44 Changed to publicSymbolPattern and symbolPattern.
61 '^(?:$operatorRE\$|$identifierRE(?:=?\$|[.](?!\$)))+?\$');
62
57 external const Symbol(String name); 63 external const Symbol(String name);
58 64
59 /** 65 /**
60 * Platform-private method used by the mirror system to create 66 * Platform-private method used by the mirror system to create
61 * otherwise invalid names. 67 * otherwise invalid names.
62 */ 68 */
63 const Symbol.unvalidated(this._name); 69 const Symbol.unvalidated(this._name);
64 70
65 // This is called by dart2js. 71 // This is called by dart2js.
66 Symbol.validated(String name) 72 Symbol.validated(String name)
(...skipping 15 matching lines...) Expand all
82 if (name.isEmpty || validationPattern.hasMatch(name)) return name; 88 if (name.isEmpty || validationPattern.hasMatch(name)) return name;
83 if (name.startsWith('_')) { 89 if (name.startsWith('_')) {
84 // There may be other private parts in a qualified name than the first 90 // 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 91 // one, but this is a common case that deserves a specific error
86 // message. 92 // message.
87 throw new ArgumentError('"$name" is a private identifier'); 93 throw new ArgumentError('"$name" is a private identifier');
88 } 94 }
89 throw new ArgumentError( 95 throw new ArgumentError(
90 '"$name" is not a valid (qualified) symbol name'); 96 '"$name" is not a valid (qualified) symbol name');
91 } 97 }
98
99 static String validatePrivate(String name) {
100 if (name.isEmpty || privateValidationPattern.hasMatch(name)) return name;
101 if (name.startsWith('_')) {
102 // There may be other private parts in a qualified name than the first
103 // one, but this is a common case that deserves a specific error
104 // message.
105 throw new ArgumentError('"$name" is a private identifier');
106 }
107 throw new ArgumentError(
108 '"$name" is not a valid (qualified) symbol name');
109 }
92 } 110 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698