Skip to content

Commit 719a94c

Browse files
updates
1 parent b7696bf commit 719a94c

File tree

3 files changed

+90
-34
lines changed

3 files changed

+90
-34
lines changed

lib/expr.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,19 @@ module.exports = function(incoming_state, incoming_tokens) {
100100
if(!tokens.length) return
101101

102102
advance()
103-
expression(0)
103+
var result = expression(0)
104+
105+
result.parent = state[0]
106+
emit(result)
107+
108+
function emit(node) {
109+
state.unshift(node, false)
110+
for(var i = 0, len = node.children.length; i < len; ++i) {
111+
emit(node.children[i])
112+
}
113+
state.shift()
114+
}
115+
104116
}
105117

106118
function symbol(id, binding_power) {

lib/index.js

Lines changed: 66 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ function create_stream() {
1414
// singleton!
1515
var Advance = new Object
1616

17+
var DEBUG = false
18+
1719
var _ = 0
1820
, STMT = _++
1921
, STMTLIST = _++
@@ -45,9 +47,9 @@ var NO_ASSIGN_ALLOWED = false
4547

4648
// map of tokens to stmt types
4749
var token_map = {
48-
'block-comment': COMMENT
49-
, 'line-comment': COMMENT
50-
, 'preprocessor': PREPROCESSOR
50+
'block-comment': COMMENT
51+
, 'line-comment': COMMENT
52+
, 'preprocessor': PREPROCESSOR
5153
}
5254

5355
// map of stmt types to human
@@ -137,15 +139,16 @@ function parser() {
137139
// stream functions ---------------------------------------------
138140

139141
function write(input) {
140-
if(input.type === 'whitespace') {
142+
if(input.type === 'whitespace' || input.type === 'line-comment' || input.type === 'block-comment') {
141143
whitespace.push(input)
142144
return
143145
}
144146
tokens.push(input)
145147
token = token || tokens[0]
146148

147-
if(node) {
148-
node.whitespace.concat(whitespace.splice(0, whitespace.length))
149+
if(token && whitespace.length) {
150+
token.preceding = token.preceding || []
151+
token.preceding.concat(whitespace.splice(0, whitespace.length))
149152
}
150153

151154
while(take()) switch(state[0].mode) {
@@ -205,10 +208,22 @@ function parser() {
205208
state.shift()
206209
}
207210

208-
function special_unshift(_node) {
211+
function special_unshift(_node, add_child) {
212+
_node.parent = state[0]
213+
209214
var ret = [].unshift.call(this, _node)
210215

211-
node.children.push(_node)
216+
add_child = add_child === undefined ? true : add_child
217+
218+
if(DEBUG) {
219+
var pad = ''
220+
for(var i = 0, len = this.length - 1; i < len; ++i) {
221+
pad += ' |'
222+
}
223+
console.log(pad, '\\'+_node.type)
224+
}
225+
226+
if(add_child) node.children.push(_node)
212227
node = _node
213228

214229
return ret
@@ -219,6 +234,14 @@ function parser() {
219234
, okay = check[this.length]
220235
, emit = false
221236

237+
if(DEBUG) {
238+
var pad = ''
239+
for(var i = 0, len = this.length; i < len; ++i) {
240+
pad += ' |'
241+
}
242+
console.log(pad, '/'+_node.type)
243+
}
244+
222245
if(check.length) {
223246
if(okay !== undefined) {
224247
emit = okay.test ? okay.test(_node.type) : okay === _node.type
@@ -227,7 +250,7 @@ function parser() {
227250
emit = true
228251
}
229252

230-
if(emit) stream.emit('data', node)
253+
if(emit) stream.emit('data', _node)
231254

232255
node = _node.parent
233256
return _node
@@ -248,8 +271,6 @@ function parser() {
248271
return state.scope.exit(), state.shift()
249272
}
250273
switch(token.type) {
251-
case 'block-comment':
252-
case 'line-comment':
253274
case 'preprocessor':
254275
state.fake(adhoc())
255276
tokens.shift()
@@ -329,10 +350,6 @@ function parser() {
329350
state[0].got = []
330351
}
331352

332-
if(token.type === 'block-comment' || token.type === 'line-comment') {
333-
return tokens.shift()
334-
}
335-
336353
state[0].got.push(tokens.shift())
337354

338355
if(state[0].need !== state[0].got.length)
@@ -344,8 +361,20 @@ function parser() {
344361
token = state[0].got.shift(), state.fake(keyword())
345362
} else {
346363
state.fake(keyword('<default>'))
347-
}
348-
token = state[0].got.shift(), state.fake(keyword())
364+
}
365+
token = state[0].got.shift()
366+
367+
if(token.type === 'keyword') {
368+
state.fake(keyword())
369+
} else {
370+
var lookup = state.scope.find(token.data)
371+
372+
if(!lookup) return
373+
374+
lookup = Object.create(lookup)
375+
lookup.token = token
376+
state.fake(lookup)
377+
}
349378

350379
while(state[0].got.length)
351380
tokens.unshift(token = state[0].got.pop())
@@ -354,7 +383,19 @@ function parser() {
354383
}
355384

356385
// it's a function
357-
token = state[0].got.shift(), state.fake(keyword())
386+
token = state[0].got.shift()
387+
388+
if(token.type === 'keyword') {
389+
state.fake(keyword())
390+
} else {
391+
var lookup = state.scope.find(token.data)
392+
393+
if(!lookup) return
394+
395+
lookup = Object.create(lookup)
396+
lookup.token = token
397+
state.fake(lookup)
398+
}
358399
// replace the last two tokens
359400
tokens.unshift(state[0].got.pop())
360401
tokens.unshift(token = state[0].got.pop())
@@ -364,10 +405,6 @@ function parser() {
364405

365406
function parse_decllist() {
366407
// grab ident
367-
if(token.type === 'line-comment' || token.type === 'block-comment') {
368-
return tokens.shift()
369-
}
370-
371408
if(token.type === 'ident') {
372409
var name = token.data
373410
state.unshift(ident())
@@ -432,10 +469,6 @@ function parser() {
432469
function parse_expr() {
433470
var expecting = state[0].expecting
434471

435-
if(token.type === 'block-comment' || token.type === 'line-comment')
436-
return tokens.shift()
437-
438-
439472
state[0].tokens = state[0].tokens || []
440473

441474
if(state[0].parenlevel === undefined) {
@@ -520,7 +553,7 @@ function parser() {
520553
function assert_null_string_or_array(x, y) {
521554
switch(typeof x) {
522555
case 'string': if(y !== x) {
523-
unexpected('expected `'+x+'`, got '+y);
556+
unexpected('expected `'+x+'`, got '+y+'\n'+token.data);
524557
} return !errored
525558

526559
case 'object': if(x && x.indexOf(y) === -1) {
@@ -540,10 +573,6 @@ function parser() {
540573
return function() {
541574
var current = state[0]
542575

543-
if(token.type === 'line-comment' || token.type === 'block-comment') {
544-
return tokens.shift()
545-
}
546-
547576
current.stage || (current.stage = 0)
548577

549578
step = steps[current.stage]
@@ -570,7 +599,12 @@ function parser() {
570599
var name = token.data
571600
return assert('ident') && (state.unshift(ident()), state.scope.define(name), Advance)
572601
} : function() {
573-
return assert('ident') && (state.fake(state.scope.find(token.data)), tokens.shift(), Advance)
602+
if(!assert('ident')) return
603+
604+
var s = Object.create(state.scope.find(token.data))
605+
s.token = token
606+
607+
return (tokens.shift(), Advance)
574608
}
575609
}
576610

@@ -744,7 +778,6 @@ function mknode(mode, sourcetoken, parent) {
744778
, token: sourcetoken
745779
, parent: parent
746780
, children: []
747-
, whitespace: []
748781
, type: stmt_type[mode]
749782
, id: (Math.random() * 0xFFFFFFFF).toString(16)
750783
}

test/index.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,20 @@ var tokenizer = require('glsl-tokenizer')()
44
, path = require('path').join(__dirname, 'test.glsl')
55

66
var num = 0
7+
, types = []
78

89
fs.createReadStream(path)
910
.pipe(tokenizer)
1011
.pipe(parser())
1112
.on('data', function(x) {
13+
console.log(selector(x))
1214
})
15+
16+
function selector(x) {
17+
var list = []
18+
do {
19+
list.unshift(x.type)
20+
} while(x = x.parent)
21+
22+
return list.join('/')
23+
}

0 commit comments

Comments
 (0)