-
Notifications
You must be signed in to change notification settings - Fork 0
/
shon-parser.js
170 lines (159 loc) · 4.94 KB
/
shon-parser.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
'use strict';
function ShonParser(args) {
this.name = args.name;
this.collector = args.collector;
this.required = args.required;
if (args.json != null) {
this.json = args.json;
}
}
ShonParser.prototype.json = false;
ShonParser.prototype.parse = function parse(iterator, delegate) {
if (this.collector.collected) {
return true;
}
var value = this.parseValue(iterator.cursor, delegate, true);
if (delegate.isDone() || value === null) {
return true;
}
return this.collector.collect(value, iterator, delegate);
};
ShonParser.prototype.parseValue = function parseValue(cursor, delegate, root) {
if (this.collector.collected) {
return null;
}
if (!cursor.done()) {
var arg = cursor.shift();
return this.parseRemainingValue(arg, cursor, delegate);
} else if (this.required || !root) {
delegate.error('Expected value');
delegate.cursor();
return null;
}
return null;
};
ShonParser.prototype.parseRemainingValue = function parseRemainingValue(arg, cursor, delegate) {
if (arg === '[') {
return this.parseRemainingArrayOrObject(cursor, delegate);
} else if (arg === ']') {
delegate.error('Expected value');
delegate.cursor(cursor, -1);
return null;
} else if (arg === '[]') {
return [];
} else if (arg === '[--]') {
return {};
} else if (arg === '-t') {
return true;
} else if (arg === '-f') {
return false;
} else if (arg === '-n') {
return null;
} else if (arg === '-u') {
return undefined;
} else if (arg === '--') {
return this.parseString(cursor, delegate);
} else if (this.json && arg.lastIndexOf('{', 0) === 0) {
return this.parseJSON(arg, cursor, delegate);
} else if (arg && +arg === +arg) {
return +arg;
} else if (arg.lastIndexOf('-', 0) === 0) {
delegate.error('Unexpected flag');
delegate.cursor(-1);
return null;
} else {
return arg;
}
};
ShonParser.prototype.parseRemainingArrayOrObject = function parseRemainingArrayOrObject(cursor, delegate) {
if (cursor.done()) {
delegate.error('Expected remaining array or object');
delegate.cursor();
return null;
}
var arg = cursor.peek();
if (arg === ']') {
cursor.shift();
return [];
} else if (arg !== '--' && arg.lastIndexOf('--', 0) === 0) {
return this.parseRemainingObject({}, cursor, delegate);
} else {
return this.parseRemainingArray([], cursor, delegate);
}
};
ShonParser.prototype.parseRemainingArray = function parseRemainingArray(array, cursor, delegate) {
for (;;) {
if (cursor.done()) {
delegate.error('Expected remaining array');
delegate.cursor();
return null;
}
var arg = cursor.shift();
if (arg === ']') {
return array;
} else {
var value = this.parseRemainingValue(arg, cursor, delegate);
if (delegate.isDone()) {
return null;
}
array.push(value);
}
}
};
ShonParser.prototype.parseRemainingObject = function parseRemainingObject(object, cursor, delegate) {
for (;;) {
if (cursor.done()) {
delegate.error('Expected key for remaining object');
delegate.cursor(-1);
return null;
}
var arg = cursor.shift();
if (arg === ']') {
break;
} else if (arg === '--') {
delegate.error('Expected key for remaining object');
delegate.cursor(-1);
return null;
} else if (arg.lastIndexOf('--', 0) === 0) {
var index = arg.indexOf('=', 2);
if (index >= 0) {
var key = arg.slice(2, index);
var value = arg.slice(index + 1);
value = this.parseRemainingValue(value, cursor, delegate);
if (delegate.isDone()) {
return null;
}
} else {
var key = arg.slice(2);
var value = this.parseValue(cursor, delegate);
if (delegate.isDone()) {
return null;
}
}
object[key] = value;
} else {
delegate.error('Expected key for remaining object');
delegate.cursor(-1);
return null;
}
}
return object;
};
ShonParser.prototype.parseString = function parseString(cursor, delegate) {
if (cursor.done()) {
delegate.error('Expected string');
delegate.cursor();
return null;
}
return cursor.shift();
};
ShonParser.prototype.parseJSON = function parseJSON(arg, cursor, delegate) {
try {
return JSON.parse(arg);
} catch (error) {
delegate.error(error.message);
delegate.cursor();
return null;
}
};
module.exports = ShonParser;