-
Notifications
You must be signed in to change notification settings - Fork 3
/
CommandNode.ts
70 lines (56 loc) · 2.21 KB
/
CommandNode.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
import { ASTNode, ASTNodeContext, ASTNodeParser } from "./ASTNode";
import { CurlyBracesParameterBlockNode } from "./CurlyBracesParameterBlockNode";
import { SquareBracesParameterBlockNode } from "./SquareBracesParameterBlockNode";
import { EmptyASTValue, EMPTY_AST_VALUE } from "../LatexParser";
import { ASTSyncVisitor, ASTAsyncVisitor } from "../visitors/visitors";
import { SourceFilePosition } from "../../source-files/SourceFilePosition";
type NonEmptyCommandNodeParameters = (
| CurlyBracesParameterBlockNode
| SquareBracesParameterBlockNode
)[];
export type CommandNodeParameters = (
| CurlyBracesParameterBlockNode
| SquareBracesParameterBlockNode
| EmptyASTValue
)[];
export class CommandNode extends ASTNode {
static readonly type = "command" as const;
readonly type: string = CommandNode.type;
readonly name: string;
readonly parameters: CommandNodeParameters;
protected parser: ASTNodeParser<CommandNode>;
protected readonly isLeaf: boolean = false;
constructor(
name: string,
parameters: CommandNodeParameters,
context: ASTNodeContext,
parser: ASTNodeParser<CommandNode>
) {
super(context);
this.name = name;
this.parameters = parameters;
this.parser = parser;
}
get nameEnd(): SourceFilePosition {
return this.range.from.withTranslation({ column: this.name.length + 1 });
}
get childNodes(): ASTNode[] {
return this.parameters
.filter(parameter => parameter !== EMPTY_AST_VALUE) as NonEmptyCommandNodeParameters;
}
toString(): string {
return `Command [\\${this.name}]`;
}
protected async updateWith(reparsedNode: CommandNode): Promise<void> {
super.updateWith(reparsedNode);
const writeableSelf = this as Writeable<this>;
writeableSelf.name = reparsedNode.name;
writeableSelf.parameters = reparsedNode.parameters;
}
protected syncSelfVisitWith(visitor: ASTSyncVisitor, depth: number = 0): void {
visitor.visitCommandNode(this, depth);
}
protected async asyncSelfVisitWith(visitor: ASTAsyncVisitor, depth: number = 0): Promise<void> {
await visitor.visitCommandNode(this, depth);
}
}