如何在 Parsec 中停止 endBy

How to stop a endBy in Parsec

我正在尝试使用 Parsec 编写 csv-like 解析器。到目前为止,一切都很好。 解析器解码 header 并处理一切正常。 现在,我试图跳过文件开头的一些注释。注释以 #(或空行)开头。 如果我这样做,endBy 循环不会在 header 开始时而是失败。

这是我的代码:

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'