This module parses an input SABNF grammar string into a grammar object.
Errors are reported as an array of error message strings.
To be called only by the apg-exp
contructor.
input - required, a string containing the SABNF grammar
errors - required, must be an array
module.exports = function sabnfGenerator(input) {
'use strict;';
const Api = require('../apg-api/api');
const errorName = 'apg-exp: generator: ';
const result = { obj: null, error: null, text: null, html: null };
const grammarTextTitle = 'annotated grammar:\n';
const textErrorTitle = 'annotated grammar errors:\n';
function resultError(api, resultArg, header) {
resultArg.error = header;
resultArg.text = grammarTextTitle;
resultArg.text += api.linesToAscii();
resultArg.text += textErrorTitle;
resultArg.text += api.errorsToAscii();
resultArg.html = api.linesToHtml();
resultArg.html += api.errorsToHtml();
}
let api;
const TRUE = true;
while (TRUE) {
try {
api = new Api(input);
api.scan();
} catch (e) {
result.error = errorName + e.msg;
break;
}
if (api.errors.length) {
resultError(api, result, 'grammar has validation errors');
break;
}
api.parse();
if (api.errors.length) {
resultError(api, result, 'grammar has syntax errors');
break;
}
api.translate();
if (api.errors.length) {
resultError(api, result, 'grammar has semantic errors');
break;
}
api.attributes();
if (api.errors.length) {
resultError(api, result, 'grammar has attribute errors');
break;
}
result.obj = api.toObject();
break;
}
return result;
};