-
Notifications
You must be signed in to change notification settings - Fork 1
/
grammar.peg
186 lines (158 loc) · 5.4 KB
/
grammar.peg
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
start
= pipelines
// @ parser
// ~~~~~~~~
letter = [a-zA-Z0-9_-~/]
space = [ \n\t\r]*
comma = space "," space
dot = space "." space { return 'get'; }
col = space ":" space { return 'map'; }
open_bracket = space "[" space
close_bracket = space "]" space
at = space "@" space
at_word = ls:letter+ { return ls.join(''); }
at_array = open_bracket
values:(
head:at_expression
tail:(comma t:at_expression { return t; })*
{ return [head].concat(tail); }
)
close_bracket { return values; }
okv =
k:at_word col v:at_expression { return {key: k, val: v}; }
/ k:sub_cmd col v:at_expression { return {key: [k], val: v}; }
/ k:at_word { return {key: k, val: [{get: k}]}; }
at_obj = "{" space ss:(okv comma?)+ space "}" {
ss = ss.map(function(sp) { return sp[0]; });
return {obj: ss};
}
section = at_obj / at_array / at_word
do_sub = (s:dot t:sub_cmd { return t; })
do_sub_ = (s:dot? t:sub_cmd { return t; })
do_get = (s:dot t:section { return {get: t}; })
do_get_ = (s:dot? t:section { return {get: t}; }) / (s:dot { return {get: '.'}; })
do_map = (s:col+ t:section { return {map: t, depth:s.length}; })
do_section = do_get / do_map
at_expression =
space
head:(do_get_/do_sub_/do_map)
tail:do_section*
space
{
return [head].concat(tail);
}
// Pipeline parser
// ~~~~~~~~~~~~~~~
pipe = space "|" space
ppipe = space "||" space
spipe = space "|=" space
squote = space ['] space
dquote = space ["] space
squote_ = space [']
_squote = ['] space
dquote_ = space ["]
_dquote = ["] space
decimal = "." ds:[0-9]+ { n = Number(ds.join('')); return n/Math.pow(10.0, ds.length); }
number = s:"-"? ds:[0-9]+ dec:decimal?
{ n = Number(ds.join('')); n += dec || 0; if (s) n = n*-1; return n; }
bool = "true" / "false"
ok_char = letter / [=:/.#%!?+*%^&]
escd_at = (l1:letter "@" l2:letter { return l1+'@'+l2; } / "\\@" { return "@"; })
escd_paren = "\\(" { return "("; } / "\\)" { return ")"; }
escd_dolla = "\\$"
escd_char = escd_at / escd_paren / escd_dolla
letter_word = ls:letter+ { return ls.join(''); }
var_word = "$" w:letter_word { return '$' + w; }
/ "$!"
cmd_char = escd_char / ok_char / var_word
cmd_word = cs:cmd_char+ { return cs.join('').trim(); }
squoted_cmd = squote cmd:cmd squote { return {quoted: cmd}; }
dquoted_cmd = dquote cmd:cmd dquote { return {quoted: cmd}; }
quoted_cmd = squoted_cmd / dquoted_cmd
squoted_literal = squote_ ls:("\\n" { return '\n'; } / escd_char / [^'] )* _squote { return {quoted: [ls.join('')] }; }
dquoted_literal = dquote_ ls:("\\n" { return '\n'; } / escd_char / [^"] )* _dquote { return {quoted: [ls.join('')] }; }
quoted_literal = squoted_literal / dquoted_literal
valid_key = letter_word/quoted_literal
quoted_phrase = quoted_literal / quoted_cmd
cmd_phrase = quoted_phrase / cmd_word / sub_cmd
bool_word = "true" / "false"
cmd = obj_cmd / list_cmd /
head:cmd_phrase
tail:(space c:cmd_phrase { return c; })*
{ return [head].concat(tail); }
cmd_at =
cmd:cmd
atexp:(at atexp:at_expression { return atexp; })?
{ return {cmd: cmd, at: atexp}; }
/ atexp:(at atexp:at_expression { return atexp; })
{ return {at: atexp}; }
var_exp =
var_word:var_word
atexp:(at atexp:at_expression { return atexp; })?
{ return {var: var_word, at: atexp}; }
start_cmd =
var_exp
/ number:number { return {val: number}; }
/ quoted:quoted_phrase { return {val: quoted}; }
/ bool:bool_word { return {val: bool == 'true'}; }
/ cmd_at:cmd_at { cmd_at.type = 'pipe'; return cmd_at; }
piped_cmd =
pipe cmd_at:cmd_at
{ cmd_at.type = 'pipe'; return cmd_at; }
ppiped_cmd =
ppipe cmd_at:cmd_at
{ cmd_at.type = 'ppipe'; return cmd_at; }
spiped_cmd =
spipe cmd_at:cmd_at
{ cmd_at.type = 'spipe'; return cmd_at; }
cmd_section = start_cmd / ppiped_cmd / spiped_cmd / piped_cmd
sub_cmd =
"$(" pipelines:pipelines ")"
{ return {sub:pipelines}; }
/ obj_cmd:obj_cmd
{ return {sub: [[{cmd:obj_cmd}]]}; }
/ list_cmd:list_cmd
{ return {sub: [[{cmd:list_cmd}]]}; }
pipeline =
aliasing / setting /
space
head:cmd_section
tail:cmd_section*
space
{ if (head) return [head].concat(tail); else return tail; }
semi = space ";" space
pipelines =
head:pipeline
tail:(semi pipeline:pipeline { return pipeline; })*
{ return [head].concat(tail); }
aliasing =
space "alias" space alias:letter_word space "=" space rest:[^;]+
{ return [{cmd: ['alias', alias, rest.join('').trim()]}]; }
setup = var_word:var_word space "=" space { return var_word; }
setting =
space set:setup pipeline:pipeline
{ return [{cmd: ['set', set.slice(1), {sub: [pipeline]}]}]; }
/ space set:setup
{ return [{cmd: ['set', set.slice(1)]}]; }
kv =
k:valid_key col v:pipeline { return {key: k, val: v}; }
obj_cmd =
"{" space "}" { return ['obj']; }
/ "{" space head:kv tail:(comma kv)* space "}"
{
var kvs = [head].concat(tail.map(function(t) { return t[1]; }));
var obj_cmd = ['obj'];
for (var i in kvs) {
var kv = kvs[i];
obj_cmd.push(kv.key);
obj_cmd.push({sub:[kv.val]});
}
return obj_cmd;
}
list_cmd = "[" space head:start_cmd tail:(comma start_cmd)* space "]"
{
var items = [head].concat(tail.map(function(t) { return t[1]; }));
items = items.map(function(item) { return {sub: [[item]]}; });
var list_cmd = ['list'].concat(items);
return list_cmd;
}