Class of data types with typed grammar representation. It has to be
instantiated in order to use the function Text.GRead.gread.
Instances can be derived automatically using the functions defined
in the module Text.GRead.Derive.
For example, given the declarations
infixl 5 :<:
infixr 6 :>:, :*:
data T1 = T1 :<: T1
| T1 :>: T1
| C1
data T2 a = a :*: T2 a
| C2
the instances of Gram can be
_0 = Zero
_1 = Suc _0
instance Gram T1 where
grammar = DGrammar _0 envT1
envT1 :: Env DGram ((),T1) ((),T1)
envT1 = consD (nonts _0) Empty
where
nonts _T1 = DLNontDefs
[ ( DRef (_T1, 5)
, DPS [ dNont (_T1, 5) .#. dTerm ":<:" .#.
dNont (_T1, 6) .#. dEnd infixL ]
)
, ( DRef (_T1, 6)
, DPS [ dNont (_T1, 7) .#. dTerm ":>:" .#.
dNont (_T1, 6) .#. dEnd infixR ]
)
, ( DRef (_T1,10)
, DPS [ dTerm "C1" .#. dEnd (const C1)
, dTerm "(" .#. dNont (_T1,0) .#.
dTerm ")" .#. dEnd parenT ]
)
]
infixL e1 _ e2 = e2 :<: e1
infixR e1 _ e2 = e2 :>: e1
instance Gram a => Gram (T2 a) where
grammar = DGrammar _0 envT2
envT2 :: (Gram a) => Env DGram (((),a),T2 a)
(((),a),T2 a)
envT2 = consD (nonts _0 _1) $
consG grammar Empty
where
nonts _T2 _A = DLNontDefs
[ ( DRef (_T2, 6)
, DPS [ dNont (_A, 7) .#. dTerm ":*:" .#.
dNont (_T2, 7) .#. dEnd infixT ]
)
, ( DRef (_T2,10)
, DPS [ dTerm "C2" .#. dEnd (const C2)
, dTerm "(" .#. dNont (_T2,0) .#.
dTerm ")" .#. dEnd parenT ]
)
]
infixP e1 _ e2 = e2 :+: e1
infixT e1 _ e2 = e2 :*: e1
In case of mutually recursive datatypes, their definitions have
to be tupled together into a single environment.
|