2021-12-22 03:28:03 +00:00
|
|
|
{-# LANGUAGE OverloadedStrings #-}
|
|
|
|
|
|
|
|
module Main where
|
|
|
|
|
2021-12-23 13:21:05 +00:00
|
|
|
import Data.Text (Text)
|
2021-12-22 03:28:03 +00:00
|
|
|
import Data.Text.IO (hGetContents)
|
2021-12-24 13:16:06 +00:00
|
|
|
import Options.Applicative ((<**>))
|
2022-01-12 11:51:56 +00:00
|
|
|
import qualified Options.Applicative as O
|
2021-12-23 13:21:05 +00:00
|
|
|
import Parser.Initial
|
2021-12-24 13:16:06 +00:00
|
|
|
import Parser.Utils
|
2021-12-22 03:28:03 +00:00
|
|
|
import System.Environment (getArgs)
|
2022-01-12 11:51:56 +00:00
|
|
|
import System.IO (IOMode (ReadMode), openFile)
|
|
|
|
import qualified Text.Megaparsec as M
|
2021-12-22 03:28:03 +00:00
|
|
|
|
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 03:28:03 +00:00
|
|
|
|
2021-12-24 13:16:06 +00:00
|
|
|
args :: O.Parser Args
|
2022-01-12 11:51:56 +00:00
|
|
|
args =
|
|
|
|
Args
|
|
|
|
<$> O.strArgument
|
2021-12-24 13:16:06 +00:00
|
|
|
( O.metavar "FILENAME" <> O.help "The file we want to parse."
|
|
|
|
)
|
2022-01-12 11:51:56 +00:00
|
|
|
<*> O.strOption
|
2021-12-24 13:16:06 +00:00
|
|
|
( O.short 'm'
|
2022-01-12 11:51:56 +00:00
|
|
|
<> O.long "method"
|
|
|
|
<> O.metavar "METHOD"
|
|
|
|
<> O.showDefault
|
|
|
|
<> O.value "naive"
|
|
|
|
<> O.help
|
|
|
|
"The parse strategy we want to try. Should be one of 'naive', \
|
|
|
|
\'single', 'strict', or 'gadt'."
|
2021-12-24 13:16:06 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
-- ========================================
|
|
|
|
-- Main
|
|
|
|
-- ========================================
|
2021-12-22 03:28:03 +00:00
|
|
|
|
|
|
|
run :: Args -> IO ()
|
|
|
|
run args = do
|
|
|
|
handle <- openFile (argsFileName args) ReadMode
|
2021-12-24 13:16:06 +00:00
|
|
|
input <- hGetContents handle
|
2021-12-22 03:28:03 +00:00
|
|
|
case argsMethod args of
|
2022-01-12 11:51:56 +00:00
|
|
|
"naive" -> runExpr parseNaive input
|
2021-12-24 13:16:06 +00:00
|
|
|
"single" -> runExpr parseSingle input
|
|
|
|
"strict" -> runExpr parseStrict input
|
2022-01-12 11:51:56 +00:00
|
|
|
"gadt" -> case runParser parseGadt input of
|
|
|
|
Left e -> print e
|
|
|
|
Right (Wrapper a) -> print $ eval a
|
|
|
|
_ -> error "Encountered an invalid parsing strategy."
|
|
|
|
where
|
|
|
|
runExpr p input = either print print (runParser p input)
|
2021-12-22 03:28:03 +00:00
|
|
|
|
|
|
|
main :: IO ()
|
2021-12-24 13:16:06 +00:00
|
|
|
main = run =<< O.execParser opts
|
2022-01-12 11:51:56 +00:00
|
|
|
where
|
|
|
|
opts =
|
|
|
|
O.info
|
|
|
|
(args <**> O.helper)
|
|
|
|
( O.fullDesc
|
|
|
|
<> O.progDesc "Different parsing strategies using initial encoding"
|
|
|
|
<> O.header "Initial encoding parsing"
|
|
|
|
)
|