content: {-
  Copyright (c) 2015, Dan Rosén
  Copyright (c) 2016, Nick Smallbone
  
  All rights reserved.
  
  Redistribution and use in source and binary forms, with or without
  modification, are permitted provided that the following conditions are met:
  
      * Redistributions of source code must retain the above copyright
        notice, this list of conditions and the following disclaimer.
  
      * Redistributions in binary form must reproduce the above
        copyright notice, this list of conditions and the following
        disclaimer in the documentation and/or other materials provided
        with the distribution.
  
      * Neither the name of the copyright holder nor the names of other
        contributors may be used to endorse or promote products derived
        from this software without specific prior written permission.
  
  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  -}
  -- File generated by the BNF Converter (bnfc 2.9.4).
  
  -- | Program to test parser.
  
  module Main where
  
  import Prelude
    ( ($), (.)
    , Either(..)
    , Int, (>)
    , String, (++), concat, unlines
    , Show, show
    , IO, (>>), (>>=), mapM_, putStrLn
    , FilePath
    , getContents, readFile
    )
  import System.Environment ( getArgs )
  import System.Exit        ( exitFailure )
  import Control.Monad      ( when )
  
  import TIP.AbsTIP   ()
  import TIP.LexTIP   ( Token, mkPosToken )
  import TIP.ParTIP   ( pStart, myLexer )
  import TIP.PrintTIP ( Print, printTree )
  import TIP.SkelTIP  ()
  
  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 -> String -> IO ()
  run v p s =
    case p ts of
      Left err -> do
        putStrLn "\nParse              Failed...\n"
        putStrV v "Tokens:"
        mapM_ (putStrV v . showPosToken . mkPosToken) ts
        putStrLn err
        exitFailure
      Right tree -> do
        putStrLn "\nParse Successful!"
        showTree v tree
    where
    ts = myLexer s
    showPosToken ((l,c),t) = concat [ show l, ":", show c, "\t", show t ]
  
  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."
      ]
  
  main :: IO ()
  main = do
    args <- getArgs
    case args of
      ["--help"] -> usage
      []         -> getContents >>= run 2 pStart
      "-s":fs    -> mapM_ (runFile 0 pStart) fs
      fs         -> mapM_ (runFile 2 pStart) fs
  
  