-
Notifications
You must be signed in to change notification settings - Fork 0
/
Lexer.cs
343 lines (315 loc) · 11.4 KB
/
Lexer.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
/*
(C) Neil Mitchell 2012
This module is licensed under the BSD license
*/
namespace VisualStudio.Haskell
{
using System;
using System.Linq;
using System.Collections.Generic;
using System.Diagnostics;
static class Lexer
{
public enum LineState
{
Normal,
MultilineComment1, // 1 nesting depth
MultilineComment2, // 2 nesting depth
MultilineComment3,
// more, but just index by integer
}
public enum Token
{
OpenParen,
CloseParen,
OpenSquare,
CloseSquare,
OpenCurly,
CloseCurly,
Unknown = 100,
String,
Char,
Comment,
Keyword,
}
public struct Lexeme
{
public int Start;
public int Length;
public Token Token;
}
public static bool IsBracket(Token t){return (int) t < 100;}
public static bool IsLeftBracket(Token t){return (int) t < 100 && (int) t % 2 == 0;}
public static bool IsRightBracket(Token t){return (int) t < 100 && (int) t % 2 == 1;}
public static Token AlternateBracket(Token t)
{
int i = (int) t;
if (i >= 100) return t;
return (Token) (i % 2 == 0 ? i + 1 : i - 1);
}
public static bool IsComment(Token t)
{
return t == Token.Comment;
}
public static bool IsString(Token t)
{
return t == Token.String || t == Token.Char;
}
public static bool IsKeyword(Token t)
{
return t == Token.Keyword;
}
private const string AscSymbols = "!#$%&*+./<=>?@\\^|-~:";
private static bool IsWhite(char c){return char.IsWhiteSpace(c);}
private static bool IsSymbol(char c){return char.IsSymbol(c) || AscSymbols.IndexOf(c) != -1;}
private static bool IsIdentStart(char c){return char.IsLetter(c) || c == '_';}
private static bool IsIdentCont(char c){return char.IsLetterOrDigit(c) || c == '_' || c == '\'';}
private static HashSet<string> KeywordIds = new HashSet<string>(new string[]
{"case","class","data","deriving","do","else","forall","if","import","in","infix"
,"infixl","infixr","instance","let","module","newtype","of","then","type","where"});
private static HashSet<string> KeywordImport = new HashSet<string>(new string[]
{"hiding","qualified","as"});
private static HashSet<string> KeywordSyms = new HashSet<string>(new string[]
{"->","<-","::","@","=","|","\\"});
private static List<string> SplitText(string s)
{
// split the text up, into words, using the rough same algorithm as VS
// divide into 3 classes, space, alphanum_, other
// take consecutive spans of each one
// Used for inside comments and strings
Func<char, int> Classify = c =>
char.IsWhiteSpace(c) ? 1 :
char.IsLetterOrDigit(c) || c == '_' ? 2 :
3;
var res = new List<string>();
if (s.Length == 0) return res;
int start = 0;
int last = Classify(s[0]);
for (int i = 1; i < s.Length; i++)
{
var now = Classify(s[i]);
if (now == last) continue;
res.Add(s.Substring(start, i - start));
start = i;
last = now;
}
res.Add(s.Substring(start));
return res;
}
private static void MultilineComment(string s, ref int Index, ref int Depth)
{
for (int i = Index; i < s.Length; i++)
{
var c = s[i];
if (c == '{' && i + 1 < s.Length && s[i+1] == '-')
Depth++;
else if (c == '}' && i > Index && s[i-1] == '-')
{
Depth--;
if (Depth == 0)
{
Index = i+1;
return;
}
}
}
Index = s.Length;
}
public static Lexeme[] Forward(string Text, ref LineState state)
{
var res = new List<Lexeme>();
var i = 0;
var isImport = false; // True if import keywords are allowed
if (state >= LineState.MultilineComment1)
{
int Depth = (int) state - (int) LineState.MultilineComment1 + 1;
MultilineComment(Text, ref i, ref Depth);
int j = 0;
foreach (var s in SplitText(Text.Substring(0, i)))
{
var r = new Lexeme();
r.Start = j;
r.Length = s.Length;
j += s.Length;
r.Token = Token.Comment;
if (!char.IsWhiteSpace(s[0]))
res.Add(r);
}
if (Depth != 0)
{
state = (LineState) ((int) LineState.MultilineComment1 + Depth - 1);
return res.ToArray();
}
state = LineState.Normal;
}
for (; i < Text.Length; )
{
var c = Text[i];
if (char.IsWhiteSpace(c))
{
i++;
continue;
}
var r = new Lexeme();
r.Start = i;
r.Length = 1;
r.Token = Token.Unknown;
isImport = isImport && (c != '(') && (c != ';');
if (IsSymbol(c))
{
int n = 1;
while (i + n < Text.Length && IsSymbol(Text[i + n]))
n++;
string sym = Text.Substring(i, n);
if (!(sym.Length >= 2 && sym.Replace("-","").Length == 0))
{
r.Token = KeywordSyms.Contains(sym) ? Token.Keyword : Token.Unknown;
r.Length = n;
}
else
{
foreach (var part in SplitText(Text.Substring(i)))
{
r = new Lexeme();
r.Start = i;
r.Length = part.Length;
r.Token = Token.Comment;
i += part.Length;
if (!char.IsWhiteSpace(part[0]))
res.Add(r);
}
break;
}
}
else if (IsIdentStart(c))
{
int n = 1;
while (i + n < Text.Length && IsIdentCont(Text[i + n]))
n++;
string ident = Text.Substring(i, n);
r.Token = KeywordIds.Contains(ident) ? Token.Keyword : Token.Unknown;
if (isImport && KeywordImport.Contains(ident)) r.Token = Token.Keyword;
if (ident == "import") isImport = true;
r.Length = n;
}
else if (c == '\"' || c == '\'')
{
int n = 1;
while (i + n < Text.Length)
{
if (Text[i+n] == c) {n++; break;}
if (Text[i+n] == '\\') n++;
n++;
}
n = Math.Min(Text.Length - i, n); // in case a trailing \ character
foreach (var part in SplitText(Text.Substring(i, n)))
{
r = new Lexeme();
r.Start = i;
r.Length = part.Length;
r.Token = c == '\"' ? Token.String : Token.Char;
i += part.Length;
if (!char.IsWhiteSpace(part[0]))
res.Add(r);
}
continue;
}
else if (c == '[') r.Token = Token.OpenSquare;
else if (c == ']') r.Token = Token.CloseSquare;
else if (c == '(') r.Token = Token.OpenParen;
else if (c == ')') r.Token = Token.CloseParen;
else if (c == '}') r.Token = Token.CloseCurly;
else if (c == '{' && Text.Length > i+1 && Text[i+1] == '-')
{
int Depth = 0;
int j = i;
MultilineComment(Text, ref i, ref Depth);
foreach (var s in SplitText(Text.Substring(j, i-j)))
{
var rr = new Lexeme();
rr.Start = j;
rr.Length = s.Length;
j += s.Length;
rr.Token = Token.Comment;
if (!char.IsWhiteSpace(s[0]))
res.Add(rr);
}
if (Depth != 0)
{
state = (LineState) ((int) LineState.MultilineComment1 + Depth - 1);
return res.ToArray();
}
continue;
}
else if (c == '{') r.Token = Token.OpenCurly;
res.Add(r);
i += Math.Max(1, r.Length);
}
return res.ToArray();
}
private enum TestResult {Norm, Key, Str, Com}
private static void Test(string command, LineState start, LineState end)
{
// first step, parse command
var cmds = command.Replace('|','\0').Replace("\0\0","|").Split('\0');
var enms = Enum.GetNames(typeof(TestResult));
var str = string.Join("", cmds.Where(s => !enms.Contains(s)).ToArray());
var st = start;
var res = Forward(str, ref st);
Assert(st == end);
var i = 0;
var expect = TestResult.Norm;
foreach (var c in cmds)
{
if (enms.Contains(c))
expect = (TestResult) Enum.Parse(typeof(TestResult), c);
else
{
var r = res[i++];
var y1 = str.Substring(r.Start, r.Length);
var y2 = c.Trim();
Assert(str.Substring(r.Start, r.Length) == c.Trim());
Assert((expect == TestResult.Key) == IsKeyword(r.Token));
Assert((expect == TestResult.Str) == IsString(r.Token));
Assert((expect == TestResult.Com) == IsComment(r.Token));
}
}
Assert(i == res.Length);
}
private static void Assert(bool b)
{
if (!b) throw new Exception();
}
public static void Test(string command)
{
Test(command, LineState.Normal, LineState.Normal);
}
public static void Test()
{
Console.WriteLine("Starting tests");
Test("Key|module |Norm|(|A|,|B|(|..|) |,|C|) |Key|where");
Test("test |Key|= |Str|\"|more|\"");
Test("test |Key|= |Str|\"|mor| stuff|\"");
Test("test |Key|= |Str|\"|mor");
Test("test |Key|= |Str|\'|mor| stuff|\' |Key|where");
Test("test |Key|= |Str|\"|mor| \\\"|stuff|\"");
Test("test |Key|= |Str|\"|mor| \\");
Test("hello |Com|-- |foo |bar");
Test("hello |Com|---------------------------------------");
Test("Key|\\|Norm|x |Key|-> |Norm|x |\\\\ |y");
Test("hello |Com|{------}|Norm|x");
Test("hello |Com|{-- |test", LineState.Normal, LineState.MultilineComment1);
Test("hello |Com|{-- |test |{-", LineState.Normal, LineState.MultilineComment2);
Test("Com|more |-}|Norm|test", LineState.MultilineComment1, LineState.Normal);
Test("Com|more |-}|test", LineState.MultilineComment2, LineState.MultilineComment1);
Test("more|-|}");
Test("{|x|-|}");
Test("Com|extra |stuff |goes |here|--", LineState.MultilineComment1, LineState.MultilineComment1);
Test("qualified |as |Key|= |Norm|as");
Test("Key|import |qualified |Norm|Prelude|.|Bar |Key|as |Norm|Foo");
Test("Key|import |qualified |Norm|Prelude|.|Bar |Key|hiding |Norm|(|as|)");
Console.WriteLine("Tests successful, enter to exit...");
Console.ReadLine();
}
}
}