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
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
-------------------------------------------------------------------------------
ross's avatar
ross committed
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
Facundo Domínguez's avatar
Facundo Domínguez committed
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
 '.'            { L _ ITdot }
eir@cis.upenn.edu's avatar
eir@cis.upenn.edu committed
417
 TYPEAPP        { L _ ITtypeApp }
chak@cse.unsw.edu.au.'s avatar
chak@cse.unsw.edu.au. committed
418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435

 '{'            { 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
436
 SIMPLEQUOTE    { L _ ITsimpleQuote      }     -- 'x
chak@cse.unsw.edu.au.'s avatar
chak@cse.unsw.edu.au. committed
437 438 439 440 441 442 443 444 445

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

600 601 602
-----------------------------------------------------------------------------
-- The Export List

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

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

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

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

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


631 632
   -- No longer allow things like [] and (,,,) to be exported
   -- They are built in syntax, always available
633
export  :: { OrdList (LIE RdrName) }
634 635
        : qcname_ext export_subspec  {% mkModuleImpExp $1 (snd $ unLoc $2)
                                          >>= \ie -> amsu (sLL $1 $> ie) (fst $ unLoc $2) }
Alan Zimmerman's avatar
Alan Zimmerman committed
636 637 638 639 640 641 642
        |  '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) }
643 644 645 646 647 648 649 650 651 652
        | '(' 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
653
        :  qcnames1 ',' qcname_ext_w_wildcard  {% case (head (snd $1)) of
654
                                                    l@(L _ Nothing) ->
655 656 657 658 659
                                                       return ([mj AnnComma $2, mj AnnDotdot l]
                                                               ,(snd (unLoc $3)  : snd $1))
                                                    l -> (ams (head (snd $1)) [mj AnnComma $2] >>
                                                          return (fst $1 ++ fst (unLoc $3),
                                                                  snd (unLoc $3) : snd $1)) }
660 661


662 663
        -- Annotations re-added in mkImpExpSubSpec
        |  qcname_ext_w_wildcard                   { (fst (unLoc $1),[snd (unLoc $1)]) }
664 665 666

-- Variable, data constructor or wildcard
-- or tagged type constructor
667 668 669
qcname_ext_w_wildcard :: { Located ([AddAnn],Located (Maybe RdrName)) }
        :  qcname_ext               { sL1 $1 ([],Just `fmap` $1) }
        |  '..'                     { sL1 $1 ([mj AnnDotdot $1], sL1 $1 Nothing) }
670 671

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

913
          -- data/newtype instance declaration