The AST
translating callback functions. These are similar to the parsers syntax callback functions
in that they are each called twice, once down the tree and once up the tree, with matched phrase information.
With the AST
, however, the matched phrase is known in the down direction as well as up.
- state -
SEM_PRE
(down) or SEM_POST
(up)
- chars - the array of character codes for the entire input string.
- phraseIndex - the index in chars of the first character of the matched phrase
- phraseLength - the number of characters in the matched phrase
- data - the user’s optional data object, passed to the translator when it is called
- return value - normally
SEM_OK
. Can also be SEM_SKIP
in the SEM_PRE
state to skip the translation of the branch below the current node.
const phoneNumber = function (state, chars, phraseIndex, phraseLength, data) {
const ret = id.SEM_OK;
if (state === id.SEM_PRE) {
if (Array.isArray(data) === false) {
throw new Error("parser's user data must be an array");
}
data.length = 0;
} else if (state === id.SEM_POST) {
}
return ret;
};
const areaCode = function (state, chars, phraseIndex, phraseLength, data) {
const ret = id.SEM_OK;
if (state === id.SEM_PRE) {
data['area-code'] = apgLib.utils.charsToString(chars, phraseIndex, phraseLength);
} else if (state === id.SEM_POST) {
}
return ret;
};
const office = function (state, chars, phraseIndex, phraseLength, data) {
const ret = id.SEM_OK;
if (state === id.SEM_PRE) {
data.office = apgLib.utils.charsToString(chars, phraseIndex, phraseLength);
} else if (state === id.SEM_POST) {
}
return ret;
};
const subscriber = function (state, chars, phraseIndex, phraseLength, data) {
const ret = id.SEM_OK;
if (state === id.SEM_PRE) {
data.subscriber = apgLib.utils.charsToString(chars, phraseIndex, phraseLength);
} else if (state === id.SEM_POST) {
}
return ret;
};
parser.ast.callbacks['phone-number'] = phoneNumber;
parser.ast.callbacks['area-code'] = areaCode;
parser.ast.callbacks.office = office;
parser.ast.callbacks.subscriber = subscriber;
const inputString = 'xxxx(555)234-5678xxxx';
const inputCharacterCodes = apgLib.utils.stringToChars(inputString);
const startRule = 'phone-number';
const result = parser.parseSubstring(grammar, startRule, inputCharacterCodes, 4, 13);
console.log();
console.log("the parser's results");
console.dir(result, inspectOptions);
if (result.success === false) {
throw new Error(`input string: '${inputString}' : parse failed`);
}