Add initializer example in README.md

redux
Tony Lukasavage 11 years ago
parent 791034fad9
commit 35a4b35f94

@ -187,7 +187,32 @@ braces (“{” and “}”). This code is executed before the generated parser
parsing. All variables and functions defined in the initializer are accessible
in rule actions and semantic predicates. The code inside the initializer can
access options passed to the parser using the `options` variable. Curly braces
in the initializer code must be balanced.
in the initializer code must be balanced. Let's look at the example grammar
from above using a simple initializer.
{
function makeInteger(o) {
return parseInt(o.join(""), 10);
}
}
start
= additive
additive
= left:multiplicative "+" right:additive { return left + right; }
/ multiplicative
multiplicative
= left:primary "*" right:multiplicative { return left * right; }
/ primary
primary
= integer
/ "(" additive:additive ")" { return additive; }
integer "integer"
= digits:[0-9]+ { return makeInteger(digits); }
The parsing expressions of the rules are used to match the input text to the
grammar. There are various types of expressions — matching characters or

Loading…
Cancel
Save