1
|
|
#region Copyright
|
2
|
|
|
3
|
|
|
4
|
|
|
5
|
|
|
6
|
|
|
7
|
|
|
8
|
|
|
9
|
|
|
10
|
|
|
11
|
|
|
12
|
|
|
13
|
|
|
14
|
|
|
15
|
|
|
16
|
|
|
17
|
|
#endregion
|
18
|
|
|
19
|
|
using System;
|
20
|
|
using System.Text;
|
21
|
|
using System.Text.RegularExpressions;
|
22
|
|
|
23
|
|
namespace Seasar.Dao.Node
|
24
|
|
{
|
25
|
|
|
26
|
|
|
27
|
|
|
28
|
|
public class ExpressionUtil
|
29
|
|
{
|
30
|
|
private IToken current;
|
31
|
|
private int start;
|
32
|
|
private string text;
|
33
|
|
private Regex reOps;
|
34
|
|
private Regex reLit;
|
35
|
|
private Regex reSym;
|
36
|
|
|
37
|
11
|
public ExpressionUtil()
|
38
|
|
{
|
39
|
11
|
reOps = new Regex(@"^\s*(&&|\|\||<=|>=|==|!=|[=+\-*/^()!<>])", RegexOptions.Compiled);
|
40
|
11
|
reSym = new Regex(@"^\s*(\-?\b*[_a-zA-Z.']+[_a-zA-Z0-9\[()\]]*)", RegexOptions.Compiled);
|
41
|
11
|
reLit = new Regex(@"^\s*([0-9]+(\.[0-9]+)?)", RegexOptions.Compiled);
|
42
|
|
}
|
43
|
|
|
44
|
11
|
public string parseExpression(string expression)
|
45
|
|
{
|
46
|
11
|
current = null;
|
47
|
11
|
text = expression;
|
48
|
11
|
StringBuilder sb = new StringBuilder(255);
|
49
|
45
|
while (!EOF())
|
50
|
|
{
|
51
|
34
|
IToken token = NextToken();
|
52
|
34
|
sb.Append(token.Value + " ");
|
53
|
|
}
|
54
|
11
|
return sb.ToString().TrimEnd(' ');
|
55
|
|
}
|
56
|
|
|
57
|
45
|
protected bool EOF()
|
58
|
|
{
|
59
|
45
|
if (current is Eof)
|
60
|
|
{
|
61
|
11
|
return true;
|
62
|
|
}
|
63
|
34
|
return false;
|
64
|
|
}
|
65
|
|
|
66
|
34
|
protected IToken NextToken()
|
67
|
|
{
|
68
|
34
|
Match match;
|
69
|
34
|
match = reOps.Match(text);
|
70
|
34
|
if (match.Length != 0)
|
71
|
|
{
|
72
|
6
|
SetOperatorToken(match);
|
73
|
|
}
|
74
|
|
else
|
75
|
|
{
|
76
|
28
|
match = reSym.Match(text);
|
77
|
28
|
if (match.Length != 0)
|
78
|
|
{
|
79
|
17
|
SetSymbolToken(match);
|
80
|
|
}
|
81
|
|
else
|
82
|
|
{
|
83
|
11
|
match = reLit.Match(text);
|
84
|
11
|
if (match.Length != 0)
|
85
|
|
{
|
86
|
0
|
SetNumberLiteralToken(match);
|
87
|
|
}
|
88
|
|
else
|
89
|
11
|
current = new Eof();
|
90
|
|
}
|
91
|
|
}
|
92
|
34
|
return current;
|
93
|
|
}
|
94
|
|
|
95
|
0
|
private void SetNumberLiteralToken(Match match)
|
96
|
|
{
|
97
|
|
IToken token;
|
98
|
|
start += match.Length;
|
99
|
|
text = text.Substring(match.Length);
|
100
|
|
token = new NumberLiteral();
|
101
|
|
token.Value = match.Groups[1].Value;
|
102
|
|
current = token;
|
103
|
|
}
|
104
|
|
|
105
|
17
|
private void SetSymbolToken(Match match)
|
106
|
|
{
|
107
|
17
|
IToken token;
|
108
|
17
|
start += match.Length;
|
109
|
17
|
text = text.Substring(match.Length);
|
110
|
17
|
token = new Symbol();
|
111
|
17
|
token.Value = match.Groups[1].Value;
|
112
|
17
|
current = token;
|
113
|
|
}
|
114
|
|
|
115
|
6
|
private void SetOperatorToken(Match match)
|
116
|
|
{
|
117
|
6
|
IToken token;
|
118
|
6
|
start += match.Length;
|
119
|
6
|
text = text.Substring(match.Length);
|
120
|
6
|
token = new Operator();
|
121
|
6
|
token.Value = match.Groups[1].Value;
|
122
|
6
|
current = token;
|
123
|
|
}
|
124
|
|
}
|
125
|
|
|
126
|
|
#region Token
|
127
|
|
public interface IToken
|
128
|
|
{
|
129
|
|
object Value {get; set;}
|
130
|
|
}
|
131
|
|
public class Eof : IToken
|
132
|
|
{
|
133
|
11
|
public Eof()
|
134
|
|
{
|
135
|
|
}
|
136
|
|
|
137
|
|
public object Value
|
138
|
|
{
|
139
|
11
|
get { return null; }
|
140
|
0
|
set { throw new NotImplementedException(); }
|
141
|
|
}
|
142
|
|
}
|
143
|
|
|
144
|
|
public class Symbol : IToken
|
145
|
|
{
|
146
|
|
private string _value;
|
147
|
|
private string[] _escapes = { "null", "true", "false" };
|
148
|
|
|
149
|
17
|
public Symbol()
|
150
|
|
{
|
151
|
|
}
|
152
|
|
|
153
|
|
public object Value
|
154
|
|
{
|
155
|
17
|
get { return GetArgValue(); }
|
156
|
17
|
set { _value = (string) value; }
|
157
|
|
}
|
158
|
|
|
159
|
17
|
private string GetArgValue()
|
160
|
|
{
|
161
|
17
|
if(_value.StartsWith("'") && _value.EndsWith("'"))
|
162
|
0
|
return _value;
|
163
|
|
|
164
|
17
|
foreach(string escape in _escapes)
|
165
|
|
{
|
166
|
38
|
if ( _value.ToLower() == escape)
|
167
|
11
|
return _value.ToLower();
|
168
|
|
}
|
169
|
|
|
170
|
6
|
return "self.GetArg('" + _value + "')";
|
171
|
|
}
|
172
|
|
|
173
|
0
|
public override string ToString()
|
174
|
|
{
|
175
|
|
return _value.ToString();
|
176
|
|
}
|
177
|
|
}
|
178
|
|
|
179
|
|
public class NumberLiteral : IToken
|
180
|
|
{
|
181
|
|
private float _value;
|
182
|
|
|
183
|
0
|
public NumberLiteral()
|
184
|
|
{
|
185
|
|
}
|
186
|
|
|
187
|
0
|
public object Value
|
188
|
|
{
|
189
|
|
get { return _value; }
|
190
|
|
set { _value = (float)Double.Parse(value.ToString()); }
|
191
|
|
}
|
192
|
0
|
public override string ToString()
|
193
|
|
{
|
194
|
|
return _value.ToString();
|
195
|
|
}
|
196
|
|
}
|
197
|
|
|
198
|
|
public class Operator : IToken
|
199
|
|
{
|
200
|
|
private string _value;
|
201
|
|
|
202
|
6
|
public Operator()
|
203
|
|
{
|
204
|
|
}
|
205
|
|
|
206
|
|
public object Value
|
207
|
|
{
|
208
|
6
|
get { return _value; }
|
209
|
6
|
set { _value = (string) value; }
|
210
|
|
}
|
211
|
0
|
public override string ToString()
|
212
|
|
{
|
213
|
|
return _value.ToString();
|
214
|
|
}
|
215
|
|
}
|
216
|
|
#endregion
|
217
|
|
}
|
218
|
|
|