Converters
Converters — scripts that are used to interpret the values of the text in each token.
Converters allow you to convert data tokens placed in $parseTree.
Declaration
Converter declaration:
- when declaring a pattern;
patterns:
$four = four|| converter = function() {return 4}- under
inittags;
init:
function amountConverter(pt) {
var ret = pt.Number[0].value;
return ret
}- in the
.js-library file, for exampleconverters.js.
function amountConverter(pt) {
var ret = pt.Number[0].value;
return ret
}Functions of the converters get $parseTree as the first argument.
You can use inside a converter the variable refering to $parseTree. To do this, set its name as the first argument of the function; the second argument is the context.
Usage
Using converters:
- Get value from the text.
Script:
$Digit = $regexp<\d+> || converter = numberConverterDigit.js-file:
function numberConverterDigit(parseTree) {
return parseInt(parseTree.text);
}- Convert value from mapping.
Script:
$Numeral = (one:1|...) || converter = valueToNumberConverter.js-file:
function valueToNumberConverter(parseTree) {
return parseInt(parseTree.value);
}- Get value from nested tokens.
Script:
$Numeral = (one:1|...)
$Minutes = $Numeral || converter = minutesConverter.js-file:
function minutesConverter(parseTree) {
return parseInt(parseTree.Numeral.value)
};- Get value from dictionaries.
Script:
$City = $entity<Cities> || converter = сityConverter.js-file:
function сityConverter(parseTree) {
var id = parseTree.City[0].value;
return Cities[id].value;
}The $entity rule stores as a value only the entity identifier. The list of associated values is contained in the dictionary.