Skip to content
This repository was archived by the owner on May 21, 2019. It is now read-only.

Commit 4324a11

Browse files
committed
Pass tokens to BranchNode.
1 parent 922eae3 commit 4324a11

File tree

5 files changed

+65
-25
lines changed

5 files changed

+65
-25
lines changed

src/Autocompletion.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,6 @@ export const {getSuggestions} = new class {
7878
// };
7979

8080
getSuggestions = async (job: Job) => {
81-
const leaf = leafNodeAt(job.prompt.value.length, parse(scan(job.prompt.value)));
82-
debugger;
83-
return leaf.suggestions;
81+
return leafNodeAt(job.prompt.value.length, job.prompt.ast).suggestions;
8482
};
8583
};

src/Prompt.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import * as events from "events";
22
import {Job} from "./Job";
33
import {scan, Token} from "./shell/Scanner";
44
import {expandAliases} from "./shell/CommandExpander";
5+
import {ASTNode, parse} from "./shell/Parser2";
56

67
export class Prompt extends events.EventEmitter {
78
private _value = "";
@@ -19,13 +20,17 @@ export class Prompt extends events.EventEmitter {
1920
setValue(value: string): void {
2021
this._value = value;
2122
this._tokens = scan(this.value);
22-
this._expanded = expandAliases(this._tokens, this.job.session.aliases);
23+
this._expanded = []; // expandAliases(this._tokens, this.job.session.aliases);
2324
}
2425

2526
get tokens(): Token[] {
2627
return this._tokens;
2728
}
2829

30+
get ast(): ASTNode {
31+
return parse(this.tokens);
32+
}
33+
2934
get expanded(): Token[] {
3035
return this._expanded;
3136
}

src/shell/Parser2.ts

Lines changed: 43 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import {Token} from "./Scanner";
1+
import {Token, Empty} from "./Scanner";
22
import * as _ from "lodash";
33
import {Suggestion} from "../plugins/autocompletion_providers/Suggestions";
44

5-
abstract class ASTNode {
5+
export abstract class ASTNode {
66
abstract get fullStart(): number;
77
abstract get fullEnd(): number;
88
}
@@ -17,7 +17,7 @@ abstract class LeafNode extends ASTNode {
1717
}
1818

1919
get fullEnd(): number {
20-
return this.fullStart + this.token.raw.length - 1;
20+
return this.fullStart + this.token.raw.length;
2121
}
2222

2323
get value(): string {
@@ -28,12 +28,10 @@ abstract class LeafNode extends ASTNode {
2828
}
2929

3030
abstract class BranchNode extends ASTNode {
31-
readonly children: ASTNode[];
31+
abstract get children(): ASTNode[];
3232

33-
constructor(children: ASTNode[]) {
33+
constructor(protected childTokens: Token[]) {
3434
super();
35-
36-
this.children = children;
3735
}
3836

3937
get fullStart(): number {
@@ -46,21 +44,27 @@ abstract class BranchNode extends ASTNode {
4644
}
4745

4846
class CompleteCommand extends BranchNode {
47+
get children(): ASTNode[] {
48+
return [new Command(this.childTokens)];
49+
}
4950
}
5051

5152
class Command extends BranchNode {
52-
readonly commandWord: CommandWord;
53-
readonly argumentList: ArgumentList;
54-
55-
constructor(tokens: Token[]) {
56-
const commandWord = new CommandWord(tokens[0]);
53+
get children(): ASTNode[] {
54+
const children: ASTNode[] = [this.commandWord];
55+
if (this.childTokens.length > 1) {
56+
children.push(this.argumentList);
57+
}
5758

58-
const argumentList = tokens.length === 1 ? new EmptyArgumentList(() => this) : new ArgumentList(tokens.slice(1).map(token => new Argument(token, this)));
59+
return children;
60+
}
5961

60-
super([commandWord, argumentList]);
62+
get commandWord(): CommandWord {
63+
return new CommandWord(this.childTokens[0]);
64+
}
6165

62-
this.commandWord = commandWord;
63-
this.argumentList = argumentList;
66+
get argumentList(): ArgumentList | undefined {
67+
return new ArgumentList(this.childTokens.slice(1), this);
6468
}
6569
}
6670

@@ -71,6 +75,13 @@ class CommandWord extends LeafNode {
7175
}
7276

7377
class ArgumentList extends BranchNode {
78+
constructor(childTokens: Token[], private command: Command) {
79+
super (childTokens);
80+
}
81+
82+
get children(): ASTNode[] {
83+
return this.childTokens.map(token => new Argument(token, this.command));
84+
}
7485
}
7586

7687
class Argument extends LeafNode {
@@ -90,10 +101,22 @@ class Argument extends LeafNode {
90101
}
91102
}
92103

93-
export function parse(tokens: Token[]): CompleteCommand {
94-
return new CompleteCommand([
95-
new Command(tokens),
96-
]);
104+
class EmptyNode extends LeafNode {
105+
constructor() {
106+
super(new Empty());
107+
}
108+
109+
get suggestions(): Suggestion[] {
110+
return [];
111+
}
112+
}
113+
114+
export function parse(tokens: Token[]): ASTNode {
115+
if (tokens.length === 0) {
116+
return new EmptyNode();
117+
}
118+
119+
return new CompleteCommand(tokens);
97120
}
98121

99122
export function leafNodeAt(position: number, node: ASTNode): LeafNode {

src/shell/Scanner.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,20 @@ export abstract class Token {
1919
}
2020
}
2121

22+
export class Empty extends Token {
23+
constructor() {
24+
super("", 0);
25+
}
26+
27+
get value() {
28+
return "";
29+
}
30+
31+
get escapedValue() {
32+
return <EscapedShellWord>this.raw.trim();
33+
}
34+
}
35+
2236
export class Word extends Token {
2337
get value() {
2438
return this.raw.trim().replace(/\\\s/, " ");

test/shell/parser_spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ async function parse(parser: Parser, input: string) {
1919
));
2020
}
2121

22-
describe.only("parser", () => {
22+
describe("parser", () => {
2323
describe("sequence", () => {
2424
describe("git commit", () => {
2525
const git = string("git");

0 commit comments

Comments
 (0)