Skip to content

Commit 6db1008

Browse files
fix function/statement interaction, add token.succeeding for trailing whitespace
1 parent 3326b17 commit 6db1008

File tree

1 file changed

+35
-11
lines changed

1 file changed

+35
-11
lines changed

lib/index.js

Lines changed: 35 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ function parser() {
112112
, tokens = []
113113
, whitespace = []
114114
, errored = false
115+
, program
115116
, token
116117
, node
117118

@@ -132,14 +133,17 @@ function parser() {
132133
node.expecting = '(eof)'
133134
node.mode = STMTLIST
134135
node.token = {type: '(program)', data: '(program)'}
136+
program = node
135137

138+
stream.program = program
136139
state.unshift(node)
137140
return stream
138141

139142
// stream functions ---------------------------------------------
140143

141144
function write(input) {
142145
if(input.type === 'whitespace' || input.type === 'line-comment' || input.type === 'block-comment') {
146+
143147
whitespace.push(input)
144148
return
145149
}
@@ -148,7 +152,8 @@ function parser() {
148152

149153
if(token && whitespace.length) {
150154
token.preceding = token.preceding || []
151-
token.preceding.concat(whitespace.splice(0, whitespace.length))
155+
token.preceding = token.preceding.concat(whitespace)
156+
whitespace = []
152157
}
153158

154159
while(take()) switch(state[0].mode) {
@@ -182,16 +187,19 @@ function parser() {
182187
return
183188
}
184189

190+
stream.emit('end')
185191
stream.readable = false
186192
stream.closed = true
187193
stream.emit('close')
188194
}
189195

190196

191197
function destroy() {
192-
this.write = function(){}
193-
this.writable = false
194-
this.emit('close')
198+
stream.emit('end')
199+
200+
stream.write = function(){}
201+
stream.writable = false
202+
stream.emit('close')
195203
}
196204

197205
function take() {
@@ -220,10 +228,10 @@ function parser() {
220228
for(var i = 0, len = this.length - 1; i < len; ++i) {
221229
pad += ' |'
222230
}
223-
console.log(pad, '\\'+_node.type)
231+
console.log(pad, '\\'+_node.type, _node.token.data)
224232
}
225233

226-
if(add_child) node.children.push(_node)
234+
if(add_child && node !== _node) node.children.push(_node)
227235
node = _node
228236

229237
return ret
@@ -426,8 +434,6 @@ function parser() {
426434

427435
tokens.shift()
428436

429-
// push old child back onto stack without emitting it.
430-
431437
state.unshift(expr(',', ';'))
432438
return
433439
}
@@ -464,6 +470,7 @@ function parser() {
464470
return unexpected('expected user-defined name, got '+token.data)
465471
}
466472

473+
state[0].data = token.data
467474
return state.shift(), tokens.shift()
468475
}
469476

@@ -589,7 +596,20 @@ function parser() {
589596
}
590597

591598
function advance(op, t) {
592-
return function() { return assert(t || 'operator', op) && (tokens.shift(), Advance) }
599+
t = t || 'operator'
600+
return function() {
601+
if(!assert(t, op)) return
602+
603+
var last = tokens.shift()
604+
, children = state[0].children
605+
, last_node = children[children.length - 1]
606+
607+
if(last_node && last_node.token && last.preceding) {
608+
last_node.token.succeeding = last_node.token.succeeding || []
609+
last_node.token.succeeding = last_node.token.succeeding.concat(last.preceding)
610+
}
611+
return Advance
612+
}
593613
}
594614

595615
function advance_expr(until) {
@@ -638,7 +658,11 @@ function parser() {
638658
, advance_ident(true)
639659
, advance('{')
640660
, function() {
641-
if(token.data === '}') { popstmt()(), tokens.shift(); return }
661+
if(token.data === '}') {
662+
state[0].token.succeeding = token.preceding
663+
popstmt()(), tokens.shift();
664+
return
665+
}
642666
if(token.data === ';') { tokens.shift(); return }
643667
state.unshift(decl(NO_ASSIGN_ALLOWED))
644668
}
@@ -754,7 +778,7 @@ function parser() {
754778
, advance('{')
755779
, advance_stmtlist()
756780
, advance('}')
757-
, popstmt()
781+
, function() { return state.shift(), state.shift(), state.shift() }
758782
)
759783

760784
parse_function_args =

0 commit comments

Comments
 (0)