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: 1999/11/17 16:57:42 $
17 * ------------------------------------------------------------------------*/
23 #define defTycon(n,l,lhs,rhs,w) tyconDefn(intOf(l),lhs,rhs,w); sp-=n
24 #define sigdecl(l,vs,t) ap(SIGDECL,triple(l,vs,t))
25 #define fixdecl(l,ops,a,p) ap(FIXDECL,\
26 triple(l,ops,mkInt(mkSyntax(a,intOf(p)))))
27 #define grded(gs) ap(GUARDED,gs)
28 #define bang(t) ap(BANG,t)
29 #define only(t) ap(ONLY,t)
30 #define letrec(bs,e) (nonNull(bs) ? ap(LETREC,pair(bs,e)) : e)
31 #define qualify(ps,t) (nonNull(ps) ? ap(QUAL,pair(ps,t)) : t)
32 #define exportSelf() singleton(ap(MODULEENT, \
33 mkCon(module(currentModule).text)))
34 #define yyerror(s) /* errors handled elsewhere */
37 static Cell local gcShadow Args((Int,Cell));
38 static Void local syntaxError Args((String));
39 static String local unexpected Args((Void));
40 static Cell local checkPrec Args((Cell));
41 static Void local fixDefn Args((Syntax,Cell,Cell,List));
42 static Cell local buildTuple Args((List));
43 static List local checkCtxt Args((List));
44 static Cell local checkPred Args((Cell));
45 static Pair local checkDo Args((List));
46 static Cell local checkTyLhs Args((Cell));
48 static Void local noTREX Args((String));
51 static Void local noIP Args((String));
54 /* For the purposes of reasonably portable garbage collection, it is
55 * necessary to simulate the YACC stack on the Hugs stack to keep
56 * track of all intermediate constructs. The lexical analyser
57 * pushes a token onto the stack for each token that is found, with
58 * these elements being removed as reduce actions are performed,
59 * taking account of look-ahead tokens as described by gcShadow()
62 * Of the non-terminals used below, only start, topDecl & begin
63 * do not leave any values on the Hugs stack. The same is true for the
64 * terminals EXPR and SCRIPT. At the end of a successful parse, there
65 * should only be one element left on the stack, containing the result
69 #define gc0(e) gcShadow(0,e)
70 #define gc1(e) gcShadow(1,e)
71 #define gc2(e) gcShadow(2,e)
72 #define gc3(e) gcShadow(3,e)
73 #define gc4(e) gcShadow(4,e)
74 #define gc5(e) gcShadow(5,e)
75 #define gc6(e) gcShadow(6,e)
76 #define gc7(e) gcShadow(7,e)
80 %token EXPR CONTEXT SCRIPT
81 %token CASEXP OF DATA TYPE IF
82 %token THEN ELSE WHERE LET IN
83 %token INFIXN INFIXL INFIXR FOREIGN TNEWTYPE
84 %token DEFAULT DERIVING DO TCLASS TINSTANCE
88 %token REPEAT ALL NUMLIT CHARLIT STRINGLIT
89 %token VAROP VARID CONOP CONID
90 %token QVAROP QVARID QCONOP QCONID
92 %token RECSELID IPVARID
94 %token COCO '=' UPTO '@' '\\'
95 %token '|' '-' FROM ARROW '~'
96 %token '!' IMPLIES '(' ',' ')'
97 %token '[' ';' ']' '`' '.'
98 %token TMODULE IMPORT HIDING QUALIFIED ASMOD
99 %token EXPORT UUEXPORT INTERFACE REQUIRES UNSAFE
100 %token INSTIMPORT DYNAMIC CCALL STDCALL
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 {valDefns = $2; sp-=1;}
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 ifName NUMLIT checkVersion WHERE ifDecls
125 | INTERFACE error {syntaxError("interface file");}
127 ifDecls: {$$=gc0(NIL);}
128 | ifDecl ';' ifDecls {$$=gc3(cons($1,$3));}
131 : VARID { $$=gc1($1); }
132 | CONID { $$=gc1($1); }
134 opt_bang : '!' {$$=gc1(NIL);}
137 ifName : CONID {openGHCIface(textOf($1));
140 : NUMLIT {$$ = gc1(NIL); }
143 : IMPORT CONID opt_bang NUMLIT COCO version_list_junk
144 { addGHCImports(intOf($4),textOf($2),
149 | INSTIMPORT CONID {$$=gc2(NIL);}
151 | UUEXPORT CONID ifEntities { addGHCExports($2,$3);
154 | NUMLIT INFIXL optDigit varid_or_conid
155 {$$ = gc4(fixdecl($2,singleton($4),
157 | NUMLIT INFIXR optDigit varid_or_conid
158 {$$ = gc4(fixdecl($2,singleton($4),
160 | NUMLIT INFIXN optDigit varid_or_conid
161 {$$ = gc4(fixdecl($2,singleton($4),
164 | TINSTANCE ifCtxInst ifInstHd '=' ifVar
165 { addGHCInstance(intOf($1),$2,$3,
168 | NUMLIT TYPE ifCon ifKindedTyvarL '=' ifType
169 { addGHCSynonym(intOf($2),$3,$4,$6);
172 | NUMLIT DATA ifCtxDecl ifConData ifKindedTyvarL ifConstrs
173 { addGHCDataDecl(intOf($2),
177 | NUMLIT TNEWTYPE ifCtxDecl ifConData ifKindedTyvarL ifNewTypeConstr
178 { addGHCNewType(intOf($2),
181 | NUMLIT TCLASS ifCtxDecl ifCon ifTyvar ifCmeths
182 { addGHCClass(intOf($2),$3,$4,$5,$6);
184 | NUMLIT ifVar COCO ifType
185 { addGHCVar(intOf($3),textOf($2),$4);
187 | error { syntaxError(
188 "interface declaration"); }
192 /*- Interface variable and constructor ids ----------------*/
193 ifTyvar : VARID {$$ = $1;}
195 ifVar : VARID {$$ = gc1($1);}
197 ifCon : CONID {$$ = gc1($1);}
199 ifQCon : CONID {$$ = gc1($1);}
200 | QCONID {$$ = gc1($1);}
202 ifConData : ifCon {$$ = gc1($1);}
203 | '(' ')' {$$ = gc2(typeUnit);}
204 | '[' ']' {$$ = gc2(typeList);}
205 | '(' ARROW ')' {$$ = gc3(typeArrow);}
207 ifTCName : CONID { $$ = gc1($1); }
208 | CONOP { $$ = gc1($1); }
209 | '(' ARROW ')' { $$ = gc3(typeArrow); }
210 | '[' ']' { $$ = gc1(typeList); }
212 ifQTCName : ifTCName { $$ = gc1($1); }
213 | QCONID { $$ = gc1($1); }
214 | QCONOP { $$ = gc1($1); }
218 /*- Interface contexts ------------------------------------*/
219 ifCtxInst /* __forall [a b] {M.C1 a, M.C2 b} => */
220 /* :: [(QConId, VarId)] */
221 : ALL ifForall ifCtxDecl {$$=gc3($3);}
222 | ALL ifForall IMPLIES {$$=gc3(NIL);}
225 ifInstHd /* { Class aType } :: (ConId, Type) */
226 : '{' ifCon ifAType '}' {$$=gc4(pair($2,$3));}
229 ifCtxDecl /* {M.C1 a, C2 b} :: [(QConId, VarId)] */
231 | '{' ifCtxDeclL '}' IMPLIES { $$ = gc4($2); }
233 ifCtxDeclT /* {M.C1 a, C2 b} :: [(QConId, VarId)] */
235 | '{' ifCtxDeclL '}' { $$ = gc3($2); }
237 ifCtxDeclL /* M.C1 a, C2 b :: [(QConId, VarId)] */
238 : ifCtxDeclLE ',' ifCtxDeclL {$$=gc3(cons($1,$3));}
239 | ifCtxDeclLE {$$=gc1(cons($1,NIL));}
242 ifCtxDeclLE /* M.C1 a :: (QConId,VarId) */
243 : ifQCon ifTyvar {$$=gc2(pair($1,$2));}
247 /*- Interface data declarations - constructor lists -------*/
248 ifConstrs /* = Con1 | ... | ConN :: [(ConId,[(Type,Text)],NIL)] */
250 | '=' ifConstrL {$$ = gc2($2);}
252 ifConstrL /* [(ConId,[(Type,Text)],NIL)] */
253 : ifConstr {$$ = gc1(singleton($1));}
254 | ifConstr '|' ifConstrL {$$ = gc3(cons($1,$3));}
256 ifConstr /* (ConId,[(Type,Text)],NIL) */
257 : ifConData ifDataAnonFieldL {$$ = gc2(triple($1,$2,NIL));}
258 | ifConData '{' ifDataNamedFieldL '}'
259 {$$ = gc4(triple($1,$3,NIL));}
261 ifDataAnonFieldL /* [(Type,Text)] */
263 | ifDataAnonField ifDataAnonFieldL
264 {$$=gc2(cons($1,$2));}
266 ifDataNamedFieldL /* [(Type,Text)] */
268 | ifDataNamedField {$$=gc1(cons($1,NIL));}
269 | ifDataNamedField ',' ifDataNamedFieldL
270 {$$=gc3(cons($1,$3));}
272 ifDataAnonField /* (Type,Text) */
273 : ifAType {$$=gc1(pair($1,NIL));}
275 ifDataNamedField /* (Type,Text) */
276 : VARID COCO ifAType {$$=gc3(pair($3,$1));}
280 /*- Interface class declarations - methods ----------------*/
281 ifCmeths /* [(VarId,Type)] */
283 | WHERE '{' ifCmethL '}' { $$ = gc4($3); }
285 ifCmethL /* [(VarId,Type)] */
286 : ifCmeth { $$ = gc1(singleton($1)); }
287 | ifCmeth ';' ifCmethL { $$ = gc3(cons($1,$3)); }
289 ifCmeth /* (VarId,Type) */
290 : ifVar COCO ifType { $$ = gc3(pair($1,$3)); }
291 | ifVar '=' COCO ifType { $$ = gc4(pair($1,$4)); }
292 /* has default method */
296 /*- Interface newtype declararions ------------------------*/
297 ifNewTypeConstr /* (ConId,Type) */
298 : '=' ifCon ifAType { $$ = gc3(pair($2,$3)); }
302 /*- Interface type expressions ----------------------------*/
303 ifType : ALL ifForall ifCtxDeclT IMPLIES ifType
306 $$=gc5(pair(QUAL,pair($3,$5)));
308 | ifBType ARROW ifType { $$ = gc3(fn($1,$3)); }
309 | ifBType { $$ = gc1($1); }
311 ifForall /* [(VarId,Kind)] */
312 : '[' ifKindedTyvarL ']' { $$ = gc3($2); }
314 ifTypes2 : ifType ',' ifType { $$ = gc3(doubleton($1,$3)); }
315 | ifType ',' ifTypes2 { $$ = gc3(cons($1,$3)); }
317 ifBType : ifAType { $$ = gc1($1); }
318 | ifBType ifAType { $$ = gc2(ap($1,$2)); }
320 ifAType : ifQTCName { $$ = gc1($1); }
321 | ifTyvar { $$ = gc1($1); }
322 | '(' ')' { $$ = gc2(typeUnit); }
323 | '(' ifTypes2 ')' { $$ = gc3(buildTuple($2)); }
324 | '[' ifType ']' { $$ = gc3(ap(typeList,$2));}
325 | '{' ifQTCName ifATypes '}' { $$ = gc4(ap(DICTAP,
327 | '(' ifType ')' { $$ = gc3($2); }
329 ifATypes : { $$ = gc0(NIL); }
330 | ifAType ifATypes { $$ = gc2(cons($1,$2)); }
334 /*- Interface kinds ---------------------------------------*/
335 ifKindedTyvarL /* [(VarId,Kind)] */
337 | ifKindedTyvar ifKindedTyvarL { $$ = gc2(cons($1,$2)); }
339 ifKindedTyvar /* (VarId,Kind) */
340 : ifTyvar { $$ = gc1(pair($1,STAR)); }
341 | ifTyvar COCO ifAKind { $$ = gc3(pair($1,$3)); }
343 ifKind : ifAKind { $$ = gc1($1); }
344 | ifAKind ARROW ifKind { $$ = gc3(fn($1,$3)); }
346 ifAKind : VAROP { $$ = gc1(STAR); }
348 | '(' ifKind ')' { $$ = gc3($2); }
352 /*- Interface version/export/import stuff -----------------*/
355 | ifEntity ifEntities { $$ = gc2(cons($1,$2)); }
358 : ifEntityOcc {$$=gc1($1);}
359 | ifEntityOcc ifStuffInside {$$=gc2(pair($1,$2));}
362 : ifVar { $$ = gc1($1); }
363 | ifCon { $$ = gc1($1); }
364 | ARROW { $$ = gc1(typeArrow); }
365 | '(' ARROW ')' { $$ = gc3(typeArrow); }
366 /* why allow both? */
369 : '{' ifValOccs '}' { $$ = gc3($2); }
373 | ifVar ifValOccs { $$ = gc2(cons($1,$2)); }
374 | ifCon ifValOccs { $$ = gc2(cons($1,$2)); }
378 | VARID NUMLIT version_list_junk {$$=gc3(cons($1,$3));}
379 | CONID NUMLIT version_list_junk {$$=gc3(cons($1,$3));}
383 /*- Haskell module header/import parsing: -----------------------------------
385 * Syntax for Haskell modules (module headers and imports) is parsed but
386 * most of it is ignored. However, module names in import declarations
387 * are used, of course, if import chasing is turned on.
388 *-------------------------------------------------------------------------*/
390 /* In Haskell 1.2, the default module header was "module Main where"
391 * In 1.3, this changed to "module Main(main) where".
392 * We use the 1.2 header because it breaks much less pre-module code.
394 topModule : startMain begin modBody end {
395 setExportList(singleton(
397 mkCon(module(currentModule).text)
401 | TMODULE modname expspec WHERE '{' modBody end
402 {setExportList($3); $$ = gc7($6);}
403 | TMODULE error {syntaxError("module definition");}
405 /* To implement the Haskell module system, we have to keep track of the
406 * current module. We rely on the use of LALR parsing to ensure that this
407 * side effect happens before any declarations within the module.
409 startMain : /* empty */ {startModule(conMain);
412 modname : CONID {startModule($1); $$ = gc1(NIL);}
414 modid : CONID {$$ = $1;}
415 | STRINGLIT { extern String scriptFile;
417 = findPathname(scriptFile,
418 textToStr(textOf($1)));
420 /* fillin pathname if known */
421 $$ = mkStr(findText(modName));
427 modBody : topDecls {$$ = $1;}
428 | impDecls chase {$$ = gc2(NIL);}
429 | impDecls ';' chase topDecls {$$ = gc4($4);}
432 /*- Exports: --------------------------------------------------------------*/
434 expspec : /* empty */ {$$ = gc0(exportSelf());}
435 | '(' ')' {$$ = gc2(NIL);}
436 | '(' exports ')' {$$ = gc3($2);}
437 | '(' exports ',' ')' {$$ = gc4($2);}
439 exports : exports ',' export {$$ = gc3(cons($3,$1));}
440 | export {$$ = gc1(singleton($1));}
442 /* The qcon should be qconid.
443 * Relaxing the rule lets us explicitly export (:) from the Prelude.
445 export : qvar {$$ = $1;}
447 | qconid '(' UPTO ')' {$$ = gc4(pair($1,DOTDOT));}
448 | qconid '(' qnames ')' {$$ = gc4(pair($1,$3));}
449 | TMODULE modid {$$ = gc2(ap(MODULEENT,$2));}
451 qnames : /* empty */ {$$ = gc0(NIL);}
452 | ',' {$$ = gc1(NIL);}
454 | qnames1 ',' {$$ = gc2($1);}
456 qnames1 : qnames1 ',' qname {$$ = gc3(cons($3,$1));}
457 | qname {$$ = gc1(singleton($1));}
459 qname : qvar {$$ = $1;}
463 /*- Import declarations: --------------------------------------------------*/
465 impDecls : impDecls ';' impDecl {imps = cons($3,imps); $$=gc3(NIL);}
466 | impDecl {imps = singleton($1); $$=gc1(NIL);}
468 chase : /* empty */ {if (chase(imps)) {
478 /* Note that qualified import ignores the import list. */
479 impDecl : IMPORT modid impspec {addQualImport($2,$2);
480 addUnqualImport($2,$3);
482 | IMPORT modid ASMOD modid impspec
483 {addQualImport($2,$4);
484 addUnqualImport($2,$5);
486 | IMPORT QUALIFIED modid ASMOD modid impspec
487 {addQualImport($3,$5);
489 | IMPORT QUALIFIED modid impspec
490 {addQualImport($3,$3);
492 | IMPORT error {syntaxError("import declaration");}
494 impspec : /* empty */ {$$ = gc0(DOTDOT);}
495 | HIDING '(' imports ')' {$$ = gc4(ap(HIDDEN,$3));}
496 | '(' imports ')' {$$ = gc3($2);}
498 imports : /* empty */ {$$ = gc0(NIL);}
499 | ',' {$$ = gc1(NIL);}
500 | imports1 {$$ = $1;}
501 | imports1 ',' {$$ = gc2($1);}
503 imports1 : imports1 ',' import {$$ = gc3(cons($3,$1));}
504 | import {$$ = gc1(singleton($1));}
506 import : var {$$ = $1;}
508 | CONID '(' UPTO ')' {$$ = gc4(pair($1,DOTDOT));}
509 | CONID '(' names ')' {$$ = gc4(pair($1,$3));}
511 names : /* empty */ {$$ = gc0(NIL);}
512 | ',' {$$ = gc1(NIL);}
514 | names1 ',' {$$ = gc2($1);}
516 names1 : names1 ',' name {$$ = gc3(cons($3,$1));}
517 | name {$$ = gc1(singleton($1));}
519 name : var {$$ = $1;}
523 /*- Top-level declarations: -----------------------------------------------*/
525 topDecls : /* empty */ {$$ = gc0(NIL);}
526 | ';' {$$ = gc1(NIL);}
527 | topDecls1 {$$ = $1;}
528 | topDecls1 ';' {$$ = gc2($1);}
530 topDecls1 : topDecls1 ';' topDecl {$$ = gc2($1);}
531 | topDecls1 ';' decl {$$ = gc3(cons($3,$1));}
532 | topDecl {$$ = gc0(NIL);}
533 | decl {$$ = gc1(cons($1,NIL));}
536 /*- Type declarations: ----------------------------------------------------*/
538 topDecl : TYPE tyLhs '=' type {defTycon(4,$3,$2,$4,SYNONYM);}
539 | TYPE tyLhs '=' type IN invars
541 ap($4,$6),RESTRICTSYN);}
542 | TYPE error {syntaxError("type definition");}
543 | DATA btype2 '=' constrs deriving
544 {defTycon(5,$3,checkTyLhs($2),
545 ap(rev($4),$5),DATATYPE);}
546 | DATA context IMPLIES tyLhs '=' constrs deriving
548 ap(qualify($2,rev($6)),
550 | DATA btype2 {defTycon(2,$1,checkTyLhs($2),
551 ap(NIL,NIL),DATATYPE);}
552 | DATA context IMPLIES tyLhs {defTycon(4,$1,$4,
555 | DATA error {syntaxError("data definition");}
556 | TNEWTYPE btype2 '=' nconstr deriving
557 {defTycon(5,$3,checkTyLhs($2),
559 | TNEWTYPE context IMPLIES tyLhs '=' nconstr deriving
563 | TNEWTYPE error {syntaxError("newtype definition");}
565 tyLhs : tyLhs varid {$$ = gc2(ap($1,$2));}
567 | error {syntaxError("type defn lhs");}
569 invars : invars ',' invar {$$ = gc3(cons($3,$1));}
570 | invar {$$ = gc1(cons($1,NIL));}
572 invar : var COCO topType {$$ = gc3(sigdecl($2,singleton($1),
576 constrs : constrs '|' pconstr {$$ = gc3(cons($3,$1));}
577 | pconstr {$$ = gc1(cons($1,NIL));}
579 pconstr : ALL varids '.' qconstr {$$ = gc4(ap(POLYTYPE,
583 qconstr : context IMPLIES constr {$$ = gc3(qualify($1,$3));}
586 constr : '!' btype conop bbtype {$$ = gc4(ap(ap($3,bang($2)),$4));}
587 | btype1 conop bbtype {$$ = gc3(ap(ap($2,$1),$3));}
588 | btype2 conop bbtype {$$ = gc3(ap(ap($2,$1),$3));}
589 | bpolyType conop bbtype {$$ = gc3(ap(ap($2,$1),$3));}
593 | con '{' fieldspecs '}' {$$ = gc4(ap(LABC,pair($1,rev($3))));}
594 | con '{' '}' {$$ = gc3(ap(LABC,pair($1,NIL)));}
595 | error {syntaxError("data type definition");}
597 btype3 : btype2 '!' atype {$$ = gc3(ap($1,bang($3)));}
598 | btype3 '!' atype {$$ = gc3(ap($1,bang($3)));}
599 | btype3 atype {$$ = gc2(ap($1,$2));}
601 btype4 : btype2 bpolyType {$$ = gc2(ap($1,$2));}
602 | btype3 bpolyType {$$ = gc2(ap($1,$2));}
603 | btype4 bpolyType {$$ = gc2(ap($1,$2));}
604 | btype4 atype {$$ = gc2(ap($1,$2));}
605 | btype4 '!' atype {$$ = gc3(ap($1,bang($3)));}
607 bbtype : '!' btype {$$ = gc2(bang($2));}
609 | bpolyType {$$ = $1;}
611 nconstr : pconstr {$$ = gc1(singleton($1));}
613 fieldspecs: fieldspecs ',' fieldspec {$$ = gc3(cons($3,$1));}
614 | fieldspec {$$ = gc1(cons($1,NIL));}
616 fieldspec : vars COCO polyType {$$ = gc3(pair(rev($1),$3));}
617 | vars COCO type {$$ = gc3(pair(rev($1),$3));}
618 | vars COCO '!' type {$$ = gc4(pair(rev($1),bang($4)));}
620 deriving : /* empty */ {$$ = gc0(NIL);}
621 | DERIVING qconid {$$ = gc2(singleton($2));}
622 | DERIVING '(' derivs0 ')' {$$ = gc4($3);}
624 derivs0 : /* empty */ {$$ = gc0(NIL);}
625 | derivs {$$ = gc1(rev($1));}
627 derivs : derivs ',' qconid {$$ = gc3(cons($3,$1));}
628 | qconid {$$ = gc1(singleton($1));}
631 /*- Processing definitions of primitives ----------------------------------*/
633 topDecl : FOREIGN IMPORT callconv ext_loc ext_name unsafe_flag var COCO type
634 {foreignImport($1,$3,pair($4,$5),$7,$9); sp-=9;}
635 | FOREIGN EXPORT callconv DYNAMIC qvarid COCO type
636 {foreignExport($1,$3,$4,$5,$7); sp-=7;}
639 callconv : CCALL {$$ = gc1(textCcall);}
640 | STDCALL {$$ = gc1(textStdcall);}
641 | /* empty */ {$$ = gc0(NIL);}
643 ext_loc : STRINGLIT {$$ = $1;}
645 ext_name : STRINGLIT {$$ = $1;}
647 unsafe_flag: /* empty */ {$$ = gc0(NIL);}
648 | UNSAFE {$$ = gc1(NIL); /* ignored */ }
652 /*- Class declarations: ---------------------------------------------------*/
654 topDecl : TCLASS crule fds wherePart {classDefn(intOf($1),$2,$4,$3); sp-=4;}
655 | TINSTANCE irule wherePart {instDefn(intOf($1),$2,$3); sp-=3;}
656 | DEFAULT '(' dtypes ')' {defaultDefn(intOf($1),$3); sp-=4;}
657 | TCLASS error {syntaxError("class declaration");}
658 | TINSTANCE error {syntaxError("instance declaration");}
659 | DEFAULT error {syntaxError("default declaration");}
661 crule : context IMPLIES btype2 {$$ = gc3(pair($1,checkPred($3)));}
662 | btype2 {$$ = gc1(pair(NIL,checkPred($1)));}
664 irule : context IMPLIES btype2 {$$ = gc3(pair($1,checkPred($3)));}
665 | btype2 {$$ = gc1(pair(NIL,checkPred($1)));}
667 dtypes : /* empty */ {$$ = gc0(NIL);}
668 | dtypes1 {$$ = gc1(rev($1));}
670 dtypes1 : dtypes1 ',' type {$$ = gc3(cons($3,$1));}
671 | type {$$ = gc1(cons($1,NIL));}
674 fds : /* empty */ {$$ = gc0(NIL);}
675 | '|' fds1 {h98DoesntSupport(row,"dependent parameters");
678 fds1 : fds1 ',' fd {$$ = gc3(cons($3,$1));}
679 | fd {$$ = gc1(cons($1,NIL));}
682 fd : varids0 ARROW varids0 {$$ = gc3(pair(rev($1),rev($3)));}
684 varids0 : /* empty */ {$$ = gc0(NIL);}
685 | varids0 varid {$$ = gc2(cons($2,$1));}
688 /*- Type expressions: -----------------------------------------------------*/
690 topType : ALL varids '.' topType0 {$$ = gc4(ap(POLYTYPE,
692 | topType0 {$$ = $1;}
694 topType0 : context IMPLIES topType1 {$$ = gc3(qualify($1,$3));}
695 | topType1 {$$ = $1;}
697 topType1 : bpolyType ARROW topType1 {$$ = gc3(fn($1,$3));}
698 | btype1 ARROW topType1 {$$ = gc3(fn($1,$3));}
699 | btype2 ARROW topType1 {$$ = gc3(fn($1,$3));}
702 polyType : ALL varids '.' sigType {$$ = gc4(ap(POLYTYPE,
704 | context IMPLIES type {$$ = gc3(qualify($1,$3));}
705 | bpolyType {$$ = $1;}
707 bpolyType : '(' polyType ')' {$$ = gc3($2);}
709 varids : varids varid {$$ = gc2(cons($2,$1));}
710 | varid {$$ = gc1(singleton($1));}
712 sigType : context IMPLIES type {$$ = gc3(qualify($1,$3));}
715 context : '(' ')' {$$ = gc2(NIL);}
716 | btype2 {$$ = gc1(singleton(checkPred($1)));}
717 | '(' btype2 ')' {$$ = gc3(singleton(checkPred($2)));}
718 | '(' btypes2 ')' {$$ = gc3(checkCtxt(rev($2)));}
720 | lacks {$$ = gc1(singleton($1));}
721 | '(' lacks1 ')' {$$ = gc3(checkCtxt(rev($2)));}
723 lacks : varid '\\' varid {
725 $$ = gc3(ap(mkExt(textOf($3)),$1));
727 noTREX("a type context");
730 | IPVARID COCO type {
732 $$ = gc3(pair(mkIParam($1),$3));
734 noIP("a type context");
738 lacks1 : btypes2 ',' lacks {$$ = gc3(cons($3,$1));}
739 | lacks1 ',' btype2 {$$ = gc3(cons($3,$1));}
740 | lacks1 ',' lacks {$$ = gc3(cons($3,$1));}
741 | btype2 ',' lacks {$$ = gc3(cons($3,cons($1,NIL)));}
742 | lacks {$$ = gc1(singleton($1));}
746 type : type1 {$$ = $1;}
749 type1 : btype1 {$$ = $1;}
750 | btype1 ARROW type {$$ = gc3(fn($1,$3));}
751 | btype2 ARROW type {$$ = gc3(fn($1,$3));}
752 | error {syntaxError("type expression");}
754 btype : btype1 {$$ = $1;}
757 btype1 : btype1 atype {$$ = gc2(ap($1,$2));}
760 btype2 : btype2 atype {$$ = gc2(ap($1,$2));}
763 atype : atype1 {$$ = $1;}
766 atype1 : varid {$$ = $1;}
767 | '(' ')' {$$ = gc2(typeUnit);}
768 | '(' ARROW ')' {$$ = gc3(typeArrow);}
769 | '(' type1 ')' {$$ = gc3($2);}
770 | '(' btype2 ')' {$$ = gc3($2);}
771 | '(' tupCommas ')' {$$ = gc3($2);}
772 | '(' btypes2 ')' {$$ = gc3(buildTuple($2));}
773 | '(' typeTuple ')' {$$ = gc3(buildTuple($2));}
776 $$ = gc3(revOnto($2,typeNoRow));
781 | '(' tfields '|' type ')' {
783 $$ = gc5(revOnto($2,$4));
788 | '[' type ']' {$$ = gc3(ap(typeList,$2));}
789 | '[' ']' {$$ = gc2(typeList);}
790 | '_' {h98DoesntSupport(row,"anonymous type variables");
791 $$ = gc1(inventVar());}
793 btypes2 : btypes2 ',' btype2 {$$ = gc3(cons($3,$1));}
794 | btype2 ',' btype2 {$$ = gc3(cons($3,cons($1,NIL)));}
796 typeTuple : type1 ',' type {$$ = gc3(cons($3,cons($1,NIL)));}
797 | btype2 ',' type1 {$$ = gc3(cons($3,cons($1,NIL)));}
798 | btypes2 ',' type1 {$$ = gc3(cons($3,$1));}
799 | typeTuple ',' type {$$ = gc3(cons($3,$1));}
802 tfields : tfields ',' tfield {$$ = gc3(cons($3,$1));}
803 | tfield {$$ = gc1(singleton($1));}
805 tfield : varid COCO type {h98DoesntSupport(row,"extensible records");
806 $$ = gc3(ap(mkExt(textOf($1)),$3));}
810 /*- Value declarations: ---------------------------------------------------*/
812 gendecl : INFIXN optDigit ops {$$ = gc3(fixdecl($1,$3,NON_ASS,$2));}
813 | INFIXN error {syntaxError("fixity decl");}
814 | INFIXL optDigit ops {$$ = gc3(fixdecl($1,$3,LEFT_ASS,$2));}
815 | INFIXL error {syntaxError("fixity decl");}
816 | INFIXR optDigit ops {$$ = gc3(fixdecl($1,$3,RIGHT_ASS,$2));}
817 | INFIXR error {syntaxError("fixity decl");}
818 | vars COCO topType {$$ = gc3(sigdecl($2,$1,$3));}
819 | vars COCO error {syntaxError("type signature");}
821 optDigit : NUMLIT {$$ = gc1(checkPrec($1));}
822 | /* empty */ {$$ = gc0(mkInt(DEF_PREC));}
824 ops : ops ',' op {$$ = gc3(cons($3,$1));}
825 | op {$$ = gc1(singleton($1));}
827 vars : vars ',' var {$$ = gc3(cons($3,$1));}
828 | var {$$ = gc1(singleton($1));}
830 decls : '{' decls0 end {$$ = gc3($2);}
831 | '{' decls1 end {$$ = gc3($2);}
833 decls0 : /* empty */ {$$ = gc0(NIL);}
834 | decls0 ';' {$$ = gc2($1);}
835 | decls1 ';' {$$ = gc2($1);}
837 decls1 : decls0 decl {$$ = gc2(cons($2,$1));}
839 decl : gendecl {$$ = $1;}
840 | funlhs rhs {$$ = gc2(ap(FUNBIND,pair($1,$2)));}
841 | funlhs COCO type rhs {$$ = gc4(ap(FUNBIND,
844 | pat0 rhs {$$ = gc2(ap(PATBIND,pair($1,$2)));}
846 funlhs : funlhs0 {$$ = $1;}
850 funlhs0 : pat10_vI varop pat0 {$$ = gc3(ap2($2,$1,$3));}
851 | infixPat varop pat0 {$$ = gc3(ap2($2,$1,$3));}
852 | NUMLIT varop pat0 {$$ = gc3(ap2($2,$1,$3));}
853 | var varop_pl pat0 {$$ = gc3(ap2($2,$1,$3));}
854 | var '+' pat0_INT {$$ = gc3(ap2(varPlus,$1,$3));}
856 funlhs1 : '(' funlhs0 ')' apat {$$ = gc4(ap($2,$4));}
857 | '(' funlhs1 ')' apat {$$ = gc4(ap($2,$4));}
858 | '(' npk ')' apat {$$ = gc4(ap($2,$4));}
859 | var apat {$$ = gc2(ap($1,$2));}
860 | funlhs1 apat {$$ = gc2(ap($1,$2));}
862 rhs : rhs1 wherePart {$$ = gc2(letrec($2,$1));}
863 | error {syntaxError("declaration");}
865 rhs1 : '=' exp {$$ = gc2(pair($1,$2));}
866 | gdrhs {$$ = gc1(grded(rev($1)));}
868 gdrhs : gdrhs gddef {$$ = gc2(cons($2,$1));}
869 | gddef {$$ = gc1(singleton($1));}
871 gddef : '|' exp0 '=' exp {$$ = gc4(pair($3,pair($2,$4)));}
873 wherePart : /* empty */ {$$ = gc0(NIL);}
874 | WHERE decls {$$ = gc2($2);}
877 /*- Patterns: -------------------------------------------------------------*/
882 pat_npk : pat0 COCO type {$$ = gc3(ap(ESIGN,pair($1,$3)));}
885 npk : var '+' NUMLIT {$$ = gc3(ap2(varPlus,$1,$3));}
887 pat0 : var {$$ = $1;}
891 pat0_INT : var {$$ = $1;}
894 pat0_vI : pat10_vI {$$ = $1;}
895 | infixPat {$$ = gc1(ap(INFIX,$1));}
897 infixPat : '-' pat10 {$$ = gc2(ap(NEG,only($2)));}
898 | '-' error {syntaxError("pattern");}
899 | var qconop pat10 {$$ = gc3(ap(ap($2,only($1)),$3));}
900 | var qconop '-' pat10 {$$ = gc4(ap(NEG,ap2($2,only($1),$4)));}
901 | NUMLIT qconop pat10 {$$ = gc3(ap(ap($2,only($1)),$3));}
902 | NUMLIT qconop '-' pat10 {$$ = gc4(ap(NEG,ap2($2,only($1),$4)));}
903 | pat10_vI qconop pat10 {$$ = gc3(ap(ap($2,only($1)),$3));}
904 | pat10_vI qconop '-' pat10 {$$ = gc4(ap(NEG,ap2($2,only($1),$4)));}
905 | infixPat qconop pat10 {$$ = gc3(ap(ap($2,$1),$3));}
906 | infixPat qconop '-' pat10 {$$ = gc4(ap(NEG,ap(ap($2,$1),$4)));}
908 pat10 : fpat {$$ = $1;}
911 pat10_vI : fpat {$$ = $1;}
914 fpat : fpat apat {$$ = gc2(ap($1,$2));}
915 | gcon apat {$$ = gc2(ap($1,$2));}
917 apat : NUMLIT {$$ = $1;}
921 apat_vI : var '@' apat {$$ = gc3(ap(ASPAT,pair($1,$3)));}
923 | qcon '{' patbinds '}' {$$ = gc4(ap(CONFLDS,pair($1,$3)));}
925 | STRINGLIT {$$ = $1;}
926 | '_' {$$ = gc1(WILDCARD);}
927 | '(' pat_npk ')' {$$ = gc3($2);}
928 | '(' npk ')' {$$ = gc3($2);}
929 | '(' pats2 ')' {$$ = gc3(buildTuple($2));}
930 | '[' pats1 ']' {$$ = gc3(ap(FINLIST,rev($2)));}
931 | '~' apat {$$ = gc2(ap(LAZYPAT,$2));}
933 | '(' patfields ')' {
935 $$ = gc3(revOnto($2,nameNoRec));
940 | '(' patfields '|' pat ')' {$$ = gc5(revOnto($2,$4));}
943 pats2 : pats2 ',' pat {$$ = gc3(cons($3,$1));}
944 | pat ',' pat {$$ = gc3(cons($3,singleton($1)));}
946 pats1 : pats1 ',' pat {$$ = gc3(cons($3,$1));}
947 | pat {$$ = gc1(singleton($1));}
949 patbinds : /* empty */ {$$ = gc0(NIL);}
950 | patbinds1 {$$ = gc1(rev($1));}
952 patbinds1 : patbinds1 ',' patbind {$$ = gc3(cons($3,$1));}
953 | patbind {$$ = gc1(singleton($1));}
955 patbind : qvar '=' pat {$$ = gc3(pair($1,$3));}
959 patfields : patfields ',' patfield {$$ = gc3(cons($3,$1));}
960 | patfield {$$ = gc1(singleton($1));}
962 patfield : varid '=' pat {
964 $$ = gc3(ap(mkExt(textOf($1)),$3));
972 /*- Expressions: ----------------------------------------------------------*/
974 exp : exp_err {$$ = $1;}
975 | error {syntaxError("expression");}
977 exp_err : exp0a COCO sigType {$$ = gc3(ap(ESIGN,pair($1,$3)));}
978 | exp0a WITH dbinds {
980 $$ = gc3(ap(WITHEXP,pair($1,$3)));
982 noIP("an expression");
987 exp0 : exp0a {$$ = $1;}
990 exp0a : infixExpa {$$ = gc1(ap(INFIX,$1));}
993 exp0b : infixExpb {$$ = gc1(ap(INFIX,$1));}
996 infixExpa : infixExpa qop '-' exp10a {$$ = gc4(ap(NEG,ap(ap($2,$1),$4)));}
997 | infixExpa qop exp10a {$$ = gc3(ap(ap($2,$1),$3));}
998 | '-' exp10a {$$ = gc2(ap(NEG,only($2)));}
999 | exp10a qop '-' exp10a {$$ = gc4(ap(NEG,
1000 ap(ap($2,only($1)),$4)));}
1001 | exp10a qop exp10a {$$ = gc3(ap(ap($2,only($1)),$3));}
1003 infixExpb : infixExpa qop '-' exp10b {$$ = gc4(ap(NEG,ap(ap($2,$1),$4)));}
1004 | infixExpa qop exp10b {$$ = gc3(ap(ap($2,$1),$3));}
1005 | '-' exp10b {$$ = gc2(ap(NEG,only($2)));}
1006 | exp10a qop '-' exp10b {$$ = gc4(ap(NEG,
1007 ap(ap($2,only($1)),$4)));}
1008 | exp10a qop exp10b {$$ = gc3(ap(ap($2,only($1)),$3));}
1010 exp10a : CASEXP exp OF '{' alts end {$$ = gc6(ap(CASE,pair($2,rev($5))));}
1011 | DO '{' stmts end {$$ = gc4(ap(DOCOMP,checkDo($3)));}
1014 exp10b : '\\' pats ARROW exp {$$ = gc4(ap(LAMBDA,
1017 | LET decls IN exp {$$ = gc4(letrec($2,$4));}
1018 | IF exp THEN exp ELSE exp {$$ = gc6(ap(COND,triple($2,$4,$6)));}
1019 | DLET dbinds IN exp {
1021 $$ = gc4(ap(WITHEXP,pair($4,$2)));
1023 noIP("an expression");
1027 pats : pats apat {$$ = gc2(cons($2,$1));}
1028 | apat {$$ = gc1(cons($1,NIL));}
1030 appExp : appExp aexp {$$ = gc2(ap($1,$2));}
1033 aexp : qvar {$$ = $1;}
1034 | qvar '@' aexp {$$ = gc3(ap(ASPAT,pair($1,$3)));}
1035 | '~' aexp {$$ = gc2(ap(LAZYPAT,$2));}
1036 | IPVARID {$$ = $1;}
1037 | '_' {$$ = gc1(WILDCARD);}
1039 | qcon '{' fbinds '}' {$$ = gc4(ap(CONFLDS,pair($1,$3)));}
1040 | aexp '{' fbinds '}' {$$ = gc4(ap(UPDFLDS,
1041 triple($1,NIL,$3)));}
1043 | CHARLIT {$$ = $1;}
1044 | STRINGLIT {$$ = $1;}
1046 | '(' exp ')' {$$ = gc3($2);}
1047 | '(' exps2 ')' {$$ = gc3(buildTuple($2));}
1051 $$ = gc3(revOnto($2,nameNoRec));
1056 | '(' vfields '|' exp ')' {$$ = gc5(revOnto($2,$4));}
1057 | RECSELID {$$ = $1;}
1059 | '[' list ']' {$$ = gc3($2);}
1060 | '(' exp10a qop ')' {$$ = gc4(ap($3,$2));}
1061 | '(' qvarop_mi exp0 ')' {$$ = gc4(ap(ap(nameFlip,$2),$3));}
1062 | '(' qconop exp0 ')' {$$ = gc4(ap(ap(nameFlip,$2),$3));}
1064 exps2 : exps2 ',' exp {$$ = gc3(cons($3,$1));}
1065 | exp ',' exp {$$ = gc3(cons($3,cons($1,NIL)));}
1068 vfields : vfields ',' vfield {$$ = gc3(cons($3,$1));}
1069 | vfield {$$ = gc1(singleton($1));}
1071 vfield : varid '=' exp {
1073 $$ = gc3(ap(mkExt(textOf($1)),$3));
1075 noTREX("an expression");
1080 alts : alts1 {$$ = $1;}
1081 | alts1 ';' {$$ = gc2($1);}
1083 alts1 : alts1 ';' alt {$$ = gc3(cons($3,$1));}
1084 | alt {$$ = gc1(cons($1,NIL));}
1086 alt : pat altRhs wherePart {$$ = gc3(pair($1,letrec($3,$2)));}
1088 altRhs : guardAlts {$$ = gc1(grded(rev($1)));}
1089 | ARROW exp {$$ = gc2(pair($1,$2));}
1090 | error {syntaxError("case expression");}
1092 guardAlts : guardAlts guardAlt {$$ = gc2(cons($2,$1));}
1093 | guardAlt {$$ = gc1(cons($1,NIL));}
1095 guardAlt : '|' exp0 ARROW exp {$$ = gc4(pair($3,pair($2,$4)));}
1097 stmts : stmts1 ';' {$$ = gc2($1);}
1100 stmts1 : stmts1 ';' stmt {$$ = gc3(cons($3,$1));}
1101 | stmt {$$ = gc1(cons($1,NIL));}
1103 stmt : exp_err FROM exp {$$ = gc3(ap(FROMQUAL,pair($1,$3)));}
1104 | LET decls {$$ = gc2(ap(QWHERE,$2));}
1105 /* | IF exp {$$ = gc2(ap(BOOLQUAL,$2));}*/
1106 | exp_err {$$ = gc1(ap(DOQUAL,$1));}
1108 fbinds : /* empty */ {$$ = gc0(NIL);}
1109 | fbinds1 {$$ = gc1(rev($1));}
1111 fbinds1 : fbinds1 ',' fbind {$$ = gc3(cons($3,$1));}
1112 | fbind {$$ = gc1(singleton($1));}
1114 fbind : var {$$ = $1;}
1115 | qvar '=' exp {$$ = gc3(pair($1,$3));}
1118 dbinds : '{' dbs0 end {$$ = gc3($2);}
1119 | '{' dbs1 end {$$ = gc3($2);}
1121 dbs0 : /* empty */ {$$ = gc0(NIL);}
1122 | dbs0 ';' {$$ = gc2($1);}
1123 | dbs1 ';' {$$ = gc2($1);}
1125 dbs1 : dbs0 dbind {$$ = gc2(cons($2,$1));}
1127 dbind : IPVARID '=' exp {$$ = gc3(pair($1,$3));}
1130 /*- List Expressions: -------------------------------------------------------*/
1132 list : exp {$$ = gc1(ap(FINLIST,cons($1,NIL)));}
1133 | exps2 {$$ = gc1(ap(FINLIST,rev($1)));}
1134 | exp '|' quals {$$ = gc3(ap(COMP,pair($1,rev($3))));}
1135 | exp UPTO exp {$$ = gc3(ap(ap(nameFromTo,$1),$3));}
1136 | exp ',' exp UPTO {$$ = gc4(ap(ap(nameFromThen,$1),$3));}
1137 | exp UPTO {$$ = gc2(ap(nameFrom,$1));}
1138 | exp ',' exp UPTO exp {$$ = gc5(ap(ap(ap(nameFromThenTo,
1141 quals : quals ',' qual {$$ = gc3(cons($3,$1));}
1142 | qual {$$ = gc1(cons($1,NIL));}
1144 qual : exp FROM exp {$$ = gc3(ap(FROMQUAL,pair($1,$3)));}
1145 | exp {$$ = gc1(ap(BOOLQUAL,$1));}
1146 | LET decls {$$ = gc2(ap(QWHERE,$2));}
1149 /*- Identifiers and symbols: ----------------------------------------------*/
1151 gcon : qcon {$$ = $1;}
1152 | '(' ')' {$$ = gc2(nameUnit);}
1153 | '[' ']' {$$ = gc2(nameNil);}
1154 | '(' tupCommas ')' {$$ = gc3($2);}
1156 tupCommas : tupCommas ',' {$$ = gc2(mkTuple(tupleOf($1)+1));}
1157 | ',' {$$ = gc1(mkTuple(2));}
1159 varid : VARID {$$ = $1;}
1160 | HIDING {$$ = gc1(varHiding);}
1161 | QUALIFIED {$$ = gc1(varQualified);}
1162 | ASMOD {$$ = gc1(varAsMod);}
1164 qconid : QCONID {$$ = $1;}
1167 var : varid {$$ = $1;}
1168 | '(' VAROP ')' {$$ = gc3($2);}
1169 | '(' '+' ')' {$$ = gc3(varPlus);}
1170 | '(' '-' ')' {$$ = gc3(varMinus);}
1171 | '(' '!' ')' {$$ = gc3(varBang);}
1172 | '(' '.' ')' {$$ = gc3(varDot);}
1174 qvar : QVARID {$$ = $1;}
1175 | '(' QVAROP ')' {$$ = gc3($2);}
1178 con : CONID {$$ = $1;}
1179 | '(' CONOP ')' {$$ = gc3($2);}
1181 qcon : QCONID {$$ = $1;}
1182 | '(' QCONOP ')' {$$ = gc3($2);}
1185 varop : '+' {$$ = gc1(varPlus);}
1186 | '-' {$$ = gc1(varMinus);}
1187 | varop_mipl {$$ = $1;}
1189 varop_mi : '+' {$$ = gc1(varPlus);}
1190 | varop_mipl {$$ = $1;}
1192 varop_pl : '-' {$$ = gc1(varMinus);}
1193 | varop_mipl {$$ = $1;}
1195 varop_mipl: VAROP {$$ = $1;}
1196 | '`' varid '`' {$$ = gc3($2);}
1197 | '!' {$$ = gc1(varBang);}
1198 | '.' {$$ = gc1(varDot);}
1200 qvarop : '-' {$$ = gc1(varMinus);}
1201 | qvarop_mi {$$ = $1;}
1203 qvarop_mi : QVAROP {$$ = $1;}
1204 | '`' QVARID '`' {$$ = gc3($2);}
1205 | varop_mi {$$ = $1;}
1208 conop : CONOP {$$ = $1;}
1209 | '`' CONID '`' {$$ = gc3($2);}
1211 qconop : QCONOP {$$ = $1;}
1212 | '`' QCONID '`' {$$ = gc3($2);}
1215 op : varop {$$ = $1;}
1218 qop : qvarop {$$ = $1;}
1222 /*- Stuff from STG hugs ---------------------------------------------------*/
1224 qvarid : varid1 {$$ = gc1($1);}
1225 | QVARID {$$ = gc1($1);}
1227 varid1 : VARID {$$ = gc1($1);}
1228 | HIDING {$$ = gc1(varHiding);}
1229 | QUALIFIED {$$ = gc1(varQualified);}
1230 | ASMOD {$$ = gc1(varAsMod);}
1233 /*- Tricks to force insertion of leading and closing braces ---------------*/
1235 begin : error {yyerrok;
1236 if (offsideON) goOffside(startColumn);}
1238 /* deal with trailing semicolon */
1239 end : '}' {$$ = $1;}
1241 if (offsideON && canUnOffside()) {
1243 /* insert extra token on stack*/
1245 pushed(0) = pushed(1);
1246 pushed(1) = mkInt(column);
1249 syntaxError("definition");
1253 /*-------------------------------------------------------------------------*/
1257 static Cell local gcShadow(n,e) /* keep parsed fragments on stack */
1260 /* If a look ahead token is held then the required stack transformation
1263 * x1 | ... | xn | la ===> e | la
1266 * Otherwise, the transformation is:
1268 * x1 | ... | xn ===> e
1272 pushed(n-1) = top();
1281 static Void local syntaxError(s) /* report on syntax error */
1283 ERRMSG(row) "Syntax error in %s (unexpected %s)", s, unexpected()
1287 static String local unexpected() { /* find name for unexpected token */
1288 static char buffer[100];
1289 static char *fmt = "%s \"%s\"";
1290 static char *kwd = "keyword";
1293 case 0 : return "end of input";
1295 #define keyword(kw) sprintf(buffer,fmt,kwd,kw); return buffer;
1296 case INFIXL : keyword("infixl");
1297 case INFIXR : keyword("infixr");
1298 case INFIXN : keyword("infix");
1299 case FOREIGN : keyword("foreign");
1300 case UNSAFE : keyword("unsafe");
1301 case TINSTANCE : keyword("instance");
1302 case TCLASS : keyword("class");
1303 case CASEXP : keyword("case");
1304 case OF : keyword("of");
1305 case IF : keyword("if");
1306 case THEN : keyword("then");
1307 case ELSE : keyword("else");
1308 case WHERE : keyword("where");
1309 case TYPE : keyword("type");
1310 case DATA : keyword("data");
1311 case TNEWTYPE : keyword("newtype");
1312 case LET : keyword("let");
1313 case IN : keyword("in");
1314 case DERIVING : keyword("deriving");
1315 case DEFAULT : keyword("default");
1316 case IMPORT : keyword("import");
1317 case TMODULE : keyword("module");
1318 /* AJG: Hugs98/Classic use the keyword forall
1319 rather than __forall.
1320 Agree on one or the other
1322 case ALL : keyword("__forall");
1324 case DLET : keyword("dlet");
1325 case WITH : keyword("with");
1329 case ARROW : return "`->'";
1330 case '=' : return "`='";
1331 case COCO : return "`::'";
1332 case '-' : return "`-'";
1333 case '!' : return "`!'";
1334 case ',' : return "comma";
1335 case '@' : return "`@'";
1336 case '(' : return "`('";
1337 case ')' : return "`)'";
1338 case '{' : return "`{', possibly due to bad layout";
1339 case '}' : return "`}', possibly due to bad layout";
1340 case '_' : return "`_'";
1341 case '|' : return "`|'";
1342 case '.' : return "`.'";
1343 case ';' : return "`;', possibly due to bad layout";
1344 case UPTO : return "`..'";
1345 case '[' : return "`['";
1346 case ']' : return "`]'";
1347 case FROM : return "`<-'";
1348 case '\\' : return "backslash (lambda)";
1349 case '~' : return "tilde";
1350 case '`' : return "backquote";
1352 case RECSELID : sprintf(buffer,"selector \"#%s\"",
1353 textToStr(extText(snd(yylval))));
1357 case IPVARID : sprintf(buffer,"implicit parameter \"?%s\"",
1358 textToStr(textOf(yylval)));
1364 case CONID : sprintf(buffer,"symbol \"%s\"",
1365 textToStr(textOf(yylval)));
1370 case QCONID : sprintf(buffer,"symbol \"%s\"",
1371 identToStr(yylval));
1373 case HIDING : return "symbol \"hiding\"";
1374 case QUALIFIED : return "symbol \"qualified\"";
1375 case ASMOD : return "symbol \"as\"";
1376 case NUMLIT : return "numeric literal";
1377 case CHARLIT : return "character literal";
1378 case STRINGLIT : return "string literal";
1379 case IMPLIES : return "`=>'";
1380 default : return "token";
1384 static Cell local checkPrec(p) /* Check for valid precedence value*/
1386 if (!isInt(p) || intOf(p)<MIN_PREC || intOf(p)>MAX_PREC) {
1387 ERRMSG(row) "Precedence value must be an integer in the range [%d..%d]",
1394 static Cell local buildTuple(tup) /* build tuple (x1,...,xn) from */
1395 List tup; { /* list [xn,...,x1] */
1401 x = fst(t); /* / \ / \ */
1402 fst(t) = snd(t); /* xn . . xn */
1403 snd(t) = x; /* . ===> . */
1405 t = fun(x); /* . . */
1407 } while (nonNull(t)); /* x1 NIL (n) x1 */
1408 fst(x) = mkTuple(n);
1412 static List local checkCtxt(con) /* validate context */
1414 mapOver(checkPred, con);
1418 static Cell local checkPred(c) /* check that type expr is a valid */
1419 Cell c; { /* constraint */
1420 Cell cn = getHead(c);
1422 if (isExt(cn) && argCount==1)
1429 if (!isQCon(cn) /*|| argCount==0*/)
1430 syntaxError("class expression");
1434 static Pair local checkDo(dqs) /* convert reversed list of dquals */
1435 List dqs; { /* to an (expr,quals) pair */
1436 if (isNull(dqs) || whatIs(hd(dqs))!=DOQUAL) {
1437 ERRMSG(row) "Last generator in do {...} must be an expression"
1440 fst(dqs) = snd(fst(dqs)); /* put expression in fst of pair */
1441 snd(dqs) = rev(snd(dqs)); /* & reversed list of quals in snd */
1445 static Cell local checkTyLhs(c) /* check that lhs is of the form */
1446 Cell c; { /* T a1 ... a */
1448 while (isAp(tlhs) && whatIs(arg(tlhs))==VARIDCELL) {
1451 if (whatIs(tlhs)!=CONIDCELL) {
1452 ERRMSG(row) "Illegal left hand side in datatype definition"
1460 static Void local noTREX(where)
1462 ERRMSG(row) "Attempt to use TREX records while parsing %s.\n", where ETHEN
1463 ERRTEXT "(TREX is disabled in this build of Hugs)"
1468 static Void local noIP(where)
1470 ERRMSG(row) "Attempt to use Implicit Parameters while parsing %s.\n", where ETHEN
1471 ERRTEXT "(Implicit Parameters are disabled in this build of Hugs)"
1476 /*-------------------------------------------------------------------------*/