2 /* --------------------------------------------------------------------------
3 * Hugs parser (included as part of input.c)
5 * Expect 6 shift/reduce conflicts when passing this grammar through yacc,
6 * but don't worry; they should all be resolved in an appropriate manner.
8 * The Hugs 98 system is Copyright (c) Mark P Jones, Alastair Reid, the
9 * Yale Haskell Group, and the Oregon Graduate Institute of Science and
10 * Technology, 1994-1999, All rights reserved. It is distributed as
11 * free software under the license in the file "License", which is
12 * included in the distribution.
14 * $RCSfile: parser.y,v $
16 * $Date: 2000/04/25 17:43:50 $
17 * ------------------------------------------------------------------------*/
23 #define sigdecl(l,vs,t) ap(SIGDECL,triple(l,vs,t))
24 #define fixdecl(l,ops,a,p) ap(FIXDECL,\
25 triple(l,ops,mkInt(mkSyntax(a,intOf(p)))))
26 #define grded(gs) ap(GUARDED,gs)
27 #define only(t) ap(ONLY,t)
28 #define letrec(bs,e) (nonNull(bs) ? ap(LETREC,pair(bs,e)) : e)
29 #define qualify(ps,t) (nonNull(ps) ? ap(QUAL,pair(ps,t)) : t)
30 #define yyerror(s) /* errors handled elsewhere */
33 static Cell local gcShadow ( Int,Cell );
34 static Void local syntaxError ( String );
35 static String local unexpected ( Void );
36 static Cell local checkPrec ( Cell );
37 static Void local fixDefn ( Syntax,Cell,Cell,List );
38 static Cell local buildTuple ( List );
39 static List local checkCtxt ( List );
40 static Cell local checkPred ( Cell );
41 static Pair local checkDo ( List );
42 static Cell local checkTyLhs ( Cell );
44 static Void local noTREX ( String );
47 static Void local noIP ( String );
50 /* For the purposes of reasonably portable garbage collection, it is
51 * necessary to simulate the YACC stack on the Hugs stack to keep
52 * track of all intermediate constructs. The lexical analyser
53 * pushes a token onto the stack for each token that is found, with
54 * these elements being removed as reduce actions are performed,
55 * taking account of look-ahead tokens as described by gcShadow()
58 * Of the non-terminals used below, only start, topDecl & begin
59 * do not leave any values on the Hugs stack. The same is true for the
60 * terminals EXPR and SCRIPT. At the end of a successful parse, there
61 * should only be one element left on the stack, containing the result
65 #define gc0(e) gcShadow(0,e)
66 #define gc1(e) gcShadow(1,e)
67 #define gc2(e) gcShadow(2,e)
68 #define gc3(e) gcShadow(3,e)
69 #define gc4(e) gcShadow(4,e)
70 #define gc5(e) gcShadow(5,e)
71 #define gc6(e) gcShadow(6,e)
72 #define gc7(e) gcShadow(7,e)
73 #define gc8(e) gcShadow(8,e)
74 #define gc9(e) gcShadow(9,e)
78 %token EXPR CONTEXT SCRIPT
79 %token CASEXP OF DATA TYPE IF
80 %token THEN ELSE WHERE LET IN
81 %token INFIXN INFIXL INFIXR FOREIGN TNEWTYPE
82 %token DEFAULT DERIVING DO TCLASS TINSTANCE
87 %token REPEAT ALL NUMLIT CHARLIT STRINGLIT
88 %token VAROP VARID CONOP CONID
89 %token QVAROP QVARID QCONOP QCONID
91 %token RECSELID IPVARID
93 %token COCO '=' UPTO '@' '\\'
94 %token '|' '-' FROM ARROW '~'
95 %token '!' IMPLIES '(' ',' ')'
96 %token '[' ';' ']' '`' '.'
97 %token TMODULE IMPORT HIDING QUALIFIED ASMOD
98 %token EXPORT UUEXPORT INTERFACE REQUIRES UNSAFE
99 %token INSTIMPORT DYNAMIC CCALL STDKALL
100 %token UTL UTR UUUSAGE
103 /*- Top level script/module structure -------------------------------------*/
105 start : EXPR exp wherePart {inputExpr = letrec($3,$2); sp-=2;}
106 | CONTEXT context {inputContext = $2; sp-=1;}
107 | SCRIPT topModule {drop(); push($2);}
108 | INTERFACE iface {sp-=1;}
109 | error {syntaxError("input");}
113 /*- GHC interface file parsing: -------------------------------------------*/
115 /* Reading in an interface file is surprisingly like reading
116 * a normal Haskell module: we read in a bunch of declarations,
117 * construct symbol table entries, etc. The "only" differences
118 * are that there's no syntactic sugar to deal with and we don't
119 * have to read in expressions.
122 /*- Top-level interface files -----------------------------*/
123 iface : INTERFACE STRINGLIT ifCon NUMLIT ifOrphans ifCheckVersion WHERE ifTopDecls
124 {$$ = gc8(ap(I_INTERFACE,
126 | INTERFACE error {syntaxError("interface file");}
129 ifTopDecls: {$$=gc0(NIL);}
130 | ifTopDecl ';' ifTopDecls {$$=gc3(cons($1,$3));}
134 : IMPORT CONID NUMLIT ifOrphans ifIsBoot ifOptCOCO ifVersionList
135 {$$=gc7(ap(I_IMPORT,zpair($2,$7))); }
137 | INSTIMPORT CONID {$$=gc2(ap(I_INSTIMPORT,NIL));}
139 | UUEXPORT CONID ifEntities {$$=gc3(ap(I_EXPORT,zpair($2,$3)));}
141 | NUMLIT INFIXL optDigit ifVarCon
142 {$$=gc4(ap(I_FIXDECL,
143 ztriple($3,mkInt(LEFT_ASS),$4)));}
144 | NUMLIT INFIXR optDigit ifVarCon
145 {$$=gc4(ap(I_FIXDECL,
146 ztriple($3,mkInt(RIGHT_ASS),$4)));}
147 | NUMLIT INFIXN optDigit ifVarCon
148 {$$=gc4(ap(I_FIXDECL,
149 ztriple($3,mkInt(NON_ASS),$4)));}
151 | TINSTANCE ifCtxInst ifInstHdL '=' ifVar
152 {$$=gc5(ap(I_INSTANCE,
153 z5ble($1,$2,$3,$5,NIL)));}
155 | NUMLIT TYPE ifCon ifKindedTyvarL '=' ifType
157 z4ble($2,$3,$4,$6)));}
159 | NUMLIT DATA ifCtxDecl ifConData ifKindedTyvarL ifConstrs
161 z5ble($2,$3,$4,$5,$6)));}
163 | NUMLIT TNEWTYPE ifCtxDecl ifConData ifKindedTyvarL ifNewTypeConstr
164 {$$=gc6(ap(I_NEWTYPE,
165 z5ble($2,$3,$4,$5,$6)));}
167 | NUMLIT TCLASS ifCtxDecl ifCon ifKindedTyvar ifCmeths
170 singleton($5),$6)));}
172 | NUMLIT ifVar COCO ifType
174 ztriple($3,$2,$4)));}
176 | error { syntaxError(
177 "interface declaration"); }
181 /*- Top-level misc interface stuff ------------------------*/
182 ifOrphans : '!' {$$=gc1(NIL);}
184 ifIsBoot : '@' {$$=gc1(NIL);}
187 ifOptCOCO : COCO {$$=gc1(NIL);}
191 : NUMLIT {$$ = gc1(NIL); }
196 /*- Interface variable and constructor ids ----------------*/
197 ifTyvar : VARID {$$ = $1;}
199 ifVar : VARID {$$ = gc1($1);}
201 ifCon : CONID {$$ = gc1($1);}
204 ifVarCon : VARID {$$ = gc1($1);}
205 | CONID {$$ = gc1($1);}
208 ifQCon : CONID {$$ = gc1($1);}
209 | QCONID {$$ = gc1($1);}
211 ifConData : ifCon {$$ = gc1($1);}
212 | '(' ')' {$$ = gc2(typeUnit);}
213 | '[' ']' {$$ = gc2(typeList);}
214 | '(' ARROW ')' {$$ = gc3(typeArrow);}
216 ifTCName : CONID { $$ = gc1($1); }
217 | CONOP { $$ = gc1($1); }
218 | '(' ARROW ')' { $$ = gc3(typeArrow); }
219 | '[' ']' { $$ = gc1(typeList); }
221 ifQTCName : ifTCName { $$ = gc1($1); }
222 | QCONID { $$ = gc1($1); }
223 | QCONOP { $$ = gc1($1); }
227 /*- Interface contexts ------------------------------------*/
228 ifCtxInst /* __forall [a b] => :: [((VarId,Kind))] */
229 : ALL ifForall IMPLIES {$$=gc3($2);}
232 ifInstHd /* { Class aType } :: ((ConId, Type)) */
233 : '{' ifQCon ifAType '}' {$$=gc4(ap(DICTAP,
237 ifInstHdL /* { C a1 } -> { C2 a2 } -> ... -> { Cn an } :: Type */
238 : ifInstHd ARROW ifInstHdL {$$=gc3(ap($1,$3));}
239 | ifInstHd {$$=gc1($1);}
242 ifCtxDecl /* {M.C1 a, C2 b} => :: [(QConId, VarId)] */
243 : ifCtxDeclT IMPLIES { $$ = gc2($1); }
246 ifCtxDeclT /* {M.C1 a, C2 b} :: [(QConId, VarId)] */
248 | '{' ifCtxDeclL '}' { $$ = gc3($2); }
251 ifCtxDeclL /* M.C1 a, C2 b :: [(QConId, VarId)] */
252 : ifCtxDeclLE ',' ifCtxDeclL {$$=gc3(cons($1,$3));}
253 | ifCtxDeclLE {$$=gc1(cons($1,NIL));}
256 ifCtxDeclLE /* M.C1 a :: (QConId,VarId) */
257 : ifQCon ifTyvar {$$=gc2(zpair($1,$2));}
261 /*- Interface data declarations - constructor lists -------*/
262 /* The (Type,VarId,Int) are (field type, name (or NIL), strictness).
263 Strictness is a number: mkInt(0) indicates lazy, mkInt(1)
264 indicates a strict field (!type) as in standard H98, and
265 mkInt(2) indicates unpacked -- a GHC extension.
268 ifConstrs /* = Con1 | ... | ConN :: [((ConId,[((Type,VarId,Int))]))] */
270 | '=' ifConstrL {$$ = gc2($2);}
272 ifConstrL /* [((ConId,[((Type,VarId,Int))]))] */
273 : ifConstr {$$ = gc1(singleton($1));}
274 | ifConstr '|' ifConstrL {$$ = gc3(cons($1,$3));}
276 ifConstr /* ((ConId,[((Type,VarId,Int))])) */
277 : ifConData ifDataAnonFieldL {$$ = gc2(zpair($1,$2));}
278 | ifConData '{' ifDataNamedFieldL '}'
279 {$$ = gc4(zpair($1,$3));}
281 ifDataAnonFieldL /* [((Type,VarId,Int))] */
283 | ifDataAnonField ifDataAnonFieldL
284 {$$=gc2(cons($1,$2));}
286 ifDataNamedFieldL /* [((Type,VarId,Int))] */
288 | ifDataNamedField {$$=gc1(cons($1,NIL));}
289 | ifDataNamedField ',' ifDataNamedFieldL
290 {$$=gc3(cons($1,$3));}
292 ifDataAnonField /* ((Type,VarId,Int)) */
293 : ifAType {$$=gc1(ztriple($1,NIL,mkInt(0)));}
294 | '!' ifAType {$$=gc2(ztriple($2,NIL,mkInt(1)));}
295 | '!' '!' ifAType {$$=gc3(ztriple($3,NIL,mkInt(2)));}
297 ifDataNamedField /* ((Type,VarId,Int)) */
298 : ifVar COCO ifAType {$$=gc3(ztriple($3,$1,mkInt(0)));}
299 | ifVar COCO '!' ifAType {$$=gc4(ztriple($4,$1,mkInt(1)));}
300 | ifVar COCO '!' '!' ifAType {$$=gc5(ztriple($5,$1,mkInt(2)));}
304 /*- Interface class declarations - methods ----------------*/
305 ifCmeths /* [((VarId,Type))] */
307 | WHERE '{' ifCmethL '}' { $$ = gc4($3); }
309 ifCmethL /* [((VarId,Type))] */
310 : ifCmeth { $$ = gc1(singleton($1)); }
311 | ifCmeth ';' ifCmethL { $$ = gc3(cons($1,$3)); }
313 ifCmeth /* ((VarId,Type)) */
314 : ifVar COCO ifType { $$ = gc3(zpair($1,$3)); }
315 | ifVar '=' COCO ifType { $$ = gc4(zpair($1,$4)); }
316 /* has default method */
320 /*- Interface newtype declararions ------------------------*/
321 ifNewTypeConstr /* ((ConId,Type)) */
322 : '=' ifCon ifAType { $$ = gc3(zpair($2,$3)); }
326 /*- Interface type expressions ----------------------------*/
327 ifType : ALL ifForall ifCtxDeclT IMPLIES ifType
330 $$=gc5(pair(QUAL,pair($3,$5)));
332 | ifBType ARROW ifType { $$ = gc3(fn($1,$3)); }
333 | ifBType { $$ = gc1($1); }
335 ifForall /* [((VarId,Kind))] */
336 : '[' ifKindedTyvarL ']' { $$ = gc3($2); }
339 ifTypeL2 /* [Type], 2 or more */
340 : ifType ',' ifType { $$ = gc3(doubleton($1,$3)); }
341 | ifType ',' ifTypeL2 { $$ = gc3(cons($1,$3)); }
344 ifTypeL /* [Type], 0 or more */
345 : ifType ',' ifTypeL { $$ = gc3(cons($1,$3)); }
346 | ifType { $$ = gc1(singleton($1)); }
350 ifBType : ifAType { $$ = gc1($1); }
351 | ifBType ifAType { $$ = gc2(ap($1,$2)); }
352 | UUUSAGE ifUsage ifAType { $$ = gc3($3); }
355 ifAType : ifQTCName { $$ = gc1($1); }
356 | ifTyvar { $$ = gc1($1); }
357 | '(' ')' { $$ = gc2(typeUnit); }
358 | '(' ifTypeL2 ')' { $$ = gc3(buildTuple(reverse($2))); }
359 | '[' ifType ']' { $$ = gc3(ap(mkCon(tycon(typeList).text),
361 | '{' ifQTCName ifAType '}' { $$ = gc4(ap(DICTAP,
363 | '(' ifType ')' { $$ = gc3($2); }
364 | UTL ifTypeL UTR { $$ = gc3(ap(UNBOXEDTUP,$2)); }
368 /*- KW's usage stuff --------------------------------------*/
369 ifUsage : '-' { $$ = gc1(NIL); }
370 | '!' { $$ = gc1(NIL); }
371 | ifVar { $$ = gc1(NIL); }
375 /*- Interface kinds ---------------------------------------*/
376 ifKindedTyvarL /* [((VarId,Kind))] */
378 | ifKindedTyvar ifKindedTyvarL { $$ = gc2(cons($1,$2)); }
380 ifKindedTyvar /* ((VarId,Kind)) */
381 : ifTyvar { $$ = gc1(zpair($1,STAR)); }
382 | ifTyvar COCO ifAKind { $$ = gc3(zpair($1,$3)); }
384 ifKind : ifAKind { $$ = gc1($1); }
385 | ifAKind ARROW ifKind { $$ = gc3(ap($1,$3)); }
387 ifAKind : VAROP { $$ = gc1(STAR); }
389 | '(' ifKind ')' { $$ = gc3($2); }
393 /*- Interface version/export/import stuff -----------------*/
396 | ifEntity ifEntities { $$ = gc2(cons($1,$2)); }
399 : ifEntityOcc {$$=gc1($1);}
400 | ifEntityOcc ifStuffInside {$$=gc2(zpair($1,$2));}
403 : ifVar { $$ = gc1($1); }
404 | ifCon { $$ = gc1($1); }
405 | ARROW { $$ = gc1(typeArrow); }
406 | '(' ARROW ')' { $$ = gc3(typeArrow); }
407 /* why allow both? */
410 : '{' ifValOccs '}' { $$ = gc3($2); }
414 | ifVar ifValOccs { $$ = gc2(cons($1,$2)); }
415 | ifCon ifValOccs { $$ = gc2(cons($1,$2)); }
420 | VARID NUMLIT ifVersionList {$$=gc3(cons($1,$3));}
421 | CONID NUMLIT ifVersionList {$$=gc3(cons($1,$3));}
425 /*- Haskell module header/import parsing: -----------------------------------
426 * Module chasing is now totally different from Classic Hugs98. We parse
427 * the entire syntax tree. Subsequent passes over the tree collect and
428 * chase imports; we no longer attempt to do so whilst parsing.
429 *-------------------------------------------------------------------------*/
431 /* In Haskell 1.2, the default module header was "module Main where"
432 * In 1.3, this changed to "module Main(main) where".
433 * We use the 1.2 header because it breaks much less pre-module code.
434 * STG Hugs, 15 March 00: disallow default headers (pro tem).
436 topModule : TMODULE modname expspec WHERE '{' modBody end
438 ztriple($2,$3,$6)));}
439 | TMODULE modname WHERE '{' modBody end
443 singleton(ap(MODULEENT,$2)),
446 | begin modBody end {ConId fakeNm = mkCon(module(
447 moduleBeingParsed).text);
448 $$ = gc2(ap(M_MODULE,
450 singleton(ap(MODULEENT,fakeNm)),
453 | TMODULE error {syntaxError("module definition");}
456 modname : CONID {$$ = gc1($1);}
458 modid : CONID {$$ = gc1($1);}
460 modBody : topDecls {$$ = gc1($1);}
461 | impDecls {$$ = gc1($1);}
462 | impDecls ';' topDecls {$$ = gc3(appendOnto($1,$3));}
465 /*- Exports: --------------------------------------------------------------*/
467 expspec : '(' ')' {$$ = gc2(NIL);}
468 | '(' exports ')' {$$ = gc3($2);}
469 | '(' exports ',' ')' {$$ = gc4($2);}
471 exports : exports ',' export {$$ = gc3(cons($3,$1));}
472 | export {$$ = gc1(singleton($1));}
474 /* The qcon should be qconid.
475 * Relaxing the rule lets us explicitly export (:) from the Prelude.
477 export : qvar {$$ = $1;}
479 | qconid '(' UPTO ')' {$$ = gc4(pair($1,DOTDOT));}
480 | qconid '(' qnames ')' {$$ = gc4(pair($1,$3));}
481 | TMODULE modid {$$ = gc2(ap(MODULEENT,$2));}
483 qnames : /* empty */ {$$ = gc0(NIL);}
484 | ',' {$$ = gc1(NIL);}
486 | qnames1 ',' {$$ = gc2($1);}
488 qnames1 : qnames1 ',' qname {$$ = gc3(cons($3,$1));}
489 | qname {$$ = gc1(singleton($1));}
491 qname : qvar {$$ = $1;}
495 /*- Import declarations: --------------------------------------------------*/
497 impDecls : impDecls ';' impDecl {$$ = gc3(appendOnto($3,$1));}
498 | impDecl {$$ = gc1($1);}
501 /* Note that qualified import ignores the import list. */
502 impDecl : IMPORT modid impspec {$$=gc3(doubleton(
503 ap(M_IMPORT_Q,zpair($2,$2)),
504 ap(M_IMPORT_UNQ,zpair($2,$3))
506 | IMPORT modid ASMOD modid impspec
508 ap(M_IMPORT_Q,zpair($2,$4)),
509 ap(M_IMPORT_UNQ,zpair($2,$5))
511 | IMPORT QUALIFIED modid ASMOD modid impspec
513 ap(M_IMPORT_Q,zpair($3,$5))
515 | IMPORT QUALIFIED modid impspec
517 ap(M_IMPORT_Q,zpair($3,$3))
519 | IMPORT error {syntaxError("import declaration");}
521 impspec : /* empty */ {$$ = gc0(DOTDOT);}
522 | HIDING '(' imports ')' {$$ = gc4(ap(HIDDEN,$3));}
523 | '(' imports ')' {$$ = gc3($2);}
525 imports : /* empty */ {$$ = gc0(NIL);}
526 | ',' {$$ = gc1(NIL);}
527 | imports1 {$$ = $1;}
528 | imports1 ',' {$$ = gc2($1);}
530 imports1 : imports1 ',' import {$$ = gc3(cons($3,$1));}
531 | import {$$ = gc1(singleton($1));}
533 import : var {$$ = $1;}
535 | CONID '(' UPTO ')' {$$ = gc4(pair($1,DOTDOT));}
536 | CONID '(' names ')' {$$ = gc4(pair($1,$3));}
538 names : /* empty */ {$$ = gc0(NIL);}
539 | ',' {$$ = gc1(NIL);}
541 | names1 ',' {$$ = gc2($1);}
543 names1 : names1 ',' name {$$ = gc3(cons($3,$1));}
544 | name {$$ = gc1(singleton($1));}
546 name : var {$$ = $1;}
550 /*- Top-level declarations: -----------------------------------------------*/
552 topDecls : /* empty */ {$$=gc0(NIL);}
553 | topDecl ';' topDecls {$$=gc3(cons($1,$3));}
554 | decl ';' topDecls {$$=gc3(cons(ap(M_VALUE,$1),$3));}
555 | topDecl {$$=gc1(cons($1,NIL));}
556 | decl {$$=gc1(cons(ap(M_VALUE,$1),NIL));}
559 /*- Type declarations: ----------------------------------------------------*/
561 topDecl : TYPE tyLhs '=' type {$$=gc4(ap(M_TYCON,
564 | TYPE tyLhs '=' type IN invars
566 z4ble($3,$2,ap($4,$6),
568 | TYPE error {syntaxError("type definition");}
569 | DATA btype2 '=' constrs deriving
571 z4ble($3,checkTyLhs($2),
574 | DATA context IMPLIES tyLhs '=' constrs deriving
577 ap(qualify($2,rev($6)),$7),
579 | DATA btype2 {$$=gc2(ap(M_TYCON,
580 z4ble($1,checkTyLhs($2),
581 ap(NIL,NIL),DATATYPE)));}
582 | DATA context IMPLIES tyLhs {$$=gc4(ap(M_TYCON,
584 ap(qualify($2,NIL),NIL),
586 | DATA error {syntaxError("data definition");}
587 | TNEWTYPE btype2 '=' nconstr deriving
589 z4ble($3,checkTyLhs($2),
590 ap($4,$5),NEWTYPE)));}
591 | TNEWTYPE context IMPLIES tyLhs '=' nconstr deriving
594 ap(qualify($2,$6),$7),
596 | TNEWTYPE error {syntaxError("newtype definition");}
598 tyLhs : tyLhs varid {$$ = gc2(ap($1,$2));}
600 | error {syntaxError("type defn lhs");}
602 invars : invars ',' invar {$$ = gc3(cons($3,$1));}
603 | invar {$$ = gc1(cons($1,NIL));}
605 invar : var COCO topType {$$ = gc3(sigdecl($2,singleton($1),
609 constrs : constrs '|' pconstr {$$ = gc3(cons($3,$1));}
610 | pconstr {$$ = gc1(cons($1,NIL));}
612 pconstr : ALL varids '.' qconstr {$$ = gc4(ap(POLYTYPE,
616 qconstr : context IMPLIES constr {$$ = gc3(qualify($1,$3));}
619 constr : '!' btype conop bbtype {$$ = gc4(ap(ap($3,bang($2)),$4));}
620 | btype1 conop bbtype {$$ = gc3(ap(ap($2,$1),$3));}
621 | btype2 conop bbtype {$$ = gc3(ap(ap($2,$1),$3));}
622 | bpolyType conop bbtype {$$ = gc3(ap(ap($2,$1),$3));}
626 | con '{' fieldspecs '}' {$$ = gc4(ap(LABC,pair($1,rev($3))));}
627 | con '{' '}' {$$ = gc3(ap(LABC,pair($1,NIL)));}
628 | error {syntaxError("data type definition");}
630 btype3 : btype2 '!' atype {$$ = gc3(ap($1,bang($3)));}
631 | btype3 '!' atype {$$ = gc3(ap($1,bang($3)));}
632 | btype3 atype {$$ = gc2(ap($1,$2));}
634 btype4 : btype2 bpolyType {$$ = gc2(ap($1,$2));}
635 | btype3 bpolyType {$$ = gc2(ap($1,$2));}
636 | btype4 bpolyType {$$ = gc2(ap($1,$2));}
637 | btype4 atype {$$ = gc2(ap($1,$2));}
638 | btype4 '!' atype {$$ = gc3(ap($1,bang($3)));}
640 bbtype : '!' btype {$$ = gc2(bang($2));}
642 | bpolyType {$$ = $1;}
644 nconstr : pconstr {$$ = gc1(singleton($1));}
646 fieldspecs: fieldspecs ',' fieldspec {$$ = gc3(cons($3,$1));}
647 | fieldspec {$$ = gc1(cons($1,NIL));}
649 fieldspec : vars COCO polyType {$$ = gc3(pair(rev($1),$3));}
650 | vars COCO type {$$ = gc3(pair(rev($1),$3));}
651 | vars COCO '!' type {$$ = gc4(pair(rev($1),bang($4)));}
653 deriving : /* empty */ {$$ = gc0(NIL);}
654 | DERIVING qconid {$$ = gc2(singleton($2));}
655 | DERIVING '(' derivs0 ')' {$$ = gc4($3);}
657 derivs0 : /* empty */ {$$ = gc0(NIL);}
658 | derivs {$$ = gc1(rev($1));}
660 derivs : derivs ',' qconid {$$ = gc3(cons($3,$1));}
661 | qconid {$$ = gc1(singleton($1));}
664 /*- Processing definitions of primitives ----------------------------------*/
666 topDecl : FOREIGN IMPORT callconv DYNAMIC unsafe_flag var COCO type
667 {$$=gc8(ap(M_FOREIGN_IM,z5ble($1,$3,NIL,$6,$8)));}
668 | FOREIGN IMPORT callconv ext_loc ext_name unsafe_flag var COCO type
669 {$$=gc9(ap(M_FOREIGN_IM,z5ble($1,$3,pair($4,$5),$7,$9)));}
670 | FOREIGN EXPORT callconv DYNAMIC qvarid COCO type
671 {$$=gc7(ap(M_FOREIGN_EX,z5ble($1,$3,$4,$5,$7)));}
674 callconv : CCALL {$$ = gc1(textCcall);}
675 | STDKALL {$$ = gc1(textStdcall);}
676 | /* empty */ {$$ = gc0(NIL);}
678 ext_loc : STRINGLIT {$$ = $1;}
680 ext_name : STRINGLIT {$$ = $1;}
682 unsafe_flag: /* empty */ {$$ = gc0(NIL);}
683 | UNSAFE {$$ = gc1(NIL); /* ignored */ }
687 /*- Class declarations: ---------------------------------------------------*/
689 topDecl : TCLASS crule fds wherePart {$$=gc4(ap(M_CLASS,z4ble($1,$2,$4,$3)));}
690 | TINSTANCE irule wherePart {$$=gc3(ap(M_INST,ztriple($1,$2,$3)));}
691 | DEFAULT '(' dtypes ')' {$$=gc4(ap(M_DEFAULT,zpair($1,$3)));}
692 | TCLASS error {syntaxError("class declaration");}
693 | TINSTANCE error {syntaxError("instance declaration");}
694 | DEFAULT error {syntaxError("default declaration");}
696 crule : context IMPLIES btype2 {$$ = gc3(pair($1,checkPred($3)));}
697 | btype2 {$$ = gc1(pair(NIL,checkPred($1)));}
699 irule : context IMPLIES btype2 {$$ = gc3(pair($1,checkPred($3)));}
700 | btype2 {$$ = gc1(pair(NIL,checkPred($1)));}
702 dtypes : /* empty */ {$$ = gc0(NIL);}
703 | dtypes1 {$$ = gc1(rev($1));}
705 dtypes1 : dtypes1 ',' type {$$ = gc3(cons($3,$1));}
706 | type {$$ = gc1(cons($1,NIL));}
709 fds : /* empty */ {$$ = gc0(NIL);}
710 | '|' fds1 {h98DoesntSupport(row,"dependent parameters");
713 fds1 : fds1 ',' fd {$$ = gc3(cons($3,$1));}
714 | fd {$$ = gc1(cons($1,NIL));}
717 fd : varids0 ARROW varids0 {$$ = gc3(pair(rev($1),rev($3)));}
719 varids0 : /* empty */ {$$ = gc0(NIL);}
720 | varids0 varid {$$ = gc2(cons($2,$1));}
723 /*- Type expressions: -----------------------------------------------------*/
725 topType : ALL varids '.' topType0 {$$ = gc4(ap(POLYTYPE,
727 | topType0 {$$ = $1;}
729 topType0 : context IMPLIES topType1 {$$ = gc3(qualify($1,$3));}
730 | topType1 {$$ = $1;}
732 topType1 : bpolyType ARROW topType1 {$$ = gc3(fn($1,$3));}
733 | btype1 ARROW topType1 {$$ = gc3(fn($1,$3));}
734 | btype2 ARROW topType1 {$$ = gc3(fn($1,$3));}
737 polyType : ALL varids '.' sigType {$$ = gc4(ap(POLYTYPE,
739 | context IMPLIES type {$$ = gc3(qualify($1,$3));}
740 | bpolyType {$$ = $1;}
742 bpolyType : '(' polyType ')' {$$ = gc3($2);}
744 varids : varids varid {$$ = gc2(cons($2,$1));}
745 | varid {$$ = gc1(singleton($1));}
747 sigType : context IMPLIES type {$$ = gc3(qualify($1,$3));}
750 context : '(' ')' {$$ = gc2(NIL);}
751 | btype2 {$$ = gc1(singleton(checkPred($1)));}
752 | '(' btype2 ')' {$$ = gc3(singleton(checkPred($2)));}
753 | '(' btypes2 ')' {$$ = gc3(checkCtxt(rev($2)));}
755 | lacks {$$ = gc1(singleton($1));}
756 | '(' lacks1 ')' {$$ = gc3(checkCtxt(rev($2)));}
758 lacks : varid '\\' varid {
760 $$ = gc3(ap(mkExt(textOf($3)),$1));
762 noTREX("a type context");
765 | IPVARID COCO type {
767 $$ = gc3(pair(mkIParam($1),$3));
769 noIP("a type context");
773 lacks1 : btypes2 ',' lacks {$$ = gc3(cons($3,$1));}
774 | lacks1 ',' btype2 {$$ = gc3(cons($3,$1));}
775 | lacks1 ',' lacks {$$ = gc3(cons($3,$1));}
776 | btype2 ',' lacks {$$ = gc3(cons($3,cons($1,NIL)));}
777 | lacks {$$ = gc1(singleton($1));}
781 type : type1 {$$ = $1;}
784 type1 : btype1 {$$ = $1;}
785 | btype1 ARROW type {$$ = gc3(fn($1,$3));}
786 | btype2 ARROW type {$$ = gc3(fn($1,$3));}
787 | error {syntaxError("type expression");}
789 btype : btype1 {$$ = $1;}
792 btype1 : btype1 atype {$$ = gc2(ap($1,$2));}
795 btype2 : btype2 atype {$$ = gc2(ap($1,$2));}
798 atype : atype1 {$$ = $1;}
801 atype1 : varid {$$ = $1;}
802 | '(' ')' {$$ = gc2(typeUnit);}
803 | '(' ARROW ')' {$$ = gc3(typeArrow);}
804 | '(' type1 ')' {$$ = gc3($2);}
805 | '(' btype2 ')' {$$ = gc3($2);}
806 | '(' tupCommas ')' {$$ = gc3($2);}
807 | '(' btypes2 ')' {$$ = gc3(buildTuple($2));}
808 | '(' typeTuple ')' {$$ = gc3(buildTuple($2));}
811 $$ = gc3(revOnto($2,typeNoRow));
816 | '(' tfields '|' type ')' {
818 $$ = gc5(revOnto($2,$4));
823 | '[' type ']' {$$ = gc3(ap(typeList,$2));}
824 | '[' ']' {$$ = gc2(typeList);}
825 | '_' {h98DoesntSupport(row,"anonymous type variables");
826 $$ = gc1(inventVar());}
828 btypes2 : btypes2 ',' btype2 {$$ = gc3(cons($3,$1));}
829 | btype2 ',' btype2 {$$ = gc3(cons($3,cons($1,NIL)));}
831 typeTuple : type1 ',' type {$$ = gc3(cons($3,cons($1,NIL)));}
832 | btype2 ',' type1 {$$ = gc3(cons($3,cons($1,NIL)));}
833 | btypes2 ',' type1 {$$ = gc3(cons($3,$1));}
834 | typeTuple ',' type {$$ = gc3(cons($3,$1));}
837 tfields : tfields ',' tfield {$$ = gc3(cons($3,$1));}
838 | tfield {$$ = gc1(singleton($1));}
840 tfield : varid COCO type {h98DoesntSupport(row,"extensible records");
841 $$ = gc3(ap(mkExt(textOf($1)),$3));}
845 /*- Value declarations: ---------------------------------------------------*/
847 gendecl : INFIXN optDigit ops {$$ = gc3(fixdecl($1,$3,NON_ASS,$2));}
848 | INFIXN error {syntaxError("fixity decl");}
849 | INFIXL optDigit ops {$$ = gc3(fixdecl($1,$3,LEFT_ASS,$2));}
850 | INFIXL error {syntaxError("fixity decl");}
851 | INFIXR optDigit ops {$$ = gc3(fixdecl($1,$3,RIGHT_ASS,$2));}
852 | INFIXR error {syntaxError("fixity decl");}
853 | vars COCO topType {$$ = gc3(sigdecl($2,$1,$3));}
854 | vars COCO error {syntaxError("type signature");}
856 optDigit : NUMLIT {$$ = gc1(checkPrec($1));}
857 | /* empty */ {$$ = gc0(mkInt(DEF_PREC));}
859 ops : ops ',' op {$$ = gc3(cons($3,$1));}
860 | op {$$ = gc1(singleton($1));}
862 vars : vars ',' var {$$ = gc3(cons($3,$1));}
863 | var {$$ = gc1(singleton($1));}
865 decls : '{' decls0 end {$$ = gc3($2);}
866 | '{' decls1 end {$$ = gc3($2);}
868 decls0 : /* empty */ {$$ = gc0(NIL);}
869 | decls0 ';' {$$ = gc2($1);}
870 | decls1 ';' {$$ = gc2($1);}
872 decls1 : decls0 decl {$$ = gc2(cons($2,$1));}
874 decl : gendecl {$$ = $1;}
875 | funlhs rhs {$$ = gc2(ap(FUNBIND,pair($1,$2)));}
876 | funlhs COCO type rhs {$$ = gc4(ap(FUNBIND,
879 | pat0 rhs {$$ = gc2(ap(PATBIND,pair($1,$2)));}
881 funlhs : funlhs0 {$$ = $1;}
885 funlhs0 : pat10_vI varop pat0 {$$ = gc3(ap2($2,$1,$3));}
886 | infixPat varop pat0 {$$ = gc3(ap2($2,$1,$3));}
887 | NUMLIT varop pat0 {$$ = gc3(ap2($2,$1,$3));}
888 | var varop_pl pat0 {$$ = gc3(ap2($2,$1,$3));}
889 | var '+' pat0_INT {$$ = gc3(ap2(varPlus,$1,$3));}
891 funlhs1 : '(' funlhs0 ')' apat {$$ = gc4(ap($2,$4));}
892 | '(' funlhs1 ')' apat {$$ = gc4(ap($2,$4));}
893 | '(' npk ')' apat {$$ = gc4(ap($2,$4));}
894 | var apat {$$ = gc2(ap($1,$2));}
895 | funlhs1 apat {$$ = gc2(ap($1,$2));}
897 rhs : rhs1 wherePart {$$ = gc2(letrec($2,$1));}
898 | error {syntaxError("declaration");}
900 rhs1 : '=' exp {$$ = gc2(pair($1,$2));}
901 | gdrhs {$$ = gc1(grded(rev($1)));}
903 gdrhs : gdrhs gddef {$$ = gc2(cons($2,$1));}
904 | gddef {$$ = gc1(singleton($1));}
906 gddef : '|' exp0 '=' exp {$$ = gc4(pair($3,pair($2,$4)));}
908 wherePart : /* empty */ {$$ = gc0(NIL);}
909 | WHERE decls {$$ = gc2($2);}
912 /*- Patterns: -------------------------------------------------------------*/
917 pat_npk : pat0 COCO type {$$ = gc3(ap(ESIGN,pair($1,$3)));}
920 npk : var '+' NUMLIT {$$ = gc3(ap2(varPlus,$1,$3));}
922 pat0 : var {$$ = $1;}
926 pat0_INT : var {$$ = $1;}
929 pat0_vI : pat10_vI {$$ = $1;}
930 | infixPat {$$ = gc1(ap(INFIX,$1));}
932 infixPat : '-' pat10 {$$ = gc2(ap(NEG,only($2)));}
933 | '-' error {syntaxError("pattern");}
934 | var qconop pat10 {$$ = gc3(ap(ap($2,only($1)),$3));}
935 | var qconop '-' pat10 {$$ = gc4(ap(NEG,ap2($2,only($1),$4)));}
936 | NUMLIT qconop pat10 {$$ = gc3(ap(ap($2,only($1)),$3));}
937 | NUMLIT qconop '-' pat10 {$$ = gc4(ap(NEG,ap2($2,only($1),$4)));}
938 | pat10_vI qconop pat10 {$$ = gc3(ap(ap($2,only($1)),$3));}
939 | pat10_vI qconop '-' pat10 {$$ = gc4(ap(NEG,ap2($2,only($1),$4)));}
940 | infixPat qconop pat10 {$$ = gc3(ap(ap($2,$1),$3));}
941 | infixPat qconop '-' pat10 {$$ = gc4(ap(NEG,ap(ap($2,$1),$4)));}
943 pat10 : fpat {$$ = $1;}
946 pat10_vI : fpat {$$ = $1;}
949 fpat : fpat apat {$$ = gc2(ap($1,$2));}
950 | gcon apat {$$ = gc2(ap($1,$2));}
952 apat : NUMLIT {$$ = $1;}
956 apat_vI : var '@' apat {$$ = gc3(ap(ASPAT,pair($1,$3)));}
958 | qcon '{' patbinds '}' {$$ = gc4(ap(CONFLDS,pair($1,$3)));}
960 | STRINGLIT {$$ = $1;}
961 | '_' {$$ = gc1(WILDCARD);}
962 | '(' pat_npk ')' {$$ = gc3($2);}
963 | '(' npk ')' {$$ = gc3($2);}
964 | '(' pats2 ')' {$$ = gc3(buildTuple($2));}
965 | '[' pats1 ']' {$$ = gc3(ap(FINLIST,rev($2)));}
966 | '~' apat {$$ = gc2(ap(LAZYPAT,$2));}
968 | '(' patfields ')' {
970 $$ = gc3(revOnto($2,nameNoRec));
975 | '(' patfields '|' pat ')' {$$ = gc5(revOnto($2,$4));}
978 pats2 : pats2 ',' pat {$$ = gc3(cons($3,$1));}
979 | pat ',' pat {$$ = gc3(cons($3,singleton($1)));}
981 pats1 : pats1 ',' pat {$$ = gc3(cons($3,$1));}
982 | pat {$$ = gc1(singleton($1));}
984 patbinds : /* empty */ {$$ = gc0(NIL);}
985 | patbinds1 {$$ = gc1(rev($1));}
987 patbinds1 : patbinds1 ',' patbind {$$ = gc3(cons($3,$1));}
988 | patbind {$$ = gc1(singleton($1));}
990 patbind : qvar '=' pat {$$ = gc3(pair($1,$3));}
994 patfields : patfields ',' patfield {$$ = gc3(cons($3,$1));}
995 | patfield {$$ = gc1(singleton($1));}
997 patfield : varid '=' pat {
999 $$ = gc3(ap(mkExt(textOf($1)),$3));
1001 noTREX("a pattern");
1007 /*- Expressions: ----------------------------------------------------------*/
1009 exp : exp_err {$$ = $1;}
1010 | error {syntaxError("expression");}
1012 exp_err : exp0a COCO sigType {$$ = gc3(ap(ESIGN,pair($1,$3)));}
1013 | exp0a WITH dbinds {
1015 $$ = gc3(ap(WITHEXP,pair($1,$3)));
1017 noIP("an expression");
1022 exp0 : exp0a {$$ = $1;}
1025 exp0a : infixExpa {$$ = gc1(ap(INFIX,$1));}
1028 exp0b : infixExpb {$$ = gc1(ap(INFIX,$1));}
1031 infixExpa : infixExpa qop '-' exp10a {$$ = gc4(ap(NEG,ap(ap($2,$1),$4)));}
1032 | infixExpa qop exp10a {$$ = gc3(ap(ap($2,$1),$3));}
1033 | '-' exp10a {$$ = gc2(ap(NEG,only($2)));}
1034 | exp10a qop '-' exp10a {$$ = gc4(ap(NEG,
1035 ap(ap($2,only($1)),$4)));}
1036 | exp10a qop exp10a {$$ = gc3(ap(ap($2,only($1)),$3));}
1038 infixExpb : infixExpa qop '-' exp10b {$$ = gc4(ap(NEG,ap(ap($2,$1),$4)));}
1039 | infixExpa qop exp10b {$$ = gc3(ap(ap($2,$1),$3));}
1040 | '-' exp10b {$$ = gc2(ap(NEG,only($2)));}
1041 | exp10a qop '-' exp10b {$$ = gc4(ap(NEG,
1042 ap(ap($2,only($1)),$4)));}
1043 | exp10a qop exp10b {$$ = gc3(ap(ap($2,only($1)),$3));}
1045 exp10a : CASEXP exp OF '{' alts end {$$ = gc6(ap(CASE,pair($2,rev($5))));}
1046 | DO '{' stmts end {$$ = gc4(ap(DOCOMP,checkDo($3)));}
1047 | MDO '{' stmts end {$$ = gc4(ap(DOCOMP,checkDo($3)));}
1050 exp10b : '\\' pats ARROW exp {$$ = gc4(ap(LAMBDA,
1053 | LET decls IN exp {$$ = gc4(letrec($2,$4));}
1054 | IF exp THEN exp ELSE exp {$$ = gc6(ap(COND,triple($2,$4,$6)));}
1055 | DLET dbinds IN exp {
1057 $$ = gc4(ap(WITHEXP,pair($4,$2)));
1059 noIP("an expression");
1063 pats : pats apat {$$ = gc2(cons($2,$1));}
1064 | apat {$$ = gc1(cons($1,NIL));}
1066 appExp : appExp aexp {$$ = gc2(ap($1,$2));}
1069 aexp : qvar {$$ = $1;}
1070 | qvar '@' aexp {$$ = gc3(ap(ASPAT,pair($1,$3)));}
1071 | '~' aexp {$$ = gc2(ap(LAZYPAT,$2));}
1072 | IPVARID {$$ = $1;}
1073 | '_' {$$ = gc1(WILDCARD);}
1075 | qcon '{' fbinds '}' {$$ = gc4(ap(CONFLDS,pair($1,$3)));}
1076 | aexp '{' fbinds '}' {$$ = gc4(ap(UPDFLDS,
1077 triple($1,NIL,$3)));}
1079 | CHARLIT {$$ = $1;}
1080 | STRINGLIT {$$ = $1;}
1082 | '(' exp ')' {$$ = gc3($2);}
1083 | '(' exps2 ')' {$$ = gc3(buildTuple($2));}
1087 $$ = gc3(revOnto($2,nameNoRec));
1092 | '(' vfields '|' exp ')' {$$ = gc5(revOnto($2,$4));}
1093 | RECSELID {$$ = $1;}
1095 | '[' list ']' {$$ = gc3($2);}
1096 | '(' exp10a qop ')' {$$ = gc4(ap($3,$2));}
1097 | '(' qvarop_mi exp0 ')' {$$ = gc4(ap(ap(nameFlip,$2),$3));}
1098 | '(' qconop exp0 ')' {$$ = gc4(ap(ap(nameFlip,$2),$3));}
1100 exps2 : exps2 ',' exp {$$ = gc3(cons($3,$1));}
1101 | exp ',' exp {$$ = gc3(cons($3,cons($1,NIL)));}
1104 vfields : vfields ',' vfield {$$ = gc3(cons($3,$1));}
1105 | vfield {$$ = gc1(singleton($1));}
1107 vfield : varid '=' exp {
1109 $$ = gc3(ap(mkExt(textOf($1)),$3));
1111 noTREX("an expression");
1116 alts : alts1 {$$ = $1;}
1117 | alts1 ';' {$$ = gc2($1);}
1119 alts1 : alts1 ';' alt {$$ = gc3(cons($3,$1));}
1120 | alt {$$ = gc1(cons($1,NIL));}
1122 alt : pat altRhs wherePart {$$ = gc3(pair($1,letrec($3,$2)));}
1124 altRhs : guardAlts {$$ = gc1(grded(rev($1)));}
1125 | ARROW exp {$$ = gc2(pair($1,$2));}
1126 | error {syntaxError("case expression");}
1128 guardAlts : guardAlts guardAlt {$$ = gc2(cons($2,$1));}
1129 | guardAlt {$$ = gc1(cons($1,NIL));}
1131 guardAlt : '|' exp0 ARROW exp {$$ = gc4(pair($3,pair($2,$4)));}
1133 stmts : stmts1 ';' {$$ = gc2($1);}
1136 stmts1 : stmts1 ';' stmt {$$ = gc3(cons($3,$1));}
1137 | stmt {$$ = gc1(cons($1,NIL));}
1139 stmt : exp_err FROM exp {$$ = gc3(ap(FROMQUAL,pair($1,$3)));}
1140 | LET decls {$$ = gc2(ap(QWHERE,$2));}
1141 /* | IF exp {$$ = gc2(ap(BOOLQUAL,$2));}*/
1142 | exp_err {$$ = gc1(ap(DOQUAL,$1));}
1144 fbinds : /* empty */ {$$ = gc0(NIL);}
1145 | fbinds1 {$$ = gc1(rev($1));}
1147 fbinds1 : fbinds1 ',' fbind {$$ = gc3(cons($3,$1));}
1148 | fbind {$$ = gc1(singleton($1));}
1150 fbind : var {$$ = $1;}
1151 | qvar '=' exp {$$ = gc3(pair($1,$3));}
1154 dbinds : '{' dbs0 end {$$ = gc3($2);}
1155 | '{' dbs1 end {$$ = gc3($2);}
1157 dbs0 : /* empty */ {$$ = gc0(NIL);}
1158 | dbs0 ';' {$$ = gc2($1);}
1159 | dbs1 ';' {$$ = gc2($1);}
1161 dbs1 : dbs0 dbind {$$ = gc2(cons($2,$1));}
1163 dbind : IPVARID '=' exp {$$ = gc3(pair($1,$3));}
1166 /*- List Expressions: -------------------------------------------------------*/
1168 list : exp {$$ = gc1(ap(FINLIST,cons($1,NIL)));}
1169 | exps2 {$$ = gc1(ap(FINLIST,rev($1)));}
1170 | exp '|' quals {$$ = gc3(ap(COMP,pair($1,rev($3))));}
1171 | exp UPTO exp {$$ = gc3(ap(ap(nameFromTo,$1),$3));}
1172 | exp ',' exp UPTO {$$ = gc4(ap(ap(nameFromThen,$1),$3));}
1173 | exp UPTO {$$ = gc2(ap(nameFrom,$1));}
1174 | exp ',' exp UPTO exp {$$ = gc5(ap(ap(ap(nameFromThenTo,
1177 quals : quals ',' qual {$$ = gc3(cons($3,$1));}
1178 | qual {$$ = gc1(cons($1,NIL));}
1180 qual : exp FROM exp {$$ = gc3(ap(FROMQUAL,pair($1,$3)));}
1181 | exp {$$ = gc1(ap(BOOLQUAL,$1));}
1182 | LET decls {$$ = gc2(ap(QWHERE,$2));}
1185 /*- Identifiers and symbols: ----------------------------------------------*/
1187 gcon : qcon {$$ = $1;}
1188 | '(' ')' {$$ = gc2(nameUnit);}
1189 | '[' ']' {$$ = gc2(nameNil);}
1190 | '(' tupCommas ')' {$$ = gc3($2);}
1192 tupCommas : tupCommas ',' {$$ = gc2(mkTuple(tupleOf($1)+1));}
1193 | ',' {$$ = gc1(mkTuple(2));}
1195 varid : VARID {$$ = $1;}
1196 | HIDING {$$ = gc1(varHiding);}
1197 | QUALIFIED {$$ = gc1(varQualified);}
1198 | ASMOD {$$ = gc1(varAsMod);}
1200 qconid : QCONID {$$ = $1;}
1203 var : varid {$$ = $1;}
1204 | '(' VAROP ')' {$$ = gc3($2);}
1205 | '(' '+' ')' {$$ = gc3(varPlus);}
1206 | '(' '-' ')' {$$ = gc3(varMinus);}
1207 | '(' '!' ')' {$$ = gc3(varBang);}
1208 | '(' '.' ')' {$$ = gc3(varDot);}
1210 qvar : QVARID {$$ = $1;}
1211 | '(' QVAROP ')' {$$ = gc3($2);}
1214 con : CONID {$$ = $1;}
1215 | '(' CONOP ')' {$$ = gc3($2);}
1217 qcon : QCONID {$$ = $1;}
1218 | '(' QCONOP ')' {$$ = gc3($2);}
1221 varop : '+' {$$ = gc1(varPlus);}
1222 | '-' {$$ = gc1(varMinus);}
1223 | varop_mipl {$$ = $1;}
1225 varop_mi : '+' {$$ = gc1(varPlus);}
1226 | varop_mipl {$$ = $1;}
1228 varop_pl : '-' {$$ = gc1(varMinus);}
1229 | varop_mipl {$$ = $1;}
1231 varop_mipl: VAROP {$$ = $1;}
1232 | '`' varid '`' {$$ = gc3($2);}
1233 | '!' {$$ = gc1(varBang);}
1234 | '.' {$$ = gc1(varDot);}
1236 qvarop : '-' {$$ = gc1(varMinus);}
1237 | qvarop_mi {$$ = $1;}
1239 qvarop_mi : QVAROP {$$ = $1;}
1240 | '`' QVARID '`' {$$ = gc3($2);}
1241 | varop_mi {$$ = $1;}
1244 conop : CONOP {$$ = $1;}
1245 | '`' CONID '`' {$$ = gc3($2);}
1247 qconop : QCONOP {$$ = $1;}
1248 | '`' QCONID '`' {$$ = gc3($2);}
1251 op : varop {$$ = $1;}
1254 qop : qvarop {$$ = $1;}
1258 /*- Stuff from STG hugs ---------------------------------------------------*/
1260 qvarid : varid1 {$$ = gc1($1);}
1261 | QVARID {$$ = gc1($1);}
1263 varid1 : VARID {$$ = gc1($1);}
1264 | HIDING {$$ = gc1(varHiding);}
1265 | QUALIFIED {$$ = gc1(varQualified);}
1266 | ASMOD {$$ = gc1(varAsMod);}
1269 /*- Tricks to force insertion of leading and closing braces ---------------*/
1271 begin : error {yyerrok;
1272 if (offsideON) goOffside(startColumn);}
1275 end : '}' {$$ = $1;}
1277 if (offsideON && canUnOffside()) {
1279 /* insert extra token on stack*/
1281 pushed(0) = pushed(1);
1282 pushed(1) = mkInt(column);
1285 syntaxError("definition");
1289 /*-------------------------------------------------------------------------*/
1293 static Cell local gcShadow(n,e) /* keep parsed fragments on stack */
1296 /* If a look ahead token is held then the required stack transformation
1299 * x1 | ... | xn | la ===> e | la
1302 * Otherwise, the transformation is:
1304 * x1 | ... | xn ===> e
1308 pushed(n-1) = top();
1317 static Void local syntaxError(s) /* report on syntax error */
1319 ERRMSG(row) "Syntax error in %s (unexpected %s)", s, unexpected()
1323 static String local unexpected() { /* find name for unexpected token */
1324 static char buffer[100];
1325 static char *fmt = "%s \"%s\"";
1326 static char *kwd = "keyword";
1329 case 0 : return "end of input";
1331 #define keyword(kw) sprintf(buffer,fmt,kwd,kw); return buffer;
1332 case INFIXL : keyword("infixl");
1333 case INFIXR : keyword("infixr");
1334 case INFIXN : keyword("infix");
1335 case FOREIGN : keyword("foreign");
1336 case UNSAFE : keyword("unsafe");
1337 case TINSTANCE : keyword("instance");
1338 case TCLASS : keyword("class");
1339 case CASEXP : keyword("case");
1340 case OF : keyword("of");
1341 case IF : keyword("if");
1342 case THEN : keyword("then");
1343 case ELSE : keyword("else");
1344 case WHERE : keyword("where");
1345 case TYPE : keyword("type");
1346 case DATA : keyword("data");
1347 case TNEWTYPE : keyword("newtype");
1348 case LET : keyword("let");
1349 case IN : keyword("in");
1350 case DERIVING : keyword("deriving");
1351 case DEFAULT : keyword("default");
1352 case IMPORT : keyword("import");
1353 case TMODULE : keyword("module");
1354 /* AJG: Hugs98/Classic use the keyword forall
1355 rather than __forall.
1356 Agree on one or the other
1358 case ALL : keyword("__forall");
1360 case DLET : keyword("dlet");
1361 case WITH : keyword("with");
1365 case ARROW : return "`->'";
1366 case '=' : return "`='";
1367 case COCO : return "`::'";
1368 case '-' : return "`-'";
1369 case '!' : return "`!'";
1370 case ',' : return "comma";
1371 case '@' : return "`@'";
1372 case '(' : return "`('";
1373 case ')' : return "`)'";
1374 case '{' : return "`{', possibly due to bad layout";
1375 case '}' : return "`}', possibly due to bad layout";
1376 case '_' : return "`_'";
1377 case '|' : return "`|'";
1378 case '.' : return "`.'";
1379 case ';' : return "`;', possibly due to bad layout";
1380 case UPTO : return "`..'";
1381 case '[' : return "`['";
1382 case ']' : return "`]'";
1383 case FROM : return "`<-'";
1384 case '\\' : return "backslash (lambda)";
1385 case '~' : return "tilde";
1386 case '`' : return "backquote";
1388 case RECSELID : sprintf(buffer,"selector \"#%s\"",
1389 textToStr(extText(snd(yylval))));
1393 case IPVARID : sprintf(buffer,"implicit parameter \"?%s\"",
1394 textToStr(textOf(yylval)));
1400 case CONID : sprintf(buffer,"symbol \"%s\"",
1401 textToStr(textOf(yylval)));
1406 case QCONID : sprintf(buffer,"symbol \"%s\"",
1407 identToStr(yylval));
1409 case HIDING : return "symbol \"hiding\"";
1410 case QUALIFIED : return "symbol \"qualified\"";
1411 case ASMOD : return "symbol \"as\"";
1412 case NUMLIT : return "numeric literal";
1413 case CHARLIT : return "character literal";
1414 case STRINGLIT : return "string literal";
1415 case IMPLIES : return "`=>'";
1416 default : return "token";
1420 static Cell local checkPrec(p) /* Check for valid precedence value*/
1422 if (!isInt(p) || intOf(p)<MIN_PREC || intOf(p)>MAX_PREC) {
1423 ERRMSG(row) "Precedence value must be an integer in the range [%d..%d]",
1430 static Cell local buildTuple(tup) /* build tuple (x1,...,xn) from */
1431 List tup; { /* list [xn,...,x1] */
1437 x = fst(t); /* / \ / \ */
1438 fst(t) = snd(t); /* xn . . xn */
1439 snd(t) = x; /* . ===> . */
1441 t = fun(x); /* . . */
1443 } while (nonNull(t)); /* x1 NIL (n) x1 */
1444 fst(x) = mkTuple(n);
1448 static List local checkCtxt(con) /* validate context */
1450 mapOver(checkPred, con);
1454 static Cell local checkPred(c) /* check that type expr is a valid */
1455 Cell c; { /* constraint */
1456 Cell cn = getHead(c);
1458 if (isExt(cn) && argCount==1)
1465 if (!isQCon(cn) /*|| argCount==0*/)
1466 syntaxError("class expression");
1470 static Pair local checkDo(dqs) /* convert reversed list of dquals */
1471 List dqs; { /* to an (expr,quals) pair */
1472 if (isNull(dqs) || whatIs(hd(dqs))!=DOQUAL) {
1473 ERRMSG(row) "Last generator in do {...} must be an expression"
1476 fst(dqs) = snd(fst(dqs)); /* put expression in fst of pair */
1477 snd(dqs) = rev(snd(dqs)); /* & reversed list of quals in snd */
1481 static Cell local checkTyLhs(c) /* check that lhs is of the form */
1482 Cell c; { /* T a1 ... a */
1484 while (isAp(tlhs) && whatIs(arg(tlhs))==VARIDCELL) {
1487 if (whatIs(tlhs)!=CONIDCELL) {
1488 ERRMSG(row) "Illegal left hand side in datatype definition"
1496 static Void local noTREX(where)
1498 ERRMSG(row) "Attempt to use TREX records while parsing %s.\n", where ETHEN
1499 ERRTEXT "(TREX is disabled in this build of Hugs)"
1504 static Void local noIP(where)
1506 ERRMSG(row) "Attempt to use Implicit Parameters while parsing %s.\n", where ETHEN
1507 ERRTEXT "(Implicit Parameters are disabled in this build of Hugs)"
1512 /*-------------------------------------------------------------------------*/