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
| org | forum |
|-----+-----------------|
| * | [TITLE][/TITLE] |
| ** | [SIZE=4][/SIZE] |
| *** | [SIZE=2][/SIZE] |
| | |
| | |
| | |
| | |
| | |
| | |
| org | forum | Notes |
|-----+-----------------+---------------------------|
| * | [TITLE][/TITLE] | |
| ** | [SIZE=4][/SIZE] | |
| *** | [SIZE=2][/SIZE] | |
| - | [*] | Needs a [LIST] around all |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | |

View File

@ -1,20 +1,28 @@
module Main where
import Control.Applicative
import Control.Applicative hiding (some)
import Control.Monad
import Data.Functor
import Data.Void
import System.Environment
import System.Exit
import Text.Megaparsec hiding (parse, satisfy)
import Text.ParserCombinators.ReadP
import Text.Megaparsec hiding (satisfy)
import Text.Megaparsec.Char
import Text.ParserCombinators.ReadP hiding (string)
parse :: ReadP String -> String -> String
parse rules = fst . last . readP_to_S rules
type Parser = Parsec Void 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
main :: IO ()
@ -25,13 +33,17 @@ 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
else case parse argParse "" x of
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"
argParse :: ReadP String
argParse :: Parser String
argParse =
string "-" >> many1 (satisfy (/= ' '))
string "-" >> some alphaNumChar
useArgs :: String -> String
useArgs [] = ""