    {-# LANGUAGE DeriveFoldable    #-}
    {-# LANGUAGE DeriveFunctor     #-}
    {-# LANGUAGE DeriveTraversable #-}
    {-# OPTIONS_GHC -fno-warn-incomplete-patterns #-} -- uniplate patterns
    {-# OPTIONS_HADDOCK show-extensions #-}

    -- |
    -- Module      :  Yi.Syntax.OnlineTree
    -- License     :  GPL-2
    -- Maintainer  :  yi-devel@googlegroups.com
    -- Stability   :  experimental
    -- Portability :  portable
    --
    -- Module defining the 'Tree' used as part of many 'Mode's.

    module Yi.Syntax.OnlineTree (Tree(..), manyToks,
                                 tokAtOrBefore) where

    import Yi.IncrementalParse (P, Parser (Look), symbol)
    import Yi.Lexer.Alex       (Tok)
    import Yi.Syntax.Tree      (IsTree (emptyNode, uniplate), tokAtOrBefore)

    data Tree a = Bin (Tree a) (Tree a)
                | Leaf a
                | Tip
                  deriving (Int -> Tree a -> ShowS
    [Tree a] -> ShowS
    Tree a -> String
    (Int -> Tree a -> ShowS)
    -> (Tree a -> String) -> ([Tree a] -> ShowS) -> Show (Tree a)
    forall a. Show a => Int -> Tree a -> ShowS
    forall a. Show a => [Tree a] -> ShowS
    forall a. Show a => Tree a -> String
    forall a.
    (Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
    $cshowsPrec :: forall a. Show a => Int -> Tree a -> ShowS
    showsPrec :: Int -> Tree a -> ShowS
    $cshow :: forall a. Show a => Tree a -> String
    show :: Tree a -> String
    $cshowList :: forall a. Show a => [Tree a] -> ShowS
    showList :: [Tree a] -> ShowS
    Show, (forall a b. (a -> b) -> Tree a -> Tree b)
    -> (forall a b. a -> Tree b -> Tree a) -> Functor Tree
    forall a b. a -> Tree b -> Tree a
    forall a b. (a -> b) -> Tree a -> Tree 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) -> Tree a -> Tree b
    fmap :: forall a b. (a -> b) -> Tree a -> Tree b
    $c<$ :: forall a b. a -> Tree b -> Tree a
    <$ :: forall a b. a -> Tree b -> Tree a
    Functor, (forall m. Monoid m => Tree m -> m)
    -> (forall m a. Monoid m => (a -> m) -> Tree a -> m)
    -> (forall m a. Monoid m => (a -> m) -> Tree a -> m)
    -> (forall a b. (a -> b -> b) -> b -> Tree a -> b)
    -> (forall a b. (a -> b -> b) -> b -> Tree a -> b)
    -> (forall b a. (b -> a -> b) -> b -> Tree a -> b)
    -> (forall b a. (b -> a -> b) -> b -> Tree a -> b)
    -> (forall a. (a -> a -> a) -> Tree a -> a)
    -> (forall a. (a -> a -> a) -> Tree a -> a)
    -> (forall a. Tree a -> [a])
    -> (forall a. Tree a -> Bool)
    -> (forall a. Tree a -> Int)
    -> (forall a. Eq a => a -> Tree a -> Bool)
    -> (forall a. Ord a => Tree a -> a)
    -> (forall a. Ord a => Tree a -> a)
    -> (forall a. Num a => Tree a -> a)
    -> (forall a. Num a => Tree a -> a)
    -> Foldable Tree
    forall a. Eq a => a -> Tree a -> Bool
    forall a. Num a => Tree a -> a
    forall a. Ord a => Tree a -> a
    forall m. Monoid m => Tree m -> m
    forall a. Tree a -> Bool
    forall a. Tree a -> Int
    forall a. Tree a -> [a]
    forall a. (a -> a -> a) -> Tree a -> a
    forall m a. Monoid m => (a -> m) -> Tree a -> m
    forall b a. (b -> a -> b) -> b -> Tree a -> b
    forall a b. (a -> b -> b) -> b -> Tree a -> b
    forall (t :: * -> *).
    (forall m. Monoid m => t m -> m)
    -> (forall m a. Monoid m => (a -> m) -> t a -> m)
    -> (forall m a. Monoid m => (a -> m) -> t a -> m)
    -> (forall a b. (a -> b -> b) -> b -> t a -> b)
    -> (forall a b. (a -> b -> b) -> b -> t a -> b)
    -> (forall b a. (b -> a -> b) -> b -> t a -> b)
    -> (forall b a. (b -> a -> b) -> b -> t a -> b)
    -> (forall a. (a -> a -> a) -> t a -> a)
    -> (forall a. (a -> a -> a) -> t a -> a)
    -> (forall a. t a -> [a])
    -> (forall a. t a -> Bool)
    -> (forall a. t a -> Int)
    -> (forall a. Eq a => a -> t a -> Bool)
    -> (forall a. Ord a => t a -> a)
    -> (forall a. Ord a => t a -> a)
    -> (forall a. Num a => t a -> a)
    -> (forall a. Num a => t a -> a)
    -> Foldable t
    $cfold :: forall m. Monoid m => Tree m -> m
    fold :: forall m. Monoid m => Tree m -> m
    $cfoldMap :: forall m a. Monoid m => (a -> m) -> Tree a -> m
    foldMap :: forall m a. Monoid m => (a -> m) -> Tree a -> m
    $cfoldMap' :: forall m a. Monoid m => (a -> m) -> Tree a -> m
    foldMap' :: forall m a. Monoid m => (a -> m) -> Tree a -> m
    $cfoldr :: forall a b. (a -> b -> b) -> b -> Tree a -> b
    foldr :: forall a b. (a -> b -> b) -> b -> Tree a -> b
    $cfoldr' :: forall a b. (a -> b -> b) -> b -> Tree a -> b
    foldr' :: forall a b. (a -> b -> b) -> b -> Tree a -> b
    $cfoldl :: forall b a. (b -> a -> b) -> b -> Tree a -> b
    foldl :: forall b a. (b -> a -> b) -> b -> Tree a -> b
    $cfoldl' :: forall b a. (b -> a -> b) -> b -> Tree a -> b
    foldl' :: forall b a. (b -> a -> b) -> b -> Tree a -> b
    $cfoldr1 :: forall a. (a -> a -> a) -> Tree a -> a
    foldr1 :: forall a. (a -> a -> a) -> Tree a -> a
    $cfoldl1 :: forall a. (a -> a -> a) -> Tree a -> a
    foldl1 :: forall a. (a -> a -> a) -> Tree a -> a
    $ctoList :: forall a. Tree a -> [a]
    toList :: forall a. Tree a -> [a]
    $cnull :: forall a. Tree a -> Bool
    null :: forall a. Tree a -> Bool
    $clength :: forall a. Tree a -> Int
    length :: forall a. Tree a -> Int
    $celem :: forall a. Eq a => a -> Tree a -> Bool
    elem :: forall a. Eq a => a -> Tree a -> Bool
    $cmaximum :: forall a. Ord a => Tree a -> a
    maximum :: forall a. Ord a => Tree a -> a
    $cminimum :: forall a. Ord a => Tree a -> a
    minimum :: forall a. Ord a => Tree a -> a
    $csum :: forall a. Num a => Tree a -> a
    sum :: forall a. Num a => Tree a -> a
    $cproduct :: forall a. Num a => Tree a -> a
    product :: forall a. Num a => Tree a -> a
    Foldable, Functor Tree
    Foldable Tree
    (Functor Tree, Foldable Tree) =>
    (forall (f :: * -> *) a b.
     Applicative f =>
     (a -> f b) -> Tree a -> f (Tree b))
    -> (forall (f :: * -> *) a.
        Applicative f =>
        Tree (f a) -> f (Tree a))
    -> (forall (m :: * -> *) a b.
        Monad m =>
        (a -> m b) -> Tree a -> m (Tree b))
    -> (forall (m :: * -> *) a. Monad m => Tree (m a) -> m (Tree a))
    -> Traversable Tree
    forall (t :: * -> *).
    (Functor t, Foldable t) =>
    (forall (f :: * -> *) a b.
     Applicative f =>
     (a -> f b) -> t a -> f (t b))
    -> (forall (f :: * -> *) a. Applicative f => t (f a) -> f (t a))
    -> (forall (m :: * -> *) a b.
        Monad m =>
        (a -> m b) -> t a -> m (t b))
    -> (forall (m :: * -> *) a. Monad m => t (m a) -> m (t a))
    -> Traversable t
    forall (m :: * -> *) a. Monad m => Tree (m a) -> m (Tree a)
    forall (f :: * -> *) a. Applicative f => Tree (f a) -> f (Tree a)
    forall (m :: * -> *) a b.
    Monad m =>
    (a -> m b) -> Tree a -> m (Tree b)
    forall (f :: * -> *) a b.
    Applicative f =>
    (a -> f b) -> Tree a -> f (Tree b)
    $ctraverse :: forall (f :: * -> *) a b.
    Applicative f =>
    (a -> f b) -> Tree a -> f (Tree b)
    traverse :: forall (f :: * -> *) a b.
    Applicative f =>
    (a -> f b) -> Tree a -> f (Tree b)
    $csequenceA :: forall (f :: * -> *) a. Applicative f => Tree (f a) -> f (Tree a)
    sequenceA :: forall (f :: * -> *) a. Applicative f => Tree (f a) -> f (Tree a)
    $cmapM :: forall (m :: * -> *) a b.
    Monad m =>
    (a -> m b) -> Tree a -> m (Tree b)
    mapM :: forall (m :: * -> *) a b.
    Monad m =>
    (a -> m b) -> Tree a -> m (Tree b)
    $csequence :: forall (m :: * -> *) a. Monad m => Tree (m a) -> m (Tree a)
    sequence :: forall (m :: * -> *) a. Monad m => Tree (m a) -> m (Tree a)
    Traversable)

    instance IsTree Tree where
        emptyNode :: forall t. Tree t
    emptyNode = Tree t
    forall t. Tree t
    Tip
        uniplate :: forall t. Tree t -> ([Tree t], [Tree t] -> Tree t)
    uniplate (Bin Tree t
    l Tree t
    r) = ([Tree t
    l,Tree t
    r],\[Tree t
    l',Tree t
    r'] -> Tree t -> Tree t -> Tree t
    forall a. Tree a -> Tree a -> Tree a
    Bin Tree t
    l' Tree t
    r')
        uniplate Tree t
    t = ([],Tree t -> [Tree t] -> Tree t
    forall a b. a -> b -> a
    const Tree t
    t)

    manyToks :: P (Tok t) (Tree (Tok t))
    manyToks :: forall t. P (Tok t) (Tree (Tok t))
    manyToks = Int -> P (Tok t) (Tree (Tok t))
    forall a. Int -> P a (Tree a)
    manyToks' Int
    1

    manyToks' :: Int -> P a (Tree a)
    manyToks' :: forall a. Int -> P a (Tree a)
    manyToks' Int
    n = Parser a (Tree a) -> (a -> Parser a (Tree a)) -> Parser a (Tree a)
    forall s a. Parser s a -> (s -> Parser s a) -> Parser s a
    Look (Tree a -> Parser a (Tree a)
    forall a. a -> Parser a a
    forall (f :: * -> *) a. Applicative f => a -> f a
    pure Tree a
    forall t. Tree t
    Tip) (\a
    _ -> Tree a -> Tree a -> Tree a
    forall a. Tree a -> Tree a -> Tree a
    Bin (Tree a -> Tree a -> Tree a)
    -> Parser a (Tree a) -> Parser a (Tree a -> Tree a)
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    <$> Int -> Parser a (Tree a)
    forall a. Int -> P a (Tree a)
    subTree Int
    n Parser a (Tree a -> Tree a)
    -> Parser a (Tree a) -> Parser a (Tree a)
    forall a b. Parser a (a -> b) -> Parser a a -> Parser a b
    forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
    <*> Int -> Parser a (Tree a)
    forall a. Int -> P a (Tree a)
    manyToks' (Int
    n Int -> Int -> Int
    forall a. Num a => a -> a -> a
    * Int
    2))

    subTree :: Int -> P a (Tree a)
    subTree :: forall a. Int -> P a (Tree a)
    subTree Int
    n = Parser a (Tree a) -> (a -> Parser a (Tree a)) -> Parser a (Tree a)
    forall s a. Parser s a -> (s -> Parser s a) -> Parser s a
    Look (Tree a -> Parser a (Tree a)
    forall a. a -> Parser a a
    forall (f :: * -> *) a. Applicative f => a -> f a
    pure Tree a
    forall t. Tree t
    Tip) ((a -> Parser a (Tree a)) -> Parser a (Tree a))
    -> (Parser a (Tree a) -> a -> Parser a (Tree a))
    -> Parser a (Tree a)
    -> Parser a (Tree a)
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . Parser a (Tree a) -> a -> Parser a (Tree a)
    forall a b. a -> b -> a
    const (Parser a (Tree a) -> Parser a (Tree a))
    -> Parser a (Tree a) -> Parser a (Tree a)
    forall a b. (a -> b) -> a -> b
    $ case Int
    n of
      Int
    0 -> Tree a -> Parser a (Tree a)
    forall a. a -> Parser a a
    forall (f :: * -> *) a. Applicative f => a -> f a
    pure Tree a
    forall t. Tree t
    Tip
      Int
    1 -> a -> Tree a
    forall a. a -> Tree a
    Leaf (a -> Tree a) -> Parser a a -> Parser a (Tree a)
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    <$> (a -> Bool) -> Parser a a
    forall s. (s -> Bool) -> Parser s s
    symbol (Bool -> a -> Bool
    forall a b. a -> b -> a
    const Bool
    True)
      Int
    _ -> let m :: Int
    m = Int
    n Int -> Int -> Int
    forall a. Integral a => a -> a -> a
    `div` Int
    2 in Tree a -> Tree a -> Tree a
    forall a. Tree a -> Tree a -> Tree a
    Bin (Tree a -> Tree a -> Tree a)
    -> Parser a (Tree a) -> Parser a (Tree a -> Tree a)
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    <$> Int -> Parser a (Tree a)
    forall a. Int -> P a (Tree a)
    subTree Int
    m Parser a (Tree a -> Tree a)
    -> Parser a (Tree a) -> Parser a (Tree a)
    forall a b. Parser a (a -> b) -> Parser a a -> Parser a b
    forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
    <*> Int -> Parser a (Tree a)
    forall a. Int -> P a (Tree a)
    subTree Int
    m