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");
132 default: /* the others only occur in pragmas */
133 hsperror("not a valid literal pattern or expression");
138 { tree sub = gnexp(e);
139 if (ttree(sub) != lit) {
140 error_if_patt_wanted(wanted, "\"-\" applied to a non-literal");
142 literal l = glit(sub);
144 if (tliteral(l) != integer && tliteral(l) != floatr) {
145 error_if_patt_wanted(wanted, "\"-\" applied to a non-number");
148 expORpat(wanted, sub);
157 is_conapp_patt(wanted, f, a); /* does nothing unless wanted == LEGIT_PATT */
165 qid f = ginffun ((struct Sinfixap *)e);
166 tree a1 = ginfarg1((struct Sinfixap *)e);
167 tree a2 = ginfarg2((struct Sinfixap *)e);
169 expORpat(wanted, a1);
170 expORpat(wanted, a2);
172 if (wanted == LEGIT_PATT && !isconstr(qid_to_string(f)))
173 hsperror("variable application in pattern");
180 for (field = grbinds(e); tlist(field) == lcons; field = ltl(field)) {
181 expORpat(wanted, lhd(field));
187 if (tmaybe(grbindexp(e)) == just)
188 expORpat(wanted, gthing(grbindexp(e)));
194 for (tup = gtuplelist(e); tlist(tup) == lcons; tup = ltl(tup)) {
195 expORpat(wanted, lhd(tup));
203 for (l = gllist(e); tlist(l) == lcons; l = ltl(l)) {
204 expORpat(wanted, lhd(l));
209 case par: /* parenthesised */
210 expORpat(wanted, gpare(e));
226 error_if_patt_wanted(wanted, "unexpected construct in a pattern");
230 hsperror("not a pattern or expression");
235 is_conapp_patt(int wanted, tree f, tree a)
237 if (wanted == LEGIT_EXPR)
238 return; /* that was easy */
243 if (isconstr(qid_to_string(gident(f))))
249 char errbuf[ERR_BUF_SIZE];
250 sprintf(errbuf,"not a constructor application -- %s",qid_to_string(gident(f)));
255 is_conapp_patt(wanted, gfun(f), garg(f));
260 is_conapp_patt(wanted, gpare(f), a);
265 char errbuf[ERR_BUF_SIZE];
266 sprintf(errbuf,"tuple pattern `applied' to arguments (missing comma?)");
272 hsperror("not a constructor application");
277 error_if_expr_wanted(int wanted, char *msg)
279 if (wanted == LEGIT_EXPR)
284 error_if_patt_wanted(int wanted, char *msg)
286 if (wanted == LEGIT_PATT)
290 /* ---------------------------------------------------------------------- */
292 BOOLEAN /* return TRUE if LHS is a pattern */
298 switch (tliteral(glit(e))) {
310 hsperror("Literal is not a valid LHS");
322 expORpat(LEGIT_PATT, e);
330 tree f = function(e);
332 /* These lines appear to duplicate what's in function(e).
335 tree a = garg(e); -- do not "unparen", otherwise the error
336 -- fromInteger ((x,y) {-no comma-} z)
339 -- definitions must have pattern arguments
340 expORpat(LEGIT_PATT, a);
343 if(ttree(f) == ident)
344 return(isconstr(qid_to_string(gident(f))));
346 else if(ttree(f) == infixap)
347 return(lhs_is_patt(f));
350 hsperror("Syntax error: not a legal pattern binding in LHS");
355 qid f = ginffun((struct Sinfixap *)e);
356 tree a1 = unparen(ginfarg1((struct Sinfixap *)e)),
357 a2 = unparen(ginfarg2((struct Sinfixap *)e));
359 /* definitions must have pattern arguments */
360 expORpat(LEGIT_PATT, a1);
361 expORpat(LEGIT_PATT, a2);
363 return(isconstr(qid_to_string(f)));
367 return(lhs_is_patt(gpare(e)));
369 /* Anything else must be an illegal LHS */
371 hsperror("Syntax error: not a valid LHS");
374 abort(); /* should never get here */
380 Return the function at the root of a series of applications,
381 checking on the way that the arguments are patterns.
391 expORpat(LEGIT_PATT, garg(e));
392 return(function(gfun(e)));
395 return(function(gpare(e)));
407 while (ttree(e) == par)
415 Extend a function by adding a new definition to its list of bindings.
423 /* fprintf(stderr,"extending binding (%d)\n",tbinding(bind));*/
424 if(tbinding(bind) == abind)
425 bind = gabindsnd(bind);
427 if(tbinding(bind) == pbind)
428 gpbindl(bind) = lconc(gpbindl(bind), gpbindl(rule));
429 else if(tbinding(bind) == fbind)
430 gfbindl(bind) = lconc(gfbindl(bind), gfbindl(rule));
432 fprintf(stderr,"bind error in decl (%d)\n",tbinding(bind));
437 createpat(guards,where)
446 func = mknoqual(install_literal(""));
448 return(mkpgrhs(PREVPATT,guards,where,func,endlineno));
456 char *p = xmalloc(strlen(i)+2);
464 Check the ordering of declarations in a cbody.
465 All signatures must appear before any declarations.
472 /* The ordering must be correct for a singleton */
473 if(tbinding(decls)!=abind)
476 checkorder2(decls,TRUE);
480 checkorder2(decls,sigs)
484 while(tbinding(decls)==abind)
486 /* Perform a left-traversal if necessary */
487 binding left = gabindfst(decls);
488 if(tbinding(left)==abind)
489 sigs = checkorder2(left,sigs);
491 sigs = checksig(sigs,left);
492 decls = gabindsnd(decls);
495 return(checksig(sigs,decls));
503 BOOLEAN issig = tbinding(decl) == sbind || tbinding(decl) == nullbind;
505 hsperror("Signature appears after definition in class body");
512 Check the last expression in a list of do statements.
519 if (tlist(stmts) == lnil)
520 hsperror("do expression with no statements");
522 for(; tlist(ltl(stmts)) != lnil; stmts = ltl(stmts))
524 if (ttree(lhd(stmts)) != doexp)
525 hsperror("do statements must end with expression");
530 Checks there are no bangs in a tycon application.
537 if(tttype(app) == tapp)
539 if(tttype(gtarg((struct Stapp *)app)) == tbang)
540 hsperror("syntax error: unexpected ! in type");
542 checknobangs(gtapp((struct Stapp *)app));
548 Splits a tycon application into its constructor and a list of types.
552 splittyconapp(app, tyc, tys)
557 switch (tttype(app)) {
559 splittyconapp(gtapp((struct Stapp *)app), tyc, tys);
560 *tys = lapp(*tys, gtarg((struct Stapp *)app));
565 *tyc = gtypeid((struct Stname *)app);
570 hsperror("bad left argument to constructor op");
577 Precedence Parsing Is Now Done In The Compiler !!!
581 Precedence Parser for Haskell. By default operators are left-associative,
582 so it is only necessary to rearrange the parse tree where the new operator
583 has a greater precedence than the existing one, or where two operators have
584 the same precedence and are both right-associative. Error conditions are
587 Note: Prefix negation has the same precedence as infix minus.
588 The algorithm must thus take account of explicit negates.
594 if(ttree(t) == infixap)
596 tree left = ginfarg1(t);
598 if(ttree(left) == negate)
600 struct infix *ttabpos = infixlookup(ginffun(t));
601 struct infix *ntabpos = infixlookup(mknoqual(install_literal("-")));
603 if(pprecedence(ntabpos) < pprecedence(ttabpos))
605 /* (-x)*y ==> -(x*y) */
606 qid lop = ginffun(t);
607 tree arg1 = gnexp(left);
608 tree arg2 = ginfarg2(t);
617 ginfarg1(left) = arg1;
618 ginfarg2(left) = arg2;
624 else if(ttree(left) == infixap)
626 struct infix *ttabpos = infixlookup(ginffun(t));
627 struct infix *lefttabpos = infixlookup(ginffun(left));
629 if(pprecedence(lefttabpos) < pprecedence(ttabpos))
630 rearrangeprec(left,t);
632 else if(pprecedence(lefttabpos) == pprecedence(ttabpos))
634 if(pfixity(lefttabpos) == INFIXR && pfixity(ttabpos) == INFIXR)
635 rearrangeprec(left,t);
637 else if(pfixity(lefttabpos) == INFIXL && pfixity(ttabpos) == INFIXL)
642 char errbuf[ERR_BUF_SIZE];
643 sprintf(errbuf,"Cannot mix %s and %s in the same infix expression",
644 qid_to_string(ginffun(left)), qid_to_string(ginffun(t)));
654 Rearrange a tree to effectively insert an operator in the correct place.
656 x+y*z ==parsed== (x+y)*z ==> x+(y*z)
658 The recursive call to precparse ensures this filters down as necessary.
662 rearrangeprec(tree left, tree t)
664 qid top = ginffun(left);
665 qid lop = ginffun(t);
666 tree arg1 = ginfarg1(left);
667 tree arg2 = ginfarg2(left);
668 tree arg3 = ginfarg2(t);
675 ginfarg1(left) = arg2;
676 ginfarg2(left) = arg3;
683 Check the precedence of a pattern or expression to ensure that
684 sections and function definitions have the correct parse.
688 checkprec(exp,qfn,right)
693 if(ttree(exp) == infixap)
695 struct infix *ftabpos = infixlookup(qfn);
696 struct infix *etabpos = infixlookup(ginffun(exp));
698 if (pprecedence(etabpos) > pprecedence(ftabpos) ||
699 (pprecedence(etabpos) == pprecedence(ftabpos) &&
700 ((pfixity(etabpos) == INFIXR && pfixity(ftabpos) == INFIXR && right) ||
701 ((pfixity(etabpos) == INFIXL && pfixity(ftabpos) == INFIXL && !right)))))
705 char errbuf[ERR_BUF_SIZE];
706 sprintf(errbuf,"Cannot mix %s and %s on a LHS or in a section",
707 qid_to_string(qfn), qid_to_string(ginffun(exp)));
717 /* Reverse a list, in place */
719 list reverse_list( l )
722 list temp, acc = Lnil;
724 while (tlist( l ) != lnil) {