{- Copyright 2010 Dominique Devriese This file is part of the grammar-combinators library. The grammar-combinators library is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. Foobar is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with Foobar. If not, see . -} {-# LANGUAGE EmptyDataDecls #-} {-# LANGUAGE FlexibleContexts #-} {-# LANGUAGE GADTs #-} {-# LANGUAGE TypeFamilies #-} module Text.GrammarCombinators.Test.IndRec where import Text.GrammarCombinators.Base import Text.GrammarCombinators.Utils.PrintGrammar import Text.GrammarCombinators.Parser.Parsec import Text.GrammarCombinators.Parser.Packrat import Text.GrammarCombinators.Transform.OptimizeGrammar import Text.GrammarCombinators.Transform.FoldLoops import Text.GrammarCombinators.Transform.UnfoldDead import Text.GrammarCombinators.Utils.IsDead import Text.GrammarCombinators.Utils.EnumerateGrammar import Text.GrammarCombinators.Transform.UniformPaull import Text.GrammarCombinators.Transform.LeftCorner import Text.GrammarCombinators.Transform.FilterDies import Text.GrammarCombinators.Utils.IsReachable data A data B data ABDom ix where A :: ABDom A B :: ABDom B instance MemoFam ABDom where data Memo ABDom r = ABM (r A) (r B) fromMemo (ABM v _) A = v fromMemo (ABM _ v) B = v toMemo f = ABM (f A) (f B) instance EqFam ABDom where overrideIdx f A v A = v overrideIdx f B v B = v overrideIdx f _ _ idx = f idx instance ShowFam ABDom where showIdx A = "A" showIdx B = "B" instance FoldFam ABDom where foldFam f n = f A $ f B n instance Domain ABDom data PFAB r ix where A1F :: r B -> r A -> PFAB r A A3F :: PFAB r A B1F :: r A -> PFAB r B B2F :: PFAB r B type instance PF ABDom = PFAB grammar :: ContextFreeGrammar ABDom Char grammar A = A1F $>> ref B >>>* token 'a' >>> ref A ||| A3F $>>* token 'a' grammar B = B1F $>> ref A >>>* token 'b' ||| B2F $>>* token 'b' proc :: Processor ABDom (K0 String) proc A (A1F (K0 b) (K0 a)) = K0 $ b ++ "A" ++ a proc A A3F = K0 "" proc B (B1F (K0 a)) = K0 $ a ++ "B" proc B B2F = K0 "B" pg :: ProcessingContextFreeGrammar ABDom Char (K0 String) pg = applyProcessor grammar proc