
[英]How to return multiple parse failures within Parsec's monadic context?
[英]How to return multiple parsed ADTs in Parsec?
我正在使用Parsec来解析一个看起来像这样的文件:
## public_id
ANALOG-CLOCK
## name
Analog Wall Clock
Some more text...
## domain
Time measurement
More text here...
除了文件可以具有可变数量的节外,每个节均以“ ##”行开头,并且可以包含0+个内容行。
type Model = [Section]
data Section = Section String [String]
deriving Show
headingLine :: Parser String
headingLine = do
try (string "##")
spaces
title <- many1 (noneOf "\r\n")
char '\n'
return title
nonHeadingLine :: Parser String
nonHeadingLine = do
try (noneOf "#")
contents <- many1 (noneOf "\r\n")
char '\n'
return contents
section :: Parser Section
section = do
title <- headingLine
contentLines <- many1 nonHeadingLine
spaces
return $ Section title contentLines
model :: Parser Model
model = do
s1 <- section
s2 <- section
s3 <- section
return [s1, s2, s3]
--return $ many1 section
main :: IO ()
main = do
modelStr <- readFile "analog3.md"
let result = do parse model "" modelStr
case result of
Left err -> putStrLn $ "Error " ++ show err
Right mdl -> putStrLn $ show mdl
我希望输出看起来像这样:
[Section "public_id" "ANALOG-CLOCK",Section "name" "Analog Wall Clock",Section "domain" "Time measurement"]
当我每节只有一条内容行并产生上面的输出时,代码起作用。
我有两个问题:
1)当我每个部分有多个内容行时,该代码不起作用,并且
2)当我使用return $ many1 section
而不是return [s1, s2, s3]
,出现类型错误“无法匹配类型'Text.Parsec.Prim.ParsecT String()Data.Functor.Identity.Identity [ Section]'和'[Section]'“。
如何处理多个内容行?如何处理多个部分? 谢谢。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.