lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On Thu, Apr 2, 2009 at 1:01 PM, Jim Whitehead II <jnwhiteh@gmail.com> wrote:
> I'm working on a grammar in LPeg, but I'm running into an issue where
> I need to match whichever is the longest of two patterns, but I
> suspect I have a problem that I'm not seeing and I'm not sure how to
> fix the issue.  I would like to match lines of text of the following
> format:
>
> mydomain.com: some message text here
> jnwhiteh@mydomain.com: some message text here
>
> The : is just a leading character and the text following can be either
> a hostname or a user@hostname.  I'm using the following definitions:

Someone mentioned that I forgot to include the shortcuts so here they are:

require("lpeg")
local P, V, C = lpeg.P, lpeg.V, lpeg.C

> LETTER      = lpeg.R"az", "AZ"
> DIGIT       = lpeg.R"09"
> SPECIAL     = lpeg.S";[]\\`_^{|}!"
> PERIOD      = lpeg.P"."
> triple      = DIGIT * DIGIT^-2
> hostaddr    = triple * PERIOD * triple * PERIOD * triple * PERIOD * triple
> shortname   = (LETTER + DIGIT) * (LETTER + DIGIT + P"-")^0 * (LETTER + DIGIT)^0
> hostname    = shortname * (PERIOD * shortname)^0
> host        = hostname + hostaddr
> user        = LETTER * (LETTER + DIGIT + SPECIAL)^-8
> userathost  = user + P"@" + host
> source      = host + userathost
> params      = P" :" * (LETTER + DIGIT + SPECIAL + P" ")^-1
> line        = P":" * source * params

There was also an error in the above definitions, userathost should be
user * P"@" * host, instead of using alternation.  The updated code is
here:

http://pastey.net/111459

- Jim