expexp
Version:
The express model io and express model and data representation.
52 lines (42 loc) • 1.39 kB
JavaScript
import nearley from 'nearley'
import grammar from './grammar.js'
import {traverse, path2str} from './tree.js'
const GEN_TLOG_FOR_TESTS = false
function pruneParsingInfo(cJson) {
const pruningFct = function(json, path, ii) {
if (GEN_TLOG_FOR_TESTS) console.log('A', path2str(ii, path, json))
delete json.p
}
const beforeFct = function(json, path, ii) {
if (GEN_TLOG_FOR_TESTS) console.log('B', path2str(ii, path, json))
}
// TODO still so many levels - cJson[0] - ?
const PRESORT = (GEN_TLOG_FOR_TESTS?[]:undefined)
traverse(cJson[0], pruningFct, beforeFct, PRESORT)
}
// This model deserializer is just a slim wrapper arround the actual logic written as
// nearley parser with moo in the grammar.ne file.
export function ModelDeserializer() {
const me = {}
let parser
me.withParsingInfo = false
const setup = function() {
parser = new nearley.Parser(nearley.Grammar.fromCompiled(grammar))
}
// The arrBuf must be the expressive data model file.
me.syntaxJsonFromArrayBuffer = function(arrBuff) {
const str = String.fromCharCode.apply(null, new Uint16Array(arrBuf))
return me.syntaxJsonFromString(str)
}
me.syntaxJsonFromString = function(string) {
parser.feed(string)
// TODO check results.length == 0 or <1
const json = parser.results[0]
if (me.withParsingInfo == false) {
pruneParsingInfo(json)
}
return json[0]
}
setup()
return me
}