Parsec:跳过第一线

时间:2014-03-27 07:11:55

标签: haskell parsec

我编写了一个parsec代码,可以完美地满足我的需求。它按预期解析以下文件:

4,5
6,7

相应的代码如下:

import Text.ParserCombinators.Parsec
import Control.Applicative hiding ((<|>))
import Control.Monad

data Test = Test Integer Integer deriving Show

integer :: Parser Integer
integer = rd <$> many1 digit
    where rd = read :: String -> Integer

testParser :: Parser Test
testParser = do
  a <- integer
  char ','
  b <- integer
  return $ Test a b

testParserFile = endBy testParser eol

eol :: Parser Char
eol = char '\n'

main = do
  a <- parseFromFile testParserFile "./jack.txt"
  print a

但我的实际文件是这样的:

col 1,col 2
4,5
6,7

有没有办法制作上面的解析器,只是跳过第一行?

1 个答案:

答案 0 :(得分:4)

testParserFile = manyTill anyChar newline *> endBy testParser eol

manyTill p endend成功之前应用p。 *>对两个动作进行排序并丢弃第一个值。

注意:如果您的实际文件最后不包含换行符,则需要使用sepEndBy而不是endBy。但是,这可能是StackOverflow上的Markdown解析器的结果。

相关问题