UNPKG

@smartdcc/gbcs-parser

Version:
115 lines 3.71 kB
"use strict"; /* * * Original copyright holders for the GBCS message parser tool: * * Copyright (c) 2019 Andre B. Oliveira * 2019 Enrique Giraldo * 2019 Cristóbal Borrero * * Copyright for subsequent changes, including porting to NodeJS, * updating for TypeScript and refactor to support unit testing: * * Copyright (c) 2022 Smart DCC Limited * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ Object.defineProperty(exports, "__esModule", { value: true }); exports.putSeparator = putSeparator; exports.putBytes = putBytes; exports.putUnparsedBytes = putUnparsedBytes; exports.minimizeItem = minimizeItem; exports.minimizeBlock = minimizeBlock; exports.minimizeMessage = minimizeMessage; function putSeparator(ctx, title) { const sep = { type: 'SEPARATOR', depth: 0, children: {}, }; if (title in ctx.output) { throw new Error(`overwrite output ${title}`); } ctx.current = [sep]; ctx.output[title] = sep; } function putBytes(ctx, name, bytes, notes) { const h = '0123456789ABCDEF'; const n = bytes.end - bytes.index; let hex = ''; for (let i = 0; i < n; i++) { if (i > 0) { hex += ' '; } const b = bytes.input.byte(bytes.index++); hex += h.charAt((b >> 4) & 15) + h.charAt(b & 15); } const result = name.match(/^ */); const depth = (result?.[0].length ?? 0) + 1; const item = { type: 'ITEM', depth, hex, notes, }; /* pop off item deeper or equal */ while (ctx.current[ctx.current.length - 1].depth >= depth) { ctx.current.pop(); } if (depth > 1 && (ctx.current[ctx.current.length - 1].type === 'SEPARATOR' || depth !== ctx.current[ctx.current.length - 1].depth + 1)) { throw new Error(`incorrect nesting ${name}`); } const parent = ctx.current[ctx.current.length - 1]; if (parent.children === undefined) { parent.children = {}; } if (name.trimStart() in parent.children) { throw new Error(`overwrite output ${name}`); } parent.children[name.trimStart()] = item; ctx.current.push(item); } function putUnparsedBytes(bytes) { if (bytes.end - bytes.index > 0) { throw new Error(`unexpected data ${bytes}`); } } function minimizeItem({ hex, notes, children, }) { const result = { hex, }; if (notes) { result.notes = notes; } if (children && Object.keys(children).length >= 1) { result.children = Object.fromEntries(Object.keys(children).map((k) => [k, minimizeItem(children[k])])); } return result; } function minimizeBlock({ children }) { return Object.fromEntries(Object.keys(children).map((k) => [k, minimizeItem(children[k])])); } /** * Removes files from ParsedMessage structure that are only relevant for * internal use. * * @param message * @returns */ function minimizeMessage(message) { return Object.fromEntries(Object.keys(message).map((k) => [k, minimizeBlock(message[k])])); } //# sourceMappingURL=context.js.map