From 39f5aa8770918780df5be6a1c32f1f48479ba080 Mon Sep 17 00:00:00 2001 From: thepenguin Date: Thu, 27 Oct 2022 00:15:32 +0200 Subject: [PATCH] Cli parser using new parsing technique --- README.org | 23 ++++++++++++----------- app/Main.hs | 34 +++++++++++++++++++++++----------- 2 files changed, 35 insertions(+), 22 deletions(-) diff --git a/README.org b/README.org index ae3cf84..29581d0 100644 --- a/README.org +++ b/README.org @@ -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 | +| | | | +| | | | +| | | | +| | | | +| | | | +| | | | diff --git a/app/Main.hs b/app/Main.hs index 6e1c30f..0dd7577 100644 --- a/app/Main.hs +++ b/app/Main.hs @@ -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 [] = ""