module Data.Cfg.LookaheadSet (
LookaheadSet,
mkLookaheadSet,
fromList,
toSet,
(<>),
empty,
singleton,
unions
) where
import Data.Cfg.Augment(AugT(..))
import Data.Monoid(Monoid(..), (<>))
import qualified Data.Set as S
newtype LookaheadSet t = LookaheadSet {
toSet :: S.Set (AugT t)
}
deriving (Eq, Ord, Show)
instance Ord t => Monoid (LookaheadSet t) where
mempty = LookaheadSet $ S.singleton EOF
l@(LookaheadSet s) `mappend` LookaheadSet s'
= if EOF `S.member` s
then LookaheadSet $ S.delete EOF s `S.union` s'
else l
mkLookaheadSet :: (Ord t)
=> Bool
-> [t]
-> LookaheadSet t
mkLookaheadSet hasEOF = LookaheadSet . S.fromList . f . map AugT
where
f = if hasEOF then (EOF:) else id
fromList :: Ord t => [AugT t] -> LookaheadSet t
fromList = LookaheadSet . S.fromList
empty :: LookaheadSet t
empty = LookaheadSet S.empty
singleton :: AugT t -> LookaheadSet t
singleton = LookaheadSet . S.singleton
unions :: Ord t => [LookaheadSet t] -> LookaheadSet t
unions = LookaheadSet . S.unions . map toSet