Skip to content

Commit

Permalink
input from terminal args
Browse files Browse the repository at this point in the history
  • Loading branch information
PiotrJustyna committed May 24, 2024
1 parent e497068 commit 39f6992
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 21 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
17 changes: 16 additions & 1 deletion app/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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
31 changes: 15 additions & 16 deletions app/Parser.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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)])

Expand Down Expand Up @@ -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 =
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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''
iconDefinition :: Parser Icon.Icon
iconDefinition = token iconDefinition'
2 changes: 1 addition & 1 deletion run.sh
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
#!/bin/zsh
cabal run drakon-renderer -- -o diagram-troubleshooting-on.svg -w 1000
cabal run drakon-renderer "Title t \"title - description\""

0 comments on commit 39f6992

Please sign in to comment.