const { charsToString } = apgLib.utils;
Define the UDT
callback function.
data
is not used here. With a normal apg
parser, the user has the option of passing
in a data object for use by the user-written functions.
But there is no facility for this in apg-exp
.
const udtPhrase = function udtPhrase(sysData, chars, phraseIndex) {
let i;
let j;
let an;
sysData.state = id.NOMATCH;
sysData.phraseLength = 0;
const ai = phraseIndex;
an = 0;
for (i = ai; i < chars.length; i += 1) {
if (chars[i] === 65 || chars[i] === 97) {
an += 1;
} else {
break;
}
}
if (an === 0) {
return;
}
const bi = ai + an;
const bend = bi + an;
if (bend > chars.length) {
return;
}
for (i = ai, j = bi; j < bend; i += 1, j += 1) {
if (chars[j] === 66) {
if (chars[i] !== 65) {
return;
}
} else if (chars[j] === 98) {
if (chars[i] !== 97) {
return;
}
} else {
return;
}
}
const ci = bi + an;
const cend = ci + an;
if (cend > chars.length) {
return;
}
for (i = ai, j = ci; j < cend; i += 1, j += 1) {
if (chars[j] === 67) {
if (chars[i] !== 65) {
return;
}
} else if (chars[j] === 99) {
if (chars[i] !== 97) {
return;
}
} else {
return;
}
}
sysData.state = id.MATCH;
sysData.phraseLength = 3 * an;
};
try {
let grammar;
let exp;
let result;
let str;
grammar = 'anbncn = u_phrase\n';
str = 'aaAAaabbBBbbccCCcc';
exp = new ApgExp(grammar);