Skip to content

Commit

Permalink
Use new dotted AST field in incrementals.
Browse files Browse the repository at this point in the history
Closes #422.
  • Loading branch information
flatheadmill committed Apr 18, 2020
1 parent b060c2f commit bc8b765
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 23 deletions.
22 changes: 9 additions & 13 deletions parse.inc.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ function generate (packet) {
return { start: $start, object: null, parse }
}
${path.join('.')} = $buffer[$start++]
${path} = $buffer[$start++]
`)
}
Expand All @@ -44,7 +44,7 @@ function generate (packet) {
$byte${direction}
}
${path.join('.')} = $_
${path} = $_
`)
}
Expand Down Expand Up @@ -94,7 +94,7 @@ function generate (packet) {
// Invoked here to set `again`.
const again = step
const source = $(`
`, dispatch([ `${path.join('.')}[${i}]` ], packet.element), `
`, dispatch([ `${path}[${i}]` ], packet.element), `
if (++${i} != ${I}) {
$step = ${again}
continue
Expand Down Expand Up @@ -127,16 +127,14 @@ function generate (packet) {
_conditional = true
const { parse } = conditional
const sip = join(parse.sip.map(field => {
return dispatch([ '$sip[0]' ], field)
return dispatch('$sip[0]', field)
}))
const start = step++
const steps = []
for (const condition of parse.conditions) {
steps.push({
number: step,
source: join(condition.fields.map(field => {
return dispatch(path, field)
}))
source: join(condition.fields.map(field => dispatch(path, field)))
})
}
const ladder = []
Expand All @@ -145,7 +143,7 @@ function generate (packet) {
const keyword = typeof condition.source == 'boolean' ? 'else'
: i == 0 ? 'if' : 'else if'
ladder.push($(`
${keyword} ((${condition.source})($sip[0], ${path.join('.')}, ${packet.name})) {
${keyword} ((${condition.source})($sip[0], ${path}, ${packet.name})) {
$step = ${steps[i].number}
continue
}
Expand Down Expand Up @@ -178,15 +176,13 @@ function generate (packet) {
const push = $(`
case ${step++}:
${path.join('.')} = {
${path} = {
`, vivify(packet, 0), `
}
$step = ${step}
`)
const source = join(packet.fields.map(function (f) {
return dispatch(f.name ? path.concat(f.name) : path, f)
}.bind(this)))
const source = join(packet.fields.map(field => dispatch(path + field.dotted, field)))
return $(`
`, push, `
`, source, `
Expand All @@ -203,7 +199,7 @@ function generate (packet) {
return $(`
case ${step++}:
${path.join('.')} = (${packet.source})($sip[0])
${path} = (${packet.source})($sip[0])
`)
case 'literal':
return literal(packet)
Expand Down
18 changes: 8 additions & 10 deletions serialize.inc.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ function generate (packet) {
$step = ${step}
$byte = ${bite}
$_ = ${path.join('.')}
$_ = ${path}
case ${step++}:
Expand All @@ -35,7 +35,7 @@ function generate (packet) {
function lengthEncoding (path, packet) {
index++
return $(`
`, integer(path.concat('length'), packet), `
`, integer(path + '.length', packet), `
$i.push(0)
`)

Expand All @@ -46,9 +46,9 @@ function generate (packet) {
const I = `$I[${index}]`
const again = step
const source = $(`
`, dispatch([ `${path.join('.')}[${i}]` ], packet.element), `
`, dispatch([ `${path}[${i}]` ], packet.element), `
if (++${i} != ${path.concat('length').join('.')}) {
if (++${i} != ${path + '.length'}) {
$step = ${again}
continue
}
Expand Down Expand Up @@ -90,9 +90,7 @@ function generate (packet) {
for (const condition of conditional.serialize.conditions) {
steps.push({
step: step,
source: join(condition.fields.map(field => {
return dispatch(field.name ? path.concat(field.name) : path, field)
}))
source: join(condition.fields.map(field => dispatch(path, field)))
})
}
const ladder = []
Expand All @@ -101,7 +99,7 @@ function generate (packet) {
const keyword = typeof condition.source == 'boolean' ? 'else'
: i == 0 ? 'if' : 'else if'
ladder.push($(`
${keyword} ((${condition.source})(${path.join('.')}, ${packet.name})) {
${keyword} ((${condition.source})(${path}, ${packet.name})) {
$step = ${steps[i].step}
continue
}
Expand Down Expand Up @@ -131,7 +129,7 @@ function generate (packet) {
switch (packet.type) {
case 'structure':
return join(packet.fields.map(field => {
const source = dispatch(field.name ? path.concat(field.name) : path, field)
const source = dispatch(path + field.dotted, field)
return $(`
`, source, `
`)
Expand All @@ -151,7 +149,7 @@ function generate (packet) {

let source = $(`
switch ($step) {
`, dispatch([ packet.name ], packet), `
`, dispatch(packet.name, packet), `
$step = ${step}
Expand Down

0 comments on commit bc8b765

Please sign in to comment.