{-  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
    <http://www.gnu.org/licenses/>.
-}
{-# LANGUAGE RankNTypes #-}

module Text.GrammarCombinators.Utils.MemoizeGrammar (
  memoizeGrammarR,
  memoizeGrammar,
  memoizeGrammarE
  ) where

import Text.GrammarCombinators.Base

newtype WrapCFGR p rr ix = WCFGR { unWCFGR :: p (rr ix) }

-- | Memoize the production rules of a regular grammar. Currently not sure if this is ever useful.
memoizeGrammarR :: (MemoFam phi) => 
                  GRegularGrammar phi t r rr -> GRegularGrammar phi t r rr
memoizeGrammarR gram idx = unWCFGR (memoFamily (\idx' -> WCFGR (gram idx')) idx)

-- | Memoize the production rules of a grammar. Currently not sure if this is ever useful.
memoizeGrammar :: (MemoFam phi) => 
                  GContextFreeGrammar phi t r rr -> GContextFreeGrammar phi t r rr
memoizeGrammar gram idx = unWCFGR (memoFamily (\idx' -> WCFGR (gram idx')) idx)

-- | Memoize the production rules of an extended grammar. Currently not sure if this is ever useful.
memoizeGrammarE :: (MemoFam phi) => 
                  GExtendedContextFreeGrammar phi t r rr -> GExtendedContextFreeGrammar phi t r rr
memoizeGrammarE gram idx = unWCFGR (memoFamily (\idx' -> WCFGR (gram idx')) idx)