Skip to content

Instantly share code, notes, and snippets.

@mathfur
Created May 30, 2010 14:10
Show Gist options
  • Save mathfur/419052 to your computer and use it in GitHub Desktop.
Save mathfur/419052 to your computer and use it in GitHub Desktop.
-- foo.txtを読み込んで行番号をつけて表示
-- ※同ディレクトリにApplicativeParsec.hsが要る
import ApplicativeParsec
p :: CharParser () [PLine]
p = many line
eol = string "\n"
letters = many1 letter
data PLine = PLine Int String deriving Eq
instance Show PLine where
show (PLine i str) = (show i) ++ ": " ++ str
line :: CharParser () PLine
line = do
i <- getPosition
str <- letters
eol
return $ PLine (sourceLine i) str
main = do
cs <- readFile "foo.txt"
case parse p "(stdin)" cs of
Left e -> do putStrLn "Error parsing input:"
print e
Right r -> mapM_ (putStrLn.show) r
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment