Skip to content

Commit 6aa0817

Browse files
authored
Implements shell redirections (#237)
* Adds shell redirection into the grammar * Starts implementing shell redirections * Adds more tests * Fixes linting
1 parent 49d0f6c commit 6aa0817

File tree

6 files changed

+611
-286
lines changed

6 files changed

+611
-286
lines changed

packages/berry-parsers/sources/grammars/shell.d.ts

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,31 @@
1-
export type CommandSegment = string |
2-
{type: `shell`, shell: ShellLine, quoted: boolean} |
3-
{type: `variable`, name: string, defaultValue?: Array<Array<CommandSegment>>, quoted: boolean};
1+
// From the top to the bottom:
2+
//
3+
// - A shell line is composed of multiple command lines, first separated by ";" and then chained by "&&" / "||"
4+
// - A command line is composed of multiple commands chained by "|" / "|&"
5+
// - A command is composed of multiple arguments separated by spaces
6+
// - An argument can be a value argument (sent to the underlying program), or a redirection
7+
// - A value argument is a combination of argument segments
8+
9+
export type ArgumentSegment =
10+
| {type: `text`, text: string}
11+
| {type: `shell`, shell: ShellLine, quoted: boolean}
12+
| {type: `variable`, name: string, defaultValue?: Array<ValueArgument>, quoted: boolean};
13+
14+
export type Argument =
15+
| {type: `redirection`, subtype: `>` | `<` | `>>` | `<<<`, args: Array<ValueArgument>}
16+
| ValueArgument;
17+
18+
export type ValueArgument =
19+
| {type: `argument`, segments: Array<ArgumentSegment>};
20+
21+
export type EnvSegment = {
22+
name: string,
23+
args: [] | [ValueArgument],
24+
};
425

526
export type Command = {
627
type: `command`,
7-
args: Array<Array<CommandSegment>>,
28+
args: Array<Argument>,
829
envs: Array<EnvSegment>,
930
} | {
1031
type: `subshell`,
@@ -36,9 +57,3 @@ export type CommandLineThen = {
3657
export type ShellLine = Array<CommandLine>;
3758

3859
export declare const parse: (code: string) => ShellLine;
39-
40-
export type EnvSegment = {
41-
name: string,
42-
args: Array<Array<CommandSegment>>,
43-
};
44-

0 commit comments

Comments
 (0)