Last active
March 3, 2016 15:25
-
-
Save cvk77/244bcdf7eb4f0f049221 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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