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 (tup = gutuplelist(e); tlist(tup) == lcons; tup = ltl(tup)) {
204 expORpat(wanted, lhd(tup));
212 for (l = gllist(e); tlist(l) == lcons; l = ltl(l)) {
213 expORpat(wanted, lhd(l));
218 case par: /* parenthesised */
219 expORpat(wanted, gpare(e));
235 error_if_patt_wanted(wanted, "unexpected construct in a pattern");
239 hsperror("not a pattern or expression");
244 is_conapp_patt(int wanted, tree f, tree a)
246 if (wanted == LEGIT_EXPR)
247 return; /* that was easy */
252 if (isconstr(qid_to_string(gident(f))))
258 char errbuf[ERR_BUF_SIZE];
259 sprintf(errbuf,"not a constructor application -- %s",qid_to_string(gident(f)));
264 is_conapp_patt(wanted, gfun(f), garg(f));
269 is_conapp_patt(wanted, gpare(f), a);
274 char errbuf[ERR_BUF_SIZE];
275 sprintf(errbuf,"tuple pattern `applied' to arguments (missing comma?)");
281 hsperror("not a constructor application");
286 error_if_expr_wanted(int wanted, char *msg)
288 if (wanted == LEGIT_EXPR)
293 error_if_patt_wanted(int wanted, char *msg)
295 if (wanted == LEGIT_PATT)
299 /* ---------------------------------------------------------------------- */
301 BOOLEAN /* return TRUE if LHS is a pattern */
307 switch (tliteral(glit(e))) {
319 hsperror("Literal is not a valid LHS");
331 expORpat(LEGIT_PATT, e);
339 tree f = function(e);
341 /* These lines appear to duplicate what's in function(e).
344 tree a = garg(e); -- do not "unparen", otherwise the error
345 -- fromInteger ((x,y) {-no comma-} z)
348 -- definitions must have pattern arguments
349 expORpat(LEGIT_PATT, a);
352 if(ttree(f) == ident)
353 return(isconstr(qid_to_string(gident(f))));
355 else if(ttree(f) == infixap)
356 return(lhs_is_patt(f));
359 hsperror("Syntax error: not a legal pattern binding in LHS");
364 qid f = ginffun((struct Sinfixap *)e);
365 tree a1 = unparen(ginfarg1((struct Sinfixap *)e)),
366 a2 = unparen(ginfarg2((struct Sinfixap *)e));
368 /* definitions must have pattern arguments */
369 expORpat(LEGIT_PATT, a1);
370 expORpat(LEGIT_PATT, a2);
372 return(isconstr(qid_to_string(f)));
376 return(lhs_is_patt(gpare(e)));
378 /* Anything else must be an illegal LHS */
380 hsperror("Syntax error: not a valid LHS");
383 abort(); /* should never get here */
389 Return the function at the root of a series of applications,
390 checking on the way that the arguments are patterns.
400 expORpat(LEGIT_PATT, garg(e));
401 return(function(gfun(e)));
404 return(function(gpare(e)));
416 while (ttree(e) == par)
424 Extend a function by adding a new definition to its list of bindings.
432 /* fprintf(stderr,"extending binding (%d)\n",tbinding(bind));*/
433 if(tbinding(bind) == abind)
434 bind = gabindsnd(bind);
436 if(tbinding(bind) == pbind)
437 gpbindl(bind) = lconc(gpbindl(bind), gpbindl(rule));
438 else if(tbinding(bind) == fbind)
439 gfbindl(bind) = lconc(gfbindl(bind), gfbindl(rule));
441 fprintf(stderr,"bind error in decl (%d)\n",tbinding(bind));
446 createpat(guards,where)
455 func = mknoqual(install_literal(""));
457 return(mkpgrhs(PREVPATT,guards,where,func,endlineno));
465 char *p = xmalloc(strlen(i)+2);
473 Check the ordering of declarations in a cbody.
474 All signatures must appear before any declarations.
481 /* The ordering must be correct for a singleton */
482 if(tbinding(decls)!=abind)
485 checkorder2(decls,TRUE);
489 checkorder2(decls,sigs)
493 while(tbinding(decls)==abind)
495 /* Perform a left-traversal if necessary */
496 binding left = gabindfst(decls);
497 if(tbinding(left)==abind)
498 sigs = checkorder2(left,sigs);
500 sigs = checksig(sigs,left);
501 decls = gabindsnd(decls);
504 return(checksig(sigs,decls));
512 BOOLEAN issig = tbinding(decl) == sbind || tbinding(decl) == nullbind;
514 hsperror("Signature appears after definition in class body");
521 Check the last expression in a list of do statements.
528 if (tlist(stmts) == lnil)
529 hsperror("do expression with no statements");
531 for(; tlist(ltl(stmts)) != lnil; stmts = ltl(stmts))
533 if (ttree(lhd(stmts)) != doexp)
534 hsperror("do statements must end with expression");
539 Checks there are no bangs in a tycon application.
546 if(tttype(app) == tapp)
548 if(tttype(gtarg((struct Stapp *)app)) == tbang)
549 hsperror("syntax error: unexpected ! in type");
551 checknobangs(gtapp((struct Stapp *)app));
556 /* Check that a type is of the form
558 where n>=1, and the ai are all type variables
559 This is used to check that a class decl is well formed.
562 check_class_decl_head_help( app, n )
564 int n; /* Number of args so far */
566 switch (tttype(app)) {
568 /* Check the arg is a type variable */
569 switch (tttype (gtarg((struct Stapp *) app))) {
570 case namedtvar: break;
571 default: hsperror("Class declaration head must use only type variables");
574 /* Check the fun part */
575 check_class_decl_head_help( gtapp((struct Stapp *) app), n+1 );
579 /* Class name; check there is at least one argument */
581 hsperror("Class must have at least one argument");
586 hsperror("Illegal syntax in class declaration head");
591 check_class_decl_head( app )
593 { check_class_decl_head_help( app, 0 ); }
598 Splits a tycon application into its constructor and a list of types.
602 splittyconapp(app, tyc, tys)
607 switch (tttype(app)) {
609 splittyconapp(gtapp((struct Stapp *)app), tyc, tys);
610 *tys = lapp(*tys, gtarg((struct Stapp *)app));
615 *tyc = gtypeid((struct Stname *)app);
620 hsperror("bad left argument to constructor op");
627 Precedence Parsing Is Now Done In The Compiler !!!
631 Precedence Parser for Haskell. By default operators are left-associative,
632 so it is only necessary to rearrange the parse tree where the new operator
633 has a greater precedence than the existing one, or where two operators have
634 the same precedence and are both right-associative. Error conditions are
637 Note: Prefix negation has the same precedence as infix minus.
638 The algorithm must thus take account of explicit negates.
644 if(ttree(t) == infixap)
646 tree left = ginfarg1(t);
648 if(ttree(left) == negate)
650 struct infix *ttabpos = infixlookup(ginffun(t));
651 struct infix *ntabpos = infixlookup(mknoqual(install_literal("-")));
653 if(pprecedence(ntabpos) < pprecedence(ttabpos))
655 /* (-x)*y ==> -(x*y) */
656 qid lop = ginffun(t);
657 tree arg1 = gnexp(left);
658 tree arg2 = ginfarg2(t);
667 ginfarg1(left) = arg1;
668 ginfarg2(left) = arg2;
674 else if(ttree(left) == infixap)
676 struct infix *ttabpos = infixlookup(ginffun(t));
677 struct infix *lefttabpos = infixlookup(ginffun(left));
679 if(pprecedence(lefttabpos) < pprecedence(ttabpos))
680 rearrangeprec(left,t);
682 else if(pprecedence(lefttabpos) == pprecedence(ttabpos))
684 if(pfixity(lefttabpos) == INFIXR && pfixity(ttabpos) == INFIXR)
685 rearrangeprec(left,t);
687 else if(pfixity(lefttabpos) == INFIXL && pfixity(ttabpos) == INFIXL)
692 char errbuf[ERR_BUF_SIZE];
693 sprintf(errbuf,"Cannot mix %s and %s in the same infix expression",
694 qid_to_string(ginffun(left)), qid_to_string(ginffun(t)));
704 Rearrange a tree to effectively insert an operator in the correct place.
706 x+y*z ==parsed== (x+y)*z ==> x+(y*z)
708 The recursive call to precparse ensures this filters down as necessary.
712 rearrangeprec(tree left, tree t)
714 qid top = ginffun(left);
715 qid lop = ginffun(t);
716 tree arg1 = ginfarg1(left);
717 tree arg2 = ginfarg2(left);
718 tree arg3 = ginfarg2(t);
725 ginfarg1(left) = arg2;
726 ginfarg2(left) = arg3;
733 Check the precedence of a pattern or expression to ensure that
734 sections and function definitions have the correct parse.
738 checkprec(exp,qfn,right)
743 if(ttree(exp) == infixap)
745 struct infix *ftabpos = infixlookup(qfn);
746 struct infix *etabpos = infixlookup(ginffun(exp));
748 if (pprecedence(etabpos) > pprecedence(ftabpos) ||
749 (pprecedence(etabpos) == pprecedence(ftabpos) &&
750 ((pfixity(etabpos) == INFIXR && pfixity(ftabpos) == INFIXR && right) ||
751 ((pfixity(etabpos) == INFIXL && pfixity(ftabpos) == INFIXL && !right)))))
755 char errbuf[ERR_BUF_SIZE];
756 sprintf(errbuf,"Cannot mix %s and %s on a LHS or in a section",
757 qid_to_string(qfn), qid_to_string(ginffun(exp)));
767 /* Reverse a list, in place */
769 list reverse_list( l )
772 list temp, acc = Lnil;
774 while (tlist( l ) != lnil) {