-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAST.cs
100 lines (85 loc) · 2.73 KB
/
AST.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
using ORM.Lexicography.Tokens;
namespace ORM.AST
{
class Node
{
private readonly Token token;
private List<Node> children;
private List<Lexicography.Token> tokens;
private Node? next;
public Node(Token token, List<Lexicography.Token> tokens)
{
this.token = token;
this.tokens = tokens;
this.children = new List<Node>();
this.next = null;
this.Organize();
}
public void Append(Node node)
{
var currentNode = this;
while (currentNode.next != null)
{
currentNode = currentNode.next;
}
currentNode.next = node;
}
public List<Node> Children()
{
return this.children;
}
public void AddChild(AST.Node node)
{
this.children.Add(node);
}
private void Organize()
{
if (this.token == Token.SELECT)
{
// Only keep OBJECT_PROPERTY tokens.
this.tokens = this.tokens.Where(t => t.Type() == Token.OBJECT_PROPERTY).ToList();
// For every OBJECT_PROPERTY, append as child.
foreach (var token in this.tokens)
{
var node = new Node(Token.OBJECT_PROPERTY, new List<Lexicography.Token>() { token });
this.children.Add(node);
}
// Remove OBJECT_PROPERTY tokens because information has been moved to children.
this.tokens.Clear();
}
else if (this.token == Token.FROM)
{
// Verify if only one token is passed as parameter OR the token is not identified as a word.
if (this.tokens.Count != 1 || this.tokens[0].Type() != Token.WORD)
{
throw new Exception("FROM: incorrect tokens or token type is not word.");
}
var node = new Node(Token.WORD, new List<Lexicography.Token>() { this.tokens[0] });
this.children.Add(node);
// Remove WORD token because information has been moved to child.
this.tokens.Clear();
}
}
public Node Last()
{
var current = this;
while (current.next != null)
{
current = current.next;
}
return current;
}
public Node? NextNode()
{
return this.next;
}
public List<Lexicography.Token> GetTokens()
{
return this.tokens;
}
public Token TokenType()
{
return this.token;
}
}
}