Fix $or
[factor/jcg.git] / extra / shell / parser / parser.factor
blob2ecca6199c92ecd8ae4f724a297dab5027cbbffa
2 USING: kernel arrays strings sequences sequences.deep accessors peg peg.ebnf
3        newfx ;
5 IN: shell.parser
7 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
9 TUPLE: basic-expr         command  stdin stdout background ;
10 TUPLE: pipeline-expr      commands stdin stdout background ;
11 TUPLE: single-quoted-expr expr ;
12 TUPLE: double-quoted-expr expr ;
13 TUPLE: back-quoted-expr   expr ;
14 TUPLE: glob-expr          expr ;
15 TUPLE: variable-expr      expr ;
16 TUPLE: factor-expr        expr ;
18 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
20 : ast>basic-expr ( ast -- obj ) first4 basic-expr boa ;
22 : ast>pipeline-expr ( ast -- obj )
23   pipeline-expr new
24     over [ 1st ] [ 4th [ 1st ] map ] [ 5th ] tri suffix prefix-on >>commands
25     over 2nd >>stdin
26     over 6th   >>stdout
27     swap 7th   >>background ;
29 : ast>single-quoted-expr ( ast -- obj )
30   2nd >string single-quoted-expr boa ;
32 : ast>double-quoted-expr ( ast -- obj )
33   2nd >string double-quoted-expr boa ;
35 : ast>back-quoted-expr ( ast -- obj )
36   2nd >string back-quoted-expr boa ;
38 : ast>glob-expr ( ast -- obj ) flatten concat glob-expr boa ;
40 : ast>variable-expr ( ast -- obj ) 2nd variable-expr boa ;
42 : ast>factor-expr ( ast -- obj ) 2nd >string factor-expr boa ;
44 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
46 EBNF: expr
48 space = " "
50 tab   = "\t"
52 white = (space | tab)
54 _ = (white)* => [[ drop ignore ]]
56 sq = "'"
57 dq = '"'
58 bq = "`"
60 single-quoted = sq (!(sq) .)* sq => [[ ast>single-quoted-expr ]]
61 double-quoted = dq (!(dq) .)* dq => [[ ast>double-quoted-expr ]]
62 back-quoted   = bq (!(bq) .)* bq => [[ ast>back-quoted-expr   ]]
64 factor = "$(" (!(")") .)* ")" => [[ ast>factor-expr ]]
66 variable = "$" other => [[ ast>variable-expr ]]
68 glob-char = ("*" | "?")
70 non-glob-char = !(glob-char | white) .
72 glob-beginning-string = (non-glob-char)* => [[ >string ]]
74 glob-rest-string = (non-glob-char)+ => [[ >string ]]
76 glob = glob-beginning-string glob-char (glob-rest-string | glob-char)* => [[ ast>glob-expr ]]
78 other = (!(white | "&" | ">" | ">>" | "<" | "|") .)+ => [[ >string ]]
80 element = (single-quoted | double-quoted | back-quoted | factor | variable | glob | other)
82 command = (element _)+
84 to-file = ">"  _ other => [[ second ]]
85 in-file = "<"  _ other => [[ second ]]
86 ap-file = ">>" _ other => [[ second ]]
88 basic = _ command _ (in-file)? _ (to-file | ap-file)? _ ("&")? => [[ ast>basic-expr ]]
90 pipeline = _ command _ (in-file)? _ "|" _ (command _ "|" _)* command _ (to-file | ap-file)? _ ("&")? => [[ ast>pipeline-expr ]]
92 submission = (pipeline | basic)
94 ;EBNF