| 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._collection.dev; | |
| 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 static final RegExp validationPattern = | |
| 19 new RegExp(r'^(?:[a-zA-Z$][a-zA-Z$0-9_]*\.)*(?:[a-zA-Z$][a-zA-Z$0-9_]*=?|' | |
| 20 r'-|' | |
| 21 r'unary-|' | |
| 22 r'\[\]=|' | |
| 23 r'~|' | |
| 24 r'==|' | |
| 25 r'\[\]|' | |
| 26 r'\*|' | |
| 27 r'/|' | |
| 28 r'%|' | |
| 29 r'~/|' | |
| 30 r'\+|' | |
| 31 r'<<|' | |
| 32 r'>>|' | |
| 33 r'>=|' | |
| 34 r'>|' | |
| 35 r'<=|' | |
| 36 r'<|' | |
| 37 r'&|' | |
| 38 r'\^|' | |
| 39 r'\|' | |
| 40 r')$'); | |
| 41 | |
| 42 external const Symbol(String name); | |
| 43 | |
| 44 /** | |
| 45 * Platform-private method used by the mirror system to create | |
| 46 * otherwise invalid names. | |
| 47 */ | |
| 48 const Symbol.unvalidated(this._name); | |
| 49 | |
| 50 // This is called by dart2js. | |
| 51 Symbol.validated(String name) | |
| 52 : this._name = validate(name); | |
| 53 | |
| 54 bool operator ==(other) => other is Symbol && _name == other._name; | |
| 55 | |
| 56 int get hashCode { | |
| 57 const arbitraryPrime = 664597; | |
| 58 return 0x1fffffff & (arbitraryPrime * _name.hashCode); | |
| 59 } | |
| 60 | |
| 61 toString() => 'Symbol("$_name")'; | |
| 62 | |
| 63 /// Platform-private accessor which cannot be called from user libraries. | |
| 64 static String getName(Symbol symbol) => symbol._name; | |
| 65 | |
| 66 static String validate(String name) { | |
| 67 if (name.isEmpty) return name; | |
| 68 if (name.startsWith('_')) { | |
| 69 throw new ArgumentError('"$name" is a private identifier'); | |
| 70 } | |
| 71 if (!validationPattern.hasMatch(name)) { | |
| 72 throw new ArgumentError( | |
| 73 '"$name" is not an identifier or an empty String'); | |
| 74 } | |
| 75 return name; | |
| 76 } | |
| 77 } | |
| OLD | NEW |