{-  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 FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE KindSignatures #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE RankNTypes #-}

module Text.GrammarCombinators.Utils.IsEpsilon ( 
  isEpsilon
  ) where

import Text.GrammarCombinators.Base

newtype IsEpsilonRule (phi :: * -> *) (r :: * -> *) t v = MkIER {
  unIER :: Bool
  }
                        
instance ProductionRule (IsEpsilonRule phi r t) where
  ra >>> rb = MkIER $ unIER ra && unIER rb
  ra ||| rb = MkIER $ unIER ra && unIER rb
  die = MkIER False
  endOfInput = MkIER False

instance LiftableProductionRule (IsEpsilonRule phi r t) where
  epsilonL v _ = epsilon v

instance EpsProductionRule (IsEpsilonRule phi r t) where
  epsilon _ = MkIER True

instance TokenProductionRule (IsEpsilonRule phi r t) t where
  token _ = MkIER False
  anyToken = MkIER False
  
instance RecProductionRule (IsEpsilonRule phi r t) phi r where
  ref _ = MkIER False

instance LoopProductionRule (IsEpsilonRule phi r t) phi r where
  manyRef _ = MkIER False

-- | Detect if a given context-free rule is an epsilon rule.
isEpsilon :: ContextFreeRule phi r t v -> Bool
isEpsilon r = unIER r