module.exports = function universal() {
try {
const { apgLib } = require('apg-js');
const grammar = new (require('./universal-u'))();
const { parser: ParserCtor, ids } = apgLib;
const parser = new ParserCtor();
const isAlpha = function isAlpha(n) {
if (n >= 65 && n <= 90) return true;
if (n >= 97 && n <= 122) return true;
return false;
};
const isAlphaNum = function isAlphaNum(n) {
if (n >= 65 && n <= 90) return true;
if (n >= 97 && n <= 122) return true;
if (n >= 48 && n <= 57) return true;
return false;
};
const UDTHtml = function UDTHtml(result, chars, phraseIndex, data) {
if (isAlpha(chars[phraseIndex])) {
let len = 1;
for (let i = phraseIndex + 1; i < chars.length; i += 1) {
if (isAlphaNum(chars[i])) {
len += 1;
} else {
break;
}
}
result.state = ids.MATCH;
result.phraseLength = len;
} else {
result.state = ids.NOMATCH;
result.phraseLength = 0;
}
};
parser.callbacks.u_name = UDTHtml;
let inputString = '<ROOT><Parent><child>...</child></Child></CHILD>';
let inputCharacterCodes = apgLib.utils.stringToChars(inputString);
console.log('the grammar:');
console.log(grammar.toString());
console.log('universal mode should only match the last HTML tag name');
console.log('a proper input string:');
console.log(inputString);
const startRule = 'HTML';
let result = parser.parse(grammar, startRule, inputCharacterCodes);
console.log();
console.log("the parser's results");
console.dir(result);
if (result.success === false) {
throw new Error(`input string: '${inputString}' : parse failed`);
}
inputString = '<ROOT><Parent><child>...</child></Parent></ROOT>';
inputCharacterCodes = apgLib.utils.stringToChars(inputString);
console.log('the grammar:');
console.log(grammar.toString());
console.log('improper input string:');
console.log(inputString);
result = parser.parse(grammar, startRule, inputCharacterCodes);
console.log();
console.log("the parser's results");
console.dir(result);
if (result.success === false) {
throw new Error(`input string: '${inputString}' : parse failed`);
}
} catch (e) {
console.log('EXCEPTION THROWN:');
if (e instanceof Error) {
console.log(`${e.name}: ${e.message}`);
} else if (typeof e === 'string') {
console.log(e);
} else {
console.log('unknown exception thrown');
}
}
};