Skip to content

Commit

Permalink
Implement user specified require of modules.
Browse files Browse the repository at this point in the history
Closes #375.
  • Loading branch information
flatheadmill committed Jul 1, 2020
1 parent 9bed3dd commit 0b742f5
Show file tree
Hide file tree
Showing 350 changed files with 10,027 additions and 8,295 deletions.
2 changes: 1 addition & 1 deletion constants.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Node.js API.
const util = require('util')

// Format source code maintaining indents.
// Format source code maintaining indentation.
const $ = require('programmatic')

function generate (packet) {
Expand Down
2 changes: 1 addition & 1 deletion inliner.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Format source code maintaining indents.
// Format source code maintaining indentation.
const $ = require('programmatic')

//
Expand Down
67 changes: 48 additions & 19 deletions parse.all.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,33 @@
const join = require('./join')
const map = require('./map')
const snuggle = require('./snuggle')
const unpack = require('./unpack')
const unsign = require('./fiddle/unsign')
// Format source code maintaining indentation.
const $ = require('programmatic')

// Generate literal object construction.
const vivify = require('./vivify')

// Generate two's compliment conversion.
const unsign = require('./fiddle/unsign')

// Generate integer unpacking.
const unpack = require('./unpack')

// Maintain a set of lookup constants.
const lookup = require('./lookup')

// Generate inline function source.
const inliner = require('./inliner')

// Generate required modules and functions.
const required = require('./required')

const map = require('./map')

// Format source code maintaining indentation.
const join = require('./join')

// Join an array of strings with first line of subsequent element catenated to
// last line of previous element.
const snuggle = require('./snuggle')

function expand (fields) {
const expanded = []
for (const field of fields) {
Expand Down Expand Up @@ -52,7 +70,7 @@ function expand (fields) {
return expanded
}

function generate (packet, bff) {
function generate (packet, { require, bff }) {
let $i = -1, $I = -1, $sip = -1, $step = 1

const variables = { packet: true, step: true }
Expand Down Expand Up @@ -521,40 +539,51 @@ function generate (packet, bff) {
const lookups = Object.keys($lookup).length != 0
? `const $lookup = ${JSON.stringify($lookup, null, 4)}`
: null

const requires = required(require)

if (bff) {
return $(`
parsers.bff.${packet.name} = function () {
`, requires, -1, `
`, lookups, -1, `
return function parse ($buffer, $start, $end) {
`, lets.length ? `let ${lets.join(', ')}` : null, -1, `
return function () {
return function parse ($buffer, $start, $end) {
`, lets.length ? `let ${lets.join(', ')}` : null, -1, `
`, vivify.structure(`const ${packet.name}`, packet), `
`, vivify.structure(`const ${packet.name}`, packet), `
`, source, `
`, source, `
return { start: $start, object: object, parse: null }
}
return { start: $start, object: object, parse: null }
}
} ()
}
`)
}

return $(`
parsers.all.${packet.name} = function ($buffer, $start) {
parsers.all.${packet.name} = function () {
`, requires, -1, `
`, lookups, -1, `
`, lets.length ? `let ${lets.join(', ')}` : null, -1, `
return function ($buffer, $start) {
`, lets.length ? `let ${lets.join(', ')}` : null, -1, `
`, 'const ' + vivify.structure(packet.name, packet), `
`, 'const ' + vivify.structure(packet.name, packet), `
`, source, `
`, source, `
return ${packet.name}
}
return ${packet.name}
}
} ()
`)
}

module.exports = function (definition, options = {}) {
const expanded = expand(JSON.parse(JSON.stringify(definition)))
return join(expanded.map(packet => generate(packet, options.bff)))
return join(expanded.map(packet => generate(packet, options)))
}
52 changes: 39 additions & 13 deletions parse.inc.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,34 @@
const join = require('./join')
const map = require('./map')
const snuggle = require('./snuggle')
const unpack = require('./unpack')
const unsign = require('./fiddle/unsign')
// Format source code maintaining indentation.
const $ = require('programmatic')

// Generate literal object construction.
const vivify = require('./vivify')

// Generate two's compliment conversion.
const unsign = require('./fiddle/unsign')

// Generate integer unpacking.
const unpack = require('./unpack')

// Maintain a set of lookup constants.
const lookup = require('./lookup')

// Generate inline function source.
const inliner = require('./inliner')

function generate (packet) {
// Generate required modules and functions.
const required = require('./required')

const map = require('./map')

// Format source code maintaining indentation.
const join = require('./join')

// Join an array of strings with first line of subsequent element catenated to
// last line of previous element.
const snuggle = require('./snuggle')

function generate (packet, { require = null }) {
let $step = 0, $i = -1, $sip = -1, iterate = false, _terminated = false, surround = false

const variables = { packet: true, step: true }
Expand Down Expand Up @@ -515,21 +533,29 @@ function generate (packet) {
}

const object = `parsers.inc.${packet.name}`

const lookups = Object.keys($lookup).length != 0
? `const $lookup = ${JSON.stringify($lookup, null, 4)}`
: null

const requires = required(require)

return $(`
${object} = function (${signature.join(', ')}) {
parsers.inc.${packet.name} = function () {
`, requires, -1, `
`, lookups, -1, `
let $_, $bite
return function parse ($buffer, $start, $end) {
`, source, `
return function (${signature.join(', ')}) {
let $_, $bite
return function parse ($buffer, $start, $end) {
`, source, `
}
}
}
} ()
`)
}

module.exports = function (definition) {
return join(JSON.parse(JSON.stringify(definition)).map(packet => generate(packet)))
module.exports = function (definition, options) {
return join(JSON.parse(JSON.stringify(definition)).map(packet => generate(packet, options)))
}
16 changes: 16 additions & 0 deletions required.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Node.js API.
const util = require('util')

module.exports = function (required) {
return required != null
? Object.keys(required).map(name => {
for (const name in required) {
switch (typeof required[name]) {
case 'function':
return `const ${name} = null`
case 'string':
return `const ${name} = require(${util.inspect(required[name])})`
}
}
}).join('\n') : null
}
50 changes: 38 additions & 12 deletions serialize.all.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,32 @@
const join = require('./join')
const map = require('./map')
const lookup = require('./lookup')
const snuggle = require('./snuggle')

// Generate integer packing.
const pack = require('./pack')

// Maintain a set of lookup constants.
const lookup = require('./lookup')

// Format source code maintaining indentation.
const $ = require('programmatic')

// Generate inline function source.
const inliner = require('./inliner')

// Generate required modules and functions.
const required = require('./required')

// Format source code maintaining indentation.
const join = require('./join')

// Join an array of strings with first line of subsequent element catenated to
// last line of previous element.
const snuggle = require('./snuggle')

//

// Add implicit field definitions to the given array of field definitions.

//
function expand (fields) {
const expanded = []
for (const field of fields) {
Expand Down Expand Up @@ -123,7 +142,7 @@ function checkpoints (path, fields, index = 0) {
})
}

function generate (packet, bff) {
function generate (packet, { require = null, bff }) {
let $step = 0, $i = -1, $$ = -1

const variables = { packet: true, step: true }
Expand Down Expand Up @@ -432,20 +451,27 @@ function generate (packet, bff) {
const lookups = Object.keys($lookup).length != 0
? `const $lookup = ${JSON.stringify($lookup, null, 4)}`
: null

const requires = required(require)

return $(`
serializers.${bff ? 'bff' : 'all'}.${packet.name} = function (${packet.name}) {
serializers.${bff ? 'bff' : 'all'}.${packet.name} = function () {
`, requires, -1, `
`, lookups, -1, `
return function ($buffer, $start, $end) {
`, consts.length != 0 ? `const ${consts.join(', ')}` : null, -1, `
return function (${packet.name}) {
return function ($buffer, $start, $end) {
`, consts.length != 0 ? `const ${consts.join(', ')}` : null, -1, `
`, lets.length != 0 ? `let ${lets.join(', ')}` : null, -1, `
`, lets.length != 0 ? `let ${lets.join(', ')}` : null, -1, `
`, source, `
`, source, `
return { start: $start, serialize: null }
return { start: $start, serialize: null }
}
}
}
} ()
`)
}

Expand All @@ -455,6 +481,6 @@ module.exports = function (definition, options = {}) {
if (options.bff) {
packet.fields = checkpoints(packet.name, packet.fields)
}
return generate(packet, options.bff)
return generate(packet, options)
}))
}
Loading

0 comments on commit 0b742f5

Please sign in to comment.