Skip to content

Instantly share code, notes, and snippets.

@cvk77
Last active March 3, 2016 15:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cvk77/244bcdf7eb4f0f049221 to your computer and use it in GitHub Desktop.
Save cvk77/244bcdf7eb4f0f049221 to your computer and use it in GitHub Desktop.
import Text.ParserCombinators.Parsec
{-# ANN module "HLint: ignore Redundant do" #-}
value :: Parser String
value = do
many1 (alphaNum <|> char '+' <|> char '-')
part :: Parser String
part = do
char '/'
value
country :: Parser String
country = do
string "http"
optional (char 's')
string "://example.org"
part
commaDelimited :: Parser [String]
commaDelimited =
value `sepBy` char ','
packs :: Parser [String]
packs = do
string "/packs/"
commaDelimited
options :: Parser [String]
options = do
string "/options/"
commaDelimited
accessories :: Parser [String]
accessories = do
string "/accessories/"
commaDelimited
customization :: Parser [String]
customization = do
choice [try packs, try accessories, try options]
vehicle :: Parser [String]
vehicle = do
string "/vehicle"
manyTill part (lookAhead customization)
theRest :: Parser ()
theRest =
skipMany anyChar
parser :: Parser (String, [String], [[String]])
parser = do
country <- country
vehicle <- vehicle
customization <- many1 customization
theRest
eof
return (country, vehicle, customization)
main :: IO ()
main = do
let demoUrl = "https://example.org/de/vehicle/trabant/5-doors/0815+universal/options/1,4711,815/packs/p7/accessories/a,b,c/width/1024/height/768/exterior-45.jpg"
print $ parse parser "" demoUrl
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment