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 BOOLEAN inpat = FALSE;
40 static BOOLEAN checkorder2 PROTO((binding, BOOLEAN));
41 static BOOLEAN checksig PROTO((BOOLEAN, binding));
44 check infix value in range 0..9
53 sscanf(vals,"%d",&value);
55 if (value < 0 || value > 9)
58 value = value < 0 ? 0 : 9;
59 fprintf(stderr,"Precedence must be between 0 and 9 (value given: %d, changed to %d)\n",
67 Check Previous Pattern usage
74 char *this = qid_to_string(fn);
75 char *was = (FN==NULL) ? NULL : qid_to_string(FN);
77 SAMEFN = (was != NULL && strcmp(this,was) == 0);
81 printf("%u\n",startlineno);
83 fprintf(stderr,"%u\tchecksamefn:%s\n",startlineno,this);
92 hsperror("pattern syntax used in expression");
95 /* ------------------------------------------------------------------------
99 expORpat(int wanted, tree e)
103 case ident: /* a pattern or expr */
107 error_if_expr_wanted(wanted, "wildcard in expression");
111 error_if_expr_wanted(wanted, "`as'-pattern instead of an expression");
112 expORpat(wanted, gase(e));
116 error_if_expr_wanted(wanted, "irrefutable pattern instead of an expression");
117 expORpat(wanted, glazyp(e));
121 switch (tliteral(glit(e))) {
131 break; /* pattern or expr */
134 error_if_patt_wanted(wanted, "``literal-literal'' in pattern");
136 default: /* the others only occur in pragmas */
137 hsperror("not a valid literal pattern or expression");
142 { tree sub = gnexp(e);
143 if (ttree(sub) != lit) {
144 error_if_patt_wanted(wanted, "\"-\" applied to a non-literal");
146 literal l = glit(sub);
148 if (tliteral(l) != integer && tliteral(l) != floatr) {
149 error_if_patt_wanted(wanted, "\"-\" applied to a non-number");
152 expORpat(wanted, sub);
161 is_conapp_patt(wanted, f, a); /* does nothing unless wanted == LEGIT_PATT */
169 qid f = ginffun ((struct Sinfixap *)e);
170 tree a1 = ginfarg1((struct Sinfixap *)e);
171 tree a2 = ginfarg2((struct Sinfixap *)e);
173 expORpat(wanted, a1);
174 expORpat(wanted, a2);
176 if (wanted == LEGIT_PATT && !isconstr(qid_to_string(f)))
177 hsperror("variable application in pattern");
184 for (field = grbinds(e); tlist(field) == lcons; field = ltl(field)) {
185 expORpat(wanted, lhd(field));
191 if (tmaybe(grbindexp(e)) == just)
192 expORpat(wanted, gthing(grbindexp(e)));
198 for (tup = gtuplelist(e); tlist(tup) == lcons; tup = ltl(tup)) {
199 expORpat(wanted, lhd(tup));
207 for (l = gllist(e); tlist(l) == lcons; l = ltl(l)) {
208 expORpat(wanted, lhd(l));
213 case par: /* parenthesised */
214 expORpat(wanted, gpare(e));
230 error_if_patt_wanted(wanted, "unexpected construct in a pattern");
234 hsperror("not a pattern or expression");
239 is_conapp_patt(int wanted, tree f, tree a)
241 if (wanted == LEGIT_EXPR)
242 return; /* that was easy */
247 if (isconstr(qid_to_string(gident(f))))
253 char errbuf[ERR_BUF_SIZE];
254 sprintf(errbuf,"not a constructor application -- %s",qid_to_string(gident(f)));
259 is_conapp_patt(wanted, gfun(f), garg(f));
264 is_conapp_patt(wanted, gpare(f), a);
269 char errbuf[ERR_BUF_SIZE];
270 sprintf(errbuf,"tuple pattern `applied' to arguments (missing comma?)");
276 hsperror("not a constructor application");
281 error_if_expr_wanted(int wanted, char *msg)
283 if (wanted == LEGIT_EXPR)
288 error_if_patt_wanted(int wanted, char *msg)
290 if (wanted == LEGIT_PATT)
294 /* ---------------------------------------------------------------------- */
296 BOOLEAN /* return TRUE if LHS is a pattern */
302 switch (tliteral(glit(e))) {
314 hsperror("Literal is not a valid LHS");
325 expORpat(LEGIT_PATT, e);
330 /* This change might break ap infixop below. BEWARE.
331 return (isconstr(qid_to_string(gident(e))));
336 tree f = function(e);
337 tree a = garg(e); /* do not "unparen", otherwise the error
338 fromInteger ((x,y) {-no comma-} z)
342 /* definitions must have pattern arguments */
343 expORpat(LEGIT_PATT, a);
345 if(ttree(f) == ident)
346 return(isconstr(qid_to_string(gident(f))));
348 else if(ttree(f) == infixap)
349 return(lhs_is_patt(f));
352 hsperror("Not a legal pattern binding in LHS");
357 qid f = ginffun((struct Sinfixap *)e);
358 tree a1 = unparen(ginfarg1((struct Sinfixap *)e)),
359 a2 = unparen(ginfarg2((struct Sinfixap *)e));
361 /* definitions must have pattern arguments */
362 expORpat(LEGIT_PATT, a1);
363 expORpat(LEGIT_PATT, a2);
365 return(isconstr(qid_to_string(f)));
369 return(lhs_is_patt(gpare(e)));
371 /* Anything else must be an illegal LHS */
373 hsperror("Not a valid LHS");
376 abort(); /* should never get here */
382 Return the function at the root of a series of applications.
392 expORpat(LEGIT_PATT, garg(e));
393 return(function(gfun(e)));
396 return(function(gpare(e)));
408 while (ttree(e) == par)
416 Extend a function by adding a new definition to its list of bindings.
424 /* fprintf(stderr,"extending binding (%d)\n",tbinding(bind));*/
425 if(tbinding(bind) == abind)
426 bind = gabindsnd(bind);
428 if(tbinding(bind) == pbind)
429 gpbindl(bind) = lconc(gpbindl(bind), gpbindl(rule));
430 else if(tbinding(bind) == fbind)
431 gfbindl(bind) = lconc(gfbindl(bind), gfbindl(rule));
433 fprintf(stderr,"bind error in decl (%d)\n",tbinding(bind));
438 Precedence Parser for Haskell. By default operators are left-associative,
439 so it is only necessary to rearrange the parse tree where the new operator
440 has a greater precedence than the existing one, or where two operators have
441 the same precedence and are both right-associative. Error conditions are
444 Note: Prefix negation has the same precedence as infix minus.
445 The algorithm must thus take account of explicit negates.
451 if(ttree(t) == infixap)
453 tree left = ginfarg1(t);
455 if(ttree(left) == negate)
457 struct infix *ttabpos = infixlookup(ginffun(t));
458 struct infix *ntabpos = infixlookup(mknoqual(install_literal("-")));
460 if(pprecedence(ntabpos) < pprecedence(ttabpos))
462 /* (-x)*y ==> -(x*y) */
463 qid lop = ginffun(t);
464 tree arg1 = gnexp(left);
465 tree arg2 = ginfarg2(t);
474 ginfarg1(left) = arg1;
475 ginfarg2(left) = arg2;
481 else if(ttree(left) == infixap)
483 struct infix *ttabpos = infixlookup(ginffun(t));
484 struct infix *lefttabpos = infixlookup(ginffun(left));
486 if(pprecedence(lefttabpos) < pprecedence(ttabpos))
487 rearrangeprec(left,t);
489 else if(pprecedence(lefttabpos) == pprecedence(ttabpos))
491 if(pfixity(lefttabpos) == INFIXR && pfixity(ttabpos) == INFIXR)
492 rearrangeprec(left,t);
494 else if(pfixity(lefttabpos) == INFIXL && pfixity(ttabpos) == INFIXL)
499 char errbuf[ERR_BUF_SIZE];
500 sprintf(errbuf,"Cannot mix %s and %s in the same infix expression",
501 qid_to_string(ginffun(left)), qid_to_string(ginffun(t)));
511 Rearrange a tree to effectively insert an operator in the correct place.
513 x+y*z ==parsed== (x+y)*z ==> x+(y*z)
515 The recursive call to precparse ensures this filters down as necessary.
519 rearrangeprec(tree left, tree t)
521 qid top = ginffun(left);
522 qid lop = ginffun(t);
523 tree arg1 = ginfarg1(left);
524 tree arg2 = ginfarg2(left);
525 tree arg3 = ginfarg2(t);
532 ginfarg1(left) = arg2;
533 ginfarg2(left) = arg3;
539 createpat(guards,where)
548 func = mknoqual(install_literal(""));
550 return(mkpgrhs(PREVPATT,guards,where,func,endlineno));
557 char *p = xmalloc(strlen(i)+2);
565 /* UNUSED: at the moment */
567 checkmodname(import,interface)
568 id import, interface;
570 if(strcmp(import,interface) != 0)
572 char errbuf[ERR_BUF_SIZE];
573 sprintf(errbuf,"interface name (%s) does not agree with import name (%s)",interface,import);
580 Check the ordering of declarations in a cbody.
581 All signatures must appear before any declarations.
588 /* The ordering must be correct for a singleton */
589 if(tbinding(decls)!=abind)
592 checkorder2(decls,TRUE);
596 checkorder2(decls,sigs)
600 while(tbinding(decls)==abind)
602 /* Perform a left-traversal if necessary */
603 binding left = gabindfst(decls);
604 if(tbinding(left)==abind)
605 sigs = checkorder2(left,sigs);
607 sigs = checksig(sigs,left);
608 decls = gabindsnd(decls);
611 return(checksig(sigs,decls));
620 BOOLEAN issig = tbinding(decl) == sbind || tbinding(decl) == nullbind;
622 hsperror("Signature appears after definition in class body");
629 Check the last expression in a list of do statements.
636 if (tlist(stmts) == lnil)
637 hsperror("do expression with no statements");
639 for(; tlist(ltl(stmts)) != lnil; stmts = ltl(stmts))
641 if (ttree(lhd(stmts)) != doexp)
642 hsperror("do statements must end with expression");
647 Check the precedence of a pattern or expression to ensure that
648 sections and function definitions have the correct parse.
652 checkprec(exp,qfn,right)
657 if(ttree(exp) == infixap)
659 struct infix *ftabpos = infixlookup(qfn);
660 struct infix *etabpos = infixlookup(ginffun(exp));
662 if (pprecedence(etabpos) > pprecedence(ftabpos) ||
663 (pprecedence(etabpos) == pprecedence(ftabpos) &&
664 ((pfixity(etabpos) == INFIXR && pfixity(ftabpos) == INFIXR && right) ||
665 ((pfixity(etabpos) == INFIXL && pfixity(ftabpos) == INFIXL && !right)))))
669 char errbuf[ERR_BUF_SIZE];
670 sprintf(errbuf,"Cannot mix %s and %s on a LHS or in a section",
671 qid_to_string(qfn), qid_to_string(ginffun(exp)));
679 Checks there are no bangs in a tycon application.
686 if(tttype(app) == tapp)
688 if(tttype(gtarg((struct Stapp *)app)) == tbang)
689 hsperror("syntax error: unexpected ! in type");
691 checknobangs(gtapp((struct Stapp *)app));
697 Splits a tycon application into its constructor and a list of types.
701 splittyconapp(app, tyc, tys)
706 if(tttype(app) == tapp)
708 splittyconapp(gtapp((struct Stapp *)app), tyc, tys);
709 *tys = lapp(*tys, gtarg((struct Stapp *)app));
711 else if(tttype(app) == tname)
713 *tyc = gtypeid((struct Stname *)app);
718 hsperror("panic: splittyconap: bad tycon application (no tycon)");