{- 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 FlexibleContexts #-} {-# LANGUAGE FlexibleInstances #-} {-# LANGUAGE KindSignatures #-} {-# LANGUAGE MultiParamTypeClasses #-} {-# LANGUAGE RankNTypes #-} {-# LANGUAGE ScopedTypeVariables #-} module Text.GrammarCombinators.Test.Paper.SimplePrinter where import Text.GrammarCombinators.Base import Text.GrammarCombinators.Transform.FilterDies import Text.GrammarCombinators.Utils.IsReachable import Text.GrammarCombinators.Test.Paper.PaperExample newtype PrintProductionRule (phi :: * -> *) (r :: * -> *) t v = IPP { printIPP :: String } instance ProductionRule (PrintProductionRule phi r t) where die = IPP "die" endOfInput = IPP "EOI" a ||| b = IPP $ "(" ++ printIPP a ++ " | " ++ printIPP b ++ ")" a >>> b = IPP $ printIPP a ++ " " ++ printIPP b instance LiftableProductionRule (PrintProductionRule phi r t) where epsilonL v _ = IPP "epsilon" instance EpsProductionRule (PrintProductionRule phi r t) where epsilon v = IPP "epsilon" instance (Token t) => TokenProductionRule (PrintProductionRule phi r t) t where token t = IPP $ show t instance (ShowFam phi) => RecProductionRule (PrintProductionRule phi r t) phi r where ref idx = IPP $ "<" ++ showIdx idx ++ ">" instance (ShowFam phi) => LoopProductionRule (PrintProductionRule phi r t) phi r where manyRef idx = IPP $ "<" ++ showIdx idx ++ ">" ++ "*" printRule :: (Domain phi, Token t) => GExtendedContextFreeGrammar phi t r rr -> phi ix -> String printRule gram idx = "<" ++ showIdx idx ++ ">" ++ " ::= " ++ printIPP (gram idx) printGrammar :: forall phi t r rr. (Domain phi, Token t) => GExtendedContextFreeGrammar phi t r rr -> String printGrammar gram = foldFam ((++) . (++"\n") . printRule gram) []