-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.js
177 lines (141 loc) · 3.13 KB
/
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
171
172
173
174
175
176
177
function Parser(tokens)
{
this.tokens = tokens;
this.index = 0;
}
Parser.prototype.current = function()
{
if (this.done())
{
console.log(this.tokens);
throw new Error('Parser went past end of token list');
}
return this.tokens[this.index];
}
Parser.prototype.advance = function()
{
this.index += 1;
}
Parser.prototype.next = function()
{
this.index += 1;
const token = this.current();
return token;
}
Parser.prototype.done = function()
{
return this.index === this.tokens.length
}
Parser.prototype.parse = function()
{
const token = this.current();
const json = this.parseValue(token);
if (this.done())
{
return json;
}
throw new Error(`Extraneous content at index ${this.index}`);
}
Parser.prototype.parseObject = function()
{
const members = [];
let token = this.current();
if (token.value === '}')
{
this.advance();
return {};
}
while (true)
{
let member = {}
if (token.type !== 'string' && token.type !== 'literal')
{
this.fail(token.index);
}
member.key = token.value;
token = this.next();
if (token.value !== ':') this.fail(token.index);
member.value = this.parseValue(this.next());
members.push(member);
token = this.current();
if (token.value === '}')
{
this.advance();
break;
}
if (token.value === ',') token = this.next();
else this.fail();
}
const object = {};
for (const member of members)
{
if (object[member.key] !== undefined)
{
throw new Error(`Duplicate key ${member.key}`);
}
object[member.key] = member.value;
}
return object;
}
Parser.prototype.parseArray = function()
{
const array = [];
let token = this.current();
if (token.value === ']')
{
this.advance();
return [];
}
while (true)
{
array.push(this.parseValue(token));
token = this.current();
if (token.value === ']')
{
this.advance();
break;
}
if (token.value === ',') token = this.next();
else this.fail();
}
return array;
}
Parser.prototype.parseValue = function(token)
{
this.advance();
if (token.type === 'string') return this.parseString(token.value)
if (token.type === 'number') return token.value;
if (token.value === '{') return this.parseObject();
if (token.value === '[') return this.parseArray();
if (token.type === 'literal')
{
if (token.value === 'null') return null;
if (token.value === 'false') return false;
if (token.value === 'true') return true;
}
throw new Error(`Unknown token type ${token.type}`);
}
Parser.prototype.parseString = function(string)
{
if (this.done()) return string;
const symbol = this.current();
if (symbol.type === 'symbol' && symbol.value === '+')
{
const token = this.next();
if (token.type === 'string')
{
this.advance();
return this.parseString(`${string}${token.value}`);
}
else
{
throw new Error(`Dangling plus sign after string at ${string.index}`);
}
}
return string;
}
Parser.prototype.fail = function()
{
throw new Error(`Unexpected input at index ${this.tokens[this.index].index}.`);
}
exports.Parser = Parser;