From 35a4b35f94fd7e2ff9221ca7752d36119a2366cb Mon Sep 17 00:00:00 2001 From: Tony Lukasavage Date: Fri, 2 Aug 2013 11:31:37 -0400 Subject: [PATCH] Add initializer example in README.md --- README.md | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 5f2c611..aa1d36a 100644 --- a/README.md +++ b/README.md @@ -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