-
Notifications
You must be signed in to change notification settings - Fork 2
/
parse_json.js
200 lines (182 loc) · 5 KB
/
parse_json.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
// # parse_json.js
//
// Parser for JSON written in Simplified JavaScript.
//
// A stripped-down version of the TDOP JavaScript parser in parse.js
define(["text!parse_json.js", "tokenize"], function make_parse_json(parse_json_source, tokenize) {
var symbol_table = {};
var token;
var tokens;
var token_nr;
var itself = function () {
return this;
};
var error = function(obj, message, t) {
t = t || obj;
t.name = "Syntax Error";
if (t.from || t.to) { message += ' ['+t.from+'-'+t.to+']'; }
t.message = message;
/*console.warn(JSON.stringify(t));*/
Object.Throw(t);
};
var hasOwnProperty = function(obj, prop) {
return Object.prototype.hasOwnProperty.call(obj, prop);
};
var advance = function (id) {
var a, o, t, v;
if (id && token.id !== id) {
error(token, "Expected '" + id + "'.");
}
if (token_nr >= tokens.length) {
token = symbol_table["(end)"];
return;
}
t = tokens[token_nr];
token_nr += 1;
v = t.value;
a = t.type;
if (a === "name" && hasOwnProperty(symbol_table, v)) {
o = symbol_table[v];
} else if (a === "operator") {
o = symbol_table[v];
if (!o) {
error(t, "Unknown operator: " + v);
}
} else if (a === "string" || a === "number") {
o = symbol_table["(literal)"];
a = "literal";
} else {
error(t, "Unexpected token.");
}
token = Object.create(o);
token.from = t.from;
token.to = t.to;
token.value = v;
token.arity = a;
return token;
};
var expression = function (rbp) {
var left;
var t = token;
advance();
left = t.nud();
while (rbp < token.lbp) {
t = token;
advance();
left = t.led(left);
}
return left;
};
var original_symbol = {
nud: function () {
error(this, "Undefined: " + this.value);
},
led: function (left) {
error(this, "Missing operator.");
}
};
var symbol = function (id, bp) {
var s = hasOwnProperty(symbol_table, id) ? symbol_table[id] : null;
bp = bp || 0;
if (s) {
if (bp >= s.lbp) {
s.lbp = bp;
}
} else {
s = Object.create(original_symbol);
s.id = s.value = id;
s.lbp = bp;
symbol_table[id] = s;
}
return s;
};
var constant = function (s, v) {
var x = symbol(s);
x.nud = function () {
//scope.reserve(this);
this.value = symbol_table[this.id].value;
this.arity = "literal";
return this;
};
x.value = v;
return x;
};
var prefix = function (id, nud) {
var s = symbol(id);
s.nud = nud || function () {
//scope.reserve(this);
this.first = expression(70);
this.arity = "unary";
return this;
};
return s;
};
symbol("(end)");
symbol(":");
symbol("]");
symbol("}");
symbol(",");
constant("true", true);
constant("false", false);
constant("null", null);
symbol("(literal)").nud = itself;
prefix("-");
prefix("[", function () {
var a = [];
if (token.id !== "]") {
while (true) {
a.push(expression(0));
if (token.id !== ",") {
break;
}
advance(",");
}
}
advance("]");
this.first = a;
this.arity = "unary";
return this;
});
prefix("{", function () {
var a = [], n, v;
if (token.id !== "}") {
while (true) {
n = token;
if (n.arity !== "name" && n.arity !== "literal") {
error(token, "Bad property name.");
}
advance();
advance(":");
v = expression(0);
v.key = n.value;
a.push(v);
if (token.id !== ",") {
break;
}
advance(",");
}
}
advance("}");
this.first = a;
this.arity = "unary";
return this;
});
var parse_json = function(source) {
tokens = tokenize(source, '=<>!+-*&|/%^', '=<>&|');
token_nr = 0;
advance();
var e = expression(0);
advance("(end)");
var tree = [{
value: "return",
arity: "statement",
first: e
}];
return tree;
};
parse_json.__module_name__ = "parse_json";
parse_json.__module_init__ = make_parse_json;
parse_json.__module_deps__ = ["tokenize"];
parse_json.__module_source__ = parse_json_source;
return parse_json;
});