我正在使用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
字符串()Data.Functor.Identity.Identity [Section]”和“ [Section]””。
如何使它处理多个内容行以及如何使它处理多个部分?谢谢。