Parser.y 163 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
import Control.Applicative ((<$))
37

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

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

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

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

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

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

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

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

82 83
-- compiler/utils
import Util             ( looksLikePackageName )
84
import Prelude
85

86
import qualified GHC.LanguageExtensions as LangExt
87 88
}

89
{- Last updated: 18 Nov 2015
90

91
Conflicts: 36 shift/reduce
92

93 94
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:
95

96
    happy -agc --strict compiler/parser/Parser.y -idetailed-info
97

98 99 100 101 102 103
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).
104

105 106 107 108 109
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.
110

111
-------------------------------------------------------------------------------
Gabor Greif's avatar
Gabor Greif committed
112

113
state 0 contains 1 shift/reduce conflicts.
Gabor Greif's avatar
Gabor Greif committed
114

115
    Conflicts: DOCNEXT (empty missing_module_keyword reduces)
116

117 118 119
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.
120

121
-------------------------------------------------------------------------------
122

123 124 125 126 127 128 129 130 131
state 46 contains 2 shift/reduce conflicts.

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

    Conflicts: '~' '!'

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

132
state 50 contains 1 shift/reduce conflict.
133

134 135 136
        context -> btype .                                  (rule 295)
    *** type -> btype .                                     (rule 297)
        type -> btype . '->' ctype                          (rule 298)
137

138
    Conflicts: '->'
139

140 141 142 143 144 145
-------------------------------------------------------------------------------

state 51 contains 9 shift/reduce conflicts.

    *** btype -> tyapps .                                   (rule 303)
        tyapps -> tyapps . tyapp                            (rule 307)
146

147
    Conflicts: ':' '-' '!' '.' '`' VARSYM CONSYM QVARSYM QCONSYM
148

149
-------------------------------------------------------------------------------
150

151
state 132 contains 14 shift/reduce conflicts.
152

153 154 155 156 157 158 159
        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)
160

161
    Conflicts: ':' '::' '-' '!' '-<' '>-' '-<<' '>>-'
162
               '.' '`' VARSYM CONSYM QVARSYM QCONSYM
163

164 165 166 167
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)
168

169 170 171 172
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)'
173

174
-------------------------------------------------------------------------------
175

176
state 292 contains 1 shift/reduce conflicts.
177

178
        rule -> STRING . rule_activation rule_forall infixexp '=' exp    (rule 215)
179

180
    Conflict: '[' (empty rule_activation reduces)
181

182 183 184
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
185

186 187
Example ambiguity:
    '{-# RULE [0] f = ... #-}'
188

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

192
-------------------------------------------------------------------------------
193

194
state 301 contains 1 shift/reduce conflict.
195

196 197
    *** type -> btype .                                     (rule 297)
        type -> btype . '->' ctype                          (rule 298)
198

199
    Conflict: '->'
200

201
Same as state 50 but without contexts.
202

203
-------------------------------------------------------------------------------
204

205
state 337 contains 1 shift/reduce conflicts.
206

207 208 209
        tup_exprs -> commas . tup_tail                      (rule 505)
        sysdcon_nolist -> '(' commas . ')'                  (rule 616)
        commas -> commas . ','                              (rule 734)
210

211
    Conflict: ')' (empty tup_tail reduces)
212

213 214 215 216
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.
217

218
-------------------------------------------------------------------------------
219

220
state 388 contains 1 shift/reduce conflicts.
221

222 223 224
        tup_exprs -> commas . tup_tail                      (rule 505)
        sysdcon_nolist -> '(#' commas . '#)'                (rule 618)
        commas -> commas . ','                              (rule 734)
225 226 227

    Conflict: '#)' (empty tup_tail reduces)

228
Same as State 324 for unboxed tuples.
229 230 231

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

232
state 460 contains 1 shift/reduce conflict.
233

234 235
        oqtycon -> '(' qtyconsym . ')'                      (rule 621)
    *** qtyconop -> qtyconsym .                             (rule 628)
236

237
    Conflict: ')'
238 239 240 241 242

TODO: Why?

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

243
state 635 contains 1 shift/reduce conflicts.
244

245 246
    *** aexp2 -> ipvar .                                    (rule 466)
        dbind -> ipvar . '=' exp                            (rule 590)
247 248 249 250 251 252 253 254 255 256 257

    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.

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

258
state 702 contains 1 shift/reduce conflicts.
259

260
        rule -> STRING rule_activation . rule_forall infixexp '=' exp    (rule 215)
261 262 263 264 265 266 267 268 269 270 271 272 273 274

    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'.

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

275
state 930 contains 1 shift/reduce conflicts.
276

277 278 279
        transformqual -> 'then' 'group' . 'using' exp       (rule 528)
        transformqual -> 'then' 'group' . 'by' exp 'using' exp    (rule 529)
    *** special_id -> 'group' .                             (rule 711)
280 281 282

    Conflict: 'by'

283 284 285 286 287 288 289 290 291 292
-------------------------------------------------------------------------------

state 1270 contains 1 shift/reduce conflict.

    *** atype -> tyvar .                                    (rule 314)
        tv_bndr -> '(' tyvar . '::' kind ')'                (rule 346)

    Conflict: '::'

TODO: Why?
293 294

-------------------------------------------------------------------------------
295
-- API Annotations
296
--
297 298 299 300 301 302 303 304 305 306 307

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.

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

313 314 315 316 317
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.

318 319 320 321 322
-- -----------------------------------------------------------------------------

-}

%token
chak@cse.unsw.edu.au.'s avatar
chak@cse.unsw.edu.au. committed
323 324
 '_'            { L _ ITunderscore }            -- Haskell keywords
 'as'           { L _ ITas }
325 326 327
 'case'         { L _ ITcase }
 'class'        { L _ ITclass }
 'data'         { L _ ITdata }
chak@cse.unsw.edu.au.'s avatar
chak@cse.unsw.edu.au. committed
328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348
 '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 }

349
 'forall'       { L _ (ITforall _) }                -- GHC extension keywords
chak@cse.unsw.edu.au.'s avatar
chak@cse.unsw.edu.au. committed
350 351
 'foreign'      { L _ ITforeign }
 'export'       { L _ ITexport }
352
 'label'        { L _ ITlabel }
chak@cse.unsw.edu.au.'s avatar
chak@cse.unsw.edu.au. committed
353 354
 'dynamic'      { L _ ITdynamic }
 'safe'         { L _ ITsafe }
355
 'interruptible' { L _ ITinterruptible }
chak@cse.unsw.edu.au.'s avatar
chak@cse.unsw.edu.au. committed
356 357 358
 'unsafe'       { L _ ITunsafe }
 'mdo'          { L _ ITmdo }
 'family'       { L _ ITfamily }
359
 'role'         { L _ ITrole }
360 361
 'stdcall'      { L _ ITstdcallconv }
 'ccall'        { L _ ITccallconv }
362
 'capi'         { L _ ITcapiconv }
363
 'prim'         { L _ ITprimcallconv }
thoughtpolice's avatar
thoughtpolice committed
364
 'javascript'   { L _ ITjavascriptcallconv }
chak@cse.unsw.edu.au.'s avatar
chak@cse.unsw.edu.au. committed
365 366
 'proc'         { L _ ITproc }          -- for arrow notation extension
 'rec'          { L _ ITrec }           -- for arrow notation extension
367 368 369
 '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
370
 'pattern'      { L _ ITpattern } -- for pattern synonyms
371
 'static'       { L _ ITstatic }  -- for static pointers extension
372

Alan Zimmerman's avatar
Alan Zimmerman committed
373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395
 '{-# 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
396 397 398

 '..'           { L _ ITdotdot }                        -- reserved symbols
 ':'            { L _ ITcolon }
399
 '::'           { L _ (ITdcolon _) }
chak@cse.unsw.edu.au.'s avatar
chak@cse.unsw.edu.au. committed
400 401
 '='            { L _ ITequal }
 '\\'           { L _ ITlam }
402
 'lcase'        { L _ ITlcase }
chak@cse.unsw.edu.au.'s avatar
chak@cse.unsw.edu.au. committed
403
 '|'            { L _ ITvbar }
404 405
 '<-'           { L _ (ITlarrow _) }
 '->'           { L _ (ITrarrow _) }
chak@cse.unsw.edu.au.'s avatar
chak@cse.unsw.edu.au. committed
406 407 408
 '@'            { L _ ITat }
 '~'            { L _ ITtilde }
 '~#'           { L _ ITtildehsh }
409
 '=>'           { L _ (ITdarrow _) }
chak@cse.unsw.edu.au.'s avatar
chak@cse.unsw.edu.au. committed
410 411
 '-'            { L _ ITminus }
 '!'            { L _ ITbang }
412 413 414 415
 '-<'           { L _ (ITlarrowtail _) }            -- for arrow notation
 '>-'           { L _ (ITrarrowtail _) }            -- for arrow notation
 '-<<'          { L _ (ITLarrowtail _) }            -- for arrow notation
 '>>-'          { L _ (ITRarrowtail _) }            -- for arrow notation
chak@cse.unsw.edu.au.'s avatar
chak@cse.unsw.edu.au. committed
416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434
 '.'            { 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
435
 SIMPLEQUOTE    { L _ ITsimpleQuote      }     -- 'x
chak@cse.unsw.edu.au.'s avatar
chak@cse.unsw.edu.au. committed
436 437 438 439 440 441 442 443 444

 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  _) }
445

chak@cse.unsw.edu.au.'s avatar
chak@cse.unsw.edu.au. committed
446
 IPDUPVARID     { L _ (ITdupipvarid   _) }              -- GHC extension
Adam Gundry's avatar
Adam Gundry committed
447
 LABELVARID     { L _ (ITlabelvarid   _) }
chak@cse.unsw.edu.au.'s avatar
chak@cse.unsw.edu.au. committed
448

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

454 455 456 457
 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
458 459 460 461 462 463 464
 PRIMFLOAT      { L _ (ITprimfloat  _) }
 PRIMDOUBLE     { L _ (ITprimdouble _) }

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

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

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

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

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

510 511 512 513 514 515 516 517 518 519
-----------------------------------------------------------------------------
-- 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
520 521 522 523 524 525 526
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) }
527
        | body2
chak@cse.unsw.edu.au.'s avatar
chak@cse.unsw.edu.au. committed
528
                {% fileSrcSpan >>= \ loc ->
Alan Zimmerman's avatar
Alan Zimmerman committed
529 530 531
                   ams (L loc (HsModule Nothing Nothing
                               (fst $ snd $1) (snd $ snd $1) Nothing Nothing))
                       (fst $1) }
532

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

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

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

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

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

top     :: { ([AddAnn]
             ,([LImportDecl RdrName], [LHsDecl RdrName])) }
Alan Zimmerman's avatar
Alan Zimmerman committed
563 564 565 566 567 568 569 570 571 572
        : 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
573
        | cvtopdecls                    { ([],([],$1)) }
574 575

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

578 579 580
-----------------------------------------------------------------------------
-- Module declaration & imports only

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

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

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

599 600 601
-----------------------------------------------------------------------------
-- The Export List

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

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

612
exportlist1 :: { OrdList (LIE RdrName) }
Alan Zimmerman's avatar
Alan Zimmerman committed
613 614 615 616 617 618
        : 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 }
619

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

624
exp_doc :: { OrdList (LIE RdrName) }
rodlogic's avatar
rodlogic committed
625 626 627
        : 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))) }
628 629


630 631
   -- No longer allow things like [] and (,,,) to be exported
   -- They are built in syntax, always available
632
export  :: { OrdList (LIE RdrName) }
633 634
        : qcname_ext export_subspec  {% mkModuleImpExp $1 (snd $ unLoc $2)
                                          >>= \ie -> amsu (sLL $1 $> ie) (fst $ unLoc $2) }
Alan Zimmerman's avatar
Alan Zimmerman committed
635 636 637 638 639 640 641
        |  '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) }
642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669
        | '(' qcnames ')'         {% mkImpExpSubSpec (reverse (snd $2))
                                      >>= \(as,ie) -> return $ sLL $1 $>
                                            (as ++ [mop $1,mcp $3] ++ fst $2, ie) }


qcnames :: { ([AddAnn], [Located (Maybe RdrName)]) }
  : {- empty -}                   { ([],[]) }
  | qcnames1                      { $1 }

qcnames1 :: { ([AddAnn], [Located (Maybe RdrName)]) }     -- A reversed list
        :  qcnames1 ',' qcname_ext_w_wildcard  {% case (last (snd $1)) of
                                                    l@(L _ Nothing) ->
                                                      return ([mj AnnComma $2, mj AnnDotdot l]
                                                              ,($3  : snd $1))
                                                    l -> (aa l (AnnComma, $2) >>
                                                          return (fst $1, $3 : snd $1)) }


        -- Annotations readded in mkImpExpSubSpec
        |  qcname_ext_w_wildcard                   { ([],[$1])  }

-- Variable, data constructor or wildcard
-- or tagged type constructor
qcname_ext_w_wildcard :: { Located (Maybe RdrName) }
        :  qcname_ext               { Just `fmap` $1 }
        |  '..'                     { Nothing <$ $1 }

qcname_ext :: { Located RdrName }
Alan Zimmerman's avatar
Alan Zimmerman committed
670
        :  qcname                   { $1 }
671
        |  'type' oqtycon           {% amms (mkTypeImpExp (sLL $1 $> (unLoc $2)))
672
                                            [mj AnnType $1,mj AnnVal $2] }
673

674
qcname  :: { Located RdrName }  -- Variable or type constructor
675 676
        :  qvar                 { $1 }
        |  oqtycon_no_varcon    { $1 } -- see Note [Type constructors in export list]
677 678 679 680 681 682 683

-----------------------------------------------------------------------------
-- 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
684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699
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 -}            { ([],[]) }
700 701

importdecl :: { LImportDecl RdrName }
702
        : 'import' maybe_src maybe_safe optqualified maybe_pkg modid maybeas maybeimpspec
Alan Zimmerman's avatar
Alan Zimmerman committed
703
                {% ams (L (comb4 $1 $6 (snd $7) $8) $
Alan Zimmerman's avatar
Alan Zimmerman committed
704 705
                  ImportDecl { ideclSourceSrc = snd $ fst $2
                             , ideclName = $6, ideclPkgQual = snd $5
Alan Zimmerman's avatar
Alan Zimmerman committed
706 707 708 709
                             , 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
710
                   ((mj AnnImport $1 : (fst $ fst $2) ++ fst $3 ++ fst $4
711
                                    ++ fst $5 ++ fst $7)) }
Alan Zimmerman's avatar
Alan Zimmerman committed
712

Alan Zimmerman's avatar
Alan Zimmerman committed
713 714 715 716
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
717 718 719 720 721

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

722
maybe_pkg :: { ([AddAnn],Maybe StringLiteral) }
723 724
        : STRING  {% let pkgFS = getSTRING $1 in
                     if looksLikePackageName (unpackFS pkgFS)
725
                        then return ([mj AnnPackageName $1], Just (StringLiteral (getSTRINGs $1) pkgFS))
726 727 728 729
                        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
730 731 732 733 734 735 736
        | {- empty -}                           { ([],Nothing) }

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

maybeas :: { ([AddAnn],Located (Maybe ModuleName)) }
737 738 739
        : 'as' modid                           { ([mj AnnAs $1,mj AnnVal $2]
                                                 ,sLL $1 $> (Just (unLoc $2))) }
        | {- empty -}                          { ([],noLoc Nothing) }
740

741
maybeimpspec :: { Located (Maybe (Bool, Located [LIE RdrName])) }
742 743 744 745
        : impspec                  {% let (b, ie) = unLoc $1 in
                                       checkImportSpec ie
                                        >>= \checkedIe ->
                                          return (L (gl $1) (Just (b, checkedIe)))  }
Alan Zimmerman's avatar
Alan Zimmerman committed
746
        | {- empty -}              { noLoc Nothing }
747

748
impspec :: { Located (Bool, Located [LIE RdrName]) }
Alan Zimmerman's avatar
Alan Zimmerman committed
749 750 751 752 753 754
        :  '(' 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] }
755 756 757 758

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

759 760 761 762
prec    :: { Located Int }
        : {- empty -}           { noLoc 9 }
        | INTEGER
                 {% checkPrecP (sL1 $1 (fromInteger (getINTEGER $1))) }
763

chak@cse.unsw.edu.au.'s avatar
chak@cse.unsw.edu.au. committed
764
infix   :: { Located FixityDirection }
rodlogic's avatar
rodlogic committed
765 766 767
        : 'infix'                               { sL1 $1 InfixN  }
        | 'infixl'                              { sL1 $1 InfixL  }
        | 'infixr'                              { sL1 $1 InfixR }
768

Alan Zimmerman's avatar
Alan Zimmerman committed
769
ops     :: { Located (OrdList (Located RdrName)) }
Alan Zimmerman's avatar
Alan Zimmerman committed
770 771 772
        : ops ',' op       {% addAnnotation (oll $ unLoc $1) AnnComma (gl $2) >>
                              return (sLL $1 $> ((unLoc $1) `appOL` unitOL $3))}
        | op               { sL1 $1 (unitOL $1) }
773 774 775 776

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

777
topdecls :: { OrdList (LHsDecl RdrName) }
778
        : topdecls ';' topdecl        {% addAnnotation (oll $1) AnnSemi (gl $2)
779
                                         >> return ($1 `appOL` unitOL $3) }
Alan Zimmerman's avatar
Alan Zimmerman committed
780 781
        | topdecls ';'                {% addAnnotation (oll $1) AnnSemi (gl $2)
                                         >> return $1 }
782 783 784 785 786 787 788 789 790
        | 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
791
                                                         [mj AnnDefault $1
thomasw's avatar
thomasw committed
792
                                                         ,mop $2,mcp $4] }
793
        | 'foreign' fdecl          {% ams (sLL $1 $> (snd $ unLoc $2))
Alan Zimmerman's avatar
Alan Zimmerman committed
794
                                           (mj AnnForeign $1:(fst $ unLoc $2)) }
795
        | '{-# DEPRECATED' deprecations '#-}'   {% ams (sLL $1 $> $ WarningD (Warnings (getDEPRECATED_PRAGs $1) (fromOL $2)))
Alan Zimmerman's avatar
Alan Zimmerman committed
796
                                                       [mo $1,mc $3] }
797
        | '{-# WARNING' warnings '#-}'          {% ams (sLL $1 $> $ WarningD (Warnings (getWARNING_PRAGs $1) (fromOL $2)))
Alan Zimmerman's avatar
Alan Zimmerman committed
798
                                                       [mo $1,mc $3] }
799
        | '{-# RULES' rules '#-}'               {% ams (sLL $1 $> $ RuleD (HsRules (getRULES_PRAGs $1) (fromOL $2)))
Alan Zimmerman's avatar
Alan Zimmerman committed
800
                                                       [mo $1,mc $3] }
801
        | '{-# VECTORISE' qvar '=' exp '#-}' {% ams (sLL $1 $> $ VectD (HsVect (getVECT_PRAGs $1) $2 $4))
Alan Zimmerman's avatar
Alan Zimmerman committed
802 803
                                                    [mo $1,mj AnnEqual $3
                                                    ,mc $5] }
804
        | '{-# NOVECTORISE' qvar '#-}'       {% ams (sLL $1 $> $ VectD (HsNoVect (getNOVECT_PRAGs $1) $2))
Alan Zimmerman's avatar
Alan Zimmerman committed
805
                                                     [mo $1,mc $3] }
806
        | '{-# VECTORISE' 'type' gtycon '#-}'
807
                                {% ams (sLL $1 $> $
Alan Zimmerman's avatar
Alan Zimmerman committed
808
                                    VectD (HsVectTypeIn (getVECT_PRAGs $1) False $3 Nothing))
Alan Zimmerman's avatar
Alan Zimmerman committed
809 810
                                    [mo $1,mj AnnType $2,mc $4] }

811
        | '{-# VECTORISE_SCALAR' 'type' gtycon '#-}'
812
                                {% ams (sLL $1 $> $
Alan Zimmerman's avatar
Alan Zimmerman committed
813
                                    VectD (HsVectTypeIn (getVECT_SCALAR_PRAGs $1) True $3 Nothing))
Alan Zimmerman's avatar
Alan Zimmerman committed
814 815
                                    [mo $1,mj AnnType $2,mc $4] }

816
        | '{-# VECTORISE' 'type' gtycon '=' gtycon '#-}'
817
                                {% ams (sLL $1 $> $
Alan Zimmerman's avatar
Alan Zimmerman committed
818
                                    VectD (HsVectTypeIn (getVECT_PRAGs $1) False $3 (Just $5)))
Alan Zimmerman's avatar
Alan Zimmerman committed
819
                                    [mo $1,mj AnnType $2,mj AnnEqual $4,mc $6] }
820
        | '{-# VECTORISE_SCALAR' 'type' gtycon '=' gtycon '#-}'
821
                                {% ams (sLL $1 $> $
Alan Zimmerman's avatar
Alan Zimmerman committed
822
                                    VectD (HsVectTypeIn (getVECT_SCALAR_PRAGs $1) True $3 (Just $5)))
Alan Zimmerman's avatar
Alan Zimmerman committed
823 824 825
                                    [mo $1,mj AnnType $2,mj AnnEqual $4,mc $6] }

        | '{-# VECTORISE' 'class' gtycon '#-}'
826
                                         {% ams (sLL $1 $>  $ VectD (HsVectClassIn (getVECT_PRAGs $1) $3))
Alan Zimmerman's avatar
Alan Zimmerman committed
827
                                                 [mo $1,mj AnnClass $2,mc $4] }
828 829
        | annotation { $1 }
        | decl_no_th                            { $1 }
830 831 832

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

837 838 839
-- Type classes
--
cl_decl :: { LTyClDecl RdrName }
840
        : 'class' tycl_hdr fds where_cls
Alan Zimmerman's avatar
Alan Zimmerman committed
841
                {% amms (mkClassDecl (comb4 $1 $2 $3 $4) $2 $3 (snd $ unLoc $4))
Alan Zimmerman's avatar
Alan Zimmerman committed
842
                        (mj AnnClass $1:(fst $ unLoc $3)++(fst $ unLoc $4)) }
843

844
-- Type declarations (toplevel)
845 846
--
ty_decl :: { LTyClDecl RdrName }
847
           -- ordinary type synonyms
848
        : 'type' type '=' ctypedoc
chak@cse.unsw.edu.au.'s avatar
chak@cse.unsw.edu.au. committed
849 850 851 852 853 854
                -- 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
855
                -- infix type constructors to be declared
856
                {% amms (mkTySynonym (comb2 $1 $4) $2 $4)
Alan Zimmerman's avatar
Alan Zimmerman committed
857
                        [mj AnnType $1,mj AnnEqual $3] }
858 859

           -- type family declarations
Jan Stolarek's avatar
Jan Stolarek committed
860 861
        | '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
862 863
                -- Note the use of type for the head; this allows
                -- infix type constructors to be declared
Jan Stolarek's avatar
Jan Stolarek committed
864 865 866 867
                {% 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)) }
868

869
          -- ordinary data type or newtype declaration
870
        | data_or_newtype capi_ctype tycl_hdr constrs deriving
Alan Zimmerman's avatar
Alan Zimmerman committed
871 872 873
                {% amms (mkTyData (comb4 $1 $3 $4 $5) (snd $ unLoc $1) $2 $3
                           Nothing (reverse (snd $ unLoc $4))
                                   (unLoc $5))
874
                                   -- We need the location on tycl_hdr in case
chak@cse.unsw.edu.au.'s avatar
chak@cse.unsw.edu.au. committed
875
                                   -- constrs and deriving are both empty
Alan Zimmerman's avatar
Alan Zimmerman committed
876
                        ((fst $ unLoc $1):(fst $ unLoc $4)) }
877

878
          -- ordinary GADT declaration
879
        | data_or_newtype capi_ctype tycl_hdr opt_kind_sig
chak@cse.unsw.edu.au.'s avatar
chak@cse.unsw.edu.au. committed
880 881
                 gadt_constrlist
                 deriving
Alan Zimmerman's avatar
Alan Zimmerman committed
882
            {% amms (mkTyData (comb4 $1 $3 $5 $6) (snd $ unLoc $1) $2 $3
883
                            (snd $ unLoc $4) (snd $ unLoc $5) (unLoc $6) )
884
                                   -- We need the location on tycl_hdr in case
chak@cse.unsw.edu.au.'s avatar
chak@cse.unsw.edu.au. committed
885
                                   -- constrs and deriving are both empty
886
                    ((fst $ unLoc $1):(fst $ unLoc $4)++(fst $ unLoc $5)) }
887

888
          -- data/newtype family
Jan Stolarek's avatar
Jan Stolarek committed
889 890 891
        | 'data' 'family' type opt_datafam_kind_sig
                {% amms (mkFamDecl (comb3 $1 $2 $4) DataFamily $3
                                   (snd $ unLoc $4) Nothing)
892
                        (mj AnnData $1:mj AnnFamily $2:(fst $ unLoc $4)) }
893

894
inst_decl :: { LInstDecl RdrName }
895
        : 'instance' overlap_pragma inst_type where_inst
Alan Zimmerman's avatar
Alan Zimmerman committed
896 897
       {% do { (binds, sigs, _, ats, adts, _) <- cvBindsAndSigs (snd $ unLoc $4)
             ; let cid = ClsInstDecl { cid_poly_ty = $3, cid_binds = binds
898 899
                                     , cid_sigs = mkClassOpSigs sigs
                                     , cid_tyfam_insts = ats
Alan Zimmerman's avatar
Alan Zimmerman committed
900 901
                                     , cid_overlap_mode = $2
                                     , cid_datafam_insts = adts }
902
             ; ams (L (comb3 $1 (hsSigType $3) $4) (ClsInstD { cid_inst = cid }))
Alan Zimmerman's avatar
Alan Zimmerman committed
903
                   (mj AnnInstance $1 : (fst $ unLoc $4)) } }
904 905

           -- type instance declarations
906
        | 'type' 'instance' ty_fam_inst_eqn
907 908 909
                {% ams $3 (fst $ unLoc $3)
                >> amms (mkTyFamInst (comb2 $1 $3) (snd $ unLoc $3))
                    (mj AnnType $1:mj AnnInstance $2:(fst $ unLoc $3)) }
910

911
          -- data/newtype instance declaration
912
        | data_or_newtype 'instance' capi_ctype tycl_hdr constrs deriving
Alan Zimmerman's avatar
Alan Zimmerman committed
913 914 915 916
            {% 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)) }
917

918
          -- GADT instance declaration
919
        | data_or_newtype 'instance' capi_ctype tycl_hdr opt_kind_sig
chak@cse.unsw.edu.au.'s avatar
chak@cse.unsw.edu.au. committed
920 921
                 gadt_constrlist
                 deriving
Alan Zimmerman's avatar
Alan Zimmerman committed
922
            {% amms (mkDataFamInst (comb4 $1 $4 $6 $7) (snd $ unLoc $1) $3 $4
923
                                   (snd $ unLoc $5) (snd $ unLoc $6) (unLoc $7))
Alan Zimmerman's avatar
Alan Zimmerman committed
924
                    ((fst $ unLoc $1):mj AnnInstance $2
925
                       :(fst $ unLoc $5)++(fst $ unLoc $6)) }
926

927
overlap_pragma :: { Maybe (Located OverlapMode) }
Alan Zimmerman's avatar
Alan Zimmerman committed
928
  : '{-# OVERLAPPABLE'    '#-}' {% ajs (Just (sLL $1 $> (Overlappable (getOVERLAPPABLE_PRAGs $1))))
Alan Zimmerman's avatar
Alan Zimmerman committed
929
                                       [mo $1,mc $2] }
Alan Zimmerman's avatar
Alan Zimmerman committed
930
  | '{-# OVERLAPPING'     '#-}' {% ajs (Just (sLL $1 $> (Overlapping (getOVERLAPPING_PRAGs $1))))
Alan Zimmerman's avatar
Alan Zimmerman committed
931
                                       [mo $1,mc $2] }
Alan Zimmerman's avatar
Alan Zimmerman committed
932
  | '{-# OVERLAPS'        '#-}' {% ajs (Just (sLL $1 $> (Overlaps (getOVERLAPS_PRAGs $1))))
Alan Zimmerman's avatar
Alan Zimmerman committed
933
                                       [mo $1,mc $2] }
Alan Zimmerman's avatar
Alan Zimmerman committed
934
  | '{-# INCOHERENT'      '#-}' {% ajs (Just (sLL $1 $> (Incoherent (getINCOHERENT_PRAGs $1))))
Alan Zimmerman's avatar
Alan Zimmerman committed
935
                                       [mo $1,mc $2] }
936
  | {- empty -}                 { Nothing }
937 938


Jan Stolarek's avatar
Jan Stolarek committed
939 940 941 942 943 944 945 946 947
-- 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
948
           { sLL $1 $> ( [mu AnnRarrow $2]
Jan Stolarek's avatar
Jan Stolarek committed
949 950 951 952 953 954
                       , (sLL $1 $> (InjectivityAnn $1 (reverse (unLoc $3))))) }

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

955 956
-- Closed type families

Alan Zimmerman's avatar
Alan Zimmerman committed
957 958
where_type_family :: { Located ([AddAnn],FamilyInfo RdrName) }
        : {- empty -}                      { noLoc ([],OpenTypeFamily) }
959
        | 'where' ty_fam_inst_eqn_list