RunParsers.hs revision 605065f212e589cd3ea24be7db99a71aeb858215
01aafb6a9520f05df5ff467b591ecb5474dcfc86Christian Maeder{- |
01aafb6a9520f05df5ff467b591ecb5474dcfc86Christian MaederModule : $Header$
e6d40133bc9f858308654afb1262b8b483ec5922Till MossakowskiDescription : test some parsers (and printers)
1549f3abf73c1122acff724f718b615c82fa3648Till MossakowskiCopyright : (c) Christian Maeder and Uni Bremen 2002-2003
97018cf5fa25b494adffd7e9b4e87320dae6bf47Christian MaederLicense : GPLv2 or higher, see LICENSE.txt
01aafb6a9520f05df5ff467b591ecb5474dcfc86Christian Maeder
b4fbc96e05117839ca409f5f20f97b3ac872d1edTill MossakowskiMaintainer : Christian.Maeder@dfki.de
01aafb6a9520f05df5ff467b591ecb5474dcfc86Christian MaederStability : provisional
01aafb6a9520f05df5ff467b591ecb5474dcfc86Christian MaederPortability : portable
f3a94a197960e548ecd6520bb768cb0d547457bbChristian Maeder
e6d40133bc9f858308654afb1262b8b483ec5922Till Mossakowskitest some parsers (and printers)
1549f3abf73c1122acff724f718b615c82fa3648Till Mossakowski-}
01aafb6a9520f05df5ff467b591ecb5474dcfc86Christian Maeder
1549f3abf73c1122acff724f718b615c82fa3648Till Mossakowskimodule Common.RunParsers (exec, StringParser, toStringParser, fromAParser)
1549f3abf73c1122acff724f718b615c82fa3648Till Mossakowski where
1549f3abf73c1122acff724f718b615c82fa3648Till Mossakowski
1549f3abf73c1122acff724f718b615c82fa3648Till Mossakowskiimport Common.AnalyseAnnos (addGlobalAnnos)
a001917177db7ae636853b37c0d0f9f4e90a83ffChristian Maederimport Common.AnnoParser
a001917177db7ae636853b37c0d0f9f4e90a83ffChristian Maederimport Common.AnnoState
01aafb6a9520f05df5ff467b591ecb5474dcfc86Christian Maederimport Common.DocUtils
01aafb6a9520f05df5ff467b591ecb5474dcfc86Christian Maederimport Common.GlobalAnnotations
01aafb6a9520f05df5ff467b591ecb5474dcfc86Christian Maederimport Common.Lexer (parseString)
01aafb6a9520f05df5ff467b591ecb5474dcfc86Christian Maederimport Common.Parsec ((<<))
f8b715ab2993083761c0aedb78f1819bcf67b6ccChristian Maederimport Common.Result
01aafb6a9520f05df5ff467b591ecb5474dcfc86Christian Maederimport Common.IO
575a55eadc8dcab8ee350324b417cbd9e52e69c0Christian Maeder
01aafb6a9520f05df5ff467b591ecb5474dcfc86Christian Maederimport System.Environment
01aafb6a9520f05df5ff467b591ecb5474dcfc86Christian Maeder
5e46b572ed576c0494768998b043d9d340594122Till Mossakowskiimport Text.ParserCombinators.Parsec
db453fe9625a9dab5d108f7a5e464598814144b8Jian Chun Wangimport Text.ParserCombinators.Parsec.Pos
23a00c966f2aa8da525d7a7c51933c99964426c0Christian Maeder
575a55eadc8dcab8ee350324b417cbd9e52e69c0Christian Maedertype StringParser = GlobalAnnos -> AParser () String
575a55eadc8dcab8ee350324b417cbd9e52e69c0Christian Maeder
575a55eadc8dcab8ee350324b417cbd9e52e69c0Christian MaederfromAParser :: Pretty a => AParser () a -> StringParser
db453fe9625a9dab5d108f7a5e464598814144b8Jian Chun WangfromAParser p ga = fmap (flip (showGlobalDoc ga) "") p
575a55eadc8dcab8ee350324b417cbd9e52e69c0Christian Maeder
575a55eadc8dcab8ee350324b417cbd9e52e69c0Christian MaedertoStringParser :: Pretty a => (GlobalAnnos -> AParser () a) -> StringParser
a001917177db7ae636853b37c0d0f9f4e90a83ffChristian MaedertoStringParser p ga = fmap (flip (showGlobalDoc ga) "") $ p ga
a001917177db7ae636853b37c0d0f9f4e90a83ffChristian Maeder
58b671de3fe578346fef9642ffa3c5a0a0edb3cbTill Mossakowskiexec :: [(String, StringParser)] -> [(String, StringParser)] -> IO ()
01aafb6a9520f05df5ff467b591ecb5474dcfc86Christian Maederexec lps fps = do
0e2ae85e2453466d03c1fc5884a3d693235bb9d9Christian Maeder l <- getArgs
01aafb6a9520f05df5ff467b591ecb5474dcfc86Christian Maeder setStdEnc Utf8
ca020e82eb3567e7bdbb1cf70729efbd07e9caa4Klaus Luettich case l of
c438c79d00fc438f99627e612498744bdc0d0c89Christian Maeder [] -> parseSpec emptyGlobalAnnos . snd $ head fps
ca020e82eb3567e7bdbb1cf70729efbd07e9caa4Klaus Luettich opt : tl -> do
ca020e82eb3567e7bdbb1cf70729efbd07e9caa4Klaus Luettich let lps' = filter (\ (s, _) -> s == opt) lps
ca020e82eb3567e7bdbb1cf70729efbd07e9caa4Klaus Luettich fps' = filter (\ (s, _) -> s == opt) fps
ca020e82eb3567e7bdbb1cf70729efbd07e9caa4Klaus Luettich ga <- case tl of
ca020e82eb3567e7bdbb1cf70729efbd07e9caa4Klaus Luettich [] -> return emptyGlobalAnnos
a001917177db7ae636853b37c0d0f9f4e90a83ffChristian Maeder annoFile : _ -> do
a001917177db7ae636853b37c0d0f9f4e90a83ffChristian Maeder str <- readFile annoFile
58b671de3fe578346fef9642ffa3c5a0a0edb3cbTill Mossakowski maybe (error "run parser") return $ maybeResult
01aafb6a9520f05df5ff467b591ecb5474dcfc86Christian Maeder $ addGlobalAnnos emptyGlobalAnnos
01aafb6a9520f05df5ff467b591ecb5474dcfc86Christian Maeder $ parseString annotations str
01aafb6a9520f05df5ff467b591ecb5474dcfc86Christian Maeder -- should not fail but may return empty annos
4b6aa93c12e4db86ccc7694a48a73e9cf7262d06Christian Maeder case (lps', fps') of
a001917177db7ae636853b37c0d0f9f4e90a83ffChristian Maeder ([], []) -> do
b10d6cef708b7a659f2d3b367e8e0db0d03ae3f5Till Mossakowski putStrLn ("unknown option: " ++ opt)
b10d6cef708b7a659f2d3b367e8e0db0d03ae3f5Till Mossakowski p <- getProgName
01aafb6a9520f05df5ff467b591ecb5474dcfc86Christian Maeder putStrLn $ "Usage: " ++ p ++ " [OPTIONS] <Annotations> < infile"
8e80792f474d154ff11762fac081a422e34f1accChristian Maeder putStrLn "where OPTIONS is one of:"
26d11a256b1433604a3dbc69913b520fff7586acChristian Maeder putStrLn $ unwords $ map fst lps ++ map fst fps
26d11a256b1433604a3dbc69913b520fff7586acChristian Maeder ([], (_, hd) : _) -> parseSpec ga hd
26d11a256b1433604a3dbc69913b520fff7586acChristian Maeder ((_, hd) : _, _) -> checkLines ga hd
26d11a256b1433604a3dbc69913b520fff7586acChristian Maeder
26d11a256b1433604a3dbc69913b520fff7586acChristian MaedercheckLines :: GlobalAnnos -> StringParser -> IO ()
26d11a256b1433604a3dbc69913b520fff7586acChristian MaedercheckLines ga p =
26d11a256b1433604a3dbc69913b520fff7586acChristian Maeder getContents >>= putStr . unlines . scanLines ga p 1 . lines
01aafb6a9520f05df5ff467b591ecb5474dcfc86Christian Maeder
4b6aa93c12e4db86ccc7694a48a73e9cf7262d06Christian MaederscanLines :: GlobalAnnos -> StringParser -> Line -> [String] -> [String]
4b6aa93c12e4db86ccc7694a48a73e9cf7262d06Christian MaederscanLines ga p n inp = case inp of
4b6aa93c12e4db86ccc7694a48a73e9cf7262d06Christian Maeder [] -> []
4b6aa93c12e4db86ccc7694a48a73e9cf7262d06Christian Maeder x : l -> parseLine ga p x n : scanLines ga p (n + 1) l
a001917177db7ae636853b37c0d0f9f4e90a83ffChristian Maeder
4fc727afa544a757d1959ce77c02208f8bf330dcChristian MaederparseLine :: GlobalAnnos -> StringParser -> String -> Line -> String
0e2ae85e2453466d03c1fc5884a3d693235bb9d9Christian MaederparseLine ga p line n =
4b6aa93c12e4db86ccc7694a48a73e9cf7262d06Christian Maeder let pos = setSourceLine (initialPos "") n
5e46b572ed576c0494768998b043d9d340594122Till Mossakowski parser = do
5e46b572ed576c0494768998b043d9d340594122Till Mossakowski setPosition pos
5e46b572ed576c0494768998b043d9d340594122Till Mossakowski i <- p ga
4b6aa93c12e4db86ccc7694a48a73e9cf7262d06Christian Maeder eof
4b6aa93c12e4db86ccc7694a48a73e9cf7262d06Christian Maeder return i
4b6aa93c12e4db86ccc7694a48a73e9cf7262d06Christian Maeder in showParse $ runParser parser (emptyAnnos ()) "" line
4b6aa93c12e4db86ccc7694a48a73e9cf7262d06Christian Maeder
7a47fbe6b987bd69a5056ce5d00fc8710f6c5e8aChristian MaederparseSpec :: GlobalAnnos -> StringParser -> IO ()
a001917177db7ae636853b37c0d0f9f4e90a83ffChristian MaederparseSpec ga p =
e01299e9b22b96b31b720ca1e9f9f5f25af9b024Christian Maeder getContents >>= putStrLn . showParse
a001917177db7ae636853b37c0d0f9f4e90a83ffChristian Maeder . runParser (p ga << eof) (emptyAnnos ()) ""
e01299e9b22b96b31b720ca1e9f9f5f25af9b024Christian Maeder
7a47fbe6b987bd69a5056ce5d00fc8710f6c5e8aChristian MaedershowParse :: Either ParseError String -> String
58b671de3fe578346fef9642ffa3c5a0a0edb3cbTill MossakowskishowParse e = case e of
a001917177db7ae636853b37c0d0f9f4e90a83ffChristian Maeder Left err -> "parse error at " ++ showErr err ++ "\n"
58b671de3fe578346fef9642ffa3c5a0a0edb3cbTill Mossakowski Right x -> x
58b671de3fe578346fef9642ffa3c5a0a0edb3cbTill Mossakowski