-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
102 lines (81 loc) · 2.07 KB
/
index.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
'use strict';
function StringReader(str) {
if (!(this instanceof StringReader)) {
throw new Error('StringReader is a constructor');
}
this.str = str;
this.pos = -1;
}
StringReader.prototype.next = function next() {
if (this.pos < this.str.length) {
this.current = this.str[++this.pos];
}
return this.current;
};
function isWhitespace(c) {
return c === ' ';
}
function consumeWhitespace(r) {
while (r.current !== undefined && isWhitespace(r.current)) {
r.next();
}
}
function escaped(r) {
const n = r.next();
if (n === undefined) {
throw new Error('Unterminated escape sequence');
}
return n;
}
function quoted(r) {
let s = '';
const q = r.current;
const e = q === '"';
for (let c = r.next(); c !== q; c = r.next()) {
if (c === undefined) {
throw new Error(`Unterminated quote: ${q}`);
}
if (e && c === '\\') {
s += escaped(r);
} else {
s += c;
}
}
return s;
}
module.exports = function shellParser(line) {
let args = [];
let s = '';
if (typeof line !== 'string') {
throw new Error('line: Expected string');
}
const r = new StringReader(line);
r.next();
consumeWhitespace(r);
// Indicates if we saw a quote in the current argument. This is necessary
// to allow empty arguments ('' or ""), otherwise we have no way to
// distinguish this case from nothing.
let sawQuote = false;
while (r.current !== undefined) {
if (isWhitespace(r.current)) {
args.push(s);
sawQuote = false;
s = '';
consumeWhitespace(r);
} else if (r.current == '\\') {
s += escaped(r);
r.next();
} else if (r.current === "'" || r.current === '"') {
s += quoted(r);
sawQuote = true;
r.next();
} else {
s += r.current;
r.next();
}
}
if (sawQuote || s.length) {
args.push(s);
}
return args;
};