Copyright : (c) Christian Maeder and Uni Bremen 2002-2003
Maintainer : maeder@tzi.de
test some parsers (and printers)
type StringParser = GlobalAnnos -> AParser () String
fromAParser :: (PrettyPrint a) => AParser () a -> StringParser
fromAParser p ga = fmap (show . printText0 ga) p
toStringParser :: (PrettyPrint a) => (GlobalAnnos -> AParser () a)
toStringParser p ga = fmap (show . printText0 ga) $ p ga
exec :: [(String, StringParser)] -> [(String, StringParser)] -> IO ()
exec lps fps = do l <- getArgs
parseSpec emptyGlobalAnnos $ snd $ head $ fps
lps' = filter (\(s, _) -> s == opt) lps
fps' = filter (\(s, _) -> s == opt) fps
ga <- if not $ null $ tail l then
do let annoFile = head (tail l)
maybe (error "run parser")
$ addGlobalAnnos emptyGlobalAnnos
$ parseString annotations str
-- but may return empty annos
else return emptyGlobalAnnos
if null lps' && null fps' then
do putStrLn ("unknown option: " ++ opt)
" [OPTIONS] <Annotations> < infile")
putStrLn "where OPTIONS is one of:"
(map fst lps ++ map fst fps)
then parseSpec ga $ snd $ head fps'
else checkLines ga $ snd $ head lps'
checkLines :: GlobalAnnos -> StringParser -> IO ()
putStr (unlines (scanLines ga p (lines s) 1))
scanLines :: GlobalAnnos -> StringParser -> [String] -> Line -> [String]
scanLines ga p (x:l) n = (parseLine ga p x n) : (scanLines ga p l (n+1))
parseLine :: GlobalAnnos -> StringParser -> String -> Line -> String
let pos = setSourceLine (initialPos "") n
parser = do setPosition pos
in showParse $ runParser parser (emptyAnnos ()) "" line
parseSpec :: GlobalAnnos -> StringParser -> IO ()
parseSpec ga p = do str <- getContents
runParser (p ga << eof) (emptyAnnos ()) "" str
showParse :: Either ParseError String -> String
Left err -> "parse error at " ++ showErr err ++ "\n"