{-# LANGUAGE DeriveFunctor #-}
{-# LANGUAGE DeriveGeneric #-}
module Language.Happy.GenTree
( GenTree (..)
, flatten
, renderTree
, getRules
) where
import Data.Text (Text)
import GHC.Generics (Generic)
import Language.Happy.Ast (Sym (..))
data GenTree token
= Node Sym [GenTree token]
| Leaf token
deriving (Int -> GenTree token -> ShowS
[GenTree token] -> ShowS
GenTree token -> String
(Int -> GenTree token -> ShowS)
-> (GenTree token -> String)
-> ([GenTree token] -> ShowS)
-> Show (GenTree token)
forall token. Show token => Int -> GenTree token -> ShowS
forall token. Show token => [GenTree token] -> ShowS
forall token. Show token => GenTree token -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: forall token. Show token => Int -> GenTree token -> ShowS
showsPrec :: Int -> GenTree token -> ShowS
$cshow :: forall token. Show token => GenTree token -> String
show :: GenTree token -> String
$cshowList :: forall token. Show token => [GenTree token] -> ShowS
showList :: [GenTree token] -> ShowS
Show, GenTree token -> GenTree token -> Bool
(GenTree token -> GenTree token -> Bool)
-> (GenTree token -> GenTree token -> Bool) -> Eq (GenTree token)
forall token. Eq token => GenTree token -> GenTree token -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: forall token. Eq token => GenTree token -> GenTree token -> Bool
== :: GenTree token -> GenTree token -> Bool
$c/= :: forall token. Eq token => GenTree token -> GenTree token -> Bool
/= :: GenTree token -> GenTree token -> Bool
Eq, (forall a b. (a -> b) -> GenTree a -> GenTree b)
-> (forall a b. a -> GenTree b -> GenTree a) -> Functor GenTree
forall a b. a -> GenTree b -> GenTree a
forall a b. (a -> b) -> GenTree a -> GenTree b
forall (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
$cfmap :: forall a b. (a -> b) -> GenTree a -> GenTree b
fmap :: forall a b. (a -> b) -> GenTree a -> GenTree b
$c<$ :: forall a b. a -> GenTree b -> GenTree a
<$ :: forall a b. a -> GenTree b -> GenTree a
Functor, (forall x. GenTree token -> Rep (GenTree token) x)
-> (forall x. Rep (GenTree token) x -> GenTree token)
-> Generic (GenTree token)
forall x. Rep (GenTree token) x -> GenTree token
forall x. GenTree token -> Rep (GenTree token) x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
forall token x. Rep (GenTree token) x -> GenTree token
forall token x. GenTree token -> Rep (GenTree token) x
$cfrom :: forall token x. GenTree token -> Rep (GenTree token) x
from :: forall x. GenTree token -> Rep (GenTree token) x
$cto :: forall token x. Rep (GenTree token) x -> GenTree token
to :: forall x. Rep (GenTree token) x -> GenTree token
Generic)
flatten :: GenTree token -> [token]
flatten :: forall token. GenTree token -> [token]
flatten (Leaf token
t) = [token
t]
flatten (Node Sym
_ [GenTree token]
cs) = (GenTree token -> [token]) -> [GenTree token] -> [token]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap GenTree token -> [token]
forall token. GenTree token -> [token]
flatten [GenTree token]
cs
getRules :: GenTree token -> [Text]
getRules :: forall token. GenTree token -> [Text]
getRules (Leaf token
_) = []
getRules (Node (Sym Text
name [Sym]
_) [GenTree token]
cs) = Text
name Text -> [Text] -> [Text]
forall a. a -> [a] -> [a]
: (GenTree token -> [Text]) -> [GenTree token] -> [Text]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap GenTree token -> [Text]
forall token. GenTree token -> [Text]
getRules [GenTree token]
cs
renderTree :: Show token => GenTree token -> String
renderTree :: forall token. Show token => GenTree token -> String
renderTree = Int -> GenTree token -> String
forall {a}. Show a => Int -> GenTree a -> String
go Int
0
where
go :: Int -> GenTree a -> String
go Int
indent (Leaf a
t) = Int -> Char -> String
forall a. Int -> a -> [a]
replicate Int
indent Char
' ' String -> ShowS
forall a. [a] -> [a] -> [a]
++ a -> String
forall a. Show a => a -> String
show a
t String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
"\n"
go Int
indent (Node Sym
sym [GenTree a]
children) =
Int -> Char -> String
forall a. Int -> a -> [a]
replicate Int
indent Char
' ' String -> ShowS
forall a. [a] -> [a] -> [a]
++ Sym -> String
forall a. Show a => a -> String
show Sym
sym String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
"\n" String -> ShowS
forall a. [a] -> [a] -> [a]
++ (GenTree a -> String) -> [GenTree a] -> String
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap (Int -> GenTree a -> String
go (Int
indent Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
2)) [GenTree a]
children