|
-- Program to test parser, automatically generated by BNF Converter. |
|
|
|
module Main where |
|
|
|
import Prelude |
|
( ($) |
|
, Either(..) |
|
, Int, (>) |
|
, String, (++), unlines |
|
, Show, show |
|
, IO, (>>), (>>=), mapM_, putStrLn |
|
, FilePath |
|
) |
|
import Data.Text.IO ( getContents, readFile ) |
|
import qualified Data.Text |
|
import System.Environment ( getArgs ) |
|
import System.Exit ( exitFailure, exitSuccess ) |
|
import Control.Monad ( when ) |
|
|
|
import Example.Abs () |
|
import Example.Lex ( Token ) |
|
import Example.Par ( pExample, myLexer ) |
|
import Example.Print ( Print, printTree ) |
|
|
|
type Err = Either String |
|
type ParseFun a = [Token] -> Err a |
|
type Verbosity = Int |
|
|
|
putStrV :: Verbosity -> String -> IO () |
|
putStrV v s = when (v > 1) $ putStrLn s |
|
|
|
runFile :: (Print a, Show a) => Verbosity -> ParseFun a -> FilePath -> IO () |
|
runFile v p f = putStrLn f >> readFile f >>= run v p |
|
|
|
run :: (Print a, Show a) => Verbosity -> ParseFun a -> Data.Text.Text -> IO () |
|
run v p s = |
|
case p ts of |
|
Left err -> do |
|
putStrLn "\nParse Failed...\n" |
|
putStrV v "Tokens:" |
|
putStrV v $ show ts |
|
putStrLn err |
|
exitFailure |
|
Right tree -> do |
|
putStrLn "\nParse Successful!" |
|
showTree v tree |
|
exitSuccess |
|
where |
|
ts = myLexer s |
|
|
|
showTree :: (Show a, Print a) => Int -> a -> IO () |
|
showTree v tree = do |
|
putStrV v $ "\n[Abstract Syntax]\n\n" ++ show tree |
|
putStrV v $ "\n[Linearized tree]\n\n" ++ printTree tree |
|
|
|
usage :: IO () |
|
usage = do |
|
putStrLn $ unlines |
|
[ "usage: Call with one of the following argument combinations:" |
|
, " --help Display this help message." |
|
, " (no arguments) Parse stdin verbosely." |
|
, " (files) Parse content of files verbosely." |
|
, " -s (files) Silent mode. Parse content of files silently." |
|
] |
|
exitFailure |
|
|
|
main :: IO () |
|
main = do |
|
args <- getArgs |
|
case args of |
|
["--help"] -> usage |
|
[] -> getContents >>= run 2 pExample |
|
"-s":fs -> mapM_ (runFile 0 pExample) fs |
|
fs -> mapM_ (runFile 2 pExample) fs |
|
|