Phrogz
5/5/2008 3:46:00 PM
On May 5, 9:10 am, Phrogz <phr...@mac.com> wrote:
> The benefit of the PEG (re-usable sub-expressions) may not outweigh
> the multi-line nature.
To expand on this a little bit more, and use an LPEG-like custom
grammar entirely in Ruby, using PEG instead of regexp would allow you
to do something like:
header = IPEG[17,34,15,12,89,127]
break = IPEG[13,10]
close = IPEG[0]
body = (IPEG[12,43,13,break,57,75] | IPEG[18,13,31])
message_style_1 = header + body + close
message_style_2 = header + IPEG[108,103] + break + body + close
message = message_style_1 | message_style_2
result = message.match( my_array_of_numbers )
As you can see, this allows you to break up complex expressions into
named parts. This makes it both easier to understand, and DRYs up the
expression.
In the above made up syntax:
* "IPEG" stands for "Integer Parsing Expression Grammar"
* The IPEG.[] notation might represent a literal sequence of
integers
* The + operator would combine sequences
* The | operator provides alternation