I'm writing a parser for a logfile. One of the lines in the logfile lists the parameters of an HTTP request:
Parameters: {"back"=>"true", "embed_key"=>"12affbbace", "action"=>"index", "ajax"=>"1", "controller"=>"heyzap", "embed"=>"1"}
I'm having trouble parsing this with Attoparsec. My basic idea is to parse and discard Parameters: {
, then keep the text up to }
. Then I'll parse that text into a list of (key, value)
tuples. Here's what I've got so far:
parseParams :: Parser [(Text, Text)]
parseParams = do
paramString <- " Parameters: {" *> takeTill' (== '}')
let params = splitOn ", " paramString
-- I'm not sure how to apply parseParamPair to params here
parseParamPair :: Parser (Text, Text)
parseParamPair = do
key <- parseKeyOrValue
value <- string "=>" >> parseKeyOrValue
return (key, value)
where
parseKeyOrValue :: Parser Text
parseKeyOrValue = char '"' >> takeTill' (== '"')
takeTill' :: (Char -> Bool) -> Parser Text
takeTill' func = takeTill func <* skip func
How can I implement this? Should I be using Data.Attoparsec.Text.sepBy
somehow?