Parser.y 162 KB
Newer Older
chak@cse.unsw.edu.au.'s avatar
chak@cse.unsw.edu.au. committed
1
--                                                              -*-haskell-*-
2 3 4 5 6 7 8 9 10
-- ---------------------------------------------------------------------------
-- (c) The University of Glasgow 1997-2003
---
-- The GHC grammar.
--
-- Author(s): Simon Marlow, Sven Panne 1997, 1998, 1999
-- ---------------------------------------------------------------------------

{
11 12 13 14 15 16 17 18 19 20 21 22
-- | This module provides the generated Happy parser for Haskell. It exports
-- a number of parsers which may be used in any library that uses the GHC API.
-- A common usage pattern is to initialize the parser state with a given string
-- and then parse that string:
--
-- @
--     runParser :: DynFlags -> String -> P a -> ParseResult a
--     runParser flags str parser = unP parser parseState
--     where
--       filename = "\<interactive\>"
--       location = mkRealSrcLoc (mkFastString filename) 1 1
--       buffer = stringToStringBuffer str
23
--       parseState = mkPState flags buffer location
24 25
-- @
module Parser (parseModule, parseImport, parseStatement,
26 27
               parseDeclaration, parseExpression, parsePattern,
               parseTypeSignature,
28
               parseStmt, parseIdentifier,
29 30
               parseType, parseHeader) where

rodlogic's avatar
rodlogic committed
31 32 33 34 35
-- base
import Control.Monad    ( unless, liftM )
import GHC.Exts
import Data.Char
import Control.Monad    ( mplus )
36

rodlogic's avatar
rodlogic committed
37
-- compiler/hsSyn
38
import HsSyn
rodlogic's avatar
rodlogic committed
39 40

-- compiler/main
chak@cse.unsw.edu.au.'s avatar
chak@cse.unsw.edu.au. committed
41
import HscTypes         ( IsBootInterface, WarningTxt(..) )
rodlogic's avatar
rodlogic committed
42 43 44 45
import DynFlags

-- compiler/utils
import OrdList
46
import BooleanFormula   ( BooleanFormula(..), mkTrue )
rodlogic's avatar
rodlogic committed
47 48 49 50 51
import FastString
import Maybes           ( orElse )
import Outputable

-- compiler/basicTypes
52
import RdrName
thomasw's avatar
thomasw committed
53
import OccName          ( varName, dataName, tcClsName, tvName, startsWithUnderscore )
chak@cse.unsw.edu.au.'s avatar
chak@cse.unsw.edu.au. committed
54
import DataCon          ( DataCon, dataConName )
Ian Lynagh's avatar
Ian Lynagh committed
55
import SrcLoc
56
import Module
rodlogic's avatar
rodlogic committed
57 58 59 60
import BasicTypes

-- compiler/types
import Type             ( funTyCon )
dreixel's avatar
dreixel committed
61
import Kind             ( Kind, liftedTypeKind, unliftedTypeKind, mkArrowKind )
chak@cse.unsw.edu.au.'s avatar
chak@cse.unsw.edu.au. committed
62
import Class            ( FunDep )
rodlogic's avatar
rodlogic committed
63 64 65 66

-- compiler/parser
import RdrHsSyn
import Lexer
67
import HaddockUtils
Alan Zimmerman's avatar
Alan Zimmerman committed
68
import ApiAnnotation
69

rodlogic's avatar
rodlogic committed
70 71
-- compiler/typecheck
import TcEvidence       ( emptyTcEvBinds )
72

rodlogic's avatar
rodlogic committed
73 74 75
-- compiler/prelude
import ForeignCall
import TysPrim          ( liftedTypeKindTyConName, eqPrimTyCon )
76
import TysWiredIn       ( unitTyCon, unitDataCon, tupleTyCon, tupleDataCon, nilDataCon,
rodlogic's avatar
rodlogic committed
77 78
                          unboxedUnitTyCon, unboxedUnitDataCon,
                          listTyCon_RDR, parrTyCon_RDR, consDataCon_RDR, eqTyCon_RDR )
Alan Zimmerman's avatar
Alan Zimmerman committed
79

80 81 82
-- compiler/utils
import Util             ( looksLikePackageName )

83 84
}

85
{- Last updated: 31 Jul 2015
86

87
Conflicts: 47 shift/reduce
88

89 90
If you modify this parser and add a conflict, please update this comment.
You can learn more about the conflicts by passing 'happy' the -i flag:
91

92
    happy -agc --strict compiler/parser/Parser.y -idetailed-info
93

94 95 96 97 98 99
How is this section formatted? Look up the state the conflict is
reported at, and copy the list of applicable rules (at the top).  Mark
*** for the rule that is the conflicting reduction (that is, the
interpretation which is NOT taken).  NB: Happy doesn't print a rule in a
state if it is empty, but you should include it in the list (you can
look these up in the Grammar section of the info file).
100

101 102 103 104 105
Obviously the state numbers are not stable across modifications to the parser,
the idea is to reproduce enough information on each conflict so you can figure
out what happened if the states were renumbered.  Try not to gratuitously move
productions around in this file.  It's probably less important to keep
the rule annotations up-to-date.
106

107
-------------------------------------------------------------------------------
Gabor Greif's avatar
Gabor Greif committed
108

109
state 0 contains 1 shift/reduce conflicts.
Gabor Greif's avatar
Gabor Greif committed
110

111
    Conflicts: DOCNEXT (empty missing_module_keyword reduces)
112

113 114 115
Ambiguity when the source file starts with "-- | doc". We need another
token of lookahead to determine if a top declaration or the 'module' keyword
follows. Shift parses as if the 'module' keyword follows.
116

117
-------------------------------------------------------------------------------
118

119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
state 46 contains 2 shift/reduce conflicts.

    *** strict_mark -> unpackedness .                       (rule 268)
        strict_mark -> unpackedness . strictness            (rule 269)

    Conflicts: '~' '!'

-------------------------------------------------------------------------------

state 50 contains 11 shift/reduce conflicts.

        context -> btype .                                  (rule 282)
    *** type -> btype .                                     (rule 283)
        type -> btype . qtyconop type                       (rule 284)
        type -> btype . tyvarop type                        (rule 285)
        type -> btype . '->' ctype                          (rule 286)
        type -> btype . SIMPLEQUOTE qconop type             (rule 287)
        type -> btype . SIMPLEQUOTE varop type              (rule 288)
137
        btype -> btype . atype                              (rule 299)
138

139
    Conflicts: ':' '->' '-' '!' '*' '.' '`' VARSYM CONSYM QVARSYM QCONSYM
140

141 142 143
Example of ambiguity: 'e :: a `b` c';  does this mean
    (e::a) `b` c, or
    (e :: (a `b` c))
144

145 146 147 148 149 150 151 152
The case for '->' involves view patterns rather than type operators:
    'case v of { x :: T -> T ... } '
    Which of these two is intended?
          case v of
            (x::T) -> T         -- Rhs is T
    or
          case v of
            (x::T -> T) -> ..   -- Rhs is ...
153

154
-------------------------------------------------------------------------------
155

156
state 119 contains 15 shift/reduce conflicts.
157

158 159 160 161 162 163 164
        exp -> infixexp . '::' sigtype                      (rule 416)
        exp -> infixexp . '-<' exp                          (rule 417)
        exp -> infixexp . '>-' exp                          (rule 418)
        exp -> infixexp . '-<<' exp                         (rule 419)
        exp -> infixexp . '>>-' exp                         (rule 420)
    *** exp -> infixexp .                                   (rule 421)
        infixexp -> infixexp . qop exp10                    (rule 423)
165

166 167
    Conflicts: ':' '::' '-' '!' '*' '-<' '>-' '-<<' '>>-'
               '.' '`' VARSYM CONSYM QVARSYM QCONSYM
168

169 170 171 172
Examples of ambiguity:
    'if x then y else z -< e'
    'if x then y else z :: T'
    'if x then y else z + 1' (NB: '+' is in VARSYM)
173

174 175 176 177
Shift parses as (per longest-parse rule):
    'if x then y else (z -< T)'
    'if x then y else (z :: T)'
    'if x then y else (z + 1)'
178

179
-------------------------------------------------------------------------------
180

181
state 279 contains 1 shift/reduce conflicts.
182

183
        rule -> STRING . rule_activation rule_forall infixexp '=' exp    (rule 215)
184

185
    Conflict: '[' (empty rule_activation reduces)
186

187 188 189
We don't know whether the '[' starts the activation or not: it
might be the start of the declaration with the activation being
empty.  --SDM 1/4/2002
190

191 192
Example ambiguity:
    '{-# RULE [0] f = ... #-}'
193

194 195
We parse this as having a [0] rule activation for rewriting 'f', rather
a rule instructing how to rewrite the expression '[0] f'.
196

197
-------------------------------------------------------------------------------
ross's avatar
ross committed
198

199
state 288 contains 11 shift/reduce conflicts.
200

201 202 203 204 205 206
    *** type -> btype .                                     (rule 283)
        type -> btype . qtyconop type                       (rule 284)
        type -> btype . tyvarop type                        (rule 285)
        type -> btype . '->' ctype                          (rule 286)
        type -> btype . SIMPLEQUOTE qconop type             (rule 287)
        type -> btype . SIMPLEQUOTE varop type              (rule 288)
207
        btype -> btype . atype                              (rule 299)
208

209
    Conflicts: ':' '->' '-' '!' '*' '.' '`' VARSYM CONSYM QVARSYM QCONSYM
210

211
Same as State 50, but minus the context productions.
212

213
-------------------------------------------------------------------------------
214

215
state 324 contains 1 shift/reduce conflicts.
216

217 218 219
        tup_exprs -> commas . tup_tail                      (rule 505)
        sysdcon_nolist -> '(' commas . ')'                  (rule 616)
        commas -> commas . ','                              (rule 734)
220

221
    Conflict: ')' (empty tup_tail reduces)
222

223 224 225 226
A tuple section with NO free variables '(,,)' is indistinguishable
from the Haskell98 data constructor for a tuple.  Shift resolves in
favor of sysdcon, which is good because a tuple section will get rejected
if -XTupleSections is not specified.
227

228
-------------------------------------------------------------------------------
229

230
state 376 contains 1 shift/reduce conflicts.
231

232 233 234
        tup_exprs -> commas . tup_tail                      (rule 505)
        sysdcon_nolist -> '(#' commas . '#)'                (rule 618)
        commas -> commas . ','                              (rule 734)
235 236 237

    Conflict: '#)' (empty tup_tail reduces)

238
Same as State 324 for unboxed tuples.
239 240 241

-------------------------------------------------------------------------------

242
state 404 contains 1 shift/reduce conflicts.
243

244 245 246 247
        exp10 -> 'let' binds . 'in' exp                     (rule 425)
        exp10 -> 'let' binds . 'in' error                   (rule 440)
        exp10 -> 'let' binds . error                        (rule 441)
    *** qual -> 'let' binds .                               (rule 579)
248 249 250 251 252 253 254

    Conflict: error

TODO: Why?

-------------------------------------------------------------------------------

255
state 633 contains 1 shift/reduce conflicts.
256

257 258
    *** aexp2 -> ipvar .                                    (rule 466)
        dbind -> ipvar . '=' exp                            (rule 590)
259 260 261 262 263 264 265 266 267 268 269

    Conflict: '='

Example ambiguity: 'let ?x ...'

The parser can't tell whether the ?x is the lhs of a normal binding or
an implicit binding.  Fortunately, resolving as shift gives it the only
sensible meaning, namely the lhs of an implicit binding.

-------------------------------------------------------------------------------

270
state 699 contains 1 shift/reduce conflicts.
271

272
        rule -> STRING rule_activation . rule_forall infixexp '=' exp    (rule 215)
273 274 275 276 277 278 279 280 281 282 283 284 285 286

    Conflict: 'forall' (empty rule_forall reduces)

Example ambiguity: '{-# RULES "name" forall = ... #-}'

'forall' is a valid variable name---we don't know whether
to treat a forall on the input as the beginning of a quantifier
or the beginning of the rule itself.  Resolving to shift means
it's always treated as a quantifier, hence the above is disallowed.
This saves explicitly defining a grammar for the rule lhs that
doesn't include 'forall'.

-------------------------------------------------------------------------------

287
state 950 contains 1 shift/reduce conflicts.
288

289 290 291
        transformqual -> 'then' 'group' . 'using' exp       (rule 528)
        transformqual -> 'then' 'group' . 'by' exp 'using' exp    (rule 529)
    *** special_id -> 'group' .                             (rule 711)
292 293 294

    Conflict: 'by'

295 296

-------------------------------------------------------------------------------
297
-- API Annotations
298
--
299 300 301 302 303 304 305 306 307 308 309

A lot of the productions are now cluttered with calls to
aa,am,ams,amms etc.

These are helper functions to make sure that the locations of the
various keywords such as do / let / in are captured for use by tools
that want to do source to source conversions, such as refactorers or
structured editors.

The helper functions are defined at the bottom of this file.

310 311 312 313
See
  https://ghc.haskell.org/trac/ghc/wiki/ApiAnnotations and
  https://ghc.haskell.org/trac/ghc/wiki/GhcAstAnnotations
for some background.
314

315 316 317 318 319
If you modify the parser and want to ensure that the API annotations are processed
correctly, see the README in (REPO)/utils/check-api-annotations for details on
how to set up a test using the check-api-annotations utility, and interpret the
output it generates.

320 321 322 323 324
-- -----------------------------------------------------------------------------

-}

%token
chak@cse.unsw.edu.au.'s avatar
chak@cse.unsw.edu.au. committed
325 326
 '_'            { L _ ITunderscore }            -- Haskell keywords
 'as'           { L _ ITas }
327 328 329
 'case'         { L _ ITcase }
 'class'        { L _ ITclass }
 'data'         { L _ ITdata }
chak@cse.unsw.edu.au.'s avatar
chak@cse.unsw.edu.au. committed
330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353
 'default'      { L _ ITdefault }
 'deriving'     { L _ ITderiving }
 'do'           { L _ ITdo }
 'else'         { L _ ITelse }
 'hiding'       { L _ IThiding }
 'if'           { L _ ITif }
 'import'       { L _ ITimport }
 'in'           { L _ ITin }
 'infix'        { L _ ITinfix }
 'infixl'       { L _ ITinfixl }
 'infixr'       { L _ ITinfixr }
 'instance'     { L _ ITinstance }
 'let'          { L _ ITlet }
 'module'       { L _ ITmodule }
 'newtype'      { L _ ITnewtype }
 'of'           { L _ ITof }
 'qualified'    { L _ ITqualified }
 'then'         { L _ ITthen }
 'type'         { L _ ITtype }
 'where'        { L _ ITwhere }

 'forall'       { L _ ITforall }                -- GHC extension keywords
 'foreign'      { L _ ITforeign }
 'export'       { L _ ITexport }
354
 'label'        { L _ ITlabel }
chak@cse.unsw.edu.au.'s avatar
chak@cse.unsw.edu.au. committed
355 356
 'dynamic'      { L _ ITdynamic }
 'safe'         { L _ ITsafe }
357
 'interruptible' { L _ ITinterruptible }
chak@cse.unsw.edu.au.'s avatar
chak@cse.unsw.edu.au. committed
358 359 360
 'unsafe'       { L _ ITunsafe }
 'mdo'          { L _ ITmdo }
 'family'       { L _ ITfamily }
361
 'role'         { L _ ITrole }
362 363
 'stdcall'      { L _ ITstdcallconv }
 'ccall'        { L _ ITccallconv }
364
 'capi'         { L _ ITcapiconv }
365
 'prim'         { L _ ITprimcallconv }
thoughtpolice's avatar
thoughtpolice committed
366
 'javascript'   { L _ ITjavascriptcallconv }
chak@cse.unsw.edu.au.'s avatar
chak@cse.unsw.edu.au. committed
367 368
 'proc'         { L _ ITproc }          -- for arrow notation extension
 'rec'          { L _ ITrec }           -- for arrow notation extension
369 370 371
 'group'    { L _ ITgroup }     -- for list transform extension
 'by'       { L _ ITby }        -- for list transform extension
 'using'    { L _ ITusing }     -- for list transform extension
cactus's avatar
cactus committed
372
 'pattern'      { L _ ITpattern } -- for pattern synonyms
Facundo Domínguez's avatar
Facundo Domínguez committed
373
 'static'       { L _ ITstatic }  -- for static pointers extension
374

Alan Zimmerman's avatar
Alan Zimmerman committed
375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397
 '{-# INLINE'             { L _ (ITinline_prag _ _ _) }
 '{-# SPECIALISE'         { L _ (ITspec_prag _) }
 '{-# SPECIALISE_INLINE'  { L _ (ITspec_inline_prag _ _) }
 '{-# SOURCE'             { L _ (ITsource_prag _) }
 '{-# RULES'              { L _ (ITrules_prag _) }
 '{-# CORE'               { L _ (ITcore_prag _) }      -- hdaume: annotated core
 '{-# SCC'                { L _ (ITscc_prag _)}
 '{-# GENERATED'          { L _ (ITgenerated_prag _) }
 '{-# DEPRECATED'         { L _ (ITdeprecated_prag _) }
 '{-# WARNING'            { L _ (ITwarning_prag _) }
 '{-# UNPACK'             { L _ (ITunpack_prag _) }
 '{-# NOUNPACK'           { L _ (ITnounpack_prag _) }
 '{-# ANN'                { L _ (ITann_prag _) }
 '{-# VECTORISE'          { L _ (ITvect_prag _) }
 '{-# VECTORISE_SCALAR'   { L _ (ITvect_scalar_prag _) }
 '{-# NOVECTORISE'        { L _ (ITnovect_prag _) }
 '{-# MINIMAL'            { L _ (ITminimal_prag _) }
 '{-# CTYPE'              { L _ (ITctype _) }
 '{-# OVERLAPPING'        { L _ (IToverlapping_prag _) }
 '{-# OVERLAPPABLE'       { L _ (IToverlappable_prag _) }
 '{-# OVERLAPS'           { L _ (IToverlaps_prag _) }
 '{-# INCOHERENT'         { L _ (ITincoherent_prag _) }
 '#-}'                    { L _ ITclose_prag }
chak@cse.unsw.edu.au.'s avatar
chak@cse.unsw.edu.au. committed
398 399 400 401 402 403

 '..'           { L _ ITdotdot }                        -- reserved symbols
 ':'            { L _ ITcolon }
 '::'           { L _ ITdcolon }
 '='            { L _ ITequal }
 '\\'           { L _ ITlam }
404
 'lcase'        { L _ ITlcase }
chak@cse.unsw.edu.au.'s avatar
chak@cse.unsw.edu.au. committed
405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437
 '|'            { L _ ITvbar }
 '<-'           { L _ ITlarrow }
 '->'           { L _ ITrarrow }
 '@'            { L _ ITat }
 '~'            { L _ ITtilde }
 '~#'           { L _ ITtildehsh }
 '=>'           { L _ ITdarrow }
 '-'            { L _ ITminus }
 '!'            { L _ ITbang }
 '*'            { L _ ITstar }
 '-<'           { L _ ITlarrowtail }            -- for arrow notation
 '>-'           { L _ ITrarrowtail }            -- for arrow notation
 '-<<'          { L _ ITLarrowtail }            -- for arrow notation
 '>>-'          { L _ ITRarrowtail }            -- for arrow notation
 '.'            { L _ ITdot }

 '{'            { L _ ITocurly }                        -- special symbols
 '}'            { L _ ITccurly }
 vocurly        { L _ ITvocurly } -- virtual open curly (from layout)
 vccurly        { L _ ITvccurly } -- virtual close curly (from layout)
 '['            { L _ ITobrack }
 ']'            { L _ ITcbrack }
 '[:'           { L _ ITopabrack }
 ':]'           { L _ ITcpabrack }
 '('            { L _ IToparen }
 ')'            { L _ ITcparen }
 '(#'           { L _ IToubxparen }
 '#)'           { L _ ITcubxparen }
 '(|'           { L _ IToparenbar }
 '|)'           { L _ ITcparenbar }
 ';'            { L _ ITsemi }
 ','            { L _ ITcomma }
 '`'            { L _ ITbackquote }
dreixel's avatar
dreixel committed
438
 SIMPLEQUOTE    { L _ ITsimpleQuote      }     -- 'x
chak@cse.unsw.edu.au.'s avatar
chak@cse.unsw.edu.au. committed
439 440 441 442 443 444 445 446 447

 VARID          { L _ (ITvarid    _) }          -- identifiers
 CONID          { L _ (ITconid    _) }
 VARSYM         { L _ (ITvarsym   _) }
 CONSYM         { L _ (ITconsym   _) }
 QVARID         { L _ (ITqvarid   _) }
 QCONID         { L _ (ITqconid   _) }
 QVARSYM        { L _ (ITqvarsym  _) }
 QCONSYM        { L _ (ITqconsym  _) }
448

chak@cse.unsw.edu.au.'s avatar
chak@cse.unsw.edu.au. committed
449 450
 IPDUPVARID     { L _ (ITdupipvarid   _) }              -- GHC extension

451 452 453
 CHAR           { L _ (ITchar   _ _) }
 STRING         { L _ (ITstring _ _) }
 INTEGER        { L _ (ITinteger _ _) }
chak@cse.unsw.edu.au.'s avatar
chak@cse.unsw.edu.au. committed
454
 RATIONAL       { L _ (ITrational _) }
455

456 457 458 459
 PRIMCHAR       { L _ (ITprimchar   _ _) }
 PRIMSTRING     { L _ (ITprimstring _ _) }
 PRIMINTEGER    { L _ (ITprimint    _ _) }
 PRIMWORD       { L _ (ITprimword   _ _) }
chak@cse.unsw.edu.au.'s avatar
chak@cse.unsw.edu.au. committed
460 461 462 463 464 465 466
 PRIMFLOAT      { L _ (ITprimfloat  _) }
 PRIMDOUBLE     { L _ (ITprimdouble _) }

 DOCNEXT        { L _ (ITdocCommentNext _) }
 DOCPREV        { L _ (ITdocCommentPrev _) }
 DOCNAMED       { L _ (ITdocCommentNamed _) }
 DOCSECTION     { L _ (ITdocSection _ _) }
467

468 469 470 471 472
-- Template Haskell
'[|'            { L _ ITopenExpQuote  }
'[p|'           { L _ ITopenPatQuote  }
'[t|'           { L _ ITopenTypQuote  }
'[d|'           { L _ ITopenDecQuote  }
473
'|]'            { L _ ITcloseQuote    }
474 475
'[||'           { L _ ITopenTExpQuote   }
'||]'           { L _ ITcloseTExpQuote  }
476
TH_ID_SPLICE    { L _ (ITidEscape _)  }     -- $x
chak@cse.unsw.edu.au.'s avatar
chak@cse.unsw.edu.au. committed
477
'$('            { L _ ITparenEscape   }     -- $( exp )
478 479
TH_ID_TY_SPLICE { L _ (ITidTyEscape _)  }   -- $$x
'$$('           { L _ ITparenTyEscape   }   -- $$( exp )
chak@cse.unsw.edu.au.'s avatar
chak@cse.unsw.edu.au. committed
480 481
TH_TY_QUOTE     { L _ ITtyQuote       }      -- ''T
TH_QUASIQUOTE   { L _ (ITquasiQuote _) }
482
TH_QQUASIQUOTE  { L _ (ITqQuasiQuote _) }
483 484

%monad { P } { >>= } { return }
Alan Zimmerman's avatar
Alan Zimmerman committed
485
%lexer { (lexer True) } { L _ ITeof }
486 487 488
%tokentype { (Located Token) }

-- Exported parsers
489
%name parseModule module
490 491 492 493
%name parseImport importdecl
%name parseStatement stmt
%name parseDeclaration topdecl
%name parseExpression exp
494
%name parsePattern pat
495
%name parseTypeSignature sigdecl
496 497
%name parseStmt   maybe_stmt
%name parseIdentifier  identifier
498
%name parseType ctype
499
%partial parseHeader header
500 501
%%

502 503 504
-----------------------------------------------------------------------------
-- Identifiers; one of the entry points
identifier :: { Located RdrName }
chak@cse.unsw.edu.au.'s avatar
chak@cse.unsw.edu.au. committed
505 506 507 508
        : qvar                          { $1 }
        | qcon                          { $1 }
        | qvarop                        { $1 }
        | qconop                        { $1 }
Alan Zimmerman's avatar
Alan Zimmerman committed
509 510
    | '(' '->' ')'      {% ams (sLL $1 $> $ getRdrName funTyCon)
                               [mj AnnOpenP $1,mj AnnRarrow $2,mj AnnCloseP $3] }
511

512 513 514 515 516 517 518 519 520 521
-----------------------------------------------------------------------------
-- Module Header

-- The place for module deprecation is really too restrictive, but if it
-- was allowed at its natural place just before 'module', we get an ugly
-- s/r conflict with the second alternative. Another solution would be the
-- introduction of a new pragma DEPRECATED_MODULE, but this is not very nice,
-- either, and DEPRECATED is only expected to be used by people who really
-- know what they are doing. :-)

Alan Zimmerman's avatar
Alan Zimmerman committed
522 523 524 525 526 527 528
module :: { Located (HsModule RdrName) }
       : maybedocheader 'module' modid maybemodwarning maybeexports 'where' body
             {% fileSrcSpan >>= \ loc ->
                ams (L loc (HsModule (Just $3) $5 (fst $ snd $7)
                              (snd $ snd $7) $4 $1)
                    )
                    ([mj AnnModule $2, mj AnnWhere $6] ++ fst $7) }
529
        | body2
chak@cse.unsw.edu.au.'s avatar
chak@cse.unsw.edu.au. committed
530
                {% fileSrcSpan >>= \ loc ->
Alan Zimmerman's avatar
Alan Zimmerman committed
531 532 533
                   ams (L loc (HsModule Nothing Nothing
                               (fst $ snd $1) (snd $ snd $1) Nothing Nothing))
                       (fst $1) }
534

535
maybedocheader :: { Maybe LHsDocString }
536
        : moduleheader            { $1 }
537
        | {- empty -}             { Nothing }
538 539

missing_module_keyword :: { () }
chak@cse.unsw.edu.au.'s avatar
chak@cse.unsw.edu.au. committed
540
        : {- empty -}                           {% pushCurrentContext }
541

542
maybemodwarning :: { Maybe (Located WarningTxt) }
Alan Zimmerman's avatar
Alan Zimmerman committed
543
    : '{-# DEPRECATED' strings '#-}'
Alan Zimmerman's avatar
Alan Zimmerman committed
544 545
                      {% ajs (Just (sLL $1 $> $ DeprecatedTxt (sL1 $1 (getDEPRECATED_PRAGs $1)) (snd $ unLoc $2)))
                             (mo $1:mc $3: (fst $ unLoc $2)) }
Alan Zimmerman's avatar
Alan Zimmerman committed
546
    | '{-# WARNING' strings '#-}'
Alan Zimmerman's avatar
Alan Zimmerman committed
547
                         {% ajs (Just (sLL $1 $> $ WarningTxt (sL1 $1 (getWARNING_PRAGs $1)) (snd $ unLoc $2)))
Alan Zimmerman's avatar
Alan Zimmerman committed
548
                                (mo $1:mc $3 : (fst $ unLoc $2)) }
Ian Lynagh's avatar
Ian Lynagh committed
549
    |  {- empty -}                  { Nothing }
550

Alan Zimmerman's avatar
Alan Zimmerman committed
551 552
body    :: { ([AddAnn]
             ,([LImportDecl RdrName], [LHsDecl RdrName])) }
Alan Zimmerman's avatar
Alan Zimmerman committed
553
        :  '{'            top '}'      { (moc $1:mcc $3:(fst $2)
Alan Zimmerman's avatar
Alan Zimmerman committed
554 555 556 557 558
                                         , snd $2) }
        |      vocurly    top close    { (fst $2, snd $2) }

body2   :: { ([AddAnn]
             ,([LImportDecl RdrName], [LHsDecl RdrName])) }
Alan Zimmerman's avatar
Alan Zimmerman committed
559
        :  '{' top '}'                          { (moc $1:mcc $3
Alan Zimmerman's avatar
Alan Zimmerman committed
560 561 562 563 564
                                                   :(fst $2), snd $2) }
        |  missing_module_keyword top close     { ([],snd $2) }

top     :: { ([AddAnn]
             ,([LImportDecl RdrName], [LHsDecl RdrName])) }
Alan Zimmerman's avatar
Alan Zimmerman committed
565 566 567 568 569 570 571 572 573 574
        : importdecls                   { (fst $1
                                          ,(reverse $ snd $1,[]))}
        | importdecls ';' cvtopdecls    {% if null (snd $1)
                                             then return ((mj AnnSemi $2:(fst $1))
                                                         ,(reverse $ snd $1,$3))
                                             else do
                                              { addAnnotation (gl $ head $ snd $1)
                                                              AnnSemi (gl $2)
                                              ; return (fst $1
                                                       ,(reverse $ snd $1,$3)) }}
Alan Zimmerman's avatar
Alan Zimmerman committed
575
        | cvtopdecls                    { ([],([],$1)) }
576 577

cvtopdecls :: { [LHsDecl RdrName] }
chak@cse.unsw.edu.au.'s avatar
chak@cse.unsw.edu.au. committed
578
        : topdecls                              { cvTopDecls $1 }
579

580 581 582
-----------------------------------------------------------------------------
-- Module declaration & imports only

chak@cse.unsw.edu.au.'s avatar
chak@cse.unsw.edu.au. committed
583 584 585
header  :: { Located (HsModule RdrName) }
        : maybedocheader 'module' modid maybemodwarning maybeexports 'where' header_body
                {% fileSrcSpan >>= \ loc ->
Alan Zimmerman's avatar
Alan Zimmerman committed
586 587
                   ams (L loc (HsModule (Just $3) $5 $7 [] $4 $1
                          )) [mj AnnModule $2,mj AnnWhere $6] }
588
        | header_body2
chak@cse.unsw.edu.au.'s avatar
chak@cse.unsw.edu.au. committed
589
                {% fileSrcSpan >>= \ loc ->
590
                   return (L loc (HsModule Nothing Nothing $1 [] Nothing
591
                          Nothing)) }
592 593

header_body :: { [LImportDecl RdrName] }
Alan Zimmerman's avatar
Alan Zimmerman committed
594 595
        :  '{'            importdecls           { snd $2 }
        |      vocurly    importdecls           { snd $2 }
596 597

header_body2 :: { [LImportDecl RdrName] }
Alan Zimmerman's avatar
Alan Zimmerman committed
598 599
        :  '{' importdecls                      { snd $2 }
        |  missing_module_keyword importdecls   { snd $2 }
600

601 602 603
-----------------------------------------------------------------------------
-- The Export List

Alan Zimmerman's avatar
Alan Zimmerman committed
604
maybeexports :: { (Maybe (Located [LIE RdrName])) }
Alan Zimmerman's avatar
Alan Zimmerman committed
605
        :  '(' exportlist ')'       {% ams (sLL $1 $> ()) [mop $1,mcp $3] >>
Alan Zimmerman's avatar
Alan Zimmerman committed
606 607
                                       return (Just (sLL $1 $> (fromOL $2))) }
        |  {- empty -}              { Nothing }
608

609
exportlist :: { OrdList (LIE RdrName) }
Alan Zimmerman's avatar
Alan Zimmerman committed
610 611 612
        : expdoclist ',' expdoclist   {% addAnnotation (oll $1) AnnComma (gl $2)
                                         >> return ($1 `appOL` $3) }
        | exportlist1                 { $1 }
613

614
exportlist1 :: { OrdList (LIE RdrName) }
Alan Zimmerman's avatar
Alan Zimmerman committed
615 616 617 618 619 620
        : expdoclist export expdoclist ',' exportlist1
                          {% (addAnnotation (oll ($1 `appOL` $2 `appOL` $3))
                                            AnnComma (gl $4) ) >>
                              return ($1 `appOL` $2 `appOL` $3 `appOL` $5) }
        | expdoclist export expdoclist             { $1 `appOL` $2 `appOL` $3 }
        | expdoclist                               { $1 }
621

622 623 624
expdoclist :: { OrdList (LIE RdrName) }
        : exp_doc expdoclist                           { $1 `appOL` $2 }
        | {- empty -}                                  { nilOL }
625

626
exp_doc :: { OrdList (LIE RdrName) }
rodlogic's avatar
rodlogic committed
627 628 629
        : docsection    { unitOL (sL1 $1 (case (unLoc $1) of (n, doc) -> IEGroup n doc)) }
        | docnamed      { unitOL (sL1 $1 (IEDocNamed ((fst . unLoc) $1))) }
        | docnext       { unitOL (sL1 $1 (IEDoc (unLoc $1))) }
630 631


632 633
   -- No longer allow things like [] and (,,,) to be exported
   -- They are built in syntax, always available
634
export  :: { OrdList (LIE RdrName) }
Alan Zimmerman's avatar
Alan Zimmerman committed
635
        : qcname_ext export_subspec  {% amsu (sLL $1 $> (mkModuleImpExp $1
636
                                                    (snd $ unLoc $2)))
Alan Zimmerman's avatar
Alan Zimmerman committed
637 638 639 640 641 642 643 644
                                             (fst $ unLoc $2) }
        |  'module' modid            {% amsu (sLL $1 $> (IEModuleContents $2))
                                             [mj AnnModule $1] }
        |  'pattern' qcon            {% amsu (sLL $1 $> (IEVar $2))
                                             [mj AnnPattern $1] }

export_subspec :: { Located ([AddAnn],ImpExpSubSpec) }
        : {- empty -}             { sL0 ([],ImpExpAbs) }
Alan Zimmerman's avatar
Alan Zimmerman committed
645
        | '(' '..' ')'            { sLL $1 $> ([mop $1,mcp $3,mj AnnDotdot $2]
Alan Zimmerman's avatar
Alan Zimmerman committed
646
                                       , ImpExpAll) }
Alan Zimmerman's avatar
Alan Zimmerman committed
647 648
        | '(' ')'                 { sLL $1 $> ([mop $1,mcp $2],ImpExpList []) }
        | '(' qcnames ')'         { sLL $1 $> ([mop $1,mcp $3],ImpExpList (reverse $2)) }
649

650
qcnames :: { [Located RdrName] }     -- A reversed list
Alan Zimmerman's avatar
Alan Zimmerman committed
651 652
        :  qcnames ',' qcname_ext       {% (aa (head $1) (AnnComma, $2)) >>
                                           return ($3  : $1) }
653
        |  qcname_ext                   { [$1]  }
654

chak@cse.unsw.edu.au.'s avatar
chak@cse.unsw.edu.au. committed
655 656
qcname_ext :: { Located RdrName }       -- Variable or data constructor
                                        -- or tagged type constructor
Alan Zimmerman's avatar
Alan Zimmerman committed
657
        :  qcname                   { $1 }
658
        |  'type' oqtycon           {% amms (mkTypeImpExp (sLL $1 $> (unLoc $2)))
659
                                            [mj AnnType $1,mj AnnVal $2] }
660

661
qcname  :: { Located RdrName }  -- Variable or type constructor
662 663
        :  qvar                 { $1 }
        |  oqtycon_no_varcon    { $1 } -- see Note [Type constructors in export list]
664 665 666 667 668 669 670

-----------------------------------------------------------------------------
-- Import Declarations

-- import decls can be *empty*, or even just a string of semicolons
-- whereas topdecls must contain at least one topdecl.

Alan Zimmerman's avatar
Alan Zimmerman committed
671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686
importdecls :: { ([AddAnn],[LImportDecl RdrName]) }
        : importdecls ';' importdecl
                                {% if null (snd $1)
                                     then return (mj AnnSemi $2:fst $1,$3 : snd $1)
                                     else do
                                      { addAnnotation (gl $ head $ snd $1)
                                                      AnnSemi (gl $2)
                                      ; return (fst $1,$3 : snd $1) } }
        | importdecls ';'       {% if null (snd $1)
                                     then return ((mj AnnSemi $2:fst $1),snd $1)
                                     else do
                                       { addAnnotation (gl $ head $ snd $1)
                                                       AnnSemi (gl $2)
                                       ; return $1} }
        | importdecl             { ([],[$1]) }
        | {- empty -}            { ([],[]) }
687 688

importdecl :: { LImportDecl RdrName }
689
        : 'import' maybe_src maybe_safe optqualified maybe_pkg modid maybeas maybeimpspec
Alan Zimmerman's avatar
Alan Zimmerman committed
690
                {% ams (L (comb4 $1 $6 (snd $7) $8) $
Alan Zimmerman's avatar
Alan Zimmerman committed
691 692
                  ImportDecl { ideclSourceSrc = snd $ fst $2
                             , ideclName = $6, ideclPkgQual = snd $5
Alan Zimmerman's avatar
Alan Zimmerman committed
693 694 695 696
                             , ideclSource = snd $2, ideclSafe = snd $3
                             , ideclQualified = snd $4, ideclImplicit = False
                             , ideclAs = unLoc (snd $7)
                             , ideclHiding = unLoc $8 })
Alan Zimmerman's avatar
Alan Zimmerman committed
697
                   ((mj AnnImport $1 : (fst $ fst $2) ++ fst $3 ++ fst $4
698
                                    ++ fst $5 ++ fst $7)) }
Alan Zimmerman's avatar
Alan Zimmerman committed
699

Alan Zimmerman's avatar
Alan Zimmerman committed
700 701 702 703
maybe_src :: { (([AddAnn],Maybe SourceText),IsBootInterface) }
        : '{-# SOURCE' '#-}'        { (([mo $1,mc $2],Just (getSOURCE_PRAGs $1))
                                      ,True) }
        | {- empty -}               { (([],Nothing),False) }
Alan Zimmerman's avatar
Alan Zimmerman committed
704 705 706 707 708

maybe_safe :: { ([AddAnn],Bool) }
        : 'safe'                                { ([mj AnnSafe $1],True) }
        | {- empty -}                           { ([],False) }

709
maybe_pkg :: { ([AddAnn],Maybe StringLiteral) }
710 711
        : STRING  {% let pkgFS = getSTRING $1 in
                     if looksLikePackageName (unpackFS pkgFS)
712
                        then return ([mj AnnPackageName $1], Just (StringLiteral (getSTRINGs $1) pkgFS))
713 714 715 716
                        else parseErrorSDoc (getLoc $1) $ vcat [
                             text "parse error" <> colon <+> quotes (ppr pkgFS),
                             text "Version number or non-alphanumeric" <+>
                             text "character in package name"] }
Alan Zimmerman's avatar
Alan Zimmerman committed
717 718 719 720 721 722 723
        | {- empty -}                           { ([],Nothing) }

optqualified :: { ([AddAnn],Bool) }
        : 'qualified'                           { ([mj AnnQualified $1],True)  }
        | {- empty -}                           { ([],False) }

maybeas :: { ([AddAnn],Located (Maybe ModuleName)) }
724 725 726
        : 'as' modid                           { ([mj AnnAs $1,mj AnnVal $2]
                                                 ,sLL $1 $> (Just (unLoc $2))) }
        | {- empty -}                          { ([],noLoc Nothing) }
727

728
maybeimpspec :: { Located (Maybe (Bool, Located [LIE RdrName])) }
Alan Zimmerman's avatar
Alan Zimmerman committed
729 730
        : impspec                  { L (gl $1) (Just (unLoc $1)) }
        | {- empty -}              { noLoc Nothing }
731

732
impspec :: { Located (Bool, Located [LIE RdrName]) }
Alan Zimmerman's avatar
Alan Zimmerman committed
733 734 735 736 737 738
        :  '(' exportlist ')'               {% ams (sLL $1 $> (False,
                                                      sLL $1 $> $ fromOL $2))
                                                   [mop $1,mcp $3] }
        |  'hiding' '(' exportlist ')'      {% ams (sLL $1 $> (True,
                                                      sLL $1 $> $ fromOL $3))
                                               [mj AnnHiding $1,mop $2,mcp $4] }
739 740 741 742

-----------------------------------------------------------------------------
-- Fixity Declarations

743 744 745 746
prec    :: { Located Int }
        : {- empty -}           { noLoc 9 }
        | INTEGER
                 {% checkPrecP (sL1 $1 (fromInteger (getINTEGER $1))) }
747

chak@cse.unsw.edu.au.'s avatar
chak@cse.unsw.edu.au. committed
748
infix   :: { Located FixityDirection }
rodlogic's avatar
rodlogic committed
749 750 751
        : 'infix'                               { sL1 $1 InfixN  }
        | 'infixl'                              { sL1 $1 InfixL  }
        | 'infixr'                              { sL1 $1 InfixR }
752

Alan Zimmerman's avatar
Alan Zimmerman committed
753
ops     :: { Located (OrdList (Located RdrName)) }
Alan Zimmerman's avatar
Alan Zimmerman committed
754 755 756
        : ops ',' op       {% addAnnotation (oll $ unLoc $1) AnnComma (gl $2) >>
                              return (sLL $1 $> ((unLoc $1) `appOL` unitOL $3))}
        | op               { sL1 $1 (unitOL $1) }
757 758 759 760

-----------------------------------------------------------------------------
-- Top-Level Declarations

761
topdecls :: { OrdList (LHsDecl RdrName) }
762
        : topdecls ';' topdecl        {% addAnnotation (oll $1) AnnSemi (gl $2)
763
                                         >> return ($1 `appOL` unitOL $3) }
Alan Zimmerman's avatar
Alan Zimmerman committed
764 765
        | topdecls ';'                {% addAnnotation (oll $1) AnnSemi (gl $2)
                                         >> return $1 }
766 767 768 769 770 771 772 773 774
        | topdecl                     { unitOL $1 }

topdecl :: { LHsDecl RdrName }
        : cl_decl                               { sL1 $1 (TyClD (unLoc $1)) }
        | ty_decl                               { sL1 $1 (TyClD (unLoc $1)) }
        | inst_decl                             { sL1 $1 (InstD (unLoc $1)) }
        | stand_alone_deriving                  { sLL $1 $> (DerivD (unLoc $1)) }
        | role_annot                            { sL1 $1 (RoleAnnotD (unLoc $1)) }
        | 'default' '(' comma_types0 ')'    {% ams (sLL $1 $> (DefD (DefaultDecl $3)))
thomasw's avatar
thomasw committed
775
                                                         [mj AnnDefault $1
thomasw's avatar
thomasw committed
776
                                                         ,mop $2,mcp $4] }
777
        | 'foreign' fdecl          {% ams (sLL $1 $> (snd $ unLoc $2))
Alan Zimmerman's avatar
Alan Zimmerman committed
778
                                           (mj AnnForeign $1:(fst $ unLoc $2)) }
779
        | '{-# DEPRECATED' deprecations '#-}'   {% ams (sLL $1 $> $ WarningD (Warnings (getDEPRECATED_PRAGs $1) (fromOL $2)))
Alan Zimmerman's avatar
Alan Zimmerman committed
780
                                                       [mo $1,mc $3] }
781
        | '{-# WARNING' warnings '#-}'          {% ams (sLL $1 $> $ WarningD (Warnings (getWARNING_PRAGs $1) (fromOL $2)))
Alan Zimmerman's avatar
Alan Zimmerman committed
782
                                                       [mo $1,mc $3] }
783
        | '{-# RULES' rules '#-}'               {% ams (sLL $1 $> $ RuleD (HsRules (getRULES_PRAGs $1) (fromOL $2)))
Alan Zimmerman's avatar
Alan Zimmerman committed
784
                                                       [mo $1,mc $3] }
785
        | '{-# VECTORISE' qvar '=' exp '#-}' {% ams (sLL $1 $> $ VectD (HsVect (getVECT_PRAGs $1) $2 $4))
Alan Zimmerman's avatar
Alan Zimmerman committed
786 787
                                                    [mo $1,mj AnnEqual $3
                                                    ,mc $5] }
788
        | '{-# NOVECTORISE' qvar '#-}'       {% ams (sLL $1 $> $ VectD (HsNoVect (getNOVECT_PRAGs $1) $2))
Alan Zimmerman's avatar
Alan Zimmerman committed
789
                                                     [mo $1,mc $3] }
790
        | '{-# VECTORISE' 'type' gtycon '#-}'
791
                                {% ams (sLL $1 $> $
Alan Zimmerman's avatar
Alan Zimmerman committed
792
                                    VectD (HsVectTypeIn (getVECT_PRAGs $1) False $3 Nothing))
Alan Zimmerman's avatar
Alan Zimmerman committed
793 794
                                    [mo $1,mj AnnType $2,mc $4] }

795
        | '{-# VECTORISE_SCALAR' 'type' gtycon '#-}'
796
                                {% ams (sLL $1 $> $
Alan Zimmerman's avatar
Alan Zimmerman committed
797
                                    VectD (HsVectTypeIn (getVECT_SCALAR_PRAGs $1) True $3 Nothing))
Alan Zimmerman's avatar
Alan Zimmerman committed
798 799
                                    [mo $1,mj AnnType $2,mc $4] }

800
        | '{-# VECTORISE' 'type' gtycon '=' gtycon '#-}'
801
                                {% ams (sLL $1 $> $
Alan Zimmerman's avatar
Alan Zimmerman committed
802
                                    VectD (HsVectTypeIn (getVECT_PRAGs $1) False $3 (Just $5)))
Alan Zimmerman's avatar
Alan Zimmerman committed
803
                                    [mo $1,mj AnnType $2,mj AnnEqual $4,mc $6] }
804
        | '{-# VECTORISE_SCALAR' 'type' gtycon '=' gtycon '#-}'
805
                                {% ams (sLL $1 $> $
Alan Zimmerman's avatar
Alan Zimmerman committed
806
                                    VectD (HsVectTypeIn (getVECT_SCALAR_PRAGs $1) True $3 (Just $5)))
Alan Zimmerman's avatar
Alan Zimmerman committed
807 808 809
                                    [mo $1,mj AnnType $2,mj AnnEqual $4,mc $6] }

        | '{-# VECTORISE' 'class' gtycon '#-}'
810
                                         {% ams (sLL $1 $>  $ VectD (HsVectClassIn (getVECT_PRAGs $1) $3))
Alan Zimmerman's avatar
Alan Zimmerman committed
811
                                                 [mo $1,mj AnnClass $2,mc $4] }
812 813
        | annotation { $1 }
        | decl_no_th                            { $1 }
814 815 816

        -- Template Haskell Extension
        -- The $(..) form is one possible form of infixexp
817
        -- but we treat an arbitrary expression just as if
818
        -- it had a $(..) wrapped around it
819
        | infixexp                              { sLL $1 $> $ mkSpliceDecl $1 }
820

821 822 823
-- Type classes
--
cl_decl :: { LTyClDecl RdrName }
824
        : 'class' tycl_hdr fds where_cls
Alan Zimmerman's avatar
Alan Zimmerman committed
825
                {% amms (mkClassDecl (comb4 $1 $2 $3 $4) $2 $3 (snd $ unLoc $4))
Alan Zimmerman's avatar
Alan Zimmerman committed
826
                        (mj AnnClass $1:(fst $ unLoc $3)++(fst $ unLoc $4)) }
827

828
-- Type declarations (toplevel)
829 830
--
ty_decl :: { LTyClDecl RdrName }
831
           -- ordinary type synonyms
832
        : 'type' type '=' ctypedoc
chak@cse.unsw.edu.au.'s avatar
chak@cse.unsw.edu.au. committed
833 834 835 836 837 838
                -- Note ctype, not sigtype, on the right of '='
                -- We allow an explicit for-all but we don't insert one
                -- in   type Foo a = (b,b)
                -- Instead we just say b is out of scope
                --
                -- Note the use of type for the head; this allows
839
                -- infix type constructors to be declared
840
                {% amms (mkTySynonym (comb2 $1 $4) $2 $4)
Alan Zimmerman's avatar
Alan Zimmerman committed
841
                        [mj AnnType $1,mj AnnEqual $3] }
842 843

           -- type family declarations
Jan Stolarek's avatar
Jan Stolarek committed
844 845
        | 'type' 'family' type opt_tyfam_kind_sig opt_injective_info
                          where_type_family
chak@cse.unsw.edu.au.'s avatar
chak@cse.unsw.edu.au. committed
846 847
                -- Note the use of type for the head; this allows
                -- infix type constructors to be declared
Jan Stolarek's avatar
Jan Stolarek committed
848 849 850 851
                {% amms (mkFamDecl (comb4 $1 $3 $4 $5) (snd $ unLoc $6) $3
                                   (snd $ unLoc $4) (snd $ unLoc $5))
                        (mj AnnType $1:mj AnnFamily $2:(fst $ unLoc $4)
                           ++ (fst $ unLoc $5) ++ (fst $ unLoc $6)) }
852

853
          -- ordinary data type or newtype declaration
854
        | data_or_newtype capi_ctype tycl_hdr constrs deriving
Alan Zimmerman's avatar
Alan Zimmerman committed
855 856 857
                {% amms (mkTyData (comb4 $1 $3 $4 $5) (snd $ unLoc $1) $2 $3
                           Nothing (reverse (snd $ unLoc $4))
                                   (unLoc $5))
858
                                   -- We need the location on tycl_hdr in case
chak@cse.unsw.edu.au.'s avatar
chak@cse.unsw.edu.au. committed
859
                                   -- constrs and deriving are both empty
Alan Zimmerman's avatar
Alan Zimmerman committed
860
                        ((fst $ unLoc $1):(fst $ unLoc $4)) }
861

862
          -- ordinary GADT declaration
863
        | data_or_newtype capi_ctype tycl_hdr opt_kind_sig
chak@cse.unsw.edu.au.'s avatar
chak@cse.unsw.edu.au. committed
864 865
                 gadt_constrlist
                 deriving
Alan Zimmerman's avatar
Alan Zimmerman committed
866
            {% amms (mkTyData (comb4 $1 $3 $5 $6) (snd $ unLoc $1) $2 $3
867
                            (snd $ unLoc $4) (snd $ unLoc $5) (unLoc $6) )
868
                                   -- We need the location on tycl_hdr in case
chak@cse.unsw.edu.au.'s avatar
chak@cse.unsw.edu.au. committed
869
                                   -- constrs and deriving are both empty
870
                    ((fst $ unLoc $1):(fst $ unLoc $4)++(fst $ unLoc $5)) }
871

872
          -- data/newtype family
Jan Stolarek's avatar
Jan Stolarek committed
873 874 875
        | 'data' 'family' type opt_datafam_kind_sig
                {% amms (mkFamDecl (comb3 $1 $2 $4) DataFamily $3
                                   (snd $ unLoc $4) Nothing)
876
                        (mj AnnData $1:mj AnnFamily $2:(fst $ unLoc $4)) }
877

878
inst_decl :: { LInstDecl RdrName }
879
        : 'instance' overlap_pragma inst_type where_inst
Alan Zimmerman's avatar
Alan Zimmerman committed
880 881 882 883 884 885 886
       {% do { (binds, sigs, _, ats, adts, _) <- cvBindsAndSigs (snd $ unLoc $4)
             ; let cid = ClsInstDecl { cid_poly_ty = $3, cid_binds = binds
                                     , cid_sigs = sigs, cid_tyfam_insts = ats
                                     , cid_overlap_mode = $2
                                     , cid_datafam_insts = adts }
             ; ams (L (comb3 $1 $3 $4) (ClsInstD { cid_inst = cid }))
                   (mj AnnInstance $1 : (fst $ unLoc $4)) } }
887 888

           -- type instance declarations
889
        | 'type' 'instance' ty_fam_inst_eqn
890 891 892
                {% ams $3 (fst $ unLoc $3)
                >> amms (mkTyFamInst (comb2 $1 $3) (snd $ unLoc $3))
                    (mj AnnType $1:mj AnnInstance $2:(fst $ unLoc $3)) }
893

894
          -- data/newtype instance declaration
895
        | data_or_newtype 'instance' capi_ctype tycl_hdr constrs deriving
Alan Zimmerman's avatar
Alan Zimmerman committed
896 897 898 899
            {% amms (mkDataFamInst (comb4 $1 $4 $5 $6) (snd $ unLoc $1) $3 $4
                                      Nothing (reverse (snd  $ unLoc $5))
                                              (unLoc $6))
                    ((fst $ unLoc $1):mj AnnInstance $2:(fst $ unLoc $5)) }
900

901
          -- GADT instance declaration
902
        | data_or_newtype 'instance' capi_ctype tycl_hdr opt_kind_sig
chak@cse.unsw.edu.au.'s avatar
chak@cse.unsw.edu.au. committed
903 904
                 gadt_constrlist
                 deriving
Alan Zimmerman's avatar
Alan Zimmerman committed
905
            {% amms (mkDataFamInst (comb4 $1 $4 $6 $7) (snd $ unLoc $1) $3 $4
906
                                   (snd $ unLoc $5) (snd $ unLoc $6) (unLoc $7))
Alan Zimmerman's avatar
Alan Zimmerman committed
907
                    ((fst $ unLoc $1):mj AnnInstance $2
908
                       :(fst $ unLoc $5)++(fst $ unLoc $6)) }
909

910
overlap_pragma :: { Maybe (Located OverlapMode) }
Alan Zimmerman's avatar
Alan Zimmerman committed
911
  : '{-# OVERLAPPABLE'    '#-}' {% ajs (Just (sLL $1 $> (Overlappable (getOVERLAPPABLE_PRAGs $1))))
Alan Zimmerman's avatar
Alan Zimmerman committed
912
                                       [mo $1,mc $2] }
Alan Zimmerman's avatar
Alan Zimmerman committed
913
  | '{-# OVERLAPPING'     '#-}' {% ajs (Just (sLL $1 $> (Overlapping (getOVERLAPPING_PRAGs $1))))
Alan Zimmerman's avatar
Alan Zimmerman committed
914
                                       [mo $1,mc $2] }
Alan Zimmerman's avatar
Alan Zimmerman committed
915
  | '{-# OVERLAPS'        '#-}' {% ajs (Just (sLL $1 $> (Overlaps (getOVERLAPS_PRAGs $1))))
Alan Zimmerman's avatar
Alan Zimmerman committed
916
                                       [mo $1,mc $2] }
Alan Zimmerman's avatar
Alan Zimmerman committed
917
  | '{-# INCOHERENT'      '#-}' {% ajs (Just (sLL $1 $> (Incoherent (getINCOHERENT_PRAGs $1))))
Alan Zimmerman's avatar
Alan Zimmerman committed
918
                                       [mo $1,mc $2] }
919
  | {- empty -}                 { Nothing }
920 921


Jan Stolarek's avatar
Jan Stolarek committed
922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937
-- Injective type families

opt_injective_info :: { Located ([AddAnn], Maybe (LInjectivityAnn RdrName)) }
        : {- empty -}               { noLoc ([], Nothing) }
        | '|' injectivity_cond      { sLL $1 $> ( mj AnnVbar $1 : fst (unLoc $2)
                                                , Just (snd (unLoc $2))) }

injectivity_cond :: { Located ([AddAnn], LInjectivityAnn RdrName) }
        : tyvarid '->' inj_varids
           { sLL $1 $> ( [mj AnnRarrow $2]
                       , (sLL $1 $> (InjectivityAnn $1 (reverse (unLoc $3))))) }

inj_varids :: { Located [Located RdrName] }
        : inj_varids tyvarid  { sLL $1 $> ($2 : unLoc $1) }
        | tyvarid             { sLL $1 $> [$1]            }

938 939 </