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 * Hugs 98 is Copyright (c) Mark P Jones, Alastair Reid and the Yale
9 * Haskell Group 1994-99, and is distributed as Open Source software
10 * under the Artistic License; see the file "Artistic" that is included
11 * in the distribution for details.
13 * $RCSfile: parser.y,v $
15 * $Date: 1999/07/06 15:24:40 $
16 * ------------------------------------------------------------------------*/
22 #define defTycon(n,l,lhs,rhs,w) tyconDefn(intOf(l),lhs,rhs,w); sp-=n
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 bang(t) ap(BANG,t)
28 #define only(t) ap(ONLY,t)
29 #define letrec(bs,e) (nonNull(bs) ? ap(LETREC,pair(bs,e)) : e)
30 #define qualify(ps,t) (nonNull(ps) ? ap(QUAL,pair(ps,t)) : t)
31 #define exportSelf() singleton(ap(MODULEENT, \
32 mkCon(module(currentModule).text)))
33 #define yyerror(s) /* errors handled elsewhere */
36 static Cell local gcShadow Args((Int,Cell));
37 static Void local syntaxError Args((String));
38 static String local unexpected Args((Void));
39 static Cell local checkPrec Args((Cell));
40 static Void local fixDefn Args((Syntax,Cell,Cell,List));
41 static Cell local buildTuple Args((List));
42 static List local checkContext Args((List));
43 static Cell local checkPred Args((Cell));
44 static Pair local checkDo Args((List));
45 static Cell local checkTyLhs Args((Cell));
47 static Void local noTREX Args((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)
77 %token CASEXP OF DATA TYPE IF
78 %token THEN ELSE WHERE LET IN
79 %token INFIXN INFIXL INFIXR FOREIGN TNEWTYPE
80 %token DEFAULT DERIVING DO TCLASS TINSTANCE
81 %token REPEAT ALL NUMLIT CHARLIT STRINGLIT
82 %token VAROP VARID CONOP CONID
83 %token QVAROP QVARID QCONOP QCONID
87 %token COCO '=' UPTO '@' '\\'
88 %token '|' '-' FROM ARROW '~'
89 %token '!' IMPLIES '(' ',' ')'
90 %token '[' ';' ']' '`' '.'
91 %token TMODULE IMPORT HIDING QUALIFIED ASMOD
92 %token EXPORT INTERFACE REQUIRES UNSAFE INSTIMPORT
95 /*- Top level script/module structure -------------------------------------*/
97 start : EXPR exp wherePart {inputExpr = letrec($3,$2); sp-=2;}
98 | SCRIPT topModule {valDefns = $2; sp-=1;}
99 | INTERFACE iface {sp-=1;}
100 | error {syntaxError("input");}
104 /*- GHC interface file parsing: -------------------------------------------*/
106 /* Reading in an interface file is surprisingly like reading
107 * a normal Haskell module: we read in a bunch of declarations,
108 * construct symbol table entries, etc. The "only" differences
109 * are that there's no syntactic sugar to deal with and we don't
110 * have to read in expressions.
113 /*- Top-level interface files -----------------------------*/
114 iface : INTERFACE ifName NUMLIT checkVersion WHERE ifDecls
116 | INTERFACE error {syntaxError("interface file");}
118 ifDecls: {$$=gc0(NIL);}
119 | ifDecl ';' ifDecls {$$=gc3(cons($1,$3));}
122 : VARID { $$=gc1($1); }
123 | CONID { $$=gc1($1); }
125 opt_bang : '!' {$$=gc1(NIL);}
128 ifName : CONID {openGHCIface(textOf($1));
131 : NUMLIT {$$ = gc1(NIL); }
134 : IMPORT CONID opt_bang NUMLIT COCO version_list_junk
135 { addGHCImports(intOf($4),textOf($2),
140 | INSTIMPORT CONID {$$=gc2(NIL);}
142 | EXPORT CONID ifEntities { addGHCExports($2,$3);
145 | NUMLIT INFIXL optDigit varid_or_conid
146 {$$ = gc4(fixdecl($2,singleton($4),
148 | NUMLIT INFIXR optDigit varid_or_conid
149 {$$ = gc4(fixdecl($2,singleton($4),
151 | NUMLIT INFIXN optDigit varid_or_conid
152 {$$ = gc4(fixdecl($2,singleton($4),
155 | TINSTANCE ifCtxInst ifInstHd '=' ifVar
156 { addGHCInstance(intOf($1),$2,$3,
159 | NUMLIT TYPE ifCon ifKindedTyvarL '=' ifType
160 { addGHCSynonym(intOf($2),$3,$4,$6);
163 | NUMLIT DATA ifCtxDecl ifConData ifKindedTyvarL ifConstrs
164 { addGHCDataDecl(intOf($2),
168 | NUMLIT TNEWTYPE ifCtxDecl ifConData ifKindedTyvarL ifNewTypeConstr
169 { addGHCNewType(intOf($2),
172 | NUMLIT TCLASS ifCtxDecl ifCon ifTyvar ifCmeths
173 { addGHCClass(intOf($2),$3,$4,$5,$6);
175 | NUMLIT ifVar COCO ifType
176 { addGHCVar(intOf($3),textOf($2),$4);
178 | error { syntaxError(
179 "interface declaration"); }
183 /*- Interface variable and constructor ids ----------------*/
184 ifTyvar : VARID {$$ = $1;}
186 ifVar : VARID {$$ = gc1($1);}
188 ifCon : CONID {$$ = gc1($1);}
190 ifQCon : CONID {$$ = gc1($1);}
191 | QCONID {$$ = gc1($1);}
193 ifConData : ifCon {$$ = gc1($1);}
194 | '(' ')' {$$ = gc2(typeUnit);}
195 | '[' ']' {$$ = gc2(typeList);}
196 | '(' ARROW ')' {$$ = gc3(typeArrow);}
198 ifTCName : CONID { $$ = gc1($1); }
199 | CONOP { $$ = gc1($1); }
200 | '(' ARROW ')' { $$ = gc3(typeArrow); }
201 | '[' ']' { $$ = gc1(typeList); }
203 ifQTCName : ifTCName { $$ = gc1($1); }
204 | QCONID { $$ = gc1($1); }
205 | QCONOP { $$ = gc1($1); }
209 /*- Interface contexts ------------------------------------*/
210 ifCtxInst /* __forall [a b] {M.C1 a, M.C2 b} => */
211 /* :: [(QConId, VarId)] */
212 : ALL ifForall ifCtxDecl {$$=gc3($3);}
213 | ALL ifForall IMPLIES {$$=gc3(NIL);}
216 ifInstHd /* { Class aType } :: (ConId, Type) */
217 : '{' ifCon ifAType '}' {$$=gc4(pair($2,$3));}
220 ifCtxDecl /* {M.C1 a, C2 b} :: [(QConId, VarId)] */
222 | '{' ifCtxDeclL '}' IMPLIES { $$ = gc4($2); }
224 ifCtxDeclT /* {M.C1 a, C2 b} :: [(QConId, VarId)] */
226 | '{' ifCtxDeclL '}' { $$ = gc3($2); }
228 ifCtxDeclL /* M.C1 a, C2 b :: [(QConId, VarId)] */
229 : ifCtxDeclLE ',' ifCtxDeclL {$$=gc3(cons($1,$3));}
230 | ifCtxDeclLE {$$=gc1(cons($1,NIL));}
233 ifCtxDeclLE /* M.C1 a :: (QConId,VarId) */
234 : ifQCon ifTyvar {$$=gc2(pair($1,$2));}
238 /*- Interface data declarations - constructor lists -------*/
239 ifConstrs /* = Con1 | ... | ConN :: [(ConId,[(Type,Text)],NIL)] */
241 | '=' ifConstrL {$$ = gc2($2);}
243 ifConstrL /* [(ConId,[(Type,Text)],NIL)] */
244 : ifConstr {$$ = gc1(singleton($1));}
245 | ifConstr '|' ifConstrL {$$ = gc3(cons($1,$3));}
247 ifConstr /* (ConId,[(Type,Text)],NIL) */
248 : ifConData ifDataAnonFieldL {$$ = gc2(triple($1,$2,NIL));}
249 | ifConData '{' ifDataNamedFieldL '}'
250 {$$ = gc4(triple($1,$3,NIL));}
252 ifDataAnonFieldL /* [(Type,Text)] */
254 | ifDataAnonField ifDataAnonFieldL
255 {$$=gc2(cons($1,$2));}
257 ifDataNamedFieldL /* [(Type,Text)] */
259 | ifDataNamedField {$$=gc1(cons($1,NIL));}
260 | ifDataNamedField ',' ifDataNamedFieldL
261 {$$=gc3(cons($1,$3));}
263 ifDataAnonField /* (Type,Text) */
264 : ifAType {$$=gc1(pair($1,NIL));}
266 ifDataNamedField /* (Type,Text) */
267 : VARID COCO ifAType {$$=gc3(pair($3,$1));}
271 /*- Interface class declarations - methods ----------------*/
272 ifCmeths /* [(VarId,Type)] */
274 | WHERE '{' ifCmethL '}' { $$ = gc4($3); }
276 ifCmethL /* [(VarId,Type)] */
277 : ifCmeth { $$ = gc1(singleton($1)); }
278 | ifCmeth ';' ifCmethL { $$ = gc3(cons($1,$3)); }
280 ifCmeth /* (VarId,Type) */
281 : ifVar COCO ifType { $$ = gc3(pair($1,$3)); }
282 | ifVar '=' COCO ifType { $$ = gc4(pair($1,$4)); }
283 /* has default method */
287 /*- Interface newtype declararions ------------------------*/
288 ifNewTypeConstr /* (ConId,Type) */
289 : '=' ifCon ifAType { $$ = gc3(pair($2,$3)); }
293 /*- Interface type expressions ----------------------------*/
294 ifType : ALL ifForall ifCtxDeclT IMPLIES ifType
297 $$=gc5(pair(QUAL,pair($3,$5)));
299 | ifBType ARROW ifType { $$ = gc3(fn($1,$3)); }
300 | ifBType { $$ = gc1($1); }
302 ifForall /* [(VarId,Kind)] */
303 : '[' ifKindedTyvarL ']' { $$ = gc3($2); }
305 ifTypes2 : ifType ',' ifType { $$ = gc3(doubleton($1,$3)); }
306 | ifType ',' ifTypes2 { $$ = gc3(cons($1,$3)); }
308 ifBType : ifAType { $$ = gc1($1); }
309 | ifBType ifAType { $$ = gc2(ap($1,$2)); }
311 ifAType : ifQTCName { $$ = gc1($1); }
312 | ifTyvar { $$ = gc1($1); }
313 | '(' ')' { $$ = gc2(typeUnit); }
314 | '(' ifTypes2 ')' { $$ = gc3(buildTuple($2)); }
315 | '[' ifType ']' { $$ = gc3(ap(typeList,$2));}
316 | '{' ifQTCName ifATypes '}' { $$ = gc4(ap(DICTAP,
318 | '(' ifType ')' { $$ = gc3($2); }
320 ifATypes : { $$ = gc0(NIL); }
321 | ifAType ifATypes { $$ = gc2(cons($1,$2)); }
325 /*- Interface kinds ---------------------------------------*/
326 ifKindedTyvarL /* [(VarId,Kind)] */
328 | ifKindedTyvar ifKindedTyvarL { $$ = gc2(cons($1,$2)); }
330 ifKindedTyvar /* (VarId,Kind) */
331 : ifTyvar { $$ = gc1(pair($1,STAR)); }
332 | ifTyvar COCO ifAKind { $$ = gc3(pair($1,$3)); }
334 ifKind : ifAKind { $$ = gc1($1); }
335 | ifAKind ARROW ifKind { $$ = gc3(fn($1,$3)); }
337 ifAKind : VAROP { $$ = gc1(STAR); }
339 | '(' ifKind ')' { $$ = gc3($2); }
343 /*- Interface version/export/import stuff -----------------*/
346 | ifEntity ifEntities { $$ = gc2(cons($1,$2)); }
349 : ifEntityOcc {$$=gc1($1);}
350 | ifEntityOcc ifStuffInside {$$=gc2(pair($1,$2));}
353 : ifVar { $$ = gc1($1); }
354 | ifCon { $$ = gc1($1); }
355 | ARROW { $$ = gc1(typeArrow); }
356 | '(' ARROW ')' { $$ = gc3(typeArrow); }
357 /* why allow both? */
360 : '{' ifValOccs '}' { $$ = gc3($2); }
364 | ifVar ifValOccs { $$ = gc2(cons($1,$2)); }
365 | ifCon ifValOccs { $$ = gc2(cons($1,$2)); }
369 | VARID NUMLIT version_list_junk {$$=gc3(cons($1,$3));}
370 | CONID NUMLIT version_list_junk {$$=gc3(cons($1,$3));}
374 /*- Haskell module header/import parsing: -----------------------------------
376 * Syntax for Haskell modules (module headers and imports) is parsed but
377 * most of it is ignored. However, module names in import declarations
378 * are used, of course, if import chasing is turned on.
379 *-------------------------------------------------------------------------*/
381 /* In Haskell 1.2, the default module header was "module Main where"
382 * In 1.3, this changed to "module Main(main) where".
383 * We use the 1.2 header because it breaks much less pre-module code.
385 topModule : startMain begin modBody end {
386 setExportList(singleton(
388 mkCon(module(currentModule).text)
392 | TMODULE modname expspec WHERE '{' modBody end
393 {setExportList($3); $$ = gc7($6);}
394 | TMODULE error {syntaxError("module definition");}
396 /* To implement the Haskell module system, we have to keep track of the
397 * current module. We rely on the use of LALR parsing to ensure that this
398 * side effect happens before any declarations within the module.
400 startMain : /* empty */ {startModule(conMain);
403 modname : CONID {startModule($1); $$ = gc1(NIL);}
405 modid : CONID {$$ = $1;}
406 | STRINGLIT { extern String scriptFile;
408 = findPathname(scriptFile,
409 textToStr(textOf($1)));
411 /* fillin pathname if known */
412 $$ = mkStr(findText(modName));
418 modBody : topDecls {$$ = $1;}
419 | impDecls chase {$$ = gc2(NIL);}
420 | impDecls ';' chase topDecls {$$ = gc4($4);}
423 /*- Exports: --------------------------------------------------------------*/
425 expspec : /* empty */ {$$ = gc0(exportSelf());}
426 | '(' ')' {$$ = gc2(NIL);}
427 | '(' exports ')' {$$ = gc3($2);}
428 | '(' exports ',' ')' {$$ = gc4($2);}
430 exports : exports ',' export {$$ = gc3(cons($3,$1));}
431 | export {$$ = gc1(singleton($1));}
433 /* The qcon should be qconid.
434 * Relaxing the rule lets us explicitly export (:) from the Prelude.
436 export : qvar {$$ = $1;}
438 | qconid '(' UPTO ')' {$$ = gc4(pair($1,DOTDOT));}
439 | qconid '(' qnames ')' {$$ = gc4(pair($1,$3));}
440 | TMODULE modid {$$ = gc2(ap(MODULEENT,$2));}
442 qnames : /* empty */ {$$ = gc0(NIL);}
443 | ',' {$$ = gc1(NIL);}
445 | qnames1 ',' {$$ = gc2($1);}
447 qnames1 : qnames1 ',' qname {$$ = gc3(cons($3,$1));}
448 | qname {$$ = gc1(singleton($1));}
450 qname : qvar {$$ = $1;}
454 /*- Import declarations: --------------------------------------------------*/
456 impDecls : impDecls ';' impDecl {imps = cons($3,imps); $$=gc3(NIL);}
457 | impDecl {imps = singleton($1); $$=gc1(NIL);}
459 chase : /* empty */ {if (chase(imps)) {
469 /* Note that qualified import ignores the import list. */
470 impDecl : IMPORT modid impspec {addQualImport($2,$2);
471 addUnqualImport($2,$3);
473 | IMPORT modid ASMOD modid impspec
474 {addQualImport($2,$4);
475 addUnqualImport($2,$5);
477 | IMPORT QUALIFIED modid ASMOD modid impspec
478 {addQualImport($3,$5);
480 | IMPORT QUALIFIED modid impspec
481 {addQualImport($3,$3);
483 | IMPORT error {syntaxError("import declaration");}
485 impspec : /* empty */ {$$ = gc0(DOTDOT);}
486 | HIDING '(' imports ')' {$$ = gc4(ap(HIDDEN,$3));}
487 | '(' imports ')' {$$ = gc3($2);}
489 imports : /* empty */ {$$ = gc0(NIL);}
490 | ',' {$$ = gc1(NIL);}
491 | imports1 {$$ = $1;}
492 | imports1 ',' {$$ = gc2($1);}
494 imports1 : imports1 ',' import {$$ = gc3(cons($3,$1));}
495 | import {$$ = gc1(singleton($1));}
497 import : var {$$ = $1;}
499 | CONID '(' UPTO ')' {$$ = gc4(pair($1,DOTDOT));}
500 | CONID '(' names ')' {$$ = gc4(pair($1,$3));}
502 names : /* empty */ {$$ = gc0(NIL);}
503 | ',' {$$ = gc1(NIL);}
505 | names1 ',' {$$ = gc2($1);}
507 names1 : names1 ',' name {$$ = gc3(cons($3,$1));}
508 | name {$$ = gc1(singleton($1));}
510 name : var {$$ = $1;}
514 /*- Top-level declarations: -----------------------------------------------*/
516 topDecls : /* empty */ {$$ = gc0(NIL);}
517 | ';' {$$ = gc1(NIL);}
518 | topDecls1 {$$ = $1;}
519 | topDecls1 ';' {$$ = gc2($1);}
521 topDecls1 : topDecls1 ';' topDecl {$$ = gc2($1);}
522 | topDecls1 ';' decl {$$ = gc3(cons($3,$1));}
523 | topDecl {$$ = gc0(NIL);}
524 | decl {$$ = gc1(cons($1,NIL));}
527 /*- Type declarations: ----------------------------------------------------*/
529 topDecl : TYPE tyLhs '=' type {defTycon(4,$3,$2,$4,SYNONYM);}
530 | TYPE tyLhs '=' type IN invars
532 ap($4,$6),RESTRICTSYN);}
533 | TYPE error {syntaxError("type definition");}
534 | DATA btype2 '=' constrs deriving
535 {defTycon(5,$3,checkTyLhs($2),
536 ap(rev($4),$5),DATATYPE);}
537 | DATA context IMPLIES tyLhs '=' constrs deriving
539 ap(qualify($2,rev($6)),
541 | DATA btype2 {defTycon(2,$1,checkTyLhs($2),
542 ap(NIL,NIL),DATATYPE);}
543 | DATA context IMPLIES tyLhs {defTycon(4,$1,$4,
546 | DATA error {syntaxError("data definition");}
547 | TNEWTYPE btype2 '=' nconstr deriving
548 {defTycon(5,$3,checkTyLhs($2),
550 | TNEWTYPE context IMPLIES tyLhs '=' nconstr deriving
554 | TNEWTYPE error {syntaxError("newtype definition");}
556 tyLhs : tyLhs varid {$$ = gc2(ap($1,$2));}
558 | error {syntaxError("type defn lhs");}
560 invars : invars ',' invar {$$ = gc3(cons($3,$1));}
561 | invar {$$ = gc1(cons($1,NIL));}
563 invar : var COCO topType {$$ = gc3(sigdecl($2,singleton($1),
567 constrs : constrs '|' pconstr {$$ = gc3(cons($3,$1));}
568 | pconstr {$$ = gc1(cons($1,NIL));}
570 pconstr : ALL varids '.' qconstr {$$ = gc4(ap(POLYTYPE,
574 qconstr : context IMPLIES constr {$$ = gc3(qualify($1,$3));}
577 constr : '!' btype conop bbtype {$$ = gc4(ap(ap($3,bang($2)),$4));}
578 | btype1 conop bbtype {$$ = gc3(ap(ap($2,$1),$3));}
579 | btype2 conop bbtype {$$ = gc3(ap(ap($2,$1),$3));}
580 | bpolyType conop bbtype {$$ = gc3(ap(ap($2,$1),$3));}
584 | con '{' fieldspecs '}' {$$ = gc4(ap(LABC,pair($1,rev($3))));}
585 | con '{' '}' {$$ = gc3(ap(LABC,pair($1,NIL)));}
586 | error {syntaxError("data type definition");}
588 btype3 : btype2 '!' atype {$$ = gc3(ap($1,bang($3)));}
589 | btype3 '!' atype {$$ = gc3(ap($1,bang($3)));}
590 | btype3 atype {$$ = gc2(ap($1,$2));}
592 btype4 : btype2 bpolyType {$$ = gc2(ap($1,$2));}
593 | btype3 bpolyType {$$ = gc2(ap($1,$2));}
594 | btype4 bpolyType {$$ = gc2(ap($1,$2));}
595 | btype4 atype {$$ = gc2(ap($1,$2));}
596 | btype4 '!' atype {$$ = gc3(ap($1,bang($3)));}
598 bbtype : '!' btype {$$ = gc2(bang($2));}
600 | bpolyType {$$ = $1;}
602 nconstr : pconstr {$$ = gc1(singleton($1));}
604 fieldspecs: fieldspecs ',' fieldspec {$$ = gc3(cons($3,$1));}
605 | fieldspec {$$ = gc1(cons($1,NIL));}
607 fieldspec : vars COCO polyType {$$ = gc3(pair(rev($1),$3));}
608 | vars COCO type {$$ = gc3(pair(rev($1),$3));}
609 | vars COCO '!' type {$$ = gc4(pair(rev($1),bang($4)));}
611 deriving : /* empty */ {$$ = gc0(NIL);}
612 | DERIVING qconid {$$ = gc2(singleton($2));}
613 | DERIVING '(' derivs0 ')' {$$ = gc4($3);}
615 derivs0 : /* empty */ {$$ = gc0(NIL);}
616 | derivs {$$ = gc1(rev($1));}
618 derivs : derivs ',' qconid {$$ = gc3(cons($3,$1));}
619 | qconid {$$ = gc1(singleton($1));}
622 /*- Processing definitions of primitives ----------------------------------*/
624 topDecl : FOREIGN IMPORT callconv ext_loc ext_name unsafe_flag var COCO type
625 {foreignImport($1,pair($4,$5),$7,$9); sp-=9;}
626 | FOREIGN EXPORT callconv ext_name qvarid COCO type
627 {foreignExport($1,$4,$5,$7); sp-=7;}
630 callconv : var {$$ = gc1(NIL); /* ignored */ }
632 ext_loc : STRINGLIT {$$ = $1;}
634 ext_name : STRINGLIT {$$ = $1;}
636 unsafe_flag: /* empty */ {$$ = gc0(NIL);}
637 | UNSAFE {$$ = gc1(NIL); /* ignored */ }
641 /*- Class declarations: ---------------------------------------------------*/
643 topDecl : TCLASS crule wherePart {classDefn(intOf($1),$2,$3); sp-=3;}
644 | TINSTANCE irule wherePart {instDefn(intOf($1),$2,$3); sp-=3;}
645 | DEFAULT '(' dtypes ')' {defaultDefn(intOf($1),$3); sp-=4;}
646 | TCLASS error {syntaxError("class declaration");}
647 | TINSTANCE error {syntaxError("instance declaration");}
648 | DEFAULT error {syntaxError("default declaration");}
650 crule : context IMPLIES btype2 {$$ = gc3(pair($1,checkPred($3)));}
651 | btype2 {$$ = gc1(pair(NIL,checkPred($1)));}
653 irule : context IMPLIES btype2 {$$ = gc3(pair($1,checkPred($3)));}
654 | btype2 {$$ = gc1(pair(NIL,checkPred($1)));}
656 dtypes : /* empty */ {$$ = gc0(NIL);}
657 | dtypes1 {$$ = gc1(rev($1));}
659 dtypes1 : dtypes1 ',' type {$$ = gc3(cons($3,$1));}
660 | type {$$ = gc1(cons($1,NIL));}
663 /*- Type expressions: -----------------------------------------------------*/
665 topType : context IMPLIES topType1 {$$ = gc3(qualify($1,$3));}
666 | topType1 {$$ = $1;}
668 topType1 : bpolyType ARROW topType1 {$$ = gc3(fn($1,$3));}
669 | btype1 ARROW topType1 {$$ = gc3(fn($1,$3));}
670 | btype2 ARROW topType1 {$$ = gc3(fn($1,$3));}
673 polyType : ALL varids '.' sigType {$$ = gc4(ap(POLYTYPE,
675 | bpolyType {$$ = $1;}
677 bpolyType : '(' polyType ')' {$$ = gc3($2);}
679 varids : varids ',' varid {$$ = gc3(cons($3,$1));}
680 | varid {$$ = gc1(singleton($1));}
682 sigType : context IMPLIES type {$$ = gc3(qualify($1,$3));}
685 context : '(' ')' {$$ = gc2(NIL);}
686 | btype2 {$$ = gc1(singleton(checkPred($1)));}
687 | '(' btype2 ')' {$$ = gc3(singleton(checkPred($2)));}
688 | '(' btypes2 ')' {$$ = gc3(checkContext(rev($2)));}
690 | lacks {$$ = gc1(singleton($1));}
691 | '(' lacks1 ')' {$$ = gc3(checkContext(rev($2)));}
693 lacks : varid '\\' varid {
695 $$ = gc3(ap(mkExt(textOf($3)),$1));
697 noTREX("a type context");
701 lacks1 : btypes2 ',' lacks {$$ = gc3(cons($3,$1));}
702 | lacks1 ',' btype2 {$$ = gc3(cons($3,$1));}
703 | lacks1 ',' lacks {$$ = gc3(cons($3,$1));}
704 | btype2 ',' lacks {$$ = gc3(cons($3,cons($1,NIL)));}
705 | lacks {$$ = gc1(singleton($1));}
709 type : type1 {$$ = $1;}
712 type1 : btype1 {$$ = $1;}
713 | btype1 ARROW type {$$ = gc3(fn($1,$3));}
714 | btype2 ARROW type {$$ = gc3(fn($1,$3));}
715 | error {syntaxError("type expression");}
717 btype : btype1 {$$ = $1;}
720 btype1 : btype1 atype {$$ = gc2(ap($1,$2));}
723 btype2 : btype2 atype {$$ = gc2(ap($1,$2));}
726 atype : atype1 {$$ = $1;}
729 atype1 : varid {$$ = $1;}
730 | '(' ')' {$$ = gc2(typeUnit);}
731 | '(' ARROW ')' {$$ = gc3(typeArrow);}
732 | '(' type1 ')' {$$ = gc3($2);}
733 | '(' btype2 ')' {$$ = gc3($2);}
734 | '(' tupCommas ')' {$$ = gc3($2);}
735 | '(' btypes2 ')' {$$ = gc3(buildTuple($2));}
736 | '(' typeTuple ')' {$$ = gc3(buildTuple($2));}
740 $$ = gc3(revOnto($2,typeNoRow));
745 | '(' tfields '|' type ')' {$$ = gc5(revOnto($2,$4));}
747 | '[' type ']' {$$ = gc3(ap(typeList,$2));}
748 | '[' ']' {$$ = gc2(typeList);}
749 | '_' {$$ = gc1(inventVar());}
751 btypes2 : btypes2 ',' btype2 {$$ = gc3(cons($3,$1));}
752 | btype2 ',' btype2 {$$ = gc3(cons($3,cons($1,NIL)));}
754 typeTuple : type1 ',' type {$$ = gc3(cons($3,cons($1,NIL)));}
755 | btype2 ',' type1 {$$ = gc3(cons($3,cons($1,NIL)));}
756 | btypes2 ',' type1 {$$ = gc3(cons($3,$1));}
757 | typeTuple ',' type {$$ = gc3(cons($3,$1));}
760 tfields : tfields ',' tfield {$$ = gc3(cons($3,$1));}
761 | tfield {$$ = gc1(singleton($1));}
763 tfield : varid COCO type {$$ = gc3(ap(mkExt(textOf($1)),$3));}
767 /*- Value declarations: ---------------------------------------------------*/
769 gendecl : INFIXN optDigit ops {$$ = gc3(fixdecl($1,$3,NON_ASS,$2));}
770 | INFIXN error {syntaxError("fixity decl");}
771 | INFIXL optDigit ops {$$ = gc3(fixdecl($1,$3,LEFT_ASS,$2));}
772 | INFIXL error {syntaxError("fixity decl");}
773 | INFIXR optDigit ops {$$ = gc3(fixdecl($1,$3,RIGHT_ASS,$2));}
774 | INFIXR error {syntaxError("fixity decl");}
775 | vars COCO topType {$$ = gc3(sigdecl($2,$1,$3));}
776 | vars COCO error {syntaxError("type signature");}
778 optDigit : NUMLIT {$$ = gc1(checkPrec($1));}
779 | /* empty */ {$$ = gc0(mkInt(DEF_PREC));}
781 ops : ops ',' op {$$ = gc3(cons($3,$1));}
782 | op {$$ = gc1(singleton($1));}
784 vars : vars ',' var {$$ = gc3(cons($3,$1));}
785 | var {$$ = gc1(singleton($1));}
787 decls : '{' decls0 end {$$ = gc3($2);}
788 | '{' decls1 end {$$ = gc3($2);}
790 decls0 : /* empty */ {$$ = gc0(NIL);}
791 | decls0 ';' {$$ = gc2($1);}
792 | decls1 ';' {$$ = gc2($1);}
794 decls1 : decls0 decl {$$ = gc2(cons($2,$1));}
796 decl : gendecl {$$ = $1;}
797 | funlhs rhs {$$ = gc2(ap(FUNBIND,pair($1,$2)));}
798 | funlhs COCO type rhs {$$ = gc4(ap(FUNBIND,
801 | pat0 rhs {$$ = gc2(ap(PATBIND,pair($1,$2)));}
803 funlhs : funlhs0 {$$ = $1;}
807 funlhs0 : pat10_vI varop pat0 {$$ = gc3(ap2($2,$1,$3));}
808 | infixPat varop pat0 {$$ = gc3(ap2($2,$1,$3));}
809 | NUMLIT varop pat0 {$$ = gc3(ap2($2,$1,$3));}
810 | var varop_pl pat0 {$$ = gc3(ap2($2,$1,$3));}
811 | var '+' pat0_INT {$$ = gc3(ap2(varPlus,$1,$3));}
813 funlhs1 : '(' funlhs0 ')' apat {$$ = gc4(ap($2,$4));}
814 | '(' funlhs1 ')' apat {$$ = gc4(ap($2,$4));}
815 | '(' npk ')' apat {$$ = gc4(ap($2,$4));}
816 | var apat {$$ = gc2(ap($1,$2));}
817 | funlhs1 apat {$$ = gc2(ap($1,$2));}
819 rhs : rhs1 wherePart {$$ = gc2(letrec($2,$1));}
820 | error {syntaxError("declaration");}
822 rhs1 : '=' exp {$$ = gc2(pair($1,$2));}
823 | gdrhs {$$ = gc1(grded(rev($1)));}
825 gdrhs : gdrhs gddef {$$ = gc2(cons($2,$1));}
826 | gddef {$$ = gc1(singleton($1));}
828 gddef : '|' exp0 '=' exp {$$ = gc4(pair($3,pair($2,$4)));}
830 wherePart : /* empty */ {$$ = gc0(NIL);}
831 | WHERE decls {$$ = gc2($2);}
834 /*- Patterns: -------------------------------------------------------------*/
839 pat_npk : pat0 COCO type {$$ = gc3(ap(ESIGN,pair($1,$3)));}
842 npk : var '+' NUMLIT {$$ = gc3(ap2(varPlus,$1,$3));}
844 pat0 : var {$$ = $1;}
848 pat0_INT : var {$$ = $1;}
851 pat0_vI : pat10_vI {$$ = $1;}
852 | infixPat {$$ = gc1(ap(INFIX,$1));}
854 infixPat : '-' pat10 {$$ = gc2(ap(NEG,only($2)));}
855 | var qconop pat10 {$$ = gc3(ap(ap($2,only($1)),$3));}
856 | var qconop '-' pat10 {$$ = gc4(ap(NEG,ap2($2,only($1),$4)));}
857 | NUMLIT qconop pat10 {$$ = gc3(ap(ap($2,only($1)),$3));}
858 | NUMLIT qconop '-' pat10 {$$ = gc4(ap(NEG,ap2($2,only($1),$4)));}
859 | pat10_vI qconop pat10 {$$ = gc3(ap(ap($2,only($1)),$3));}
860 | pat10_vI qconop '-' pat10 {$$ = gc4(ap(NEG,ap2($2,only($1),$4)));}
861 | infixPat qconop pat10 {$$ = gc3(ap(ap($2,$1),$3));}
862 | infixPat qconop '-' pat10 {$$ = gc4(ap(NEG,ap(ap($2,$1),$4)));}
864 pat10 : fpat {$$ = $1;}
867 pat10_vI : fpat {$$ = $1;}
870 fpat : fpat apat {$$ = gc2(ap($1,$2));}
871 | gcon apat {$$ = gc2(ap($1,$2));}
873 apat : NUMLIT {$$ = $1;}
877 apat_vI : var '@' apat {$$ = gc3(ap(ASPAT,pair($1,$3)));}
879 | qcon '{' patbinds '}' {$$ = gc4(ap(CONFLDS,pair($1,$3)));}
881 | STRINGLIT {$$ = $1;}
882 | '_' {$$ = gc1(WILDCARD);}
883 | '(' pat_npk ')' {$$ = gc3($2);}
884 | '(' npk ')' {$$ = gc3($2);}
885 | '(' pats2 ')' {$$ = gc3(buildTuple($2));}
886 | '[' pats1 ']' {$$ = gc3(ap(FINLIST,rev($2)));}
887 | '~' apat {$$ = gc2(ap(LAZYPAT,$2));}
889 | '(' patfields ')' {
891 $$ = gc3(revOnto($2,nameNoRec));
896 | '(' patfields '|' pat ')' {$$ = gc5(revOnto($2,$4));}
899 pats2 : pats2 ',' pat {$$ = gc3(cons($3,$1));}
900 | pat ',' pat {$$ = gc3(cons($3,singleton($1)));}
902 pats1 : pats1 ',' pat {$$ = gc3(cons($3,$1));}
903 | pat {$$ = gc1(singleton($1));}
905 patbinds : /* empty */ {$$ = gc0(NIL);}
906 | patbinds1 {$$ = gc1(rev($1));}
908 patbinds1 : patbinds1 ',' patbind {$$ = gc3(cons($3,$1));}
909 | patbind {$$ = gc1(singleton($1));}
911 patbind : qvar '=' pat {$$ = gc3(pair($1,$3));}
915 patfields : patfields ',' patfield {$$ = gc3(cons($3,$1));}
916 | patfield {$$ = gc1(singleton($1));}
918 patfield : varid '=' pat {
920 $$ = gc3(ap(mkExt(textOf($1)),$3));
928 /*- Expressions: ----------------------------------------------------------*/
930 exp : exp_err {$$ = $1;}
931 | error {syntaxError("expression");}
933 exp_err : exp0a COCO sigType {$$ = gc3(ap(ESIGN,pair($1,$3)));}
936 exp0 : exp0a {$$ = $1;}
939 exp0a : infixExpa {$$ = gc1(ap(INFIX,$1));}
942 exp0b : infixExpb {$$ = gc1(ap(INFIX,$1));}
945 infixExpa : infixExpa qop '-' exp10a {$$ = gc4(ap(NEG,ap(ap($2,$1),$4)));}
946 | infixExpa qop exp10a {$$ = gc3(ap(ap($2,$1),$3));}
947 | '-' exp10a {$$ = gc2(ap(NEG,only($2)));}
948 | exp10a qop '-' exp10a {$$ = gc4(ap(NEG,
949 ap(ap($2,only($1)),$4)));}
950 | exp10a qop exp10a {$$ = gc3(ap(ap($2,only($1)),$3));}
952 infixExpb : infixExpa qop '-' exp10b {$$ = gc4(ap(NEG,ap(ap($2,$1),$4)));}
953 | infixExpa qop exp10b {$$ = gc3(ap(ap($2,$1),$3));}
954 | '-' exp10b {$$ = gc2(ap(NEG,only($2)));}
955 | exp10a qop '-' exp10b {$$ = gc4(ap(NEG,
956 ap(ap($2,only($1)),$4)));}
957 | exp10a qop exp10b {$$ = gc3(ap(ap($2,only($1)),$3));}
959 exp10a : CASEXP exp OF '{' alts end {$$ = gc6(ap(CASE,pair($2,rev($5))));}
960 | DO '{' stmts end {$$ = gc4(ap(DOCOMP,checkDo($3)));}
963 exp10b : '\\' pats ARROW exp {$$ = gc4(ap(LAMBDA,
966 | LET decls IN exp {$$ = gc4(letrec($2,$4));}
967 | IF exp THEN exp ELSE exp {$$ = gc6(ap(COND,triple($2,$4,$6)));}
969 pats : pats apat {$$ = gc2(cons($2,$1));}
970 | apat {$$ = gc1(cons($1,NIL));}
972 appExp : appExp aexp {$$ = gc2(ap($1,$2));}
975 aexp : qvar {$$ = $1;}
976 | qvar '@' aexp {$$ = gc3(ap(ASPAT,pair($1,$3)));}
977 | '~' aexp {$$ = gc2(ap(LAZYPAT,$2));}
978 | '_' {$$ = gc1(WILDCARD);}
980 | qcon '{' fbinds '}' {$$ = gc4(ap(CONFLDS,pair($1,$3)));}
981 | aexp '{' fbinds '}' {$$ = gc4(ap(UPDFLDS,
982 triple($1,NIL,$3)));}
985 | STRINGLIT {$$ = $1;}
987 | '(' exp ')' {$$ = gc3($2);}
988 | '(' exps2 ')' {$$ = gc3(buildTuple($2));}
992 $$ = gc3(revOnto($2,nameNoRec));
997 | '(' vfields '|' exp ')' {$$ = gc5(revOnto($2,$4));}
998 | RECSELID {$$ = $1;}
1000 | '[' list ']' {$$ = gc3($2);}
1001 | '(' exp10a qop ')' {$$ = gc4(ap($3,$2));}
1002 | '(' qvarop_mi exp0 ')' {$$ = gc4(ap(ap(nameFlip,$2),$3));}
1003 | '(' qconop exp0 ')' {$$ = gc4(ap(ap(nameFlip,$2),$3));}
1005 exps2 : exps2 ',' exp {$$ = gc3(cons($3,$1));}
1006 | exp ',' exp {$$ = gc3(cons($3,cons($1,NIL)));}
1009 vfields : vfields ',' vfield {$$ = gc3(cons($3,$1));}
1010 | vfield {$$ = gc1(singleton($1));}
1012 vfield : varid '=' exp {
1014 $$ = gc3(ap(mkExt(textOf($1)),$3));
1016 noTREX("an expression");
1021 alts : alts1 {$$ = $1;}
1022 | alts1 ';' {$$ = gc2($1);}
1024 alts1 : alts1 ';' alt {$$ = gc3(cons($3,$1));}
1025 | alt {$$ = gc1(cons($1,NIL));}
1027 alt : pat altRhs wherePart {$$ = gc3(pair($1,letrec($3,$2)));}
1029 altRhs : guardAlts {$$ = gc1(grded(rev($1)));}
1030 | ARROW exp {$$ = gc2(pair($1,$2));}
1031 | error {syntaxError("case expression");}
1033 guardAlts : guardAlts guardAlt {$$ = gc2(cons($2,$1));}
1034 | guardAlt {$$ = gc1(cons($1,NIL));}
1036 guardAlt : '|' exp0 ARROW exp {$$ = gc4(pair($3,pair($2,$4)));}
1038 stmts : stmts1 ';' {$$ = gc2($1);}
1041 stmts1 : stmts1 ';' stmt {$$ = gc3(cons($3,$1));}
1042 | stmt {$$ = gc1(cons($1,NIL));}
1044 stmt : exp_err FROM exp {$$ = gc3(ap(FROMQUAL,pair($1,$3)));}
1045 | LET decls {$$ = gc2(ap(QWHERE,$2));}
1046 /* | IF exp {$$ = gc2(ap(BOOLQUAL,$2));}*/
1047 | exp_err {$$ = gc1(ap(DOQUAL,$1));}
1049 fbinds : /* empty */ {$$ = gc0(NIL);}
1050 | fbinds1 {$$ = gc1(rev($1));}
1052 fbinds1 : fbinds1 ',' fbind {$$ = gc3(cons($3,$1));}
1053 | fbind {$$ = gc1(singleton($1));}
1055 fbind : var {$$ = $1;}
1056 | qvar '=' exp {$$ = gc3(pair($1,$3));}
1059 /*- List Expressions: -------------------------------------------------------*/
1061 list : exp {$$ = gc1(ap(FINLIST,cons($1,NIL)));}
1062 | exps2 {$$ = gc1(ap(FINLIST,rev($1)));}
1063 | exp '|' quals {$$ = gc3(ap(COMP,pair($1,rev($3))));}
1064 | exp UPTO exp {$$ = gc3(ap(ap(nameFromTo,$1),$3));}
1065 | exp ',' exp UPTO {$$ = gc4(ap(ap(nameFromThen,$1),$3));}
1066 | exp UPTO {$$ = gc2(ap(nameFrom,$1));}
1067 | exp ',' exp UPTO exp {$$ = gc5(ap(ap(ap(nameFromThenTo,
1070 quals : quals ',' qual {$$ = gc3(cons($3,$1));}
1071 | qual {$$ = gc1(cons($1,NIL));}
1073 qual : exp FROM exp {$$ = gc3(ap(FROMQUAL,pair($1,$3)));}
1074 | exp {$$ = gc1(ap(BOOLQUAL,$1));}
1075 | LET decls {$$ = gc2(ap(QWHERE,$2));}
1078 /*- Identifiers and symbols: ----------------------------------------------*/
1080 gcon : qcon {$$ = $1;}
1081 | '(' ')' {$$ = gc2(nameUnit);}
1082 | '[' ']' {$$ = gc2(nameNil);}
1083 | '(' tupCommas ')' {$$ = gc3($2);}
1085 tupCommas : tupCommas ',' {$$ = gc2(mkTuple(tupleOf($1)+1));}
1086 | ',' {$$ = gc1(mkTuple(2));}
1088 varid : VARID {$$ = $1;}
1089 | HIDING {$$ = gc1(varHiding);}
1090 | QUALIFIED {$$ = gc1(varQualified);}
1091 | ASMOD {$$ = gc1(varAsMod);}
1093 qconid : QCONID {$$ = $1;}
1096 var : varid {$$ = $1;}
1097 | '(' VAROP ')' {$$ = gc3($2);}
1098 | '(' '+' ')' {$$ = gc3(varPlus);}
1099 | '(' '-' ')' {$$ = gc3(varMinus);}
1100 | '(' '!' ')' {$$ = gc3(varBang);}
1101 | '(' '.' ')' {$$ = gc3(varDot);}
1103 qvar : QVARID {$$ = $1;}
1104 | '(' QVAROP ')' {$$ = gc3($2);}
1107 con : CONID {$$ = $1;}
1108 | '(' CONOP ')' {$$ = gc3($2);}
1110 qcon : QCONID {$$ = $1;}
1111 | '(' QCONOP ')' {$$ = gc3($2);}
1114 varop : '+' {$$ = gc1(varPlus);}
1115 | '-' {$$ = gc1(varMinus);}
1116 | varop_mipl {$$ = $1;}
1118 varop_mi : '+' {$$ = gc1(varPlus);}
1119 | varop_mipl {$$ = $1;}
1121 varop_pl : '-' {$$ = gc1(varMinus);}
1122 | varop_mipl {$$ = $1;}
1124 varop_mipl: VAROP {$$ = $1;}
1125 | '`' varid '`' {$$ = gc3($2);}
1126 | '!' {$$ = gc1(varBang);}
1127 | '.' {$$ = gc1(varDot);}
1129 qvarop : '-' {$$ = gc1(varMinus);}
1130 | qvarop_mi {$$ = $1;}
1132 qvarop_mi : QVAROP {$$ = $1;}
1133 | '`' QVARID '`' {$$ = gc3($2);}
1134 | varop_mi {$$ = $1;}
1137 conop : CONOP {$$ = $1;}
1138 | '`' CONID '`' {$$ = gc3($2);}
1140 qconop : QCONOP {$$ = $1;}
1141 | '`' QCONID '`' {$$ = gc3($2);}
1144 op : varop {$$ = $1;}
1147 qop : qvarop {$$ = $1;}
1151 /*- Stuff from STG hugs ---------------------------------------------------*/
1153 qvarid : varid1 {$$ = gc1($1);}
1154 | QVARID {$$ = gc1($1);}
1156 varid1 : VARID {$$ = gc1($1);}
1157 | HIDING {$$ = gc1(varHiding);}
1158 | QUALIFIED {$$ = gc1(varQualified);}
1159 | ASMOD {$$ = gc1(varAsMod);}
1162 /*- Tricks to force insertion of leading and closing braces ---------------*/
1164 begin : error {yyerrok;
1165 if (offsideON) goOffside(startColumn);}
1167 /* deal with trailing semicolon */
1168 end : '}' {$$ = $1;}
1170 if (offsideON && canUnOffside()) {
1172 /* insert extra token on stack*/
1174 pushed(0) = pushed(1);
1175 pushed(1) = mkInt(column);
1178 syntaxError("definition");
1182 /*-------------------------------------------------------------------------*/
1186 static Cell local gcShadow(n,e) /* keep parsed fragments on stack */
1189 /* If a look ahead token is held then the required stack transformation
1192 * x1 | ... | xn | la ===> e | la
1195 * Otherwise, the transformation is:
1197 * x1 | ... | xn ===> e
1201 pushed(n-1) = top();
1210 static Void local syntaxError(s) /* report on syntax error */
1212 ERRMSG(row) "Syntax error in %s (unexpected %s)", s, unexpected()
1216 static String local unexpected() { /* find name for unexpected token */
1217 static char buffer[100];
1218 static char *fmt = "%s \"%s\"";
1219 static char *kwd = "keyword";
1222 case 0 : return "end of input";
1224 #define keyword(kw) sprintf(buffer,fmt,kwd,kw); return buffer;
1225 case INFIXL : keyword("infixl");
1226 case INFIXR : keyword("infixr");
1227 case INFIXN : keyword("infix");
1228 case FOREIGN : keyword("foreign");
1229 case UNSAFE : keyword("unsafe");
1230 case TINSTANCE : keyword("instance");
1231 case TCLASS : keyword("class");
1232 case CASEXP : keyword("case");
1233 case OF : keyword("of");
1234 case IF : keyword("if");
1235 case THEN : keyword("then");
1236 case ELSE : keyword("else");
1237 case WHERE : keyword("where");
1238 case TYPE : keyword("type");
1239 case DATA : keyword("data");
1240 case TNEWTYPE : keyword("newtype");
1241 case LET : keyword("let");
1242 case IN : keyword("in");
1243 case DERIVING : keyword("deriving");
1244 case DEFAULT : keyword("default");
1245 case IMPORT : keyword("import");
1246 case TMODULE : keyword("module");
1247 case ALL : keyword("__forall");
1250 case ARROW : return "`->'";
1251 case '=' : return "`='";
1252 case COCO : return "`::'";
1253 case '-' : return "`-'";
1254 case '!' : return "`!'";
1255 case ',' : return "comma";
1256 case '@' : return "`@'";
1257 case '(' : return "`('";
1258 case ')' : return "`)'";
1259 case '{' : return "`{'";
1260 case '}' : return "`}'";
1261 case '_' : return "`_'";
1262 case '|' : return "`|'";
1263 case '.' : return "`.'";
1264 case ';' : return "`;'";
1265 case UPTO : return "`..'";
1266 case '[' : return "`['";
1267 case ']' : return "`]'";
1268 case FROM : return "`<-'";
1269 case '\\' : return "backslash (lambda)";
1270 case '~' : return "tilde";
1271 case '`' : return "backquote";
1273 case RECSELID : sprintf(buffer,"selector \"#%s\"",
1274 textToStr(extText(snd(yylval))));
1280 case CONID : sprintf(buffer,"symbol \"%s\"",
1281 textToStr(textOf(yylval)));
1286 case QCONID : sprintf(buffer,"symbol \"%s\"",
1287 identToStr(yylval));
1289 case HIDING : return "symbol \"hiding\"";
1290 case QUALIFIED : return "symbol \"qualified\"";
1291 case ASMOD : return "symbol \"as\"";
1292 case NUMLIT : return "numeric literal";
1293 case CHARLIT : return "character literal";
1294 case STRINGLIT : return "string literal";
1295 case IMPLIES : return "`=>'";
1296 default : return "token";
1300 static Cell local checkPrec(p) /* Check for valid precedence value*/
1302 if (!isInt(p) || intOf(p)<MIN_PREC || intOf(p)>MAX_PREC) {
1303 ERRMSG(row) "Precedence value must be an integer in the range [%d..%d]",
1310 static Cell local buildTuple(tup) /* build tuple (x1,...,xn) from */
1311 List tup; { /* list [xn,...,x1] */
1317 x = fst(t); /* / \ / \ */
1318 fst(t) = snd(t); /* xn . . xn */
1319 snd(t) = x; /* . ===> . */
1321 t = fun(x); /* . . */
1323 } while (nonNull(t)); /* x1 NIL (n) x1 */
1324 fst(x) = mkTuple(n);
1328 static List local checkContext(con) /* validate context */
1330 mapOver(checkPred, con);
1334 static Cell local checkPred(c) /* check that type expr is a valid */
1335 Cell c; { /* constraint */
1336 Cell cn = getHead(c);
1338 if (isExt(cn) && argCount==1)
1341 if (!isQCon(cn) || argCount==0)
1342 syntaxError("class expression");
1346 static Pair local checkDo(dqs) /* convert reversed list of dquals */
1347 List dqs; { /* to an (expr,quals) pair */
1348 if (isNull(dqs) || whatIs(hd(dqs))!=DOQUAL) {
1349 ERRMSG(row) "Last generator in do {...} must be an expression"
1352 fst(dqs) = snd(fst(dqs)); /* put expression in fst of pair */
1353 snd(dqs) = rev(snd(dqs)); /* & reversed list of quals in snd */
1357 static Cell local checkTyLhs(c) /* check that lhs is of the form */
1358 Cell c; { /* T a1 ... a */
1360 while (isAp(tlhs) && whatIs(arg(tlhs))==VARIDCELL)
1362 switch (whatIs(tlhs)) {
1363 case CONIDCELL : return c;
1366 ERRMSG(row) "Illegal left hand side in datatype definition"
1369 return 0; /* NOTREACHED */
1373 static Void local noTREX(where)
1375 ERRMSG(row) "Attempt to use TREX records while parsing %s.\n", where ETHEN
1376 ERRTEXT "(TREX is disabled in this build of Hugs)"
1381 /*-------------------------------------------------------------------------*/