我正在为日志文件编写解析器。日志文件中的其中一行列出了 HTTP 请求的参数:
Parameters: {"back"=>"true", "embed_key"=>"12affbbace", "action"=>"index", "ajax"=>"1", "controller"=>"heyzap", "embed"=>"1"}
我无法用 Attoparsec 解析它。我的基本想法是解析并丢弃Parameters: {
,然后将文本保持在}
. 然后我会将该文本解析为一个(key, value)
元组列表。这是我到目前为止所得到的:
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
我该如何实施?我应该以Data.Attoparsec.Text.sepBy
某种方式使用吗?