-
Notifications
You must be signed in to change notification settings - Fork 621
/
Copy pathAstNode.ts
186 lines (151 loc) · 4.58 KB
/
AstNode.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
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
// See LICENSE in the project root for license information.
import type { Token } from './Tokenizer';
import { TextRange } from './TextRange';
export enum AstKind {
None = 'None',
Script = 'Script',
AndIf = 'AndIf',
Command = 'Command',
CompoundWord = 'CompoundWord',
VariableExpansion = 'VariableExpansion',
Text = 'Text'
}
/**
* Base class for all AST nodes.
*/
export abstract class AstBaseNode {
public readonly kind: AstKind = AstKind.None;
public range: TextRange | undefined;
/**
* Returns a diagnostic dump of the tree, showing the prefix/suffix/separator for
* each node.
*/
public getDump(indent: string = ''): string {
const nestedIndent: string = indent + ' ';
let result: string = indent + `- ${this.kind}:\n`;
const dumpText: string | undefined = this.getDumpText();
if (dumpText) {
result += nestedIndent + 'Value=' + JSON.stringify(dumpText) + '\n';
}
const fullRange: TextRange = this.getFullRange();
if (!fullRange.isEmpty()) {
result += nestedIndent + 'Range=' + JSON.stringify(fullRange.toString()) + '\n';
}
const childNodes: AstNode[] = this.getChildNodes();
for (const child of childNodes) {
result += child.getDump(nestedIndent);
}
return result;
}
public getChildNodes(): AstNode[] {
const nodes: AstNode[] = [];
this.collectChildNodesInto(nodes);
return nodes;
}
public getFullRange(): TextRange {
if (this.range) {
return this.range;
}
let encompassingRange: TextRange = TextRange.empty;
for (const child of this.getChildNodes()) {
encompassingRange = encompassingRange.getEncompassingRange(child.getFullRange());
}
return encompassingRange;
}
protected abstract collectChildNodesInto(nodes: AstNode[]): void;
protected getDumpText(): string | undefined {
return undefined;
}
}
/**
* Represents a complete script that can be executed.
*/
export class AstScript extends AstBaseNode {
public readonly kind: AstKind.Script = AstKind.Script;
public body: AstNode | undefined;
/** @override */
protected collectChildNodesInto(nodes: AstNode[]): void {
if (this.body) {
nodes.push(this.body);
}
}
}
/**
* Represents the "&&" operator, which is used to join two individual commands.
*/
export class AstAndIf extends AstBaseNode {
public readonly kind: AstKind.AndIf = AstKind.AndIf;
/**
* The command that executes first, and always.
*/
public firstCommand: AstCommand | undefined;
/**
* The command that executes second, and only if the first one succeeds.
*/
public secondCommand: AstCommand | undefined;
/** @override */
protected collectChildNodesInto(nodes: AstNode[]): void {
if (this.firstCommand) {
nodes.push(this.firstCommand);
}
if (this.secondCommand) {
nodes.push(this.secondCommand);
}
}
}
/**
* Represents a command. For example, the name of an executable to be started.
*/
export class AstCommand extends AstBaseNode {
public readonly kind: AstKind.Command = AstKind.Command;
public commandPath: AstCompoundWord | undefined;
public arguments: AstCompoundWord[] = [];
/** @override */
protected collectChildNodesInto(nodes: AstNode[]): void {
if (this.commandPath) {
nodes.push(this.commandPath);
}
nodes.push(...this.arguments);
}
}
/**
* Represents a compound word, e.g. "--the-thing" or "./the/thing".
*/
export class AstCompoundWord extends AstBaseNode {
public readonly kind: AstKind.CompoundWord = AstKind.CompoundWord;
public readonly parts: AstNode[] = [];
/** @override */
protected collectChildNodesInto(nodes: AstNode[]): void {
nodes.push(...this.parts);
}
}
/**
* Represents an environment variable expansion expression, e.g. "${VARIABLE}"
*/
export class AstVariableExpansion extends AstBaseNode {
public readonly kind: AstKind.VariableExpansion = AstKind.VariableExpansion;
/** @override */
protected collectChildNodesInto(nodes: AstNode[]): void {
// no children
}
}
/**
* Represents some plain text.
*/
export class AstText extends AstBaseNode {
public readonly kind: AstKind.Text = AstKind.Text;
public token: Token | undefined;
/** @override */
protected collectChildNodesInto(nodes: AstNode[]): void {
// no children
}
/** @override */
protected getDumpText(): string | undefined {
if (this.token) {
return this.token.text;
}
return undefined;
}
}
export type AstNode = AstScript | AstAndIf | AstCommand | AstCompoundWord | AstVariableExpansion | AstText;