const charsToHtml = function (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;
const result = parser.parse(grammar, 0, input);
console.log();
console.log(` input: ${input}`);
console.log(' results:');
console.dir(result, opts);
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();
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;
}
};