/
ast.base.ts
171 lines (133 loc) · 4.84 KB
/
ast.base.ts
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
import { Token } from '../token/token';
import { TokenHelper } from '../token/token.helper';
import { ParserError } from '../error';
import { TokenError } from '../token/token.error';
import { AbstractSyntaxTreeNode } from './ast.node';
export abstract class AbstractSyntaxTreeBase extends AbstractSyntaxTreeNode {
public findRoot(): this {
if (this.isRoot())
return this.value !== undefined || !this.leftNode
? this
: this.leftNode;
return this._parent.findRoot();
}
public isRoot(): boolean {
return !this._parent;
}
public isValid(): boolean {
return this.value && (!this.leftNode && !this.rightNode) || (!!this.leftNode && !!this.rightNode);
}
public hasOpenBracket(): boolean {
if (TokenHelper.isBracketOpen(this.value))
return true;
const leftNodeHasOpenBracket = this.leftNode ? this.leftNode.hasOpenBracket() : false;
const rightNodeHasOpenBracket = this.rightNode ? this.rightNode.hasOpenBracket() : false;
return leftNodeHasOpenBracket || rightNodeHasOpenBracket;
}
private findOpenedBracket(): this {
if (this.isRoot())
return undefined;
if (TokenHelper.isBracketOpen(this._value))
return this;
return this._parent.findOpenedBracket();
}
public removeRootBracket(): this {
const rootNode = this.findRoot();
if (TokenHelper.isBracketOpen(rootNode.value))
rootNode.leftNode.removeParent();
return this === rootNode
? rootNode.leftNode
: this;
}
public removeClosestBracket(): this {
const node = this.findOpenedBracket();
if (!node)
throw new ParserError(TokenError.missingOpenBracket);
const targetNode = node.leftNode;
targetNode.subType = Token.SubType.Group;
if (!node.parent) {
targetNode.removeParent();
return targetNode;
}
if (node.parent.leftNode === node)
node.parent.leftNode = targetNode;
else
node.parent.rightNode = targetNode;
return node.parent;
}
private climbUp(token: Token.Token): this {
return this.isClimbTop(token)
? this
: this._parent.climbUp(token);
}
private isClimbTop(token: Token.Token) {
return this.isTokenHighest(token) ||
!this.parent ||
TokenHelper.isBracketOpen(this.value);
}
private isTokenHighest(token: Token.Token) {
return TokenHelper.getPrecedenceDiff(token, this.value) > 0 && this.subType !== Token.SubType.Group;
}
private createChildNode(value?: Token.Token): this {
const node = new (<any>this.constructor)(value);
node.parent = this;
return node;
}
private createParentNode(value?: Token.Token): this {
const node = new (<any>this.constructor)(value);
this.parent = node;
return node;
}
private insertOperatorNode(value: Token.Token) {
const rootNode = this.climbUp(value);
if (TokenHelper.isBracketOpen(rootNode.value))
return rootNode.insertJointNodeToLeft(value);
if (this.needJointRight(rootNode, value))
return rootNode.insertJointNodeToRight(value);
const newNode = rootNode.createParentNode(value);
newNode.leftNode = rootNode;
return newNode;
}
private needJointRight(rootNode: this, value: Token.Token) {
return rootNode.isTokenHighest(value) && rootNode.parent || this === rootNode;
}
public insertNode(value: Token.Token): this {
if (TokenHelper.isSymbol(value))
if (!this.value) {
this.value = value;
return this;
}
if (TokenHelper.isOperator(value))
return this.insertOperatorNode(value);
const valueNode = this.createChildNode(value);
if (!this.leftNode)
this.leftNode = valueNode;
else
this.rightNode = valueNode;
return valueNode;
}
private insertJointNodeToLeft(value: Token.Token) {
const jointNode = this.createChildNode(value);
jointNode.leftNode = this.leftNode;
jointNode.rightNode = this.rightNode;
this.leftNode = jointNode;
return jointNode;
}
public insertJointNodeToRight(value: Token.Token) {
const jointNode = this.createChildNode(value);
jointNode.leftNode = this.rightNode;
this.rightNode = jointNode;
return jointNode;
}
public removeLeftNode() {
this._leftNode.removeParent();
this._leftNode = undefined;
}
public removeRightNode() {
this._rightNode.removeParent();
this._rightNode = undefined;
}
public removeParent() {
this._parent = undefined;
}
}