README in citrus-1.3.0 vs README in citrus-1.4.0
- old
+ new
@@ -234,25 +234,17 @@
matches when they are created and is used to lazily extend them when we want to
interpret them. The following example shows one way to do this.
grammar Addition
rule additive
- (number plus term) {
+ (number plus term:(additive | number)) {
def value
number.value + term.value
end
}
end
- rule term
- (additive | number) {
- def value
- first.value
- end
- }
- end
-
rule number
([0-9]+ space) {
def value
text.strip.to_i
end
@@ -266,29 +258,24 @@
rule space
[ \t]*
end
end
-In this version of the grammar the additive rule has been refactored to use the
-term rule. This makes it a little cleaner to define our semantic blocks. It's
+In this version of the grammar we have added two semantic blocks, one each for
+the additive and number rules. These blocks contain methods that will be present
+on all match objects that result from matches of those particular rules. It's
easiest to explain what is going on here by starting with the lowest level
block, which is defined within the number rule.
The semantic block associated with the number rule defines one method, value.
-This method will be present on all matches that result from this rule. Inside
-this method, we can see that the value of a number match is determined to be
-its text value, stripped of white space and converted to an integer.
+Inside this method, we can see that the value of a number match is determined to
+be its text value, stripped of white space and converted to an integer.
-Similarly, the block that is applied to term matches also defines a value
-method. However, this method works a bit differently. Since a term matches an
-additive or a number a term match will contain one submatch, the match that
-resulted from either additive or number. The first method retrieves the first
-submatch. So, the value of a term is determined to be the value of its first
-submatch.
-
-Finally, the additive rule also extends its matches with a value method. Here,
-the value of an additive is determined to be the values of its number and term
-matches added together using Ruby's addition operator.
+The additive rule also extends its matches with a value method. Notice the use
+of the "term" label within the rule definition. This label allows the match that
+is created by either the additive or the number rule to be retrieved using the
+"term" label. The value of an additive is determined to be the values of its
+number and term matches added together using Ruby's addition operator.
Since additive is the first rule defined in the grammar, any match that results
from parsing a string with this grammar will have a value method that can be
used to recursively calculate the collective value of the entire match tree.