-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathEcmaScriptIdentifier.cs
376 lines (323 loc) · 8.51 KB
/
EcmaScriptIdentifier.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
#region License
/*---------------------------------------------------------------------------------*\
Distributed under the terms of an MIT-style license:
The MIT License
Copyright (c) 2006-2009 Stephen M. McKamey
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
\*---------------------------------------------------------------------------------*/
#endregion License
using System;
namespace JsonCLR.Json
{
/// <summary>
/// Represents an ECMAScript identifier for serialization.
/// </summary>
public class EcmaScriptIdentifier : IJsonSerializable
{
#region Fields
private readonly string identifier;
#endregion Fields
#region Init
/// <summary>
/// Ctor
/// </summary>
public EcmaScriptIdentifier()
: this(null)
{
}
/// <summary>
/// Ctor
/// </summary>
public EcmaScriptIdentifier(string ident)
{
this.identifier = String.IsNullOrEmpty(ident) ? String.Empty :
EcmaScriptIdentifier.EnsureValidIdentifier(ident, true);
}
#endregion Init
#region Properties
/// <summary>
/// Gets the ECMAScript identifier represented by this instance.
/// </summary>
public string Identifier
{
get { return this.identifier; }
}
#endregion Properties
#region Methods
/// <summary>
/// Ensures is a valid EcmaScript variable expression.
/// </summary>
/// <param name="varExpr">the variable expression</param>
/// <returns>varExpr</returns>
public static string EnsureValidIdentifier(string varExpr, bool nested)
{
return EcmaScriptIdentifier.EnsureValidIdentifier(varExpr, nested, true);
}
/// <summary>
/// Ensures is a valid EcmaScript variable expression.
/// </summary>
/// <param name="varExpr">the variable expression</param>
/// <returns>varExpr</returns>
public static string EnsureValidIdentifier(string varExpr, bool nested, bool throwOnEmpty)
{
if (String.IsNullOrEmpty(varExpr))
{
if (throwOnEmpty)
{
throw new ArgumentException("Variable expression is empty.");
}
return String.Empty;
}
varExpr = varExpr.Replace(" ", "");
if (!EcmaScriptIdentifier.IsValidIdentifier(varExpr, nested))
{
throw new ArgumentException("Variable expression \""+varExpr+"\" is not supported.");
}
return varExpr;
}
/// <summary>
/// Verifies is a valid EcmaScript variable expression.
/// </summary>
/// <param name="varExpr">the variable expression</param>
/// <returns>varExpr</returns>
/// <remarks>
/// http://www.ecma-international.org/publications/files/ECMA-ST/Ecma-262.pdf
///
/// IdentifierName =
/// IdentifierStart | IdentifierName IdentifierPart
/// IdentifierStart =
/// Letter | '$' | '_'
/// IdentifierPart =
/// IdentifierStart | Digit
/// </remarks>
public static bool IsValidIdentifier(string varExpr, bool nested)
{
if (String.IsNullOrEmpty(varExpr))
{
return false;
}
if (nested)
{
string[] parts = varExpr.Split('.');
foreach (string part in parts)
{
if (!EcmaScriptIdentifier.IsValidIdentifier(part, false))
{
return false;
}
}
return true;
}
if (EcmaScriptIdentifier.IsReservedWord(varExpr))
{
return false;
}
bool indentPart = false;
foreach (char ch in varExpr)
{
if (indentPart && Char.IsDigit(ch))
{
// digits are only allowed after first char
continue;
}
// can be start or part
if (Char.IsLetter(ch) || ch == '_' || ch == '$')
{
indentPart = true;
continue;
}
return false;
}
return true;
}
private static bool IsReservedWord(string varExpr)
{
// TODO: investigate doing this like Rhino does (switch on length check first letter or two)
switch (varExpr)
{
// literals
case "null":
case "false":
case "true":
// ES5 Keywords
case "break":
case "case":
case "catch":
case "continue":
case "debugger":
case "default":
case "delete":
case "do":
case "else":
case "finally":
case "for":
case "function":
case "if":
case "in":
case "instanceof":
case "new":
case "return":
case "switch":
case "this":
case "throw":
case "try":
case "typeof":
case "var":
case "void":
case "while":
case "with":
// ES5 Future Reserved Words
case "abstract":
case "boolean":
case "byte":
case "char":
case "class":
case "const":
case "double":
case "enum":
case "export":
case "extends":
case "final":
case "float":
case "goto":
case "implements":
case "import":
case "int":
case "interface":
case "long":
case "native":
case "package":
case "private":
case "protected":
case "public":
case "short":
case "static":
case "super":
case "synchronized":
case "throws":
case "transient":
case "volatile":
// ES5 Possible Reserved Words
case "let":
case "yield":
{
return true;
}
default:
{
return false;
}
}
}
/// <summary>
/// Trivial conversion method. Essentially performs a cast.
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
/// <remarks>
/// Supports conversion via System.Web.UI.PropertyConverter.ObjectFromString(Type, MemberInfo, string)
/// </remarks>
public static EcmaScriptIdentifier Parse(string value)
{
return new EcmaScriptIdentifier(value);
}
#endregion Methods
#region Operators
/// <summary>
/// Implicit type conversion allows to be used directly as a String
/// </summary>
/// <param name="ident">valid ECMAScript identifier</param>
/// <returns></returns>
public static implicit operator string(EcmaScriptIdentifier ident)
{
if (ident == null)
{
return String.Empty;
}
return ident.identifier;
}
/// <summary>
/// Implicit type conversion allows to be used directly with Strings
/// </summary>
/// <param name="ident">valid ECMAScript identifier</param>
/// <returns></returns>
public static implicit operator EcmaScriptIdentifier(string ident)
{
return new EcmaScriptIdentifier(ident);
}
#endregion Operators
#region IJsonSerializable Members
void IJsonSerializable.ReadJson(JsonReader reader)
{
throw new NotImplementedException("The method or operation is not implemented.");
}
void IJsonSerializable.WriteJson(JsonWriter writer)
{
if (String.IsNullOrEmpty(this.identifier))
{
writer.TextWriter.Write("null");
}
else
{
// TODO: determine if this should output parens around identifier
writer.TextWriter.Write(this.identifier);
}
}
#endregion IJsonSerializable Members
#region Object Overrides
/// <summary>
/// Compares the identifiers.
/// </summary>
/// <param name="obj"></param>
/// <returns></returns>
public override bool Equals(object obj)
{
EcmaScriptIdentifier that = obj as EcmaScriptIdentifier;
if (that == null)
{
return base.Equals(obj);
}
if (String.IsNullOrEmpty(this.identifier) && String.IsNullOrEmpty(that.identifier))
{
// null and String.Empty are equivalent
return true;
}
return StringComparer.Ordinal.Equals(this.identifier, that.identifier);
}
/// <summary>
/// Returns the identifier.
/// </summary>
/// <returns></returns>
public override string ToString()
{
return this.identifier;
}
/// <summary>
/// Returns the hash code for this identifier.
/// </summary>
/// <returns></returns>
public override int GetHashCode()
{
if (this.identifier == null)
{
return 0;
}
return this.identifier.GetHashCode();
}
#endregion Object Overrides
}
}