1
Fork 0
tagless-final-parsing/initial-encoding/app/Main.hs

67 lines
1.8 KiB
Haskell
Raw Normal View History

2021-12-22 03:28:03 +00:00
{-# LANGUAGE OverloadedStrings #-}
module Main where
import Data.Text (Text)
2021-12-22 03:28:03 +00:00
import Data.Text.IO (hGetContents)
import Options.Applicative ((<**>))
2022-01-12 11:51:56 +00:00
import qualified Options.Applicative as O
import Parser.Initial
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
-- ========================================
-- Arguments
-- ========================================
data Args = Args {argsFileName :: !FilePath, argsMethod :: !Text}
2021-12-22 03:28:03 +00:00
args :: O.Parser Args
2022-01-12 11:51:56 +00:00
args =
Args
<$> O.strArgument
( O.metavar "FILENAME" <> O.help "The file we want to parse."
)
2022-01-12 11:51:56 +00:00
<*> O.strOption
( 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'."
)
-- ========================================
-- Main
-- ========================================
2021-12-22 03:28:03 +00:00
run :: Args -> IO ()
run args = do
handle <- openFile (argsFileName args) ReadMode
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
"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 ()
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"
)