Cli parser using new parsing technique

This commit is contained in:
thepenguin 2022-10-27 00:15:32 +02:00
parent 6306edab84
commit 39f5aa8770
No known key found for this signature in database
GPG Key ID: F258C8C10D060D5E
2 changed files with 35 additions and 22 deletions

View File

@ -6,14 +6,15 @@ The name is pronounced like autumn.
* Spec * Spec
| org | forum | | org | forum | Notes |
|-----+-----------------| |-----+-----------------+---------------------------|
| * | [TITLE][/TITLE] | | * | [TITLE][/TITLE] | |
| ** | [SIZE=4][/SIZE] | | ** | [SIZE=4][/SIZE] | |
| *** | [SIZE=2][/SIZE] | | *** | [SIZE=2][/SIZE] | |
| | | | - | [*] | Needs a [LIST] around all |
| | | | | | |
| | | | | | |
| | | | | | |
| | | | | | |
| | | | | | |
| | | |

View File

@ -1,20 +1,28 @@
module Main where module Main where
import Control.Applicative import Control.Applicative hiding (some)
import Control.Monad import Control.Monad
import Data.Functor import Data.Functor
import Data.Void
import System.Environment import System.Environment
import System.Exit import System.Exit
import Text.Megaparsec hiding (parse, satisfy) import Text.Megaparsec hiding (satisfy)
import Text.ParserCombinators.ReadP import Text.Megaparsec.Char
import Text.ParserCombinators.ReadP hiding (string)
parse :: ReadP String -> String -> String type Parser = Parsec Void String
parse rules = fst . last . readP_to_S rules
parseFile :: String -> String parseFile :: String -> String
parseFile = unlines . map (parse fileParser) . lines parseFile =
unlines
. map
( \x -> case parse fileParser "" x of
Left bundle -> error ("Error parsing text" ++ errorBundlePretty bundle)
Right text -> text
)
. lines
fileParser :: ReadP String fileParser :: Parser String
fileParser = undefined fileParser = undefined
main :: IO () main :: IO ()
@ -25,13 +33,17 @@ parseCli [] = return usage
parseCli [x] = parseCli [x] =
if head x /= '-' if head x /= '-'
then parseFile <$> readFile x then parseFile <$> readFile x
else return $ useArgs $ parse argParse x else case parse argParse "" x of
parseCli [x, _] = return $ useArgs $ parse argParse x Left bundle -> fail $ "Failed parsing args" ++ errorBundlePretty bundle
Right text -> return $ useArgs text
parseCli [x, _] = case parse argParse "" x of
Left bundle -> fail $ "Failed parsing args" ++ errorBundlePretty bundle
Right text -> return $ useArgs text
parseCli _ = fail "Too many arugemnts" parseCli _ = fail "Too many arugemnts"
argParse :: ReadP String argParse :: Parser String
argParse = argParse =
string "-" >> many1 (satisfy (/= ' ')) string "-" >> some alphaNumChar
useArgs :: String -> String useArgs :: String -> String
useArgs [] = "" useArgs [] = ""