mirror of
https://github.com/The1Penguin/org-to-mafiauniverse.git
synced 2025-06-29 09:27:13 +00:00
Start to use megaparsec instead of P_to_S
This commit is contained in:
43
app/Main.hs
43
app/Main.hs
@ -1,39 +1,46 @@
|
||||
module Main where
|
||||
|
||||
import Control.Applicative
|
||||
import Control.Monad
|
||||
import Data.Functor
|
||||
import System.Environment
|
||||
import System.Exit
|
||||
import Text.Megaparsec hiding (parse, satisfy)
|
||||
import Text.ParserCombinators.ReadP
|
||||
|
||||
|
||||
|
||||
parse :: ReadP String -> String -> String
|
||||
parse rules = fst . last .readP_to_S rules
|
||||
parse rules = fst . last . readP_to_S rules
|
||||
|
||||
parseFile :: String -> String
|
||||
parseFile = unlines . map (parse fileParser) . lines
|
||||
|
||||
fileParser :: ReadP String
|
||||
fileParser = undefined
|
||||
|
||||
main :: IO ()
|
||||
main = putStrLn =<< helper =<< getArgs
|
||||
|
||||
helper :: [String] -> IO String
|
||||
helper [] = return usage
|
||||
helper (x:[]) = if (head x /= '-')
|
||||
then readFile x
|
||||
else return $ useArgs $ parse argParse x
|
||||
helper (x:_:[]) = return $ useArgs $ parse argParse x
|
||||
helper _ = fail "Too many arugemnts"
|
||||
main = putStr =<< parseCli =<< getArgs
|
||||
|
||||
parseCli :: [String] -> IO String
|
||||
parseCli [] = return usage
|
||||
parseCli [x] =
|
||||
if head x /= '-'
|
||||
then parseFile <$> readFile x
|
||||
else return $ useArgs $ parse argParse x
|
||||
parseCli [x, _] = return $ useArgs $ parse argParse x
|
||||
parseCli _ = fail "Too many arugemnts"
|
||||
|
||||
argParse :: ReadP String
|
||||
argParse =
|
||||
string "-" >> (many1 (satisfy (/= ' ')))
|
||||
string "-" >> many1 (satisfy (/= ' '))
|
||||
|
||||
useArgs :: String -> String
|
||||
useArgs [] = ""
|
||||
useArgs ('h':_) = usage
|
||||
useArgs ('v':_) = version
|
||||
useArgs (_:xs) = useArgs xs
|
||||
useArgs [] = ""
|
||||
useArgs ('h' : _) = usage
|
||||
useArgs ('v' : _) = version
|
||||
useArgs (_ : xs) = useArgs xs
|
||||
|
||||
usage :: String
|
||||
usage = "Usage: otm [-hv] [file]"
|
||||
usage = "Usage: otm [-hv] [file]"
|
||||
|
||||
version :: String
|
||||
version = "otm 0.1"
|
||||
|
Reference in New Issue
Block a user