-
Notifications
You must be signed in to change notification settings - Fork 95
/
TokenReader.cs
252 lines (227 loc) · 11.7 KB
/
TokenReader.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
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
namespace Jace.Tokenizer
{
/// <summary>
/// A token reader that converts the input string in a list of tokens.
/// </summary>
public class TokenReader
{
private readonly CultureInfo cultureInfo;
private readonly char decimalSeparator;
private readonly char argumentSeparator;
public TokenReader()
: this(CultureInfo.CurrentCulture)
{
}
public TokenReader(CultureInfo cultureInfo)
{
this.cultureInfo = cultureInfo;
this.decimalSeparator = cultureInfo.NumberFormat.NumberDecimalSeparator[0];
this.argumentSeparator = cultureInfo.TextInfo.ListSeparator[0];
}
/// <summary>
/// Read in the provided formula and convert it into a list of takens that can be processed by the
/// Abstract Syntax Tree Builder.
/// </summary>
/// <param name="formula">The formula that must be converted into a list of tokens.</param>
/// <returns>The list of tokens for the provided formula.</returns>
public List<Token> Read(string formula)
{
if (string.IsNullOrEmpty(formula))
throw new ArgumentNullException("formula");
List<Token> tokens = new List<Token>();
char[] characters = formula.ToCharArray();
bool isFormulaSubPart = true;
bool isScientific = false;
for(int i = 0; i < characters.Length; i++)
{
if (IsPartOfNumeric(characters[i], true, isFormulaSubPart))
{
string buffer = "" + characters[i];
int startPosition = i;
while (++i < characters.Length && IsPartOfNumeric(characters[i], false, isFormulaSubPart))
{
if (isScientific && IsScientificNotation(characters[i]))
throw new ParseException(string.Format("Invalid token \"{0}\" detected at position {1}.", characters[i], i));
if (IsScientificNotation(characters[i]))
{
isScientific = IsScientificNotation(characters[i]);
if (characters[i + 1] == '-')
{
buffer += characters[i++];
}
}
buffer += characters[i];
}
// Verify if we do not have an int
int intValue;
if (int.TryParse(buffer, out intValue))
{
tokens.Add(new Token() { TokenType = TokenType.Integer, Value = intValue, StartPosition = startPosition, Length = i - startPosition });
isFormulaSubPart = false;
}
else
{
double doubleValue;
if (double.TryParse(buffer, NumberStyles.Float | NumberStyles.AllowThousands,
cultureInfo, out doubleValue))
{
tokens.Add(new Token() { TokenType = TokenType.FloatingPoint, Value = doubleValue, StartPosition = startPosition, Length = i - startPosition });
isFormulaSubPart = false;
}
else if (buffer == "-")
{
// Verify if we have a unary minus, we use the token '_' for a unary minus in the AST builder
tokens.Add(new Token() { TokenType = TokenType.Operation, Value = '_', StartPosition = startPosition, Length = 1 });
}
// Else we skip
}
if (i == characters.Length)
{
// Last character read
continue;
}
}
if (IsPartOfVariable(characters[i], true))
{
string buffer = "" + characters[i];
int startPosition = i;
while (++i < characters.Length && IsPartOfVariable(characters[i], false))
{
buffer += characters[i];
}
tokens.Add(new Token() { TokenType = TokenType.Text, Value = buffer, StartPosition = startPosition, Length = i -startPosition });
isFormulaSubPart = false;
if (i == characters.Length)
{
// Last character read
continue;
}
}
if (characters[i] == this.argumentSeparator)
{
tokens.Add(new Token() { TokenType = Tokenizer.TokenType.ArgumentSeparator, Value = characters[i], StartPosition = i, Length = 1 });
isFormulaSubPart = false;
}
else
{
switch (characters[i])
{
case ' ':
continue;
case '+':
case '-':
case '*':
case '/':
case '^':
case '%':
case '≤':
case '≥':
case '≠':
if (IsUnaryMinus(characters[i], tokens))
{
// We use the token '_' for a unary minus in the AST builder
tokens.Add(new Token() { TokenType = TokenType.Operation, Value = '_', StartPosition = i, Length = 1 });
}
else
{
tokens.Add(new Token() { TokenType = TokenType.Operation, Value = characters[i], StartPosition = i, Length = 1 });
}
isFormulaSubPart = true;
break;
case '(':
tokens.Add(new Token() { TokenType = TokenType.LeftBracket, Value = characters[i], StartPosition = i, Length = 1 });
isFormulaSubPart = true;
break;
case ')':
tokens.Add(new Token() { TokenType = TokenType.RightBracket, Value = characters[i], StartPosition = i, Length = 1 });
isFormulaSubPart = false;
break;
case '<':
if (i + 1 < characters.Length && characters[i + 1] == '=')
tokens.Add(new Token() { TokenType = TokenType.Operation, Value = '≤', StartPosition = i++, Length = 2 });
else
tokens.Add(new Token() { TokenType = TokenType.Operation, Value = '<', StartPosition = i, Length = 1 });
isFormulaSubPart = false;
break;
case '>':
if (i + 1 < characters.Length && characters[i + 1] == '=')
tokens.Add(new Token() { TokenType = TokenType.Operation, Value = '≥', StartPosition = i++, Length = 2 });
else
tokens.Add(new Token() { TokenType = TokenType.Operation, Value = '>', StartPosition = i, Length = 1 });
isFormulaSubPart = false;
break;
case '!':
if (i + 1 < characters.Length && characters[i + 1] == '=')
{
tokens.Add(new Token() { TokenType = TokenType.Operation, Value = '≠', StartPosition = i++, Length = 2 });
isFormulaSubPart = false;
}
else
throw new ParseException(string.Format("Invalid token \"{0}\" detected at position {1}.", characters[i], i));
break;
case '&':
if (i + 1 < characters.Length && characters[i + 1] == '&')
{
tokens.Add(new Token() { TokenType = TokenType.Operation, Value = '&', StartPosition = i++, Length = 2 });
isFormulaSubPart = false;
}
else
throw new ParseException(string.Format("Invalid token \"{0}\" detected at position {1}.", characters[i], i));
break;
case '|':
if (i + 1 < characters.Length && characters[i + 1] == '|')
{
tokens.Add(new Token() { TokenType = TokenType.Operation, Value = '|', StartPosition = i++, Length = 2 });
isFormulaSubPart = false;
}
else
throw new ParseException(string.Format("Invalid token \"{0}\" detected at position {1}.", characters[i], i));
break;
case '=':
if (i + 1 < characters.Length && characters[i + 1] == '=')
{
tokens.Add(new Token() { TokenType = TokenType.Operation, Value = '=', StartPosition = i++, Length = 2 });
isFormulaSubPart = false;
}
else
throw new ParseException(string.Format("Invalid token \"{0}\" detected at position {1}.", characters[i], i));
break;
default:
throw new ParseException(string.Format("Invalid token \"{0}\" detected at position {1}.", characters[i], i));
}
}
}
return tokens;
}
private bool IsPartOfNumeric(char character, bool isFirstCharacter, bool isFormulaSubPart)
{
return character == decimalSeparator || (character >= '0' && character <= '9') || (isFormulaSubPart && isFirstCharacter && character == '-') || character == 'e' || character == 'E';
}
private bool IsPartOfVariable(char character, bool isFirstCharacter)
{
return (character >= 'a' && character <= 'z') || (character >= 'A' && character <= 'Z') || (!isFirstCharacter && character >= '0' && character <= '9') || (!isFirstCharacter && character == '_');
}
private bool IsUnaryMinus(char currentToken, List<Token> tokens)
{
if (currentToken == '-')
{
Token previousToken = tokens[tokens.Count - 1];
return !(previousToken.TokenType == TokenType.FloatingPoint ||
previousToken.TokenType == TokenType.Integer ||
previousToken.TokenType == TokenType.Text ||
previousToken.TokenType == TokenType.RightBracket);
}
else
return false;
}
private bool IsScientificNotation(char currentToken)
{
return currentToken == 'e' || currentToken == 'E';
}
}
}