Skip to content

Commit

Permalink
Implement new return object in incremental parser.
Browse files Browse the repository at this point in the history
  • Loading branch information
flatheadmill committed Aug 9, 2016
1 parent b3b6f8c commit 6f77222
Show file tree
Hide file tree
Showing 10 changed files with 52 additions and 109 deletions.
19 changes: 7 additions & 12 deletions compose/parser/inc.js
Expand Up @@ -43,8 +43,7 @@ Generator.prototype.integer = function (field, property, cached) {
// __blank__ \n\
while (frame.bite != ' + stop + ') { \n\
if (start == end) { \n\
engine.start = start \n\
return \n\
return { start: start, object: null, parser: this } \n\
} \n\
' + when(cached, 'this.cache.push(buffer[start])') + ' \n\
frame.value += Math.pow(256, frame.bite) * buffer[start++] \n\
Expand Down Expand Up @@ -110,11 +109,7 @@ Generator.prototype.alternation = function (packet, depth) {
', choice.condition, ' \n\
// __blank__ \n\
this.step = ' + compiled.step + ' \n\
this.parse({ \n\
buffer: this.cache, \n\
start: 0, \n\
end: this.cache.length \n\
}) \n\
this.parse(this.cache, 0, this.cache.length) \n\
continue \n\
// __blank__ \n\
')
Expand All @@ -127,6 +122,7 @@ Generator.prototype.alternation = function (packet, depth) {
', steps, ' \n\
', source, ' \n\
this.step = ' + this.step + ' \n\
continue \n\
// __blank__ \n\
')
}, this)
Expand Down Expand Up @@ -173,6 +169,8 @@ Generator.prototype.lengthEncoded = function (packet, depth) {
this.step = ' + again + ' \n\
continue \n\
} \n\
this.step = ' + this.step + ' \n\
__blank__ \n\
')
return {
step: integer.step,
Expand Down Expand Up @@ -205,7 +203,7 @@ Generator.prototype.parser = function (packet) {
', source, ' \n\
case ' + this.step + ': \n\
// __blank__ \n\
engine.start = start \n\
return { start: start, object: this.object, parser: null } \n\
// __blank__ \n\
} \n\
')
Expand Down Expand Up @@ -233,10 +231,7 @@ Generator.prototype.parser = function (packet) {
' + when(this.cached, 'this.cache = null') + ' \n\
} \n\
// __blank__ \n\
parsers.' + packet.name + '.prototype.parse = function (engine) { \n\
var buffer = engine.buffer \n\
var start = engine.start \n\
var end = engine.end \n\
parsers.' + packet.name + '.prototype.parse = function (buffer, start, end) { \n\
// __blank__ \n\
var frame = this.stack[this.stack.length - 1] \n\
// __blank__ \n\
Expand Down
30 changes: 9 additions & 21 deletions t/generated/alternation.parse.inc.js
Expand Up @@ -14,10 +14,7 @@ module.exports = (function () {
this.cache = null
}

parsers.object.prototype.parse = function (engine) {
var buffer = engine.buffer
var start = engine.start
var end = engine.end
parsers.object.prototype.parse = function (buffer, start, end) {

var frame = this.stack[this.stack.length - 1]

Expand All @@ -39,8 +36,7 @@ module.exports = (function () {

while (frame.bite != -1) {
if (start == end) {
engine.start = start
return
return { start: start, object: null, parser: this }
}
this.cache.push(buffer[start])
frame.value += Math.pow(256, frame.bite) * buffer[start++]
Expand All @@ -54,21 +50,13 @@ module.exports = (function () {
if (frame.select & 0x80) {

this.step = 2
this.parse({
buffer: this.cache,
start: 0,
end: this.cache.length
})
this.parse(this.cache, 0, this.cache.length)
continue

} else {

this.step = 4
this.parse({
buffer: this.cache,
start: 0,
end: this.cache.length
})
this.parse(this.cache, 0, this.cache.length)
continue

}
Expand All @@ -87,8 +75,7 @@ module.exports = (function () {

while (frame.bite != -1) {
if (start == end) {
engine.start = start
return
return { start: start, object: null, parser: this }
}
frame.value += Math.pow(256, frame.bite) * buffer[start++]
frame.bite--
Expand All @@ -97,6 +84,7 @@ module.exports = (function () {
this.stack.pop()
this.stack[this.stack.length - 1].object.number = frame.value
this.step = 6
continue

case 4:

Expand All @@ -112,8 +100,7 @@ module.exports = (function () {

while (frame.bite != -1) {
if (start == end) {
engine.start = start
return
return { start: start, object: null, parser: this }
}
frame.value += Math.pow(256, frame.bite) * buffer[start++]
frame.bite--
Expand All @@ -122,10 +109,11 @@ module.exports = (function () {
this.stack.pop()
this.stack[this.stack.length - 1].object.number = frame.value
this.step = 6
continue

case 6:

engine.start = start
return { start: start, object: this.object, parser: null }

}

Expand Down
10 changes: 3 additions & 7 deletions t/generated/big-endian-byte.parser.inc.js
Expand Up @@ -13,10 +13,7 @@ module.exports = (function () {
}]
}

parsers.object.prototype.parse = function (engine) {
var buffer = engine.buffer
var start = engine.start
var end = engine.end
parsers.object.prototype.parse = function (buffer, start, end) {

var frame = this.stack[this.stack.length - 1]

Expand All @@ -35,8 +32,7 @@ module.exports = (function () {

while (frame.bite != -1) {
if (start == end) {
engine.start = start
return
return { start: start, object: null, parser: this }
}
frame.value += Math.pow(256, frame.bite) * buffer[start++]
frame.bite--
Expand All @@ -46,7 +42,7 @@ module.exports = (function () {
this.stack[this.stack.length - 1].object.word = frame.value
case 2:

engine.start = start
return { start: start, object: this.object, parser: null }

}
}
Expand Down
10 changes: 3 additions & 7 deletions t/generated/big-endian-integer.parser.inc.js
Expand Up @@ -13,10 +13,7 @@ module.exports = (function () {
}]
}

parsers.object.prototype.parse = function (engine) {
var buffer = engine.buffer
var start = engine.start
var end = engine.end
parsers.object.prototype.parse = function (buffer, start, end) {

var frame = this.stack[this.stack.length - 1]

Expand All @@ -35,8 +32,7 @@ module.exports = (function () {

while (frame.bite != -1) {
if (start == end) {
engine.start = start
return
return { start: start, object: null, parser: this }
}
frame.value += Math.pow(256, frame.bite) * buffer[start++]
frame.bite--
Expand All @@ -46,7 +42,7 @@ module.exports = (function () {
this.stack[this.stack.length - 1].object.integer = frame.value
case 2:

engine.start = start
return { start: start, object: this.object, parser: null }

}
}
Expand Down
10 changes: 3 additions & 7 deletions t/generated/big-endian-word.parser.inc.js
Expand Up @@ -13,10 +13,7 @@ module.exports = (function () {
}]
}

parsers.object.prototype.parse = function (engine) {
var buffer = engine.buffer
var start = engine.start
var end = engine.end
parsers.object.prototype.parse = function (buffer, start, end) {

var frame = this.stack[this.stack.length - 1]

Expand All @@ -35,8 +32,7 @@ module.exports = (function () {

while (frame.bite != -1) {
if (start == end) {
engine.start = start
return
return { start: start, object: null, parser: this }
}
frame.value += Math.pow(256, frame.bite) * buffer[start++]
frame.bite--
Expand All @@ -46,7 +42,7 @@ module.exports = (function () {
this.stack[this.stack.length - 1].object.word = frame.value
case 2:

engine.start = start
return { start: start, object: this.object, parser: null }

}
}
Expand Down
10 changes: 3 additions & 7 deletions t/generated/little-endian-integer.parser.inc.js
Expand Up @@ -13,10 +13,7 @@ module.exports = (function () {
}]
}

parsers.object.prototype.parse = function (engine) {
var buffer = engine.buffer
var start = engine.start
var end = engine.end
parsers.object.prototype.parse = function (buffer, start, end) {

var frame = this.stack[this.stack.length - 1]

Expand All @@ -35,8 +32,7 @@ module.exports = (function () {

while (frame.bite != 4) {
if (start == end) {
engine.start = start
return
return { start: start, object: null, parser: this }
}
frame.value += Math.pow(256, frame.bite) * buffer[start++]
frame.bite++
Expand All @@ -46,7 +42,7 @@ module.exports = (function () {
this.stack[this.stack.length - 1].object.integer = frame.value
case 2:

engine.start = start
return { start: start, object: this.object, parser: null }

}
}
Expand Down
10 changes: 3 additions & 7 deletions t/generated/little-endian-word.parser.inc.js
Expand Up @@ -13,10 +13,7 @@ module.exports = (function () {
}]
}

parsers.object.prototype.parse = function (engine) {
var buffer = engine.buffer
var start = engine.start
var end = engine.end
parsers.object.prototype.parse = function (buffer, start, end) {

var frame = this.stack[this.stack.length - 1]

Expand All @@ -35,8 +32,7 @@ module.exports = (function () {

while (frame.bite != 2) {
if (start == end) {
engine.start = start
return
return { start: start, object: null, parser: this }
}
frame.value += Math.pow(256, frame.bite) * buffer[start++]
frame.bite++
Expand All @@ -46,7 +42,7 @@ module.exports = (function () {
this.stack[this.stack.length - 1].object.word = frame.value
case 2:

engine.start = start
return { start: start, object: this.object, parser: null }

}
}
Expand Down
18 changes: 7 additions & 11 deletions t/generated/nested.parse.inc.js
Expand Up @@ -13,10 +13,7 @@ module.exports = (function () {
}]
}

parsers.object.prototype.parse = function (engine) {
var buffer = engine.buffer
var start = engine.start
var end = engine.end
parsers.object.prototype.parse = function (buffer, start, end) {

var frame = this.stack[this.stack.length - 1]

Expand All @@ -37,8 +34,7 @@ module.exports = (function () {

while (frame.bite != -1) {
if (start == end) {
engine.start = start
return
return { start: start, object: null, parser: this }
}
frame.value += Math.pow(256, frame.bite) * buffer[start++]
frame.bite--
Expand Down Expand Up @@ -72,8 +68,7 @@ module.exports = (function () {

while (frame.bite != -1) {
if (start == end) {
engine.start = start
return
return { start: start, object: null, parser: this }
}
frame.value += Math.pow(256, frame.bite) * buffer[start++]
frame.bite--
Expand All @@ -96,8 +91,7 @@ module.exports = (function () {

while (frame.bite != -1) {
if (start == end) {
engine.start = start
return
return { start: start, object: null, parser: this }
}
frame.value += Math.pow(256, frame.bite) * buffer[start++]
frame.bite--
Expand All @@ -112,9 +106,11 @@ module.exports = (function () {
this.step = 2
continue
}
this.step = 7

case 7:

engine.start = start
return { start: start, object: this.object, parser: null }

}

Expand Down
21 changes: 7 additions & 14 deletions t/parse/inc/alternation.t.js
Expand Up @@ -56,19 +56,12 @@ function prove (assert) {
var buffer = new Buffer([ 0xff, 0xff ])
for (var i = 0; i < buffer.length; i++) {
var parser = (new parsers.object)
var engine = {
buffer: buffer,
start: 0,
end: buffer.length - i
}
parser.parse(engine)
var engine = {
buffer: buffer,
start: buffer.length - i,
end: buffer.length
}
parser.parse(engine)
assert(parser.object, { number: 0xffff }, 'compiled')
assert(engine.start, buffer.length, 'start moved')
var first = parser.parse(buffer, 0, buffer.length - i)
assert(first.start, buffer.length - i, 'inremental start ' + i)
assert(parser.parse(buffer, buffer.length - i, buffer.length), {
start: buffer.length,
object: { number: 0xffff },
parser: null
}, 'complete ' + i)
}
}

0 comments on commit 6f77222

Please sign in to comment.