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 chars;
let html = '';
const { apgLib } = require('apg-js');
const writeHtml = require('../writeHtml');
const parser = new apgLib.parser();
if (doTrace === true) {
parser.trace = new apgLib.trace();
parser.trace.filter.operators['<ALL>'] = true;
}
console.log('The grammar:');
console.log(grammar.toString());
console.log('The input string:');
console.log(input);
const result = parser.parse(grammar, 0, input);
console.log();
console.log(`result for ${name}`);
console.dir(result, inspectOptions);
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, 'Parser Results');
if (doTrace) {
html += parser.trace.toHtml('ascii', name);
}
html = apgLib.utils.htmlToPage(html, name, name);
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, inspectOptions);
}
process.exitCode = 1;
console.log(msg);
throw e;
}
};