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
1 | 1 |
module AST where
|
2 | 2 |
|
3 | |
import Data.Text (Text)
|
| 3 |
import Data.Monoid ((<>))
|
| 4 |
import Data.Text (Text)
|
| 5 |
import Util
|
4 | 6 |
|
5 | 7 |
data Recipe = Recipe
|
6 | 8 |
{ rName :: Text
|
7 | 9 |
, rRecipe :: [Step]
|
8 | 10 |
} deriving (Eq, Show)
|
9 | 11 |
|
| 12 |
instance TShow Recipe where
|
| 13 |
text (Recipe name steps) =
|
| 14 |
"Recipe { rName = " <>
|
| 15 |
text name <>
|
| 16 |
", rRecipe = " <>
|
| 17 |
text steps <>
|
| 18 |
" }"
|
| 19 |
|
10 | 20 |
data Step = Step
|
11 | 21 |
{ sInputs :: Input
|
12 | 22 |
, sActions :: [Action]
|
13 | 23 |
} 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 |
" }"
|
14 | 32 |
|
15 | 33 |
data Input
|
16 | 34 |
= InpIngredients IngredientList
|
17 | 35 |
| InpJoin Text
|
18 | 36 |
deriving (Eq, Show)
|
19 | 37 |
|
| 38 |
instance TShow Input where
|
| 39 |
text (InpJoin ts) = text ts
|
| 40 |
text (InpIngredients is) = text is
|
| 41 |
|
20 | 42 |
data IngredientList = IngredientList
|
21 | 43 |
{ fromIngredientList :: [Ingredient]
|
22 | 44 |
} deriving (Eq, Show)
|
| 45 |
|
| 46 |
instance TShow IngredientList where
|
| 47 |
text (IngredientList is) =
|
| 48 |
"IngredientList " <> text is
|
23 | 49 |
|
24 | 50 |
data Ingredient = Ingredient
|
25 | 51 |
{ iAmount :: Maybe Text
|
26 | 52 |
, iType :: Text
|
27 | 53 |
} deriving (Eq, Show)
|
28 | 54 |
|
| 55 |
instance TShow Ingredient where
|
| 56 |
text (Ingredient i t) =
|
| 57 |
"Ingredient { iAmount = " <>
|
| 58 |
text i <>
|
| 59 |
", iType = " <>
|
| 60 |
text t <>
|
| 61 |
" }"
|
| 62 |
|
29 | 63 |
data Action
|
30 | 64 |
= Action Text (Maybe IngredientList)
|
31 | 65 |
| Join Text
|
32 | 66 |
| Done
|
33 | 67 |
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 |
|
122 | 122 |
| (i, _, r) <- children
|
123 | 123 |
]
|
124 | 124 |
|
| 125 |
showFragments :: Recipe -> Text
|
| 126 |
showFragments = T.pack . show . getChunks
|
| 127 |
|
125 | 128 |
showTree :: Recipe -> Text
|
126 | 129 |
showTree = prettyGraph . buildReverseGraph . getChunks
|
127 | 130 |
|
11 | 11 |
)
|
12 | 12 |
|
13 | 13 |
import AST (Recipe)
|
14 | |
import BuildTree (showDotGraph, showTree)
|
| 14 |
import BuildTree (showFragments, showDotGraph, showTree)
|
15 | 15 |
import Parser (parseFile)
|
| 16 |
import Util (TShow(text))
|
16 | 17 |
|
17 | 18 |
usage :: String
|
18 | 19 |
usage =
|
|
20 | 21 |
|
21 | 22 |
renderers :: [(String, Recipe -> Text)]
|
22 | 23 |
renderers =
|
23 | |
[ ("dot", showDotGraph)
|
| 24 |
[ ("ast", text)
|
| 25 |
, ("fragments", showFragments)
|
| 26 |
, ("dot", showDotGraph)
|
24 | 27 |
, ("reverse-tree", showTree)
|
25 | 28 |
]
|
26 | 29 |
|
17 | 17 |
|
18 | 18 |
instance TShow Int where
|
19 | 19 |
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
|