1 /**********************************************************************
4 * Syntax-related Utility Functions *
7 **********************************************************************/
13 #include "constants.h"
17 #include "hsparser.tab.h"
20 extern short icontexts;
22 extern unsigned endlineno, startlineno;
23 extern BOOLEAN hashIds, etags;
25 /* Forward Declarations */
27 char *ineg PROTO((char *));
28 static tree unparen PROTO((tree));
29 static void is_conapp_patt PROTO((int, tree, tree));
30 static void rearrangeprec PROTO((tree, tree));
31 static void error_if_expr_wanted PROTO((int, char *));
32 static void error_if_patt_wanted PROTO((int, char *));
34 qid fns[MAX_CONTEXTS] = { NULL };
35 BOOLEAN samefn[MAX_CONTEXTS] = { FALSE };
36 tree prevpatt[MAX_CONTEXTS] = { NULL };
38 static BOOLEAN checkorder2 PROTO((binding, BOOLEAN));
39 static BOOLEAN checksig PROTO((BOOLEAN, binding));
42 check infix value in range 0..9
51 sscanf(vals,"%d",&value);
53 if (value < 0 || value > 9)
56 value = value < 0 ? 0 : 9;
57 fprintf(stderr,"Precedence must be between 0 and 9 (value given: %d, changed to %d)\n",
65 Check Previous Pattern usage
72 char *this = qid_to_string(fn);
73 char *was = (FN==NULL) ? NULL : qid_to_string(FN);
75 SAMEFN = (was != NULL && strcmp(this,was) == 0);
79 printf("%u\n",startlineno);
81 fprintf(stderr,"%u\tchecksamefn:%s\n",startlineno,this);
86 /* ------------------------------------------------------------------------
90 expORpat(int wanted, tree e)
94 case ident: /* a pattern or expr */
98 error_if_expr_wanted(wanted, "wildcard in expression");
102 error_if_expr_wanted(wanted, "`as'-pattern instead of an expression");
103 expORpat(wanted, gase(e));
107 error_if_expr_wanted(wanted, "irrefutable pattern instead of an expression");
108 expORpat(wanted, glazyp(e));
112 switch (tliteral(glit(e))) {
122 break; /* pattern or expr */
125 error_if_patt_wanted(wanted, "``literal-literal'' in pattern");
127 default: /* the others only occur in pragmas */
128 hsperror("not a valid literal pattern or expression");
133 { tree sub = gnexp(e);
134 if (ttree(sub) != lit) {
135 error_if_patt_wanted(wanted, "\"-\" applied to a non-literal");
137 literal l = glit(sub);
139 if (tliteral(l) != integer && tliteral(l) != floatr) {
140 error_if_patt_wanted(wanted, "\"-\" applied to a non-number");
143 expORpat(wanted, sub);
152 is_conapp_patt(wanted, f, a); /* does nothing unless wanted == LEGIT_PATT */
160 qid f = ginffun ((struct Sinfixap *)e);
161 tree a1 = ginfarg1((struct Sinfixap *)e);
162 tree a2 = ginfarg2((struct Sinfixap *)e);
164 expORpat(wanted, a1);
165 expORpat(wanted, a2);
167 if (wanted == LEGIT_PATT && !isconstr(qid_to_string(f)))
168 hsperror("variable application in pattern");
175 for (field = grbinds(e); tlist(field) == lcons; field = ltl(field)) {
176 expORpat(wanted, lhd(field));
182 if (tmaybe(grbindexp(e)) == just)
183 expORpat(wanted, gthing(grbindexp(e)));
189 for (tup = gtuplelist(e); tlist(tup) == lcons; tup = ltl(tup)) {
190 expORpat(wanted, lhd(tup));
198 for (l = gllist(e); tlist(l) == lcons; l = ltl(l)) {
199 expORpat(wanted, lhd(l));
204 case par: /* parenthesised */
205 expORpat(wanted, gpare(e));
221 error_if_patt_wanted(wanted, "unexpected construct in a pattern");
225 hsperror("not a pattern or expression");
230 is_conapp_patt(int wanted, tree f, tree a)
232 if (wanted == LEGIT_EXPR)
233 return; /* that was easy */
238 if (isconstr(qid_to_string(gident(f))))
244 char errbuf[ERR_BUF_SIZE];
245 sprintf(errbuf,"not a constructor application -- %s",qid_to_string(gident(f)));
250 is_conapp_patt(wanted, gfun(f), garg(f));
255 is_conapp_patt(wanted, gpare(f), a);
260 char errbuf[ERR_BUF_SIZE];
261 sprintf(errbuf,"tuple pattern `applied' to arguments (missing comma?)");
267 hsperror("not a constructor application");
272 error_if_expr_wanted(int wanted, char *msg)
274 if (wanted == LEGIT_EXPR)
279 error_if_patt_wanted(int wanted, char *msg)
281 if (wanted == LEGIT_PATT)
285 /* ---------------------------------------------------------------------- */
287 BOOLEAN /* return TRUE if LHS is a pattern */
293 switch (tliteral(glit(e))) {
305 hsperror("Literal is not a valid LHS");
316 expORpat(LEGIT_PATT, e);
324 tree f = function(e);
325 tree a = garg(e); /* do not "unparen", otherwise the error
326 fromInteger ((x,y) {-no comma-} z)
330 /* definitions must have pattern arguments */
331 expORpat(LEGIT_PATT, a);
333 if(ttree(f) == ident)
334 return(isconstr(qid_to_string(gident(f))));
336 else if(ttree(f) == infixap)
337 return(lhs_is_patt(f));
340 hsperror("Not a legal pattern binding in LHS");
345 qid f = ginffun((struct Sinfixap *)e);
346 tree a1 = unparen(ginfarg1((struct Sinfixap *)e)),
347 a2 = unparen(ginfarg2((struct Sinfixap *)e));
349 /* definitions must have pattern arguments */
350 expORpat(LEGIT_PATT, a1);
351 expORpat(LEGIT_PATT, a2);
353 return(isconstr(qid_to_string(f)));
357 return(lhs_is_patt(gpare(e)));
359 /* Anything else must be an illegal LHS */
361 hsperror("Not a valid LHS");
364 abort(); /* should never get here */
370 Return the function at the root of a series of applications.
380 expORpat(LEGIT_PATT, garg(e));
381 return(function(gfun(e)));
384 return(function(gpare(e)));
396 while (ttree(e) == par)
404 Extend a function by adding a new definition to its list of bindings.
412 /* fprintf(stderr,"extending binding (%d)\n",tbinding(bind));*/
413 if(tbinding(bind) == abind)
414 bind = gabindsnd(bind);
416 if(tbinding(bind) == pbind)
417 gpbindl(bind) = lconc(gpbindl(bind), gpbindl(rule));
418 else if(tbinding(bind) == fbind)
419 gfbindl(bind) = lconc(gfbindl(bind), gfbindl(rule));
421 fprintf(stderr,"bind error in decl (%d)\n",tbinding(bind));
426 createpat(guards,where)
435 func = mknoqual(install_literal(""));
437 return(mkpgrhs(PREVPATT,guards,where,func,endlineno));
445 char *p = xmalloc(strlen(i)+2);
453 Check the ordering of declarations in a cbody.
454 All signatures must appear before any declarations.
461 /* The ordering must be correct for a singleton */
462 if(tbinding(decls)!=abind)
465 checkorder2(decls,TRUE);
469 checkorder2(decls,sigs)
473 while(tbinding(decls)==abind)
475 /* Perform a left-traversal if necessary */
476 binding left = gabindfst(decls);
477 if(tbinding(left)==abind)
478 sigs = checkorder2(left,sigs);
480 sigs = checksig(sigs,left);
481 decls = gabindsnd(decls);
484 return(checksig(sigs,decls));
492 BOOLEAN issig = tbinding(decl) == sbind || tbinding(decl) == nullbind;
494 hsperror("Signature appears after definition in class body");
501 Check the last expression in a list of do statements.
508 if (tlist(stmts) == lnil)
509 hsperror("do expression with no statements");
511 for(; tlist(ltl(stmts)) != lnil; stmts = ltl(stmts))
513 if (ttree(lhd(stmts)) != doexp)
514 hsperror("do statements must end with expression");
519 Checks there are no bangs in a tycon application.
526 if(tttype(app) == tapp)
528 if(tttype(gtarg((struct Stapp *)app)) == tbang)
529 hsperror("syntax error: unexpected ! in type");
531 checknobangs(gtapp((struct Stapp *)app));
537 Splits a tycon application into its constructor and a list of types.
541 splittyconapp(app, tyc, tys)
546 switch (tttype(app)) {
548 splittyconapp(gtapp((struct Stapp *)app), tyc, tys);
549 *tys = lapp(*tys, gtarg((struct Stapp *)app));
554 *tyc = gtypeid((struct Stname *)app);
559 hsperror("panic: splittyconap: bad tycon application (no tycon)");
566 Precedence Parsing Is Now Done In The Compiler !!!
570 Precedence Parser for Haskell. By default operators are left-associative,
571 so it is only necessary to rearrange the parse tree where the new operator
572 has a greater precedence than the existing one, or where two operators have
573 the same precedence and are both right-associative. Error conditions are
576 Note: Prefix negation has the same precedence as infix minus.
577 The algorithm must thus take account of explicit negates.
583 if(ttree(t) == infixap)
585 tree left = ginfarg1(t);
587 if(ttree(left) == negate)
589 struct infix *ttabpos = infixlookup(ginffun(t));
590 struct infix *ntabpos = infixlookup(mknoqual(install_literal("-")));
592 if(pprecedence(ntabpos) < pprecedence(ttabpos))
594 /* (-x)*y ==> -(x*y) */
595 qid lop = ginffun(t);
596 tree arg1 = gnexp(left);
597 tree arg2 = ginfarg2(t);
606 ginfarg1(left) = arg1;
607 ginfarg2(left) = arg2;
613 else if(ttree(left) == infixap)
615 struct infix *ttabpos = infixlookup(ginffun(t));
616 struct infix *lefttabpos = infixlookup(ginffun(left));
618 if(pprecedence(lefttabpos) < pprecedence(ttabpos))
619 rearrangeprec(left,t);
621 else if(pprecedence(lefttabpos) == pprecedence(ttabpos))
623 if(pfixity(lefttabpos) == INFIXR && pfixity(ttabpos) == INFIXR)
624 rearrangeprec(left,t);
626 else if(pfixity(lefttabpos) == INFIXL && pfixity(ttabpos) == INFIXL)
631 char errbuf[ERR_BUF_SIZE];
632 sprintf(errbuf,"Cannot mix %s and %s in the same infix expression",
633 qid_to_string(ginffun(left)), qid_to_string(ginffun(t)));
643 Rearrange a tree to effectively insert an operator in the correct place.
645 x+y*z ==parsed== (x+y)*z ==> x+(y*z)
647 The recursive call to precparse ensures this filters down as necessary.
651 rearrangeprec(tree left, tree t)
653 qid top = ginffun(left);
654 qid lop = ginffun(t);
655 tree arg1 = ginfarg1(left);
656 tree arg2 = ginfarg2(left);
657 tree arg3 = ginfarg2(t);
664 ginfarg1(left) = arg2;
665 ginfarg2(left) = arg3;
672 Check the precedence of a pattern or expression to ensure that
673 sections and function definitions have the correct parse.
677 checkprec(exp,qfn,right)
682 if(ttree(exp) == infixap)
684 struct infix *ftabpos = infixlookup(qfn);
685 struct infix *etabpos = infixlookup(ginffun(exp));
687 if (pprecedence(etabpos) > pprecedence(ftabpos) ||
688 (pprecedence(etabpos) == pprecedence(ftabpos) &&
689 ((pfixity(etabpos) == INFIXR && pfixity(ftabpos) == INFIXR && right) ||
690 ((pfixity(etabpos) == INFIXL && pfixity(ftabpos) == INFIXL && !right)))))
694 char errbuf[ERR_BUF_SIZE];
695 sprintf(errbuf,"Cannot mix %s and %s on a LHS or in a section",
696 qid_to_string(qfn), qid_to_string(ginffun(exp)));