1 USING: accessors kernel multiline peg peg.ebnf sequences
2 sequences.deep strings ;
6 TUPLE: basic-expr command stdin stdout background ;
7 TUPLE: pipeline-expr commands stdin stdout background ;
8 TUPLE: single-quoted-expr expr ;
9 TUPLE: double-quoted-expr expr ;
10 TUPLE: back-quoted-expr expr ;
11 TUPLE: glob-expr expr ;
12 TUPLE: variable-expr expr ;
13 TUPLE: factor-expr expr ;
15 : ast>basic-expr ( ast -- obj )
16 first4 basic-expr boa ;
18 : ast>pipeline-expr ( ast -- obj )
20 over [ first ] [ fourth [ first ] map ] [ 4 swap nth ] tri
21 suffix swap prefix >>commands
23 over 5 swap nth >>stdout
24 swap 6 swap nth >>background ;
26 : ast>single-quoted-expr ( ast -- obj )
27 second >string single-quoted-expr boa ;
29 : ast>double-quoted-expr ( ast -- obj )
30 second >string double-quoted-expr boa ;
32 : ast>back-quoted-expr ( ast -- obj )
33 second >string back-quoted-expr boa ;
35 : ast>glob-expr ( ast -- obj )
36 flatten concat glob-expr boa ;
38 : ast>variable-expr ( ast -- obj )
39 second variable-expr boa ;
41 : ast>factor-expr ( ast -- obj )
42 second >string factor-expr boa ;
52 _ = (white)* => [[ drop ignore ]]
58 single-quoted = sq (!(sq) .)* sq => [[ ast>single-quoted-expr ]]
59 double-quoted = dq (!(dq) .)* dq => [[ ast>double-quoted-expr ]]
60 back-quoted = bq (!(bq) .)* bq => [[ ast>back-quoted-expr ]]
62 factor = "$(" (!(")") .)* ")" => [[ ast>factor-expr ]]
64 variable = "$" other => [[ ast>variable-expr ]]
66 glob-char = ("*" | "?")
68 non-glob-char = !(glob-char | white) .
70 glob-beginning-string = (non-glob-char)* => [[ >string ]]
72 glob-rest-string = (non-glob-char)+ => [[ >string ]]
74 glob = glob-beginning-string glob-char (glob-rest-string | glob-char)* => [[ ast>glob-expr ]]
76 other = (!(white | "&" | ">" | ">>" | "<" | "|") .)+ => [[ >string ]]
78 element = (single-quoted | double-quoted | back-quoted | factor | variable | glob | other)
80 command = (element _)+
82 to-file = ">" _ other => [[ second ]]
83 in-file = "<" _ other => [[ second ]]
84 ap-file = ">>" _ other => [[ second ]]
86 basic = _ command _ (in-file)? _ (to-file | ap-file)? _ ("&")? => [[ ast>basic-expr ]]
88 pipeline = _ command _ (in-file)? _ "|" _ (command _ "|" _)* command _ (to-file | ap-file)? _ ("&")? => [[ ast>pipeline-expr ]]
90 submission = (pipeline | basic)