-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathScanner.cs
448 lines (402 loc) · 13.4 KB
/
Scanner.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
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading;
using Mono.Cecil;
namespace jamconverter
{
public class Scanner
{
private readonly string _input;
private int nextChar = 0;
private bool isInsideQuote = false;
private bool _insideVariableExpansionModifierSpan;
private bool _insideVariableExpansionModifierValueSpan;
private int _insideVariableExpansionDepth = 0;
private bool _enterringActions;
List<ScanToken> _previouslyScannedTokens = new List<ScanToken>();
public Scanner(string input)
{
_input = input;
}
public ScanResult Scan()
{
return new ScanResult(ScanAllTokens().ToArray());
}
public IEnumerable<ScanToken> ScanAllTokens()
{
while (true)
{
var sr = ScanToken();
yield return sr;
if (sr.tokenType == TokenType.EOF)
yield break;
}
}
public ScanToken ScanToken()
{
var scanToken = ScanTokenImpl();
this._previouslyScannedTokens.Add(scanToken);
return scanToken;
}
private ScanToken ScanTokenImpl()
{
if (nextChar >= _input.Length)
return new ScanToken() {literal = "", tokenType = TokenType.EOF};
var c = _input[nextChar];
if (_enterringActions)
{
if (_previouslyScannedTokens.Last().tokenType == TokenType.AccoladeOpen)
{
var sb = new StringBuilder();
while (true)
{
nextChar++;
if (_input[nextChar] == '}')
break;
sb.Append(_input[nextChar]);
}
_enterringActions = false;
return new ScanToken() { literal = sb.ToString(), tokenType = TokenType.Literal};
}
}
if (_insideVariableExpansionModifierSpan)
{
if (c == '=')
{
_insideVariableExpansionModifierSpan = false;
_insideVariableExpansionModifierValueSpan = true;
nextChar++;
var scanToken = new ScanToken() {tokenType = TokenType.Assignment, literal = c.ToString()};
return scanToken;
}
if (char.IsLetter(c) || c == '\\' || c == '/')
{
nextChar++;
var scanToken = new ScanToken() {tokenType = TokenType.VariableExpansionModifier, literal = c.ToString()};
return scanToken;
}
if (c == ')')
_insideVariableExpansionModifierSpan = false;
}
if (char.IsWhiteSpace(c))
return new ScanToken() {tokenType = TokenType.WhiteSpace, literal = ReadWhiteSpace()};
bool hasMoreCharacters = (nextChar + 1) < _input.Length;
if (c == '$' && hasMoreCharacters && _input[nextChar + 1] == '(')
{
++_insideVariableExpansionDepth;
nextChar += 2;
return new ScanToken() { tokenType = TokenType.VariableDereferencerOpen, literal = "$(" };
}
if (c == '@' && hasMoreCharacters && _input[nextChar + 1] == '(')
{
++_insideVariableExpansionDepth;
nextChar += 2;
return new ScanToken() { tokenType = TokenType.LiteralExpansionOpen, literal = "@(" };
}
if (c == ')')
{
bool processAsParenthesisClose = false;
if (_insideVariableExpansionDepth > 0)
{
--_insideVariableExpansionDepth;
processAsParenthesisClose = true;
}
if (_previouslyScannedTokens.Last().tokenType == TokenType.WhiteSpace && NextCharIfAnyIsWhiteSpace())
processAsParenthesisClose = true;
if (processAsParenthesisClose)
{
++nextChar;
return new ScanToken() {tokenType = TokenType.ParenthesisClose, literal = ")"};
}
}
if (c == '#')
{
ReadUntilEndOfLine();
return ScanToken();
}
////FIXME: do this properly; ReadLiteral should not recognize what isn't a literal
var oldPosition = nextChar;
var literal = ReadLiteral(allowColon: _insideVariableExpansionDepth==0);
var isUnquotedLiteral = (nextChar - oldPosition) == literal.Length;
if (isUnquotedLiteral && literal == ":" && !char.IsWhiteSpace(_input[nextChar]) && _insideVariableExpansionDepth>0)
_insideVariableExpansionModifierSpan = true;
var tokenType = TokenType.Literal;
if (isUnquotedLiteral)
{
bool hasWhitespaceBefore = (oldPosition == 0 || char.IsWhiteSpace(_input[oldPosition - 1]));
bool hasWhitespaceAfter = (nextChar == _input.Length || char.IsWhiteSpace(_input[nextChar]));
tokenType = TokenTypeFor(literal, hasWhitespaceAfter && hasWhitespaceBefore);
}
if (tokenType == TokenType.Actions)
_enterringActions = true;
return new ScanToken() {tokenType = tokenType, literal = literal};
}
private bool NextCharIfAnyIsWhiteSpace()
{
if (nextChar+1 >= _input.Length)
return true;
return char.IsWhiteSpace(_input[nextChar + 1]);
}
private TokenType TokenTypeFor(string literal, bool hasWhitespaceAround)
{
switch (literal)
{
case ";":
if (hasWhitespaceAround)
return TokenType.Terminator;
break;
case "[":
return TokenType.BracketOpen;
case "]":
return TokenType.BracketClose;
case ":":
if (_insideVariableExpansionDepth > 0 || hasWhitespaceAround)
return TokenType.Colon;
break;
case "{":
if (hasWhitespaceAround)
return TokenType.AccoladeOpen;
break;
case "}":
if (hasWhitespaceAround)
return TokenType.AccoladeClose;
break;
case "=":
if (_insideVariableExpansionModifierSpan || hasWhitespaceAround)
return TokenType.Assignment;
break;
case "if":
return TokenType.If;
case "rule":
return TokenType.Rule;
case "return":
return TokenType.Return;
case "actions":
return TokenType.Actions;
case "include":
case "Include":
return TokenType.Include;
case "on":
return TokenType.On;
case "!":
return TokenType.Not;
case "else":
return TokenType.Else;
case "while":
return TokenType.While;
case "+=":
return TokenType.AppendOperator;
case "-=":
return TokenType.SubtractOperator;
case "?=":
return TokenType.AssignmentIfEmpty;
case "<":
if (hasWhitespaceAround)
return TokenType.LessThan;
break;
case ">":
if (hasWhitespaceAround)
return TokenType.GreaterThan;
break;
case "for":
return TokenType.For;
case "in":
return TokenType.In;
case "continue":
return TokenType.Continue;
case "break":
return TokenType.Break;
case "case":
return TokenType.Case;
case "switch":
return TokenType.Switch;
case "local":
return TokenType.Local;
case "&&":
return TokenType.And;
case "||":
return TokenType.Or;
case "!=":
return TokenType.NotEqual;
}
return TokenType.Literal;
}
private StringBuilder _builder = new StringBuilder();
private string ReadLiteral(bool allowColon)
{
int i;
for (i = nextChar; i != _input.Length; i++)
{
//dont allow colons as the first character
bool reallyAllowCon = allowColon;// && nextChar != i;
bool hasMoreCharacters = (i + 1) < _input.Length;
char ch = _input[i];
if (ch == '\\' && hasMoreCharacters)
{
++i;
if ((_input[i] == '@' || _input[i] == '$') && (i + 1) < _input.Length && _input[i + 1] == '(')
break;
_builder.Append(_input[i]);
}
else if ((ch == '$' || ch == '@') && hasMoreCharacters && _input[i + 1] == '(')
{
Debug.Assert(i > nextChar);
break;
}
else if (ch == ')' && _insideVariableExpansionDepth > 0)
{
Debug.Assert(i > nextChar);
break;
}
else if (isInsideQuote && (_insideVariableExpansionDepth == 0 || char.IsWhiteSpace(ch)))
{
if (ch == '"')
{
isInsideQuote = false;
} else
_builder.Append(ch);
}
else if (ch == '"')
{
isInsideQuote = !isInsideQuote;
}
else if (IsLiteral(ch, reallyAllowCon) || ((ch == '$' || ch == '@') && hasMoreCharacters && _input[i + 1] != '(')) // Prevent single $ inside literal being treated as DereferenceVariable token.
{
_builder.Append(ch);
}
else
{
break;
}
}
// Return special characters recognized by TokenForLiteral from here
// even though that doesn't make any sense.
if (i == nextChar)
{
nextChar++;
return _input[i].ToString();
}
var result = _builder.ToString();
_builder.Clear();
nextChar = i;
return result;
}
private bool IsLiteral(char c, bool treatColonAsLiteral)
{
if (c == '}')
return false;
if (c == '{')
return false;
if (c == ':')
return treatColonAsLiteral;
if (c == '[')
return false;
if (c == ']')
return false;
if (c == '#')
return false;
return !char.IsWhiteSpace(c);
}
private string ReadWhiteSpace()
{
bool? wasPreviousWhiteSpaceNewLine = null;
for (int i = nextChar; i != _input.Length; i++)
{
bool isNewLine = IsNewLineCharacter(_input[i]);
if (!char.IsWhiteSpace(_input[i]) || (wasPreviousWhiteSpaceNewLine.HasValue && isNewLine != wasPreviousWhiteSpaceNewLine.Value))
{
var result = _input.Substring(nextChar, i - nextChar);
nextChar = i;
return result;
}
wasPreviousWhiteSpaceNewLine = isNewLine;
}
var result2 = _input.Substring(nextChar);
nextChar = _input.Length;
return result2;
}
private void ReadUntilEndOfLine()
{
bool inNewLineSequence = false;
for (int i = nextChar; i != _input.Length; i++)
{
var c = _input[i];
if (IsNewLineCharacter(c))
inNewLineSequence = true;
else
{
if (inNewLineSequence)
{
nextChar = i;
return;
}
}
}
nextChar = _input.Length;
}
private static readonly char[] s_NewLineCharacters = {'\n', (char)0x0d, (char)0x0a};
public static bool IsNewLineCharacter(char c)
{
return s_NewLineCharacters.Contains(c);
}
}
public class ScanToken : IEquatable<ScanToken>
{
public TokenType tokenType;
public string literal;
public ScanToken Is(TokenType tokenType)
{
if (this.tokenType != tokenType)
throw new ParsingException(string.Format("Expected token {0}, but got {1}", tokenType, this.tokenType));
return this;
}
public bool Equals(ScanToken other)
{
return (other != null && other.literal == literal && other.tokenType == tokenType);
}
}
public enum TokenType
{
Literal,
Terminator,
WhiteSpace,
BracketClose,
Colon,
BracketOpen,
VariableDereferencerOpen,
LiteralExpansionOpen,
ParenthesisClose,
Assignment,
AccoladeOpen,
AccoladeClose,
If,
Rule,
VariableExpansionModifier,
Return,
AppendOperator,
Actions,
On,
EOF,
Not,
Else,
While,
SubtractOperator,
For,
In,
Continue,
Break,
Switch,
Case,
Local,
AssignmentIfEmpty,
And,
Or,
NotEqual,
Include,
GreaterThan,
LessThan
}
}