[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: LPeg Question
- From: Peter Cawley <lua@...>
- Date: Thu, 2 Apr 2009 13:58:08 +0100
> LETTER = lpeg.R"az", "AZ"
This doesn't do what it implies. the "AZ" is interpreted as the second
clause in a multi-part assignment, not as the second parameter to R.
> * The string "mydomain.com" is matched by userathost as well as host
I cannot reproduce this:
/home/user> lua
Lua 5.1.4 Copyright (C) 1994-2008 Lua.org, PUC-Rio
> require "lpeg"
> print(lpeg.version())
0.9
> dofile "bin.lua" -- your code
> = userathost:match "mydomain.com"
nil
> = host:match "mydomain.com"
13
> * The string "jnwhiteh@mydomain.com" is partially matched by host and
fully matched by userathost
I would change the order of the "source" declaration. The + operator
is the ordered choice operator, thus making the order of its operands
important. Ordering it as "userathost + host" will make it try and
match userathost first:
> = source:match "fred@bob.com"
5
> source = userathost + host
> = source:match "fred@bob.com"
13
On Thu, Apr 2, 2009 at 1:25 PM, Jim Whitehead II <jnwhiteh@gmail.com> wrote:
> 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
>