2

I'm trying to parse URLs of the form

/123/456/789

using the following code:

{-# LANGUAGE OverloadedStrings, TemplateHaskell, TypeOperators #-}

import Prelude hiding ((.), id)
import Control.Category ((.), id)
import Text.Boomerang.TH (derivePrinterParsers)
import Web.Routes.Boomerang

data Indices = Indices [Integer]
$(derivePrinterParsers ''Indices)

sitemap :: Router () (Sitemap :- ())
sitemap = rIndices . rList (integer . eos)

Unfortunately, trying to run this parser with

> parse sitemap ["0", "1"]

causes an infinite loop.

Is there any simple way to parse a list of slash-separated integers?

4

2 回答 2

3

It seems that there is a bug somewhere in Text.Boomerang.Texts.

{-# LANGUAGE OverloadedStrings #-}

import Prelude hiding ((.))
import Control.Category ((.))

import Text.Boomerang
import Text.Boomerang.Strings as S
import Text.Boomerang.Texts as T

test1 = parseStrings (rList (S.integer . S.eos)) ["0", "1"]
test2 = parseTexts   (rList (T.integer . T.eos)) ["0", "1"]

test1 returns expected result but test2 loops forever, though they should be semantically equivalent.

*Main> test1
Right [0,1]
*Main> test2
^CInterrupted.

This is reproducible with both versions of boomerang that contain Text.Boomerang.Texts module: 1.3.2 and 1.3.3.

Back to parsing URLs. If you don't mind to get rid of boomerang package completely, you can do it like this:

import Data.Text (Text)                                                                                                                                                                       
import qualified Data.Text as T
import qualified Data.Text.Read as T

parseURL :: Text -> Either String [Int]
parseURL = fmap (map fst) . mapM T.decimal . T.splitOn "/"

Here is a test case:

*Main> parseURL "123/456/789"
Right [123,456,789]
*Main> parseURL "123/456/789/"
Left "input does not start with a digit"
于 2013-08-25T11:37:55.553 回答
2

Sorry about the delay! I was at that thing in the desert. This is indeed a bug. I have uploaded boomerang-1.3.4 which contains this change:

hunk ./Text/Boomerang/Texts.hs 193
-digits = rText digit
+digits = rText1 digit

With that fix the Text version behaves like the String version. Thanks for the report!

于 2013-09-06T15:20:29.177 回答