[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: There's a bug in my LPeg code, but I can't find it
- From: Sean Conner <sean@...>
- Date: Fri, 17 Aug 2018 23:13:14 -0400
It was thus said that the Great Daurnimator once stated:
> On 18 August 2018 at 12:12, Daurnimator <quae@daurnimator.com> wrote:
> > On 18 August 2018 at 10:33, Sean Conner <sean@conman.org> wrote:
> >> Possibly related:
> >>
> >> a,b = x:match "/status#a" print(b) -- prints 10 okay
> >> a,b = x:match "/status/#a" print(b) -- prints 8 WAT?
> > Your segment definition is incorrect: you have "either followed by end
> > of string, or at least one path character"
> > It should instead be "any number of path characters". i.e.
> >
> > segment <- {~ pchar* ~}
>
> I forgot to answer the second half here; though it's relatively the same answer.
>
> path_empty <- ! . {| |}
>
> Should be
>
> path_empty <- {| |}
I made the following changes:
diff --git a/url/url.lua b/url/url.lua
index 821a9ac..09b90ba 100644
--- a/url/url.lua
+++ b/url/url.lua
@@ -72,8 +72,8 @@ path_abempty <- {| {:root: %istrue :} ( '/' segment)* |}
path_absolute <- {| {:root: %istrue :} '/' (segment_nz ('/' segment)* )? |}
path_noscheme <- {| segment_nz_nc ('/' segment)* |}
path_rootless <- {| segment_nz ('/' segment)* |}
-path_empty <- ! . {| |}
-segment <- ! . / {~ pchar+ ~}
+path_empty <- {| |}
+segment <- {~ pchar* ~}
segment_nz <- {~ pchar+ ~}
segment_nz_nc <- {~ (unreserved / pct_encoded / sub_delims / ';' / '@')+ ~}
pchar <- unreserved / pct_encoded / sub_delims / ':' / '@'
And I still get the same results:
url = require "org.conman.parsers.url.url"
lpeg = require "lpeg"
x = url * lpeg.Cp()
_,b = x:match "/status" print(b) -- 8, which is right
--^ here is position 8
_,b = x:match "/status/" print(b) -- 9, which is right
--^ here is position 9
_,b = x:match "/status " print(b) -- 8, which is right
--^ here is position 8
_,b = x:match "/status/ " print(b) -- 8, which is incorrect, shoule be 9
--^ here is *WHERE* is should be
--^ here is where it's returning
> A simple principle to work with is that you should almost never use
> EOF in a pattern that you intend to compose: leave that for the user.
> e.g. https://github.com/daurnimator/lua-http/blob/e3ed1a0a2a2eab4d149ea8560b1a9740bc54a8f9/http/request.lua#L46
Fair enough.
-spc