apg-js-examples
Version:
Examples of using the suite of apg-js applications and libraries.
90 lines (86 loc) • 2.81 kB
JavaScript
/* eslint-disable new-cap */
/* *************************************************************************************
* copyright: Copyright (c) 2021 Lowell D. Thomas, all rights reserved
* license: BSD-2-Clause (https://opensource.org/licenses/BSD-2-Clause)
* ********************************************************************************* */
// General execution module for the substring examples.
module.exports = function setup(input, beg, len, grammar, name) {
const nodeUtil = require('node:util');
const opts = {
showHidden: true,
depth: null,
};
// Quick and dirty conversion of ascii chars to HTML format.
// Needed for HTML display of grammars that contain HTML entity characters.
const charsToHtml = function charsToHtml(chars) {
let html = '';
chars.forEach((char) => {
switch (char) {
case 32:
html += ' ';
break;
case 38:
html += '&';
break;
case 39:
html += ''';
break;
case 60:
html += '<';
break;
case 62:
html += '>';
break;
case 92:
html += '\';
break;
default:
html += String.fromCharCode(char);
}
});
return html;
};
try {
let html = '';
let chars;
const { apgLib } = require('apg-js');
const writeHtml = require('../writeHtml');
const parser = new apgLib.parser();
parser.trace = new apgLib.trace();
parser.trace.filter.operators['<ALL>'] = true;
/* parse the substring */
const result = parser.parseSubstring(grammar, 0, input, beg, len);
/* output info to console */
console.log();
console.log(` input: ${input}`);
console.log(' results:');
console.dir(result, opts);
/* put input, results and trace on HTML page */
html += '<h3>Grammar Source</h3>';
html += '<pre>';
chars = apgLib.utils.stringToChars(grammar.toString());
html += charsToHtml(chars);
html += '</pre>';
html += '<h3>Input String</h3>';
html += '<pre>';
chars = apgLib.utils.stringToChars(input);
html += apgLib.utils.charsToAsciiHtml(chars);
html += '</pre>';
html += apgLib.utils.parserResultToHtml(result);
html += parser.trace.toHtml('ascii', name);
html = apgLib.utils.htmlToPage(html);
writeHtml(html, name);
} catch (e) {
let msg = '\nEXCEPTION THROWN: \n';
if (e instanceof Error) {
msg += `${e.name}: ${e.message}`;
} else if (typeof e === 'string') {
msg += e;
} else {
msg += nodeUtil.inspect(e, opts);
}
process.exitCode = 1;
console.log(msg);
throw e;
}
};