generated from PythonCoderAS/typescript-project-template
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnode.ts
218 lines (184 loc) · 4.72 KB
/
node.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
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
/**
* Parameters for constructing a new Node object.
*/
export interface NodeConstructorParams {
name: string;
attributes?: { [key: string]: string };
value?: string;
}
/**
* The abstract base class for all nodes.
*/
export abstract class BaseNode {
/**
* The name of the node.
*/
abstract name: string;
/**
* Clones the code, creating a new deep-copied node.
*/
abstract clone(): BaseNode;
/**
* Converts the node into a textual representation of the node as BBCode.
*/
abstract toString(): string;
}
/**
* An interface for nodes that can hold children.
*/
export interface ChildrenHolder {
/**
* The children of the node.
*/
children: BaseNode[];
/**
* Adds a child to the node. If the node is a {@link TextNode}, attempts to flatten the node with an previous TextNode.
* @param child The child to add.
*/
addChild(child: BaseNode): void;
}
/**
* An interface for nodes that can hold attributes.
*/
export interface AttributeHolder {
/**
* The attributes of the node.
*/
attributes: { [key: string]: string };
/**
* Adds an attribute to the node.
* @param key The name of the attribute.
* @param value The value of the attribute.
*/
setAttribute(key: string, value: string): void;
}
/**
* A BBCode node. This represents a tag and it's children.
*/
export class Node extends BaseNode implements ChildrenHolder, AttributeHolder {
/**
* The name of the tag. If a Node is used to represent a `[b]` tag, `name` will be `b`.
*/
name: string;
attributes: { [key: string]: string };
children: BaseNode[] = [];
/**
* Stores the [simple parameterized value](https://www.bbcode.org/reference.php) of the tag.
* @example `[b=red]Hello World![/b]` -> `red`
*/
value?: string;
constructor(params: NodeConstructorParams) {
super();
this.name = params.name;
this.attributes = params.attributes || {};
this.value = params.value;
}
clone(): Node {
const node = new Node({
name: this.name,
attributes: this.attributes,
value: this.value,
});
node.children = this.children.map((child) => child.clone());
return node;
}
addChild(child: BaseNode): void {
if (child instanceof TextNode) {
const previousChild = this.children[this.children.length - 1];
if (previousChild instanceof TextNode) {
// We flatten the text nodes.
previousChild.text += child.text;
return;
}
}
this.children.push(child.clone());
}
/**
* Sets the value of the node.
* @param value
*/
setValue(value: string): void {
this.value = value;
}
setAttribute(key: string, value: string): void {
this.attributes[key] = value;
}
toString(): string {
let nodeString = `[${this.name}`;
if (this.value) {
nodeString += `=${this.value}`;
}
Object.entries(this.attributes).forEach(([key, value]) => {
nodeString += ` ${key}=${value}`;
});
nodeString += "]";
this.children.forEach((child) => {
nodeString += child.toString();
});
nodeString += `[/${this.name}]`;
return nodeString;
}
}
/**
* A node that represents a chunk of text. This node is *not* a tag.
*/
export class TextNode extends BaseNode {
/**
* The text of the node.
*/
text: string;
name = "TextNode";
constructor(text: string) {
super();
this.text = text;
}
clone(): TextNode {
return new TextNode(this.text);
}
toString(): string {
return this.text;
}
}
/**
* The root node that represents the head of the AST. It only stores children.
*/
export class RootNode extends BaseNode implements ChildrenHolder {
name = "RootNode";
children: BaseNode[];
constructor(children: BaseNode[] = []) {
super();
this.children = children;
}
addChild(child: BaseNode): void {
if (child instanceof TextNode) {
const previousChild = this.children[this.children.length - 1];
if (previousChild instanceof TextNode) {
// We flatten the text nodes.
previousChild.text += child.text;
return;
}
}
this.children.push(child.clone());
}
clone(): RootNode {
return new RootNode(this.children.map((child) => child.clone()));
}
/**
* The textual representation of the BBCode AST. It should return a string equivalent to the input to {@link Parser.parse}.
*/
toString(): string {
return this.children.map((child) => child.toString()).join("");
}
}
/**
* A node that represents a list item. It is similar to the root node.
*/
export class ListItemNode extends RootNode {
name = "*";
toString(): string {
return `[*]${super.toString()}`;
}
clone(): ListItemNode {
return new ListItemNode(this.children.map((child) => child.clone()));
}
}