Repository navigation
Expand file tree
/
Copy pathJavascript.cs
More file actions
192 lines (168 loc) · 5.53 KB
/
Copy pathJavascript.cs
File metadata and controls
192 lines (168 loc) · 5.53 KB
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
/********
* @version : 1.3.0
* @author : Ext.NET, Inc. http://www.ext.net/
* @date : 2012-02-29
* @copyright : Copyright (c) 2007-2012, Ext.NET, Inc. (http://www.ext.net/). All rights reserved.
* @license : See license.txt and http://www.ext.net/license/.
********/
using System.Text.RegularExpressions;
namespace Ext.Net.Utilities.Inflatr
{
public class Javascript : Base
{
public const string KEYWORDS = "(function|continue|default|finally|export|return|switch|import|delete|throw|const|while|catch|break|void|case|with|this|else|for|try|var|new|do|in|if)\\W";
public const string OPERATORS = "(instanceof|typeof|>>>=|===|<<=|>>=|>>>|!==|!=|>=|\\*=|<=|\\+\\+|\\-=|==|&&|>>|<<|/=|\\|\\||\\+=|\\^=|\\|=|&=|\\-\\-|\\||&|\\^|>|<|!|=|\\?|:|%|/|\\*|\\-|\\+)";
public override string Inflate(string input)
{
this.input = input;
while ( this.index < this.input.Length )
{
bool r = this.Comment() || this.String() || this.Regex() || this.Operator() ||
this.Keyword() ||this.OpenBlock() || this.CloseBlock() || this.Comma() ||
this.Parens() || this.Eos() || this.Eol() || this.NextChar();
}
return new Regex("\\s*$").Replace(this.r.ToString(), "");
}
protected virtual bool Comment()
{
string m = this.Between("//", "\n");
if (m.IsNotEmpty())
{
this.Append("//", m, this.NewLine());
return false;
}
else
{
m = this.Between(this.Escape("/*"), this.Escape("*/"));
if (m.IsNotEmpty())
{
this.Append("/*", m, "*/");
return true;
}
}
return false;
}
protected virtual bool String()
{
string m = this.Between("'");
if (m.IsNotEmpty())
{
this.Append("'" + m + "'");
return true;
}
else
{
m = this.Between("\"");
if (m.IsNotEmpty())
{
this.Append("\"" + m + "\"");
return true;
}
}
return false;
}
private Regex afterPatternRegex = new Regex("^(\\d|\\w|\\$|_\\))", RegexOptions.Compiled);
private Regex afterStartRegex = new Regex("(\\:|\\S)", RegexOptions.Compiled);
protected virtual bool Regex()
{
if (!this.After(this.afterPatternRegex, this.afterStartRegex))
{
string m = this.Between("/");
if (m.IsNotEmpty())
{
this.Append("/"+m+"/");
return true;
}
}
return false;
}
protected virtual bool Operator()
{
string m = this.Scan(Javascript.OPERATORS);
if (m.IsNotEmpty())
{
this.Append(" " + m + " ");
return true;
}
return false;
}
protected virtual bool Keyword()
{
return false;
}
protected virtual bool OpenBlock()
{
if (this.Scan("\\{").IsNotEmpty())
{
this.Options.Level += 1;
this.Append(" {", this.NewLine() );
return true;
}
return false;
}
protected virtual bool CloseBlock()
{
if(this.Scan("\\}").IsNotEmpty())
{
this.Options.Level -= 1;
this.Append( this.NewLine(), "}" );
if (this.Peek("[;,\\}]").IsNotEmpty())
{
this.Append( this.NewLine() );
}
return true;
}
return false;
}
protected virtual bool Comma()
{
if (this.Scan(",").IsNotEmpty())
{
if (this.Peek("(?!\\{\\})*;").IsNotEmpty() || (this.c < this.Options.Wrap))
{
this.Append(", ");
} else {
this.Append(",", this.NewLine() );
}
return true;
}
return false;
}
protected virtual bool Parens()
{
if (this.Scan("\\(\\s*\\)").IsNotEmpty())
{
this.Append("()");
return true;
}
else if(this.Scan("\\(").IsNotEmpty())
{
this.Append(" ( ");
return true;
}
else if (this.Scan("\\)").IsNotEmpty())
{
this.Append(" ) ");
return true;
}
return false;
}
protected virtual bool Eos()
{
if (this.Scan(";").IsNotEmpty())
{
this.Append(";");
if (this.Peek("\\s*\\}").IsNotEmpty())
{
this.Append(this.NewLine());
}
return true;
}
return false;
}
protected virtual bool Eol()
{
return this.Scan("\n").IsNotEmpty();
}
}
}