1 /**********************************************************************
4 * Syntax-related Utility Functions *
7 **********************************************************************/
13 #include "constants.h"
18 #include "hsparser.tab.h"
21 extern short icontexts;
23 extern unsigned endlineno, startlineno;
24 extern BOOLEAN hashIds, etags;
26 /* Forward Declarations */
28 char *ineg PROTO((char *));
29 static tree unparen PROTO((tree));
30 static void is_conapp_patt PROTO((int, tree, tree));
31 static void rearrangeprec PROTO((tree, tree));
32 static void error_if_expr_wanted PROTO((int, char *));
33 static void error_if_patt_wanted PROTO((int, char *));
35 qid fns[MAX_CONTEXTS] = { NULL };
36 BOOLEAN samefn[MAX_CONTEXTS] = { FALSE };
37 tree prevpatt[MAX_CONTEXTS] = { NULL };
39 static BOOLEAN checkorder2 PROTO((binding, BOOLEAN));
40 static BOOLEAN checksig PROTO((BOOLEAN, binding));
43 check infix value in range 0..9
52 sscanf(vals,"%d",&value);
54 if (value < 0 || value > 9)
57 value = value < 0 ? 0 : 9;
58 fprintf(stderr,"Precedence must be between 0 and 9 (value given: %d, changed to %d)\n",
66 Check Previous Pattern usage
73 char *this = qid_to_string(fn);
74 char *was = (FN==NULL) ? NULL : qid_to_string(FN);
76 SAMEFN = (was != NULL && strcmp(this,was) == 0);
80 printf("%u\n",startlineno);
82 fprintf(stderr,"%u\tchecksamefn:%s\n",startlineno,this);
87 /* ------------------------------------------------------------------------
91 expORpat(int wanted, tree e)
95 case ident: /* a pattern or expr */
99 error_if_expr_wanted(wanted, "wildcard in expression");
103 error_if_expr_wanted(wanted, "`as'-pattern instead of an expression");
104 expORpat(wanted, gase(e));
108 error_if_expr_wanted(wanted, "irrefutable pattern instead of an expression");
109 expORpat(wanted, glazyp(e));
116 switch (tliteral(glit(e))) {
126 break; /* pattern or expr */
129 error_if_patt_wanted(wanted, "``literal-literal'' in pattern");
131 default: /* the others only occur in pragmas */
132 hsperror("not a valid literal pattern or expression");
137 { tree sub = gnexp(e);
138 if (ttree(sub) != lit) {
139 error_if_patt_wanted(wanted, "\"-\" applied to a non-literal");
141 literal l = glit(sub);
143 if (tliteral(l) != integer && tliteral(l) != floatr) {
144 error_if_patt_wanted(wanted, "\"-\" applied to a non-number");
147 expORpat(wanted, sub);
156 is_conapp_patt(wanted, f, a); /* does nothing unless wanted == LEGIT_PATT */
164 qid f = ginffun ((struct Sinfixap *)e);
165 tree a1 = ginfarg1((struct Sinfixap *)e);
166 tree a2 = ginfarg2((struct Sinfixap *)e);
168 expORpat(wanted, a1);
169 expORpat(wanted, a2);
171 if (wanted == LEGIT_PATT && !isconstr(qid_to_string(f)))
172 hsperror("variable application in pattern");
179 for (field = grbinds(e); tlist(field) == lcons; field = ltl(field)) {
180 expORpat(wanted, lhd(field));
186 if (tmaybe(grbindexp(e)) == just)
187 expORpat(wanted, gthing(grbindexp(e)));
193 for (tup = gtuplelist(e); tlist(tup) == lcons; tup = ltl(tup)) {
194 expORpat(wanted, lhd(tup));
202 for (l = gllist(e); tlist(l) == lcons; l = ltl(l)) {
203 expORpat(wanted, lhd(l));
208 case par: /* parenthesised */
209 expORpat(wanted, gpare(e));
225 error_if_patt_wanted(wanted, "unexpected construct in a pattern");
229 hsperror("not a pattern or expression");
234 is_conapp_patt(int wanted, tree f, tree a)
236 if (wanted == LEGIT_EXPR)
237 return; /* that was easy */
242 if (isconstr(qid_to_string(gident(f))))
248 char errbuf[ERR_BUF_SIZE];
249 sprintf(errbuf,"not a constructor application -- %s",qid_to_string(gident(f)));
254 is_conapp_patt(wanted, gfun(f), garg(f));
259 is_conapp_patt(wanted, gpare(f), a);
264 char errbuf[ERR_BUF_SIZE];
265 sprintf(errbuf,"tuple pattern `applied' to arguments (missing comma?)");
271 hsperror("not a constructor application");
276 error_if_expr_wanted(int wanted, char *msg)
278 if (wanted == LEGIT_EXPR)
283 error_if_patt_wanted(int wanted, char *msg)
285 if (wanted == LEGIT_PATT)
289 /* ---------------------------------------------------------------------- */
291 BOOLEAN /* return TRUE if LHS is a pattern */
297 switch (tliteral(glit(e))) {
309 hsperror("Literal is not a valid LHS");
320 expORpat(LEGIT_PATT, e);
328 tree f = function(e);
329 tree a = garg(e); /* do not "unparen", otherwise the error
330 fromInteger ((x,y) {-no comma-} z)
334 /* definitions must have pattern arguments */
335 expORpat(LEGIT_PATT, a);
337 if(ttree(f) == ident)
338 return(isconstr(qid_to_string(gident(f))));
340 else if(ttree(f) == infixap)
341 return(lhs_is_patt(f));
344 hsperror("Not a legal pattern binding in LHS");
349 qid f = ginffun((struct Sinfixap *)e);
350 tree a1 = unparen(ginfarg1((struct Sinfixap *)e)),
351 a2 = unparen(ginfarg2((struct Sinfixap *)e));
353 /* definitions must have pattern arguments */
354 expORpat(LEGIT_PATT, a1);
355 expORpat(LEGIT_PATT, a2);
357 return(isconstr(qid_to_string(f)));
361 return(lhs_is_patt(gpare(e)));
363 /* Anything else must be an illegal LHS */
365 hsperror("Not a valid LHS");
368 abort(); /* should never get here */
374 Return the function at the root of a series of applications.
384 expORpat(LEGIT_PATT, garg(e));
385 return(function(gfun(e)));
388 return(function(gpare(e)));
400 while (ttree(e) == par)
408 Extend a function by adding a new definition to its list of bindings.
416 /* fprintf(stderr,"extending binding (%d)\n",tbinding(bind));*/
417 if(tbinding(bind) == abind)
418 bind = gabindsnd(bind);
420 if(tbinding(bind) == pbind)
421 gpbindl(bind) = lconc(gpbindl(bind), gpbindl(rule));
422 else if(tbinding(bind) == fbind)
423 gfbindl(bind) = lconc(gfbindl(bind), gfbindl(rule));
425 fprintf(stderr,"bind error in decl (%d)\n",tbinding(bind));
430 createpat(guards,where)
439 func = mknoqual(install_literal(""));
441 return(mkpgrhs(PREVPATT,guards,where,func,endlineno));
449 char *p = xmalloc(strlen(i)+2);
457 Check the ordering of declarations in a cbody.
458 All signatures must appear before any declarations.
465 /* The ordering must be correct for a singleton */
466 if(tbinding(decls)!=abind)
469 checkorder2(decls,TRUE);
473 checkorder2(decls,sigs)
477 while(tbinding(decls)==abind)
479 /* Perform a left-traversal if necessary */
480 binding left = gabindfst(decls);
481 if(tbinding(left)==abind)
482 sigs = checkorder2(left,sigs);
484 sigs = checksig(sigs,left);
485 decls = gabindsnd(decls);
488 return(checksig(sigs,decls));
496 BOOLEAN issig = tbinding(decl) == sbind || tbinding(decl) == nullbind;
498 hsperror("Signature appears after definition in class body");
505 Check the last expression in a list of do statements.
512 if (tlist(stmts) == lnil)
513 hsperror("do expression with no statements");
515 for(; tlist(ltl(stmts)) != lnil; stmts = ltl(stmts))
517 if (ttree(lhd(stmts)) != doexp)
518 hsperror("do statements must end with expression");
523 Checks there are no bangs in a tycon application.
530 if(tttype(app) == tapp)
532 if(tttype(gtarg((struct Stapp *)app)) == tbang)
533 hsperror("syntax error: unexpected ! in type");
535 checknobangs(gtapp((struct Stapp *)app));
541 Splits a tycon application into its constructor and a list of types.
545 splittyconapp(app, tyc, tys)
550 switch (tttype(app)) {
552 splittyconapp(gtapp((struct Stapp *)app), tyc, tys);
553 *tys = lapp(*tys, gtarg((struct Stapp *)app));
558 *tyc = gtypeid((struct Stname *)app);
563 hsperror("bad left argument to constructor op");
570 Precedence Parsing Is Now Done In The Compiler !!!
574 Precedence Parser for Haskell. By default operators are left-associative,
575 so it is only necessary to rearrange the parse tree where the new operator
576 has a greater precedence than the existing one, or where two operators have
577 the same precedence and are both right-associative. Error conditions are
580 Note: Prefix negation has the same precedence as infix minus.
581 The algorithm must thus take account of explicit negates.
587 if(ttree(t) == infixap)
589 tree left = ginfarg1(t);
591 if(ttree(left) == negate)
593 struct infix *ttabpos = infixlookup(ginffun(t));
594 struct infix *ntabpos = infixlookup(mknoqual(install_literal("-")));
596 if(pprecedence(ntabpos) < pprecedence(ttabpos))
598 /* (-x)*y ==> -(x*y) */
599 qid lop = ginffun(t);
600 tree arg1 = gnexp(left);
601 tree arg2 = ginfarg2(t);
610 ginfarg1(left) = arg1;
611 ginfarg2(left) = arg2;
617 else if(ttree(left) == infixap)
619 struct infix *ttabpos = infixlookup(ginffun(t));
620 struct infix *lefttabpos = infixlookup(ginffun(left));
622 if(pprecedence(lefttabpos) < pprecedence(ttabpos))
623 rearrangeprec(left,t);
625 else if(pprecedence(lefttabpos) == pprecedence(ttabpos))
627 if(pfixity(lefttabpos) == INFIXR && pfixity(ttabpos) == INFIXR)
628 rearrangeprec(left,t);
630 else if(pfixity(lefttabpos) == INFIXL && pfixity(ttabpos) == INFIXL)
635 char errbuf[ERR_BUF_SIZE];
636 sprintf(errbuf,"Cannot mix %s and %s in the same infix expression",
637 qid_to_string(ginffun(left)), qid_to_string(ginffun(t)));
647 Rearrange a tree to effectively insert an operator in the correct place.
649 x+y*z ==parsed== (x+y)*z ==> x+(y*z)
651 The recursive call to precparse ensures this filters down as necessary.
655 rearrangeprec(tree left, tree t)
657 qid top = ginffun(left);
658 qid lop = ginffun(t);
659 tree arg1 = ginfarg1(left);
660 tree arg2 = ginfarg2(left);
661 tree arg3 = ginfarg2(t);
668 ginfarg1(left) = arg2;
669 ginfarg2(left) = arg3;
676 Check the precedence of a pattern or expression to ensure that
677 sections and function definitions have the correct parse.
681 checkprec(exp,qfn,right)
686 if(ttree(exp) == infixap)
688 struct infix *ftabpos = infixlookup(qfn);
689 struct infix *etabpos = infixlookup(ginffun(exp));
691 if (pprecedence(etabpos) > pprecedence(ftabpos) ||
692 (pprecedence(etabpos) == pprecedence(ftabpos) &&
693 ((pfixity(etabpos) == INFIXR && pfixity(ftabpos) == INFIXR && right) ||
694 ((pfixity(etabpos) == INFIXL && pfixity(ftabpos) == INFIXL && !right)))))
698 char errbuf[ERR_BUF_SIZE];
699 sprintf(errbuf,"Cannot mix %s and %s on a LHS or in a section",
700 qid_to_string(qfn), qid_to_string(ginffun(exp)));
710 /* Reverse a list, in place */
712 list reverse_list( l )
715 list temp, acc = Lnil;
717 while (tlist( l ) != lnil) {