@@ -67,13 +67,13 @@ class Arithmetic < Grammy::Grammar
6767 root :expression
6868
6969 # Define the rules.
70- rule(:expression ) { term + (match (" +" ) + term)[0 ..] }
71- rule(:term ) { factor + (match (" *" ) + factor)[0 ..] }
70+ rule(:expression ) { term + (str (" +" ) + term)[0 ..] }
71+ rule(:term ) { factor + (str (" *" ) + factor)[0 ..] }
7272 rule(:factor ) { number | parens(expression) }
73- rule(:number ) { match (/\d +/ ) }
73+ rule(:number ) { reg (/\d +/ ) }
7474
7575 # Define any custom combinators.
76- def parens (exp ) = match (" (" ) + expression + match (" )" )
76+ def parens (exp ) = str (" (" ) + expression + str (" )" )
7777end
7878~~~
7979
@@ -87,13 +87,13 @@ class Arithmetic < Grammy::Grammar
8787 root :expression
8888
8989 # Define the rules.
90- rule def expression = term + (match (" +" ) + term)[0 ..]
91- rule def term = factor + (match (" *" ) + factor)[0 ..]
90+ rule def expression = term + (str (" +" ) + term)[0 ..]
91+ rule def term = factor + (str (" *" ) + factor)[0 ..]
9292 rule def factor = number | parens(expression)
93- rule def number = match (/\d +/ )
93+ rule def number = reg (/\d +/ )
9494
9595 # Define any custom combinators.
96- def parens (exp ) = match (" (" ) + expression + match (" )" )
96+ def parens (exp ) = str (" (" ) + expression + str (" )" )
9797end
9898~~~
9999
@@ -107,13 +107,13 @@ class Arithmetic < Grammy::Grammar
107107 start :expression
108108
109109 # Define the rules.
110- def expression = rule { term + (match (" +" ) + term)[0 ..] }
111- def term = rule { factor + (match (" *" ) + factor)[0 ..] }
110+ def expression = rule { term + (str (" +" ) + term)[0 ..] }
111+ def term = rule { factor + (str (" *" ) + factor)[0 ..] }
112112 def factor = rule { number | parens(expression) }
113- def number = rule { match (/\d +/ ) }
113+ def number = rule { reg (/\d +/ ) }
114114
115115 # Define any custom combinators.
116- def parens (exp ) = match (" (" ) + expression + match (" )" )
116+ def parens (exp ) = str (" (" ) + expression + str (" )" )
117117end
118118~~~
119119
@@ -124,23 +124,31 @@ Combinators are functions that take one or more parsers as arguments and return
124124
125125Only a few primitive combinators are needed.
126126
127- ### Match
127+ ### String
128128
129- The ` match ` combinator is used to match a string or a regular expression .
129+ The ` str ` combinator is used to match a string.
130130
131131~~~ ruby
132- match(" return" )
133- match(/\d +/ )
132+ str(" return" )
134133~~~
135134
135+ ### Regex
136+
137+ The ` reg ` combinator is used to match a regular expression.
138+
139+ ~~~ ruby
140+ reg(/\d +/ )
141+ ~~~
142+
143+
136144### Sequence
137145
138146You use the ` seq ` combinator to specify a sequence of what to match.
139147
140148This example matches a sequence of a term, a plus sign, and another term.
141149
142150~~~ ruby
143- seq(term, match (" +" ), term)
151+ seq(term, str (" +" ), term)
144152~~~
145153
146154### Alternatives
@@ -150,14 +158,15 @@ The `alt` combinator is used to specify alternatives (multiple choices) of what
150158This example matches either a plus sign or a minus sign.
151159
152160~~~ ruby
153- alt(match (" +" ), match (" -" ))
161+ alt(str (" +" ), str (" -" ))
154162~~~
155163
156- NOTE: This would most likely done with a single ` match ` call:
157-
158- ~~~ ruby
159- match(/[+-] / )
160- ~~~
164+ > [ !NOTE]
165+ > In practical use, this would most likely be done with a single ` reg ` call:
166+ >
167+ > ~~~ ruby
168+ > reg(/[+-] / )
169+ > ~~~
161170
162171# ## Repetition
163172
@@ -167,7 +176,7 @@ You can specify the minimum and maximum number of repetitions, using a range.
167176This example matches one or more digits.
168177
169178~~~ ruby
170- rep(match (/\d +/ ), 1 ..)
179+ rep(reg (/\d +/ ), 1 ..)
171180~~~
172181
173182### Operator DSL
@@ -180,8 +189,8 @@ The `[]` operator can be used in place of the `rep` combinator.
180189For example, the following two lines are equivalent:
181190
182191~~~ ruby
183- seq(term, match (" +" ), term)
184- term + match (" +" ) + term
192+ seq(term, str (" +" ), term)
193+ term + str (" +" ) + term
185194~~~
186195
187196## Parse Tree
0 commit comments