1 {-# LANGUAGE GeneralizedNewtypeDeriving #-}
2 {-# LANGUAGE TypeFamilies #-}
3 {-# LANGUAGE ViewPatterns #-}
4 module Language.TCT.Read
5 ( module Language.TCT.Read.Tree
6 , module Language.TCT.Read.Token
7 , module Language.TCT.Read.Cell
8 , module Language.TCT.Read
11 import Control.Applicative (Applicative(..))
12 import Control.Monad (Monad(..), join)
13 import Data.Char (Char)
14 import Data.Either (Either(..))
15 import Data.Eq (Eq(..))
16 import Data.Function (($), (.))
17 import Data.Functor ((<$>), (<$))
18 import Data.Foldable (Foldable(..))
19 import Data.Maybe (Maybe(..))
20 import Data.Ord (Ord(..))
21 import Data.Proxy (Proxy(..))
22 import Data.Semigroup (Semigroup(..))
23 import Data.String (IsString)
24 import Data.Traversable (Traversable(..))
25 import Data.TreeSeq.Strict (Tree(..), Trees)
26 import Data.Void (Void)
27 import System.IO (FilePath)
28 import Text.Show (Show(..))
29 import qualified Data.Text.Lazy as TL
30 import qualified Data.TreeSeq.Strict as Tree
31 import qualified Text.Megaparsec as P
32 -- import qualified Data.List as List
34 import Language.TCT.Tree
35 import Language.TCT.Cell
36 import Language.TCT.Read.Cell
37 import Language.TCT.Read.Tree
38 import Language.TCT.Read.Token
40 import Debug.Trace (trace)
43 FilePath -> TL.Text ->
44 Either (P.ParseError (P.Token TL.Text) (P.ErrorFancy Void)) (Trees (Cell Node))
45 readTrees inp txt = do
46 trs <- P.runParser (p_Trees <* P.eof) inp txt
48 traverse (go Nothing) $
49 trace ("### TRS ###\n"<>show (Tree.Pretty trs)) trs
54 Either (P.ParseError (P.Token TL.Text) (P.ErrorFancy Void))
56 go p t@(Tree c@(Cell bn en nod) ts) =
58 NodeGroup{} -> Tree c <$> traverse (go (Just nod)) ts
59 NodeHeader{} -> Tree c <$> traverse (go (Just nod)) ts
60 NodeToken{} -> Tree c <$> traverse (go (Just nod)) ts
61 NodePair{} -> Tree c <$> traverse (go (Just nod)) ts
62 NodePara{} -> Tree c <$> traverse (go (Just nod)) ts
63 NodeLower{} -> Right t
64 -- NodeText n | TL.null n -> Right t
67 Just (NodeHeader HeaderBar{}) -> Right t
68 Just (NodeHeader HeaderEqual{}) -> Right t
70 toks <- parseTokens <$> parseLexemes inp (n <$ c)
74 _ -> Tree (Cell bn en NodeGroup) toks
76 NodeHeader _n -> pure . Tree c . join <$> traverse (go (Just nod)) ts
77 NodeToken _n -> pure . Tree c . join <$> traverse (go (Just nod)) ts
78 NodePair _n -> pure . Tree c . join <$> traverse (go (Just nod)) ts
79 NodeLower{} -> Right $ pure t
80 NodeText n | TL.null n -> Right $ pure t
83 Just (NodeHeader HeaderBar{}) -> Right $ pure t
84 Just (NodeHeader HeaderEqual{}) -> Right $ pure t
86 acc <- parseLexemes inp (n <$ c)
87 sn <- traverse (go (Just nod)) ts
88 return $ parseTokens $
89 foldr (\s a -> orientLexemePairAny $ LexemeTree s:a) acc (join sn)