在查看 Real World Haskell 中的 CSV 示例代码后,我尝试构建一个小的 XML 解析器。但是关闭标签会出现“意外的“/”错误。你能告诉我为什么我的“closeTag”解析器不起作用(或者可能从来没有被调用过)吗?谢谢!
import Text.ParserCombinators.Parsec
xmlFile = manyTill line eof
line = manyTill tag eol
eol = char '\n'
word = many1 (noneOf "></")
tag = choice [openTag, closeTag, nullTag, word]
nullTag = between (char '<') (string "/>") word
closeTag = between (string "</") (char '>') word
openTag = between (char '<') (char '>') tagContent
attrval = between (char '"') (char '"') word
atts = do {
(char ' ')
; sepBy attr (char ' ')
}
attr = do {
word
; char '='
; attrval
}
tagContent = do {
w <- word
; option [] atts
; return w
}
parseXML :: String -> Either ParseError [[String]]
parseXML input = parse xmlFile "(unknown)" input
main =
do c <- getContents
case parse xmlFile "(stdin)" c of
Left e -> do putStrLn "Error parsing input:"
print e
Right r -> mapM_ print r