const ELEMENT = {
obj: null,
grammar: null,
input: null,
output: null,
};
function onload() {
ELEMENT.grammar = document.getElementById('grammar');
ELEMENT.input = document.getElementById('input');
ELEMENT.output = document.getElementById('output');
let float = '';
float += 'float = [sign] decimal [exponent]\n';
float += 'sign = "+" / "-"\n';
float += 'decimal = integer [dot [fraction]]\n';
float += ' / dot fraction\n';
float += 'integer = 1*%d48-57\n';
float += 'dot = "."\n';
float += 'fraction = 1*%d48-57\n';
float += 'exponent = "e" [esign] exp\n';
float += 'esign = "+" / "-"\n';
float += 'exp = 1*%d48-57\n';
ELEMENT.grammar.value = float;
ELEMENT.input.value = '123.0';
}
function generate() {
const api = new apgApi(ELEMENT.grammar.value);
api.generate();
if (api.errors.length) {
ELEMENT.output.innerHTML = "<span class='apg-nomatch'>grammar has errors</span>";
return;
}
let html = '<pre>\n';
html += api.toSource();
html += '</pre>\n';
ELEMENT.output.innerHTML = html;
ELEMENT.obj = api.toObject();
}
function parse() {
const parser = new apgLib.parser();
const result = parser.parse(ELEMENT.obj, 0, ELEMENT.input.value);
const html = apgLib.utils.parserResultToHtml(result, 'web page apg-api example');
ELEMENT.output.innerHTML = html;
}