summaryrefslogtreecommitdiffstats
path: root/src/shell.pest
blob: fce2c116236ad118148f6d63e5b3435cb088a59b (plain) (blame)
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
basic_escape_char = @{ "\\\\" | "\\'" }
escape_char       = @{ "\\" ~ ANY }

bareword_char = @{
    escape_char |
    !("|" | ";" | "\"" | "'" | "$" | WHITESPACE | COMMENT) ~ ANY
}
single_string_char = @{ basic_escape_char | (!"'" ~ ANY) }
double_string_char = @{ escape_char | (!"\"" ~ ANY) }

var = @{
    ("$" ~ XID_START ~ XID_CONTINUE*) |
    ("$" ~ ("?" | "$" | "*" | ASCII_DIGIT)) |
    ("${" ~ (!"}" ~ ANY)+ ~ "}")
}

redir_prefix  = @{ ASCII_DIGIT* ~ (">>" | ">" | "<") ~ WHITESPACE* }
bareword      = @{ bareword_char+ }
single_string = @{ single_string_char+ }
double_string = @{ double_string_char+ }

word     = ${
    redir_prefix? ~
    (var | bareword |
    "'" ~ single_string ~ "'" |
    "\"" ~ (var | double_string)+ ~ "\"")+
}

exe      = ${ word ~ (w ~ word)* }
pipeline = ${ exe ~ (w? ~ "|" ~ w? ~ exe)* }
commands = ${ pipeline ~ (w? ~ ";" ~ w? ~ pipeline)* }

line = ${ SOI ~ w? ~ commands ~ w? ~ EOI }

w          = _{ (WHITESPACE | COMMENT)+ }
WHITESPACE = _{ (" " | "\t" | "\n") }
COMMENT    = _{ "#" ~ ANY* }