我为一种语言编写parsec规则时遇到问题,而我却有另一种语言定义(问题部分) WS代表空格,而LITERAL是除空格或带引号的字符以外的任何字符(可以包含空格),因此,我编写了下一个函数: 问题是符号“;” 是有效的文字,因此(1)函数对其进行了解析,因此存在解析错误,因为( ...
提示:本站收集StackOverFlow近2千万问答,支持中英文搜索,鼠标放在语句上弹窗显示对应的参考中文或英文, 本站还提供 中文繁体 英文版本 中英对照 版本,有任何建议请联系yoyou2525@163.com。
我正在尝试使用Parsec编写类似csv的解析器。 到现在为止还挺好。 解析器解码标头,并处理一切正常。 现在,我试图在文件开头跳过一些注释。 注释以#
(或空行)开头。 如果这样做, endBy
循环不会在标头启动时出现,而是会失败。
这是我的代码:
csvParser = do
-- skipping comment bit
P.endBy ((P.char '#' >> P.many (P.noneOf "\n"))
<|> P.many P.space
) eol
-- real parsing starting
header <- parseHeader
eol
case header of
["style", "number", "quantity", "length", "width", "height"] -> parsePL
otherwise -> error $ "Can't decore following header:" ++ (show header)
where parseHeader = P.sepBy cell sep
sep = P.char ','
eol = P.char '\n'
cell = P.many (P.noneOf ",\n")
cellp = do x <- cell ; sep; return x
today = "2015/01/15" :: Date
readR :: String -> Rational
readR x = toRational x' where
x' = read x :: Float
parsePL = P.endBy (do
style <- cellp
numberOfBox <- read <$> cellp
numberPerBox <- cellp
length <- readR <$> cellp
width <- readR <$> cellp
height <- readR <$> cell
return (style, numberOfBox, length, width, height, "", 0, "", today)
) eol
我发现了问题: space
包含换行符'\\n'
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.