-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathoverrides.js
306 lines (264 loc) · 6.62 KB
/
overrides.js
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
/**
* Functions defined here are stringified and injected into grammar actions, so
* their bodies are actually run in a *different* scope.
*
* This means that function arguments are meaningless, and the function bodies
* actually operate on variable names defined in the grammar file. We still
* define them as arguments for readability and so that these functions could
* potentially be used outside of the generated parser.
*
* This also explains the "global" functions used here (such as `text()`)...
* they are defined by the generated parser.
*/
var isArray = require('isarray') // not actually used, just for readability
var rules = exports.rules = {}
exports.initializer = [
'var isArray = require("isarray")',
'var map = require("array-map")',
function join (arr) {
return arr.join("")
},
function literal (string) {
return {
type: 'literal',
value: isArray(string) ? string.join('') : string
}
},
function first (arr) {
return arr[0]
},
function second (arr) {
return arr[1]
},
function flattenConcatenation (pieces) {
// TODO - this algo sucks, it's probably on the order of n^4
var result = [pieces[0]]
, len = pieces.length
, prev = pieces[0]
, current
for (var i = 1; i < len; i++) {
current = pieces[i]
if (current.type == 'concatenation') {
current = flattenConcatenation(current.pieces)
}
if (current.type == 'concatenation') {
// it's still a concatenation, append it's pieces to ours
result = result.concat(current.pieces)
}
else if ((current.type == 'literal' || current.type == 'glob')
&& (prev.type == 'literal' || prev.type == 'glob')) {
// globs & literals can be merged
prev.value += current.value
if (prev.type != 'glob' && current.type == 'glob') {
// globs are infectious
prev.type = 'glob'
}
}
else {
result.push(current)
prev = current
}
}
return result.length == 1 ? result[0] : {
type: 'concatenation',
pieces: result
}
}
].join('\n')
rules.script = function (statements) {
return statements || []
}
rules.statementList = function (first, tail, last) {
var statements = [head]
var prev = head
map(tail, function (spaceOpSpaceCmd, i, cmds) {
setOperator(spaceOpSpaceCmd[0], prev)
statements.push(prev = spaceOpSpaceCmd[2])
})
if (last) {
if (!prev) { debugger }
setOperator(last, prev)
}
return statements
function setOperator(operator, command) {
while (command.next) {
command = command.next
}
command.control = operator
}
}
rules.subshell = function (statements) {
return {
type: 'subshell',
statements: statements,
}
}
rules.conditionalLoop = function (kind, test, body) {
return {
type: kind + 'Loop',
test: test,
body: body
}
}
rules.forLoop = function (loopVar, subjects, body) {
return {
type: 'forLoop',
loopVariable: loopVar,
subjects: subjects[1].map(second),
body: body
}
}
rules.ifBlock = function (test, body, elifBlocks, elseBody) {
return {
type: 'ifElse',
test: test,
body: body,
elifBlocks: elifBlocks.length ? elifBlocks : null,
elseBody: elseBody ? elseBody[1] : null,
}
}
rules.elifBlock = function (test, body) {
return {
type: 'ifElse',
test: test,
body: body
}
}
rules.time = function (flags, statements) {
return {
type: 'time',
flags: flags,
command: statements
}
}
rules.condition = function bare (test) {
return test
}
rules.statement = function (statement, next) {
if (typeof next !== 'undefined' && next) {
next = next[1]
statement.control = next[0]
statement.next = next[1]
} else {
statement.control = ';'
statement.next = null
}
return statement
}
rules.command = function (pre, name, post, pipe) {
var command = {
type: 'command',
command: name,
args: [],
redirects: [],
env: {},
control: ';',
next: null,
}
map(pre, first).concat(map(post, second)).forEach(function (token) {
if (!token || !token.type) return
switch (token.type) {
case 'moveFd':
case 'duplicateFd':
case 'redirectFd':
return command.redirects.push(token)
case 'variableAssignment':
return command.env[token.name] = token.value
default:
command.args.push(token)
}
})
if (pipe) {
command.redirects.push(pipe[1])
}
return command
}
rules.chainedStatement = function (operator, statement) {
return [operator, statement]
}
rules.commandName = function (name) {
return name
}
rules.controlOperator = function (op) {
return op == '\n' ? ';' : op
}
rules.processSubstitution = function (rw) {
return {
type: 'processSubstitution',
readWrite: rw,
commands: commands,
}
}
rules.environmentVariable = function () {
return {type: 'variable', name: name}
}
rules.variableSubstitution = function () {
return {
type: 'variableSubstitution',
expression: join(expr), // TODO subParser
}
}
rules.variableAssignment = function (name, value) {
return {type: 'variableAssignment', name: name, value: value}
}
rules.writableVariableName = function () { return text() }
rules.bareword = function (cs) { return literal(cs) }
rules.glob = function (cs) {
return {
type: 'glob',
value: text()
}
}
rules.escapedMetaChar = function (character) { return character }
rules.concatenation = function (pieces) {
return flattenConcatenation(pieces)
}
rules.singleQuote = function (inner) { return literal(inner) }
rules.doubleQuote = function (contents) {
var pieces = contents.map(function (it) {
return isArray(it) ? literal(it) : it
})
return flattenConcatenation(pieces)
}
rules.escapedQuote = function (character) {
return character
}
rules.parenCommandSubstitution = function (commands) {
return {
type: 'commandSubstitution',
commands: commands
}
}
rules.backQuote = function (input) {
return { type: 'commandSubstitution', commands: parse(input.join('')) }
}
rules.pipe = function (command) {
return {type: 'pipe', command: command}
}
rules.moveFd = function (fd, op, dest) {
if (fd == null) fd = op[0] == '<' ? 0 : 1;
return {
type: 'moveFd',
fd: fd,
op: op,
dest: dest
}
}
rules.duplicateFd = function (src, op, dest) {
if (src == null) src = op[0] == '<' ? 0 : 1;
return {
type: 'duplicateFd',
srcFd: src,
op: op,
destFd: dest,
}
}
rules.redirectFd = function (fd, op, filename) {
if (fd == null) fd = op[0] == '<' ? 0 : 1;
return {
type: 'redirectFd',
fd: fd,
op: op,
filename: filename
}
}