[web-devel] Re: Using a 'real' parser for URL parsing

Jeremy Shaw jeremy at n-heptane.com
Sat Mar 27 22:26:50 EDT 2010


Ok, I pushed some patches to PathInfo for the parsec stuff. It now requires
parsec 3.1.0 (which is sort of a problem, but oh well).

I change the pToken function so that it tracks the error position better,
and updated showParseError to use that information better. I also added a
function:

p2u :: Parser a -> URLParser a

so now if you write:

test :: IO ()
test =
  let segments = ["foo", "hi", "there", "world"] in
  case parse testp (show segments) segments of
    (Left e) -> putStrLn $ showParseError e
    (Right r) -> print r

testp :: URLParser (Char, String, String)
testp =
  do segment "foo"
     st <- p2u (char 'h' >> char 'o')
     sg <- anySegment
     sg' <- anySegment
     return (st,sg, sg')

You will get:

*Web.Routes.PathInfo> test
["foo","hi","there","world"] (segment 2 character 2):
unexpected "i"
expecting "o"
*Web.Routes.PathInfo>

This seems nice? The error tells you the segment and character offset in the
segment, and you can embed normal parsers from Text.Parsec.Char into the
parent parser and the errors in the Char parser give the correct character
offset.

One unresolved issue is if the sub-parser passed to p2u should be required
to consume the entire segment or not. I am thinking that it should be
required to consume the entire segment -- though the current code does not
work that way.

- jeremy

On Sat, Mar 27, 2010 at 9:17 AM, Chris Eidhof <chris at eidhof.nl> wrote:

> There's probably a better way, but I guess I would write it in a similar
> way.
>
> -chris
>
> On 26 mrt 2010, at 23:04, Jeremy Shaw wrote:
>
> > Hello,
> >
> > Is there some clean way to implement a function like:
> >
> > c2s :: GenParser Char () a -> GenParser String () a
> >
> > so that we can use Char parsers inside the URL Parser?
> >
> > the trivial implementation would be something like:
> >
> > http://moonpatio.com:8080/fastcgi/hpaste.fcgi/view?id=8965#a8965
> >
> > but that does not do a very good job of reporting the position of parse
> errors.
> >
> > - jeremy
> >
> > On Fri, Mar 26, 2010 at 4:11 PM, Chris Eidhof <chris at eidhof.nl> wrote:
> > Hey,
> >
> > I've added a darcs patch (I hope it worked, I'm a darcs newbie) to switch
> the URL parser to parsec. This simplifies the code a lot. I've used String's
> (i.e. the URL path components) as tokens for the parser. What do you guys
> think?
> >
> > I'm sorry I haven't been replying to the type-safe URL handling, I was
> busy with writing this week. The discussion has become quite large! I'll see
> if I can catch up somewhere this weekend.
> >
> > -chris
> >
> >
> >
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/web-devel/attachments/20100327/adaa78da/attachment.html


More information about the web-devel mailing list