-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBdlLexicalScanner.cs
More file actions
71 lines (61 loc) · 1.73 KB
/
BdlLexicalScanner.cs
File metadata and controls
71 lines (61 loc) · 1.73 KB
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
namespace bdl
{
public class BdlLexicalScanner
{
public Queue<string> Tokenize(string query)
{
var tokens = new Queue<string>();
var chars = query.ToCharArray();
for (int i = 0; i < chars.Length; i++)
{
var c = chars[i];
if (IsDigit(c))
{
tokens.Enqueue(ReadNumber(chars, ref i));
}
else if (IsLetter(c))
{
tokens.Enqueue(ReadWord(chars, ref i));
}
else
{
tokens.Enqueue(new string(c, 1));
}
}
return tokens;
}
private string ReadWord(char[] chars, ref int i)
{
var start = i;
while (i+1 < chars.Length && IsLetter(chars[i+1]))
{
i++;
}
var length = i - start +1;
return new string(chars, start, length).ToLowerInvariant();
}
private bool IsLetter(char c)
{
return char.IsLetter(c);
}
private string ReadNumber(char[] chars, ref int i)
{
var start = i;
while (i+1 < chars.Length && IsDigit(chars[i+1]))
{
i++;
}
var length = i - start + 1;
return new string(chars, start, length);
}
private bool IsDigit(char c)
{
return char.IsDigit(c);
}
}
}