22// See LICENSE in the project root for license information.
33
44import { Token } from './Tokenizer' ;
5-
6- export enum AstKind {
7- Script ,
8- AndIf ,
9- Command ,
10- CompoundWord ,
11- VariableExpansion ,
12- Text
5+ import { TextRange } from './TextRange' ;
6+
7+ export const enum AstKind {
8+ None = 'None' ,
9+ Script = 'Script' ,
10+ AndIf = 'AndIf' ,
11+ Command = 'Command' ,
12+ CompoundWord = 'CompoundWord' ,
13+ VariableExpansion = 'VariableExpansion' ,
14+ Text = 'Text'
1315}
1416
1517/**
1618 * Base class for all AST nodes.
1719 */
18- export abstract class AstNode {
19- public abstract get kind ( ) : AstKind ;
20+ export abstract class AstBaseNode {
21+ public readonly kind : AstKind = AstKind . None ;
22+ public range : TextRange | undefined ;
2023
2124 /**
2225 * Returns a diagnostic dump of the tree, showing the prefix/suffix/separator for
2326 * each node.
2427 */
2528 public getDump ( indent : string = '' ) : string {
26- let result : string = indent + AstKind [ this . kind ] ;
29+ const nestedIndent : string = indent + ' ' ;
30+ let result : string = indent + `- ${ this . kind } :\n` ;
2731
2832 const dumpText : string | undefined = this . getDumpText ( ) ;
2933 if ( dumpText ) {
30- result += '=' + JSON . stringify ( dumpText ) ;
34+ result += nestedIndent + 'Value=' + JSON . stringify ( dumpText ) + '\n' ;
35+ }
36+
37+ const fullRange : TextRange = this . getFullRange ( ) ;
38+ if ( ! fullRange . isEmpty ( ) ) {
39+ result += nestedIndent + 'Range=' + JSON . stringify ( fullRange . toString ( ) ) + '\n' ;
3140 }
3241
3342 const childNodes : AstNode [ ] = this . getChildNodes ( ) ;
34- if ( childNodes . length === 0 ) {
35- result += '\n' ;
36- } else {
37- result += ':\n' ;
38- for ( const child of this . getChildNodes ( ) ) {
39- result += child . getDump ( indent + ' ' ) ;
40- }
43+ for ( const child of childNodes ) {
44+ result += child . getDump ( nestedIndent ) ;
4145 }
4246
4347 return result ;
@@ -49,19 +53,34 @@ export abstract class AstNode {
4953 return nodes ;
5054 }
5155
56+ public getFullRange ( ) : TextRange {
57+ if ( this . range ) {
58+ return this . range ;
59+ }
60+
61+ let encompassingRange : TextRange = TextRange . empty ;
62+
63+ for ( const child of this . getChildNodes ( ) ) {
64+ encompassingRange = encompassingRange . getEncompassingRange ( child . getFullRange ( ) ) ;
65+ }
66+
67+ return encompassingRange ;
68+ }
69+
5270 protected abstract collectChildNodesInto ( nodes : AstNode [ ] ) : void ;
5371
5472 protected getDumpText ( ) : string | undefined {
5573 return undefined ;
5674 }
5775}
5876
59- export class AstScript extends AstNode {
60- public body : AstNode | undefined ;
77+ /**
78+ * Represents a complete script that can be executed.
79+ */
80+ export class AstScript extends AstBaseNode {
81+ public readonly kind : AstKind . Script = AstKind . Script ;
6182
62- public get kind ( ) : AstKind {
63- return AstKind . Script ;
64- }
83+ public body : AstNode | undefined ;
6584
6685 /** @override */
6786 protected collectChildNodesInto ( nodes : AstNode [ ] ) : void {
@@ -71,7 +90,12 @@ export class AstScript extends AstNode {
7190 }
7291}
7392
74- export class AstAndIf extends AstNode {
93+ /**
94+ * Represents the "&&" operator, which is used to join two individual commands.
95+ */
96+ export class AstAndIf extends AstBaseNode {
97+ public readonly kind : AstKind . AndIf = AstKind . AndIf ;
98+
7599 /**
76100 * The command that executes first, and always.
77101 */
@@ -82,10 +106,6 @@ export class AstAndIf extends AstNode {
82106 */
83107 public secondCommand : AstCommand | undefined ;
84108
85- public get kind ( ) : AstKind {
86- return AstKind . AndIf ;
87- }
88-
89109 /** @override */
90110 protected collectChildNodesInto ( nodes : AstNode [ ] ) : void {
91111 if ( this . firstCommand ) {
@@ -97,14 +117,15 @@ export class AstAndIf extends AstNode {
97117 }
98118}
99119
100- export class AstCommand extends AstNode {
120+ /**
121+ * Represents a command. For example, the name of an executable to be started.
122+ */
123+ export class AstCommand extends AstBaseNode {
124+ public readonly kind : AstKind . Command = AstKind . Command ;
125+
101126 public commandPath : AstCompoundWord | undefined ;
102127 public arguments : AstCompoundWord [ ] = [ ] ;
103128
104- public get kind ( ) : AstKind {
105- return AstKind . Command ;
106- }
107-
108129 /** @override */
109130 protected collectChildNodesInto ( nodes : AstNode [ ] ) : void {
110131 if ( this . commandPath ) {
@@ -114,35 +135,39 @@ export class AstCommand extends AstNode {
114135 }
115136}
116137
117- export class AstCompoundWord extends AstNode {
118- public readonly parts : AstNode [ ] = [ ] ;
138+ /**
139+ * Represents a compound word, e.g. "--the-thing" or "./the/thing".
140+ */
141+ export class AstCompoundWord extends AstBaseNode {
142+ public readonly kind : AstKind . CompoundWord = AstKind . CompoundWord ;
119143
120- public get kind ( ) : AstKind {
121- return AstKind . CompoundWord ;
122- }
144+ public readonly parts : AstNode [ ] = [ ] ;
123145
124146 /** @override */
125147 protected collectChildNodesInto ( nodes : AstNode [ ] ) : void {
126148 nodes . push ( ...this . parts ) ;
127149 }
128150}
129151
130- export class AstVariableExpansion extends AstNode {
131- public get kind ( ) : AstKind {
132- return AstKind . VariableExpansion ;
133- }
152+ /**
153+ * Represents an environment variable expansion expression, e.g. "${VARIABLE}"
154+ */
155+ export class AstVariableExpansion extends AstBaseNode {
156+ public readonly kind : AstKind . VariableExpansion = AstKind . VariableExpansion ;
134157
135158 /** @override */
136159 protected collectChildNodesInto ( nodes : AstNode [ ] ) : void {
137160 // no children
138161 }
139162}
140163
141- export class AstText extends AstNode {
164+ /**
165+ * Represents some plain text.
166+ */
167+ export class AstText extends AstBaseNode {
168+ public readonly kind : AstKind . Text = AstKind . Text ;
169+
142170 public token : Token | undefined ;
143- public get kind ( ) : AstKind {
144- return AstKind . Text ;
145- }
146171
147172 /** @override */
148173 protected collectChildNodesInto ( nodes : AstNode [ ] ) : void {
@@ -157,3 +182,5 @@ export class AstText extends AstNode {
157182 return undefined ;
158183 }
159184}
185+
186+ export type AstNode = AstScript | AstAndIf | AstCommand | AstCompoundWord | AstVariableExpansion | AstText ;
0 commit comments