importsThenTopdecls :: Bool -> 
  ParsecT [Token] SourcePos Identity ([ImportDeclaration], [Declaration])
importsThenTopdecls explicit =
    do
        is <- many (do { i <- impdecl
                       ; if explicit then lexSEMI else lexSEMI <|> lexINSERTED_SEMI 
                       ; return i
                       } )
        ds <- topdeclCombinator topdecl
        return (is, ds)
        
  where
    topdeclCombinator = if explicit then semiSepTerm else semiOrInsertedSemiSepTerm

        
-- JW: Need to add to topdecl '| "class" [scontext =>] tycls tyvar [where cdecls] '
-- First try " class tycon tyvar [where cdecls]" as class constraints do not yet exist
-- in a later phase add them then also data has to be changed to deal with typeclass constraints
-- please note that in ghc already this has some strange behaviour, read semantics carefully...
    
{-
topdecl  
    ->  impdecl  
     |  "data" simpletype "=" constrs derivings?
     |  "type" simpletype "=" type
     |  infixdecl
     |  decl  

derivings
    -> "deriving" derivings'

derivings'
    -> tycon 
     | "(" ")"
     | "(" tycon ( "," tycon )* ")"

simpletype  
    ->  tycon tyvar1 ... tyvark  (k>=0)  
-}

{-
    | Data                            
        range                    : Range
        context                  : ContextItems
        simpletype               : SimpleType
        constructors             : Constructors
        derivings                : Names
-}
importsThenTopdecls :: Bool -> 
  ParsecT [Token] SourcePos Identity ([ImportDeclaration], [Declaration])
importsThenTopdecls explicit =
    do
        is <- many (do { i <- impdecl
                       ; if explicit then lexSEMI else lexSEMI <|> lexINSERTED_SEMI 
                       ; return i
                       } )
        ds <- topdeclCombinator topdecl
        return (is, ds)
        
  where
    topdeclCombinator = if explicit then semiSepTerm else semiOrInsertedSemiSepTerm

        
-- JW: Need to add to topdecl '| "class" [scontext =>] tycls tyvar [where cdecls] '
-- First try " class tycon tyvar [where cdecls]" as class constraints do not yet exist
-- in a later phase add them then also data has to be changed to deal with typeclass constraints
-- please note that in ghc already this has some strange behaviour, read semantics carefully...
    
{-
topdecl  
    ->  impdecl  
     |  "data" simpletype "=" constrs derivings?
     |  "type" simpletype "=" type
     |  infixdecl
     |  decl  

derivings
    -> "deriving" derivings'

derivings'
    -> tycon 
     | "(" ")"
     | "(" tycon ( "," tycon )* ")"

simpletype  
    ->  tycon tyvar1 ... tyvark  (k>=0)  
-}

{-
    | Data                            
        range                    : Range
        context                  : ContextItems
        simpletype               : SimpleType
        constructors             : Constructors
        derivings                : Names
-}
importsThenTopdecls explicit =
    do
        is <- many (do { i <- impdecl
                       ; if explicit then lexSEMI else lexSEMI <|> lexINSERTED_SEMI 
                       ; return i
                       } )
        ds <- topdeclCombinator topdecl
        return (is, ds)
        
  where
    topdeclCombinator = if explicit then semiSepTerm else semiOrInsertedSemiSepTerm

        
-- JW: Need to add to topdecl '| "class" [scontext =>] tycls tyvar [where cdecls] '
-- First try " class tycon tyvar [where cdecls]" as class constraints do not yet exist
-- in a later phase add them then also data has to be changed to deal with typeclass constraints
-- please note that in ghc already this has some strange behaviour, read semantics carefully...
    
{-
topdecl  
    ->  impdecl  
     |  "data" simpletype "=" constrs derivings?
     |  "type" simpletype "=" type
     |  infixdecl
     |  decl  

derivings
    -> "deriving" derivings'

derivings'
    -> tycon 
     | "(" ")"
     | "(" tycon ( "," tycon )* ")"

simpletype  
    ->  tycon tyvar1 ... tyvark  (k>=0)  
-}

{-
    | Data                            
        range                    : Range
        context                  : ContextItems
        simpletype               : SimpleType
        constructors             : Constructors
        derivings                : Names
-}