RunParsers.hs revision 35db0960aa2e2a13652381c756fae5fb2b27213b
a389e88e0acb83d8489bdc5e55bc5522b152bbecEugen Kuksa
a389e88e0acb83d8489bdc5e55bc5522b152bbecEugen Kuksa{- |
a389e88e0acb83d8489bdc5e55bc5522b152bbecEugen Kuksa
a389e88e0acb83d8489bdc5e55bc5522b152bbecEugen KuksaModule : $Header$
a389e88e0acb83d8489bdc5e55bc5522b152bbecEugen KuksaCopyright : (c) Christian Maeder and Uni Bremen 2002-2003
a389e88e0acb83d8489bdc5e55bc5522b152bbecEugen KuksaLicence : similar to LGPL, see HetCATS/LICENCE.txt or LIZENZ.txt
a389e88e0acb83d8489bdc5e55bc5522b152bbecEugen Kuksa
a389e88e0acb83d8489bdc5e55bc5522b152bbecEugen KuksaMaintainer : hets@tzi.de
a389e88e0acb83d8489bdc5e55bc5522b152bbecEugen KuksaStability : provisional
a389e88e0acb83d8489bdc5e55bc5522b152bbecEugen KuksaPortability : portable
a389e88e0acb83d8489bdc5e55bc5522b152bbecEugen Kuksa
a389e88e0acb83d8489bdc5e55bc5522b152bbecEugen Kuksatest some parsers (and printers)
a389e88e0acb83d8489bdc5e55bc5522b152bbecEugen Kuksa
a389e88e0acb83d8489bdc5e55bc5522b152bbecEugen Kuksa-}
a389e88e0acb83d8489bdc5e55bc5522b152bbecEugen Kuksa
a389e88e0acb83d8489bdc5e55bc5522b152bbecEugen Kuksamodule Common.RunParsers (exec, StringParser, toStringParser, fromAParser)
a389e88e0acb83d8489bdc5e55bc5522b152bbecEugen Kuksa where
a389e88e0acb83d8489bdc5e55bc5522b152bbecEugen Kuksa
a389e88e0acb83d8489bdc5e55bc5522b152bbecEugen Kuksaimport Common.Lexer((<<), parseString)
a389e88e0acb83d8489bdc5e55bc5522b152bbecEugen Kuksaimport Text.ParserCombinators.Parsec
a389e88e0acb83d8489bdc5e55bc5522b152bbecEugen Kuksaimport Text.ParserCombinators.Parsec.Pos
a389e88e0acb83d8489bdc5e55bc5522b152bbecEugen Kuksaimport Common.Anno_Parser
a389e88e0acb83d8489bdc5e55bc5522b152bbecEugen Kuksaimport Common.AnnoState
a389e88e0acb83d8489bdc5e55bc5522b152bbecEugen Kuksaimport Common.PrettyPrint
a389e88e0acb83d8489bdc5e55bc5522b152bbecEugen Kuksaimport Common.GlobalAnnotations
a389e88e0acb83d8489bdc5e55bc5522b152bbecEugen Kuksaimport Common.AnalyseAnnos(addGlobalAnnos)
a389e88e0acb83d8489bdc5e55bc5522b152bbecEugen Kuksaimport Common.Result
a389e88e0acb83d8489bdc5e55bc5522b152bbecEugen Kuksaimport System.Environment
a389e88e0acb83d8489bdc5e55bc5522b152bbecEugen Kuksa
a389e88e0acb83d8489bdc5e55bc5522b152bbecEugen Kuksatype StringParser = GlobalAnnos -> AParser String
a389e88e0acb83d8489bdc5e55bc5522b152bbecEugen Kuksa
a389e88e0acb83d8489bdc5e55bc5522b152bbecEugen KuksafromAParser :: (PrettyPrint a) => AParser a -> StringParser
a389e88e0acb83d8489bdc5e55bc5522b152bbecEugen KuksafromAParser p ga = fmap (show . printText0 ga) p
a389e88e0acb83d8489bdc5e55bc5522b152bbecEugen Kuksa
a389e88e0acb83d8489bdc5e55bc5522b152bbecEugen KuksatoStringParser :: (PrettyPrint a) => (GlobalAnnos -> AParser a) -> StringParser
a389e88e0acb83d8489bdc5e55bc5522b152bbecEugen KuksatoStringParser p ga = fmap (show . printText0 ga) $ p ga
a389e88e0acb83d8489bdc5e55bc5522b152bbecEugen Kuksa
a389e88e0acb83d8489bdc5e55bc5522b152bbecEugen Kuksaexec :: [(String, StringParser)] -> [(String, StringParser)] -> IO ()
a389e88e0acb83d8489bdc5e55bc5522b152bbecEugen Kuksaexec lps fps = do l <- getArgs
a389e88e0acb83d8489bdc5e55bc5522b152bbecEugen Kuksa if null l then
a389e88e0acb83d8489bdc5e55bc5522b152bbecEugen Kuksa parseSpec emptyGlobalAnnos $ snd $ head $ fps
a389e88e0acb83d8489bdc5e55bc5522b152bbecEugen Kuksa else do let opt = head l
a389e88e0acb83d8489bdc5e55bc5522b152bbecEugen Kuksa lps' = filter (\(s, _) -> s == opt) lps
a389e88e0acb83d8489bdc5e55bc5522b152bbecEugen Kuksa fps' = filter (\(s, _) -> s == opt) fps
a389e88e0acb83d8489bdc5e55bc5522b152bbecEugen Kuksa ga <- if not $ null $ tail l then
a389e88e0acb83d8489bdc5e55bc5522b152bbecEugen Kuksa do let annoFile = head (tail l)
a389e88e0acb83d8489bdc5e55bc5522b152bbecEugen Kuksa str <- readFile annoFile
a389e88e0acb83d8489bdc5e55bc5522b152bbecEugen Kuksa maybe (error "run parser")
a389e88e0acb83d8489bdc5e55bc5522b152bbecEugen Kuksa return $ maybeResult
a389e88e0acb83d8489bdc5e55bc5522b152bbecEugen Kuksa $ addGlobalAnnos emptyGlobalAnnos
a389e88e0acb83d8489bdc5e55bc5522b152bbecEugen Kuksa $ parseString annotations str
a389e88e0acb83d8489bdc5e55bc5522b152bbecEugen Kuksa -- should not fail
a389e88e0acb83d8489bdc5e55bc5522b152bbecEugen Kuksa -- but may return empty annos
a389e88e0acb83d8489bdc5e55bc5522b152bbecEugen Kuksa else return emptyGlobalAnnos
a389e88e0acb83d8489bdc5e55bc5522b152bbecEugen Kuksa if null lps' && null fps' then
a389e88e0acb83d8489bdc5e55bc5522b152bbecEugen Kuksa do putStrLn ("unknown option: " ++ opt)
a389e88e0acb83d8489bdc5e55bc5522b152bbecEugen Kuksa p <- getProgName
a389e88e0acb83d8489bdc5e55bc5522b152bbecEugen Kuksa putStrLn("Usage: "++p++
a389e88e0acb83d8489bdc5e55bc5522b152bbecEugen Kuksa " [OPTIONS] <Annotations> < infile")
a389e88e0acb83d8489bdc5e55bc5522b152bbecEugen Kuksa putStrLn "where OPTIONS is one of:"
a389e88e0acb83d8489bdc5e55bc5522b152bbecEugen Kuksa putStrLn $ unwords
a389e88e0acb83d8489bdc5e55bc5522b152bbecEugen Kuksa (map fst lps ++ map fst fps)
a389e88e0acb83d8489bdc5e55bc5522b152bbecEugen Kuksa else if null lps'
then parseSpec ga $ snd $ head fps'
else checkLines ga $ snd $ head lps'
checkLines :: GlobalAnnos -> StringParser -> IO ()
checkLines ga p =
do s <- getContents
putStr (unlines (scanLines ga p (lines s) 1))
scanLines :: GlobalAnnos -> StringParser -> [String] -> Line -> [String]
scanLines _ _ [] _ = []
scanLines ga p (x:l) n = (parseLine ga p x n) : (scanLines ga p l (n+1))
parseLine :: GlobalAnnos -> StringParser -> String -> Line -> String
parseLine ga p line n =
let pos = setSourceLine (initialPos "") n
parser = do setPosition pos
i <- p ga
eof
return i
in showParse $ runParser parser emptyAnnos "" line
parseSpec :: GlobalAnnos -> StringParser -> IO ()
parseSpec ga p = do str <- getContents
putStrLn $ showParse $
runParser (p ga << eof) emptyAnnos "" str
showParse :: Either ParseError String -> String
showParse e = case e of
Left err -> "parse error at " ++ showErr err ++ "\n"
Right x -> x