gdritter repos apicius / 3fa12e7
Added two new 'renderers' for debugging: 'ast' and 'fragments' 'ast' will print the parsed AST, for any parser debugging 'fragments' will print the intermediate fragments used for creating the reverse recipe tree, for debugging the processing step Getty Ritter 8 years ago
4 changed file(s) with 56 addition(s) and 3 deletion(s). Collapse all Expand all
11 module AST where
22
3 import Data.Text (Text)
3 import Data.Monoid ((<>))
4 import Data.Text (Text)
5 import Util
46
57 data Recipe = Recipe
68 { rName :: Text
79 , rRecipe :: [Step]
810 } deriving (Eq, Show)
911
12 instance TShow Recipe where
13 text (Recipe name steps) =
14 "Recipe { rName = " <>
15 text name <>
16 ", rRecipe = " <>
17 text steps <>
18 " }"
19
1020 data Step = Step
1121 { sInputs :: Input
1222 , sActions :: [Action]
1323 } deriving (Eq, Show)
24
25 instance TShow Step where
26 text (Step inp acts) =
27 "Step { sInputs = " <>
28 text inp <>
29 ", sActions = " <>
30 text acts <>
31 " }"
1432
1533 data Input
1634 = InpIngredients IngredientList
1735 | InpJoin Text
1836 deriving (Eq, Show)
1937
38 instance TShow Input where
39 text (InpJoin ts) = text ts
40 text (InpIngredients is) = text is
41
2042 data IngredientList = IngredientList
2143 { fromIngredientList :: [Ingredient]
2244 } deriving (Eq, Show)
45
46 instance TShow IngredientList where
47 text (IngredientList is) =
48 "IngredientList " <> text is
2349
2450 data Ingredient = Ingredient
2551 { iAmount :: Maybe Text
2652 , iType :: Text
2753 } deriving (Eq, Show)
2854
55 instance TShow Ingredient where
56 text (Ingredient i t) =
57 "Ingredient { iAmount = " <>
58 text i <>
59 ", iType = " <>
60 text t <>
61 " }"
62
2963 data Action
3064 = Action Text (Maybe IngredientList)
3165 | Join Text
3266 | Done
3367 deriving (Eq, Show)
68
69 instance TShow Action where
70 text (Action t i) = "Action " <> text t <> " " <> text i
71 text (Join t) = "Join " <> text t
72 text Done = "Done"
73
122122 | (i, _, r) <- children
123123 ]
124124
125 showFragments :: Recipe -> Text
126 showFragments = T.pack . show . getChunks
127
125128 showTree :: Recipe -> Text
126129 showTree = prettyGraph . buildReverseGraph . getChunks
127130
1111 )
1212
1313 import AST (Recipe)
14 import BuildTree (showDotGraph, showTree)
14 import BuildTree (showFragments, showDotGraph, showTree)
1515 import Parser (parseFile)
16 import Util (TShow(text))
1617
1718 usage :: String
1819 usage =
2021
2122 renderers :: [(String, Recipe -> Text)]
2223 renderers =
23 [ ("dot", showDotGraph)
24 [ ("ast", text)
25 , ("fragments", showFragments)
26 , ("dot", showDotGraph)
2427 , ("reverse-tree", showTree)
2528 ]
2629
1717
1818 instance TShow Int where
1919 instance TShow Text where
20
21 instance TShow a => TShow [a] where
22 text xs = "[" <> T.intercalate ", " (map text xs) <> "]"
23
24 instance TShow a => TShow (Maybe a) where
25 text Nothing = "Nothing"
26 text (Just a) = "Just " <> text a