| 1 |
{-# LANGUAGE DeriveFoldable #-} |
| 2 |
{-# LANGUAGE DeriveFunctor #-} |
| 3 |
{-# LANGUAGE DeriveTraversable #-} |
| 4 |
{-# OPTIONS_GHC -fno-warn-incomplete-patterns #-} -- uniplate patterns |
| 5 |
{-# OPTIONS_HADDOCK show-extensions #-} |
| 6 |
|
| 7 |
-- | |
| 8 |
-- Module : Yi.Syntax.OnlineTree |
| 9 |
-- License : GPL-2 |
| 10 |
-- Maintainer : [email protected] |
| 11 |
-- Stability : experimental |
| 12 |
-- Portability : portable |
| 13 |
-- |
| 14 |
-- Module defining the 'Tree' used as part of many 'Mode's. |
| 15 |
|
| 16 |
module Yi.Syntax.OnlineTree (Tree(..), manyToks, |
| 17 |
tokAtOrBefore) where |
| 18 |
|
| 19 |
import Yi.IncrementalParse (P, Parser (Look), symbol) |
| 20 |
import Yi.Lexer.Alex (Tok) |
| 21 |
import Yi.Syntax.Tree (IsTree (emptyNode, uniplate), tokAtOrBefore) |
| 22 |
|
| 23 |
data Tree a = Bin (Tree a) (Tree a) |
| 24 |
| Leaf a |
| 25 |
| Tip |
| 26 |
deriving (Show, Functor, Foldable, Traversable) |
| 27 |
|
| 28 |
instance IsTree Tree where |
| 29 |
emptyNode = Tip |
| 30 |
uniplate (Bin l r) = ([l,r],\[l',r'] -> Bin l' r') |
| 31 |
uniplate t = ([],const t) |
| 32 |
|
| 33 |
manyToks :: P (Tok t) (Tree (Tok t)) |
| 34 |
manyToks = manyToks' 1 |
| 35 |
|
| 36 |
manyToks' :: Int -> P a (Tree a) |
| 37 |
manyToks' n = Look (pure Tip) (\_ -> Bin <$> subTree n <*> manyToks' (n * 2)) |
| 38 |
|
| 39 |
subTree :: Int -> P a (Tree a) |
| 40 |
subTree n = Look (pure Tip) . const $ case n of |
| 41 |
0 -> pure Tip |
| 42 |
1 -> Leaf <$> symbol (const True) |
| 43 |
_ -> let m = n `div` 2 in Bin <$> subTree m <*> subTree m |