-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathDCToken.cs
476 lines (456 loc) · 15.1 KB
/
DCToken.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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
/*
都昌数值表达式引擎 DCSoft.Expression
南京都昌信息科技有限公司 2018年 版权所有
公司网址 http://www.dcwriter.cn
*/
using System;
using System.Collections.Generic;
using System.Text;
namespace DCSoft.Expression
{
/// <summary>
/// 符号对象
/// </summary>
[System.Runtime.InteropServices.ComVisible(false)]
internal class DCToken
{
public CharType Type = CharType.None;
public string Text = null;
public StringBuilder _Str = new StringBuilder();
public override string ToString()
{
return this.Type + "\t:" + this.Text;
}
//public int Level
//{
// get
// {
// if (this.Text == "*"
// || this.Text == "/"
// || this.Text == "%")
// {
// return 3;
// }
// if (this.Text == "+" || this.Text == "-")
// {
// return 2;
// }
// if (this.Type == CharType.MathOperator)
// {
// return 1;
// }
// if (this.Type == CharType.Symbol || this.Type == CharType.StringConst)
// {
// return 0;
// }
// if (this.Type == CharType.Spliter)
// {
// return -1;
// }
// return 0;
// }
//}
}
/// <summary>
/// 符号对象列表
/// </summary>
[System.Runtime.InteropServices.ComVisible(false)]
internal class DCTokenList : List<DCToken>
{
public DCTokenList(string txt)
{
this.Parse(txt);
}
private int _Position = -1;
public DCToken Current
{
get
{
if (_Position >= 0 && _Position < this.Count)
{
return this[_Position];
}
return null;
}
}
public DCToken NextItem
{
get
{
if (_Position >= 0 && this._Position < this.Count - 1)
{
return this[_Position + 1];
}
return null;
}
}
public void ResetPosition()
{
this._Position = -1;
}
public bool MoveNext()
{
this._Position++;
if (this._Position >= 0 && this._Position < this.Count)
{
return true;
}
return false;
}
/// <summary>
/// 获得所有操作符字符串
/// </summary>
/// <returns></returns>
public void Parse(string text)
{
this.Clear();
if (text == null || text.Length == 0)
{
return;
}
DCToken currentToken = null;
StringMode sm = StringMode.None;
for (int position = 0; position < text.Length; position++)
{
char c = text[position];
if (sm == StringMode.SingleQuotes || sm == StringMode.DoubleQuotes)
{
// 正在定义字符串
if (c == '\\')
{
// 开始转义
const string EigthDigs = "01234567";
const string HexDigs = "0123456789ABCDEF";
char nextC = NextChar(text, position);
if (EigthDigs.IndexOf(nextC) >= 0)
{
// 三位八进制数字
string v = NextChars(text, position, 3);
if (v != null && v.Length == 3)
{
position += 3;
int num = ParseNumber(v, EigthDigs);
currentToken._Str.Append((char)num);
}
else
{
throw new System.Exception("长度不够:" + text);
}
}
else if (nextC == 'x')
{
// 两个十六进制
position++;
string v = NextChars(text, position, 2);
if (v != null && v.Length == 2)
{
v = v.ToUpper();
position += 2;
int num = ParseNumber(v, HexDigs);
currentToken._Str.Append((char)num);
}
else
{
throw new System.Exception("长度不够:" + text);
}
}
else if (nextC == 'a')
{
currentToken._Str.Append('\a');
}
else if (nextC == 'b')
{
currentToken._Str.Append('\b');
}
else if (nextC == 'n')
{
currentToken._Str.Append('\n');
}
else if (nextC == 'r')
{
currentToken._Str.Append('\r');
}
else if (nextC == 'v')
{
currentToken._Str.Append('\v');
}
else if (nextC == '"')
{
currentToken._Str.Append('"');
}
else if (nextC == '\'')
{
currentToken._Str.Append('\'');
}
else
{
throw new System.Exception("不支持的转移:" + nextC);
}
}
else if (sm == StringMode.SingleQuotes && c == '\'')
{
// 结束定义单引号字符串
currentToken._Str.Append(c);
currentToken = null;
sm = StringMode.None;
}
else if (sm == StringMode.DoubleQuotes && c == '"')
{
// 结束定义双引号字符串
currentToken._Str.Append(c);
currentToken = null;
sm = StringMode.None;
}
else
{
// 添加正常字符
currentToken._Str.Append(c);
}
}
else
{
// 不是正在定义字符串
if (c == '\'')
{
// 开始定义单引号字符串
currentToken = new DCToken();
currentToken.Type = CharType.StringConst;
currentToken._Str.Append(c);
sm = StringMode.SingleQuotes;
this.Add(currentToken);
}
else if (c == '"')
{
// 开始定义双引号字符串
currentToken = new DCToken();
currentToken.Type = CharType.StringConst;
currentToken._Str.Append(c);
sm = StringMode.DoubleQuotes;
this.Add(currentToken);
}
else if (char.IsWhiteSpace(c))
{
// 遇到空白字符则过滤掉空白字符
currentToken = null;
for (; position < text.Length; position++)
{
if (char.IsWhiteSpace(text[position]) == false)
{
position--;
break;
}
}
}
else
{
// 普通字符
if (c == '(')
{
currentToken = new DCToken();
currentToken._Str.Append(c);
currentToken.Type = CharType.CurLeft;
this.Add(currentToken);
currentToken = null;
}
else if (c == ')')
{
currentToken = new DCToken();
currentToken._Str.Append(c);
currentToken.Type = CharType.CurRight;
this.Add(currentToken);
currentToken = null;
}
else
{
CharType ct = GetChartType(c);
if (currentToken == null || currentToken.Type != ct)
{
currentToken = new DCToken();
currentToken.Type = ct;
this.Add(currentToken);
}
currentToken._Str.Append(c);
}
}
}
}//for
if (currentToken != null && currentToken._Str.Length > 0)
{
if (this.Contains(currentToken) == false)
{
this.Add(currentToken);
}
}
foreach (var item in this)
{
if (item._Str != null)
{
item.Text = item._Str.ToString();
item._Str = null;
}
}
}
/// <summary>
/// 文本转换为数字
/// </summary>
/// <param name="txt">文本</param>
/// <param name="digs">数字字符</param>
/// <returns>转换后的数字</returns>
private int ParseNumber(string txt, string digs)
{
int v = 0;
for (int iCount = 0; iCount < txt.Length; iCount++)
{
int i = digs.IndexOf(txt[iCount]);
if (i < 0)
{
throw new System.InvalidCastException(digs + ":" + txt);
}
v = v * digs.Length + i;
}
return v;
}
/// <summary>
/// 获得下一个字符
/// </summary>
/// <returns></returns>
private char NextChar(string text, int position)
{
if (position < text.Length - 1)
{
return text[position + 1];
}
return char.MinValue;
}
private string NextChars(string text, int position, int len)
{
if (position < text.Length - len)
{
string v = text.Substring(position, len);
return v;
}
return null;
}
/// <summary>
/// 获得字符类型
/// </summary>
/// <param name="c">字符</param>
/// <param name="isVB">是否为VB语法</param>
/// <returns>字符类型</returns>
private CharType GetChartType(char c)
{
if (c == '$')
{
return CharType.Symbol;
}
if (c == '(')
{
return CharType.CurLeft;
}
if (c == ')')
{
return CharType.CurRight;
}
// 为了保持兼容性,不支持方括号。
//if (c == '[')
//{
// return CharType.SquLeft;
//}
//if( c == ']')
//{
// return CharType.SquRight;
//}
if (c == ',')
{
return CharType.Spliter;
}
if (c == '+'
|| c == '-'
|| c == '*'
|| c == '/'
|| c =='%'
|| c == '\\')
{
return CharType.MathOperator;
}
if ( c == '&' || c == '^' || c == '|' || c == '='
|| c == '>' || c == '<')
{
return CharType.LogicOperator;
}
else if (char.IsWhiteSpace(c))
{
return CharType.Whitespace;
}
if (c == ':'
|| c == '!'
|| c == '.'
|| char.IsLetterOrDigit(c)
|| char.IsSymbol(c)
|| c == '[' || c == ']')
{
return CharType.Symbol;
}
return CharType.Symbol;
}
}
internal enum StringMode
{
/// <summary>
/// 无
/// </summary>
None,
/// <summary>
/// 单引号字符串
/// </summary>
SingleQuotes,
/// <summary>
/// 多引号字符串
/// </summary>
DoubleQuotes
}
internal enum CharType
{
None,
/// <summary>
/// 标识符
/// </summary>
Symbol,
/// <summary>
/// 数学运算符
/// </summary>
MathOperator,
/// <summary>
/// 逻辑运算符
/// </summary>
LogicOperator,
/// <summary>
/// 左边的圆括号
/// </summary>
CurLeft,
/// <summary>
/// 右边的圆括号
/// </summary>
CurRight,
/// <summary>
/// 左边的方括号
/// </summary>
SquLeft,
/// <summary>
/// 右边的方括号
/// </summary>
SquRight,
/// <summary>
/// 空白字符
/// </summary>
Whitespace,
/// <summary>
/// 分隔字符
/// </summary>
Spliter,
/// <summary>
/// 字符串
/// </summary>
StringConst
}
}