-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjson-stream.js
197 lines (172 loc) · 5.03 KB
/
json-stream.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
var Lexer = function(stream){
this.stream = stream
this.pos = 0
this.length = 0
this.listeners = {token:[]}
this.buffer = ''
this.currentToken = ''
this.currentTokenStart = 0
this.currentTokenIndex = 0
this['false'] = ['f', 'a', 'l', 's', 'e']
this['true'] = ['t', 'r', 'u', 'e']
this['null'] = ['n', 'u', 'l', 'l']
this.state = 'start'
var self = this
this.stream.on('data', function(data){
self.buffer += data
self.length += data.length
self.parseToken(data, 0)
})
}
Lexer.prototype.parseString = function(data, i){
while(data[i]!=="\"" && i<data.length){
i++
this.pos++
}
if(data[i]==="\""){
i++ && this.pos++
this.emit({type:'string',
value: this.buffer.substr(this.currentTokenStart, this.pos - 1 - this.currentTokenStart)})
this.currentTokenStart = this.pos
if(i<data.length){
this.parseToken(data, i)
}
}else{
//console.log("no more data, but string is not done")
}
}
Lexer.prototype.parseToken = function(data, i){
var length = data.length
, code
;
if(this.state === 'string')
{
return this.parseString(data, i)
}
if(this.state === 'number')
{
return this.parseNumber(data, i)
}
if(this.state === 'false'){
return this.parseConstant('false', false, data, i)
}
if(this.state === 'true'){
return this.parseConstant('true', true, data, i)
}
if(this.state === 'null'){
return this.parseConstant('null', null, data, i)
}
var chr = data[i]
if(chr==='{'){
++i && this.pos++
this.emit({type:'openbrace'})
}else if(chr==='}'){
++i && this.pos++
this.emit({type:'closebrace'})
}else if(chr===':'){
++i && this.pos++
this.emit({type:'colon'})
}else if(chr==="["){
++i && this.pos++
this.emit({type: 'openbracket'})
}else if(chr==="]"){
++i && this.pos++
this.emit({type:'closebracket'})
}
else if(chr==="\""){
++i && this.pos++
this.currentTokenStart = this.pos
this.state = 'string'
if(i<length){
return this.parseString(data, i)
}
}else if(chr===","){
++i && this.pos++
this.emit({type:'comma'})
}else if((code=chr.charCodeAt(0)) && code>47 && code<58){
this.state = 'number'
this.currentTokenStart = this.pos
return this.parseNumber(data, i)
}else if(chr==='f'){
this.state = 'false'
this.currentTokenIndex = 0
return this.parseConstant('false', false, data, i)
}else if(chr==='t'){
this.state = 'true'
this.currentTokenIndex = 0
return this.parseConstant('true', true, data, i)
}else if(chr==='n'){
this.state = 'null'
this.currentTokenIndex = 0
return this.parseConstant('null', null, data, i)
}
else{
throw "Don't know how to process the character: " + chr + " right now"
}
if(i<length){
//console.log('more data to parse, recursing parseToken')
this.parseToken(data, i)
}
}
Lexer.prototype.parseConstant = function(constant, value, data, i){
var length = data.length,
c = this[constant],
clength = c.length
while(this.currentTokenIndex<clength && i<length){
if(data[i] !== c[this.currentTokenIndex]){
throw "Expected " + c[this.currentTokenIndex] + " but got " + data[i]
}
++i && this.pos++
this.currentTokenIndex++
}
if(this.currentTokenIndex===clength){
this.emit({type: constant, value:value})
}else{
}
if(i<length)
this.parseToken(data, i)
}
Lexer.prototype.parseNumber = function(data, i){
var chrCode, length = data.length
while(i<length && (chrCode=data.charCodeAt(i)) && (chrCode>47 && chrCode < 58) || chrCode ===46){
i++
this.pos++
}
var num = this.buffer.substr(this.currentTokenStart, this.pos-this.currentTokenStart)
this.emit({ type: 'number', value: parseFloat(num)})
if(i<length)
this.parseToken(data, i)
}
Lexer.prototype.emit = function(token){
this.state = undefined
this.lastType = token.type
var handlers = this.listeners['token']
for(var i=0;i<handlers.length;i++){
handlers[i](token)
}
}
Lexer.prototype.on = function(ev, handler){
this.listeners[ev].push(handler)
}
exports.Lexer = Lexer
var Parser = function(lexer){
this.lexer = lexer
this.listeners = {'token':[]}
var self = this
this.lexer.on('token', function(token){
self.handle(token)
})
}
Parser.prototype.handle = function(token){
}
Parser.prototype.on = function(ev, handler){
if(typeof this.listeners[ev]==="undefined")
throw new "Event " + ev + " not supported"
this.listeners[ev].push(handler)
}
Parser.prototype.emit = function(token){
var handlers = this.listeners['token']
for(var i=0;i<handlers.length;i++){
handlers[i](token)
}
}