[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: Sat, 18 Aug 2018 00:52:56 -0400
It was thus said that the Great Albert Chan once stated:
> On Aug 17, 2018, at 8:33 PM, Sean Conner <sean@conman.org> wrote:
>
> > The following code presents the bug:
> >
> > url = require "org.conman.parsers.url.url"
> > lpeg = require "lpeg"
> >
> > x = url * lpeg.Cp()
> >
> > a,b = x:match "/status" print(b) -- prints 8, okay
> > a,b = x:match "/status/" print(b) -- prints 9, okay
> > a,b = x:match "/status " print(b) -- prints 8, okay
> > a,b = x:match "/status/ " print(b) -- prints 8, WAT?
>
> Hi, Sean
>
> Is the expected result 9 ? 10 ?
The expected result is nine.
I found the error trying to parse HTTP requests as such:
local request = Ct(
Cg(Method, 'method') * abnf.WSP
* Cg(url, 'location') * abnf.WSP
-- ^^^ this is org.conman.parsers.url.url
* Cg(version,'version')
)
It was a piece of a larger expression. It worked fine when the request
was:
GET /status HTTP/1.1
but failed to parse
GET /status/ HTTP/1.1
since the pieces being parsed were:
"GET" captured
" " parsed but ignored
"/status" captured
" " parsed but ignored
"/ HTTP/1.1" oops, this isn't a valid version ...
With the help from Daurnimator, I was able to get the parse correct, but
it revealed an issue I didn't see (but is obvious in hindsight) due to how I
store that part of the URL structure. [1].
Thank you for your try at this.
-spc
[1] Now that I think about it, I think I can live with the result.