我正在尝试解析一些文本,但我无法理解如何解析由某个分隔符分隔的符号列表,这些符号可能也可能不会出现在列表的末尾。
示例(以空格分隔的数字):
set A = 1 2 3 4 5;
set B =6 7 8 9;
set C = 10 11 12 ;
如果我使用sepBy
,在最后一个空格之后我收到错误,因为它期望另一个数字,即使我尝试在列表之后也读取many whitespace
。如果我使用endBy
,则在空格丢失时出现错误。
import Text.ParserCombinators.Parsec
main :: IO ()
main = do
let input = "set A = 1 2 3 4 5;\n" ++
"set B =6 7 8 9;\n" ++
"set C = 10 11 12 ;\n"
case parse parseInput "(unknown)" input of
Left msg ->
print msg
Right rss ->
mapM_ (\(n, vs) -> putStrLn (n ++ " = " ++ show vs)) rss
whitespace :: GenParser Char st Char
whitespace = oneOf " \t"
parseInput :: GenParser Char st [(String, [Int])]
parseInput = parseRow `endBy` newline
parseRow :: GenParser Char st (String, [Int])
parseRow = do
string "set"
many1 whitespace
name <- many1 alphaNum
many whitespace
string "="
many whitespace
values <- many1 digit `sepBy` many1 whitespace
many whitespace
string ";"
return (name, map read values)
答案 0 :(得分:8)
我认为你想要的组合子是sepEndBy
。用它给你
-- I'm using the type synonym
-- type Parser = GenParser Char ()
-- from Text.ParseCombinator.Parsec.Prim
parseRow :: Parser (String, [Int])
parseRow = do
string "set" >> many1 whitespace
name <- many1 alphaNum
spaces >> char '=' >> spaces
values <- many1 digit `sepEndBy` many1 whitespace
char ';'
return (name, map read values)
where spaces = many whitespace