From 39f6992f4ba924d0b798c81596bafd24d185b4d9 Mon Sep 17 00:00:00 2001 From: Piotr Justyna Date: Fri, 24 May 2024 09:40:23 +0000 Subject: [PATCH] input from terminal args --- README.md | 6 +++--- app/Main.hs | 17 ++++++++++++++++- app/Parser.hs | 31 +++++++++++++++---------------- run.sh | 2 +- 4 files changed, 35 insertions(+), 21 deletions(-) diff --git a/README.md b/README.md index d9b2e97..af9ee52 100644 --- a/README.md +++ b/README.md @@ -15,9 +15,9 @@ Haskell drakon renderer. Proposed input syntax to be converted to diagram images: ``` -start t "title - description" -action a1 "action - description" -end e "end - description" +Title t "title - description" +Action a1 "action - description" +End e "end - description" t > a1 a1 > e diff --git a/app/Main.hs b/app/Main.hs index ccf38de..b8f4e89 100644 --- a/app/Main.hs +++ b/app/Main.hs @@ -6,8 +6,23 @@ import qualified GHC.Utils.Ppr import qualified Parser import qualified System.IO +import qualified System.Environment + +inputValidation :: [String] -> (Bool, String) +inputValidation [] = (False, "no input provided") +inputValidation [x] = (True, x) +inputValidation inputList = (False, "only one input argument expected but " ++ show (length inputList) ++ " provided") main :: IO () main = do - GHC.Utils.Outputable.printSDocLn GHC.Utils.Outputable.defaultSDocContext GHC.Utils.Ppr.LeftMode System.IO.stdout $ GHC.Utils.Outputable.ppr $ Parser.parse Parser.iconDefinition "title t \"title - description\"" + input <- System.Environment.getArgs :: IO [String] + + case inputValidation input of + (True, x) -> do + putStrLn ("input: \"" ++ x ++ "\" translates to:") + GHC.Utils.Outputable.printSDocLn + GHC.Utils.Outputable.defaultSDocContext + GHC.Utils.Ppr.LeftMode + System.IO.stdout $ GHC.Utils.Outputable.ppr $ Parser.parse Parser.iconDefinition x + (False, x) -> putStrLn x \ No newline at end of file diff --git a/app/Parser.hs b/app/Parser.hs index a722183..126fc74 100644 --- a/app/Parser.hs +++ b/app/Parser.hs @@ -3,8 +3,7 @@ module Parser where import qualified Control.Applicative import qualified Data.Char --- TODO: to qualified -import Icon +import qualified Icon newtype Parser a = P (String -> [(a, String)]) @@ -52,8 +51,6 @@ sat :: (Char -> Bool) -> Parser Char sat f = do x <- item if f x then return x else Control.Applicative.empty - -- but the below also works: - -- if f x then P (\input -> [(x, input)]) else Control.Applicative.empty isAllowedDescriptionCharacter :: Char -> Bool isAllowedDescriptionCharacter x = @@ -72,6 +69,7 @@ string (x:xs) = do return (x:xs) -- 2024-05-23 PJ: +-- ============== -- TODO: many but no more than N iconIdentifier :: Parser String iconIdentifier = do Control.Applicative.many $ sat Data.Char.isAlphaNum @@ -84,6 +82,7 @@ iconDescription = do return name -- 2024-05-23 PJ: +-- ============== -- I thought about "many but no more than N" -- but in the end convinced myself it won't be needed. -- We can always limit the number of spaces using an @@ -104,31 +103,31 @@ token p = do symbol :: String -> Parser String symbol xs = token (string xs) -iconDefinition'' :: Parser Icon -iconDefinition'' = +iconDefinition' :: Parser Icon.Icon +iconDefinition' = do - _ <- symbol "title" + _ <- symbol "Title" identifier <- token iconIdentifier description <- token iconDescription - return Icon { iconText = description, iconType = Title} + return Icon.Icon { Icon.iconText = description, Icon.iconType = Icon.Title} Control.Applicative.<|> do - _ <- symbol "action" + _ <- symbol "Action" identifier <- token iconIdentifier description <- token iconDescription - return Icon { iconText = description, iconType = Action} + return Icon.Icon { Icon.iconText = description, Icon.iconType = Icon.Action} Control.Applicative.<|> do - _ <- symbol "question" + _ <- symbol "Question" identifier <- token iconIdentifier description <- token iconDescription - return Icon { iconText = description, iconType = Question} + return Icon.Icon { Icon.iconText = description, Icon.iconType = Icon.Question} Control.Applicative.<|> do - _ <- symbol "end" + _ <- symbol "End" identifier <- token iconIdentifier description <- token iconDescription - return Icon { iconText = description, iconType = End} + return Icon.Icon { Icon.iconText = description, Icon.iconType = Icon.End} -iconDefinition :: Parser Icon -iconDefinition = token iconDefinition'' \ No newline at end of file +iconDefinition :: Parser Icon.Icon +iconDefinition = token iconDefinition' \ No newline at end of file diff --git a/run.sh b/run.sh index ffa5382..b7d8d8b 100755 --- a/run.sh +++ b/run.sh @@ -1,2 +1,2 @@ #!/bin/zsh -cabal run drakon-renderer -- -o diagram-troubleshooting-on.svg -w 1000 \ No newline at end of file +cabal run drakon-renderer "Title t \"title - description\""