2021-12-23 13:21:05 +00:00
|
|
|
{-# LANGUAGE OverloadedStrings #-}
|
|
|
|
{-# LANGUAGE TypeApplications #-}
|
2021-12-22 14:28:47 +00:00
|
|
|
|
2021-12-23 13:21:05 +00:00
|
|
|
module Main where
|
2021-12-22 14:28:47 +00:00
|
|
|
|
2021-12-24 13:16:06 +00:00
|
|
|
import qualified Options.Applicative as O
|
|
|
|
|
2021-12-23 13:21:05 +00:00
|
|
|
import Data.Text (Text)
|
2021-12-22 14:28:47 +00:00
|
|
|
import Data.Text.IO (hGetContents)
|
2021-12-24 13:16:06 +00:00
|
|
|
import Options.Applicative ((<**>))
|
|
|
|
import Parser.Final
|
|
|
|
import Parser.Utils
|
2021-12-22 14:28:47 +00:00
|
|
|
import System.Environment (getArgs)
|
|
|
|
import System.IO (IOMode(ReadMode), openFile)
|
|
|
|
|
2021-12-24 13:16:06 +00:00
|
|
|
-- ========================================
|
|
|
|
-- Arguments
|
|
|
|
-- ========================================
|
|
|
|
|
2021-12-23 13:21:05 +00:00
|
|
|
data Args = Args {argsFileName :: !FilePath, argsMethod :: !Text}
|
2021-12-22 14:28:47 +00:00
|
|
|
|
2021-12-24 13:16:06 +00:00
|
|
|
args :: O.Parser Args
|
|
|
|
args = Args
|
|
|
|
<$> O.strArgument
|
|
|
|
( O.metavar "FILENAME" <> O.help "The file we want to parse."
|
|
|
|
)
|
|
|
|
<*> O.strOption
|
|
|
|
( O.short 'm'
|
|
|
|
<> O.long "method"
|
|
|
|
<> O.metavar "METHOD"
|
|
|
|
<> O.showDefault
|
|
|
|
<> O.value "single"
|
|
|
|
<> O.help "The parse strategy we want to try. Should be one of 'single' \
|
|
|
|
\or 'strict'."
|
|
|
|
)
|
|
|
|
|
|
|
|
-- ========================================
|
|
|
|
-- Main
|
|
|
|
-- ========================================
|
|
|
|
|
|
|
|
runExpr :: Parser (Dynamic Eval) -> Text -> IO ()
|
|
|
|
runExpr p input = case runParser p input of
|
|
|
|
Left e -> print e
|
2021-12-23 13:21:05 +00:00
|
|
|
Right d -> case fromDyn @Eval @Integer d of
|
2021-12-26 21:10:35 +00:00
|
|
|
Just (Eval a) -> print a
|
2021-12-23 13:21:05 +00:00
|
|
|
Nothing -> case fromDyn @Eval @Bool d of
|
2021-12-26 21:10:35 +00:00
|
|
|
Just (Eval a) -> print a
|
2021-12-23 13:21:05 +00:00
|
|
|
Nothing -> print "Could not evaluate expression fully."
|
|
|
|
|
|
|
|
run :: Args -> IO ()
|
|
|
|
run args = do
|
|
|
|
handle <- openFile (argsFileName args) ReadMode
|
2021-12-22 14:28:47 +00:00
|
|
|
contents <- hGetContents handle
|
2021-12-23 13:21:05 +00:00
|
|
|
case argsMethod args of
|
2021-12-24 13:16:06 +00:00
|
|
|
"single" -> runExpr parseSingle contents
|
|
|
|
"strict" -> runExpr parseStrict contents
|
2021-12-23 13:21:05 +00:00
|
|
|
_ -> error "Encountered an invalid parsing strategy."
|
|
|
|
|
|
|
|
main :: IO ()
|
2021-12-24 13:16:06 +00:00
|
|
|
main = run =<< O.execParser opts
|
2021-12-23 13:21:05 +00:00
|
|
|
where
|
2021-12-24 13:16:06 +00:00
|
|
|
opts = O.info (args <**> O.helper)
|
|
|
|
( O.fullDesc
|
|
|
|
<> O.progDesc "Different parsing strategies using initial encoding"
|
|
|
|
<> O.header "Initial encoding parsing"
|
2021-12-23 13:21:05 +00:00
|
|
|
)
|