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 We've found a function definition. See if it defines the
67 same function as the previous definition (at this indentation level).
69 Set FN to the name of the function.
82 if (ttree(fn) == ident) {
83 fn_id = gident((struct Sident *) fn);
85 else if (ttree(fn) == infixap) {
86 fn_id = ginffun((struct Sinfixap *) fn);
89 fprintf( stderr, "Wierd funlhs" );
93 this = qid_to_string(fn_id);
94 was = (FN==NULL) ? NULL : qid_to_string(FN);
95 SAMEFN = (was != NULL && strcmp(this,was) == 0);
100 printf("%u\n",startlineno);
102 fprintf(stderr,"%u\tchecksamefn:%s\n",startlineno,this);
107 /* ------------------------------------------------------------------------
111 expORpat(int wanted, tree e)
115 case ident: /* a pattern or expr */
119 error_if_expr_wanted(wanted, "wildcard in expression");
123 error_if_expr_wanted(wanted, "`as'-pattern instead of an expression");
124 expORpat(wanted, gase(e));
128 error_if_expr_wanted(wanted, "irrefutable pattern instead of an expression");
129 expORpat(wanted, glazyp(e));
136 switch (tliteral(glit(e))) {
146 break; /* pattern or expr */
149 error_if_patt_wanted(wanted, "``literal-literal'' in pattern");
152 default: /* the others only occur in pragmas */
153 hsperror("not a valid literal pattern or expression");
158 { tree sub = gnexp(e);
159 if (ttree(sub) != lit) {
160 error_if_patt_wanted(wanted, "\"-\" applied to a non-literal");
162 literal l = glit(sub);
164 if (tliteral(l) != integer && tliteral(l) != floatr) {
165 error_if_patt_wanted(wanted, "\"-\" applied to a non-number");
168 expORpat(wanted, sub);
177 is_conapp_patt(wanted, f, a); /* does nothing unless wanted == LEGIT_PATT */
185 qid f = ginffun ((struct Sinfixap *)e);
186 tree a1 = ginfarg1((struct Sinfixap *)e);
187 tree a2 = ginfarg2((struct Sinfixap *)e);
189 expORpat(wanted, a1);
190 expORpat(wanted, a2);
192 if (wanted == LEGIT_PATT && !isconstr(qid_to_string(f)))
193 hsperror("variable application in pattern");
200 for (field = grbinds(e); tlist(field) == lcons; field = ltl(field)) {
201 expORpat(wanted, lhd(field));
207 if (tmaybe(grbindexp(e)) == just)
208 expORpat(wanted, gthing(grbindexp(e)));
214 for (tup = gtuplelist(e); tlist(tup) == lcons; tup = ltl(tup)) {
215 expORpat(wanted, lhd(tup));
223 for (tup = gutuplelist(e); tlist(tup) == lcons; tup = ltl(tup)) {
224 expORpat(wanted, lhd(tup));
232 for (l = gllist(e); tlist(l) == lcons; l = ltl(l)) {
233 expORpat(wanted, lhd(l));
238 case restr: /* type sig */
239 expORpat(wanted, grestre(e));
242 case par: /* parenthesised */
243 expORpat(wanted, gpare(e));
258 error_if_patt_wanted(wanted, "unexpected construct in a pattern");
262 hsperror("not a pattern or expression");
267 is_conapp_patt(int wanted, tree f, tree a)
269 if (wanted == LEGIT_EXPR)
270 return; /* that was easy */
275 if (isconstr(qid_to_string(gident(f))))
281 char errbuf[ERR_BUF_SIZE];
282 sprintf(errbuf,"not a constructor application -- %s",qid_to_string(gident(f)));
287 is_conapp_patt(wanted, gfun(f), garg(f));
292 is_conapp_patt(wanted, gpare(f), a);
297 char errbuf[ERR_BUF_SIZE];
298 sprintf(errbuf,"tuple pattern `applied' to arguments (missing comma?)");
304 hsperror("not a constructor application");
309 error_if_expr_wanted(int wanted, char *msg)
311 if (wanted == LEGIT_EXPR)
316 error_if_patt_wanted(int wanted, char *msg)
318 if (wanted == LEGIT_PATT)
322 /* ---------------------------------------------------------------------- */
325 BOOLEAN /* return TRUE if LHS is a pattern */
331 switch (tliteral(glit(e))) {
343 hsperror("Literal is not a valid LHS");
355 expORpat(LEGIT_PATT, e);
363 tree f = function(e);
365 /* These lines appear to duplicate what's in function(e).
368 tree a = garg(e); -- do not "unparen", otherwise the error
369 -- fromInteger ((x,y) {-no comma-} z)
372 -- definitions must have pattern arguments
373 expORpat(LEGIT_PATT, a);
376 if(ttree(f) == ident)
377 return(isconstr(qid_to_string(gident(f))));
379 else if(ttree(f) == infixap)
380 return(lhs_is_patt(f));
383 hsperror("Syntax error: not a legal pattern binding in LHS");
388 qid f = ginffun((struct Sinfixap *)e);
389 tree a1 = unparen(ginfarg1((struct Sinfixap *)e)),
390 a2 = unparen(ginfarg2((struct Sinfixap *)e));
392 /* definitions must have pattern arguments */
393 expORpat(LEGIT_PATT, a1);
394 expORpat(LEGIT_PATT, a2);
396 return(isconstr(qid_to_string(f)));
400 return(lhs_is_patt(gpare(e)));
402 /* Anything else must be an illegal LHS */
404 hsperror("Syntax error: not a valid LHS");
407 abort(); /* should never get here */
413 Return the function at the root of a series of applications,
414 checking on the way that the arguments are patterns.
424 expORpat(LEGIT_PATT, garg(e));
425 return(function(gfun(e)));
428 return(function(gpare(e)));
440 while (ttree(e) == par)
448 Extend a function by adding a new definition to its list of bindings.
456 /* fprintf(stderr,"extending binding (%d)\n",tbinding(bind));*/
457 if(tbinding(bind) == abind)
458 bind = gabindsnd(bind);
460 /* if(tbinding(bind) == pbind)
461 gpbindl(bind) = lconc(gpbindl(bind), gpbindl(rule));
465 if(tbinding(bind) == fbind)
466 gfbindm(bind) = lconc(gfbindm(bind), gfbindm(rule));
468 fprintf(stderr,"bind error in decl (%d)\n",tbinding(bind));
476 char *p = xmalloc(strlen(i)+2);
484 Check the ordering of declarations in a cbody.
485 All signatures must appear before any declarations.
492 /* The ordering must be correct for a singleton */
493 if(tbinding(decls)!=abind)
496 checkorder2(decls,TRUE);
500 checkorder2(decls,sigs)
504 while(tbinding(decls)==abind)
506 /* Perform a left-traversal if necessary */
507 binding left = gabindfst(decls);
508 if(tbinding(left)==abind)
509 sigs = checkorder2(left,sigs);
511 sigs = checksig(sigs,left);
512 decls = gabindsnd(decls);
515 return(checksig(sigs,decls));
523 BOOLEAN issig = tbinding(decl) == sbind || tbinding(decl) == nullbind;
525 hsperror("Signature appears after definition in class body");
532 Check the last expression in a list of do statements.
539 if (tlist(stmts) == lnil)
540 hsperror("do expression with no statements");
542 for(; tlist(ltl(stmts)) != lnil; stmts = ltl(stmts))
544 if (ttree(lhd(stmts)) != doexp)
545 hsperror("do statements must end with expression");
550 Checks there are no bangs in a tycon application.
557 if(tttype(app) == tapp)
559 if(tttype(gtarg((struct Stapp *)app)) == tbang)
560 hsperror("syntax error: unexpected ! in type");
562 checknobangs(gtapp((struct Stapp *)app));
566 /* Check that a type is of the form
568 where n>=1, and the ai are all type variables
569 This is used to check that a class decl is well formed.
572 check_class_decl_head_help( app, n )
574 int n; /* Number of args so far */
576 switch (tttype(app)) {
578 /* Check the arg is a type variable */
579 switch (tttype (gtarg((struct Stapp *) app))) {
580 case namedtvar: break;
581 default: hsperror("Class declaration head must use only type variables");
584 /* Check the fun part */
585 check_class_decl_head_help( gtapp((struct Stapp *) app), n+1 );
589 /* Class name; check there is at least one argument */
591 hsperror("Class must have at least one argument");
596 hsperror("Illegal syntax in class declaration head");
601 check_class_decl_head( app )
603 { check_class_decl_head_help( app, 0 ); }
608 Splits a tycon application into its constructor and a list of types.
612 splittyconapp(app, tyc, tys)
617 switch (tttype(app)) {
619 splittyconapp(gtapp((struct Stapp *)app), tyc, tys);
620 *tys = lapp(*tys, gtarg((struct Stapp *)app));
625 *tyc = gtypeid((struct Stname *)app);
630 hsperror("bad left argument to constructor op");
637 Precedence Parsing Is Now Done In The Compiler !!!
641 Precedence Parser for Haskell. By default operators are left-associative,
642 so it is only necessary to rearrange the parse tree where the new operator
643 has a greater precedence than the existing one, or where two operators have
644 the same precedence and are both right-associative. Error conditions are
647 Note: Prefix negation has the same precedence as infix minus.
648 The algorithm must thus take account of explicit negates.
654 if(ttree(t) == infixap)
656 tree left = ginfarg1(t);
658 if(ttree(left) == negate)
660 struct infix *ttabpos = infixlookup(ginffun(t));
661 struct infix *ntabpos = infixlookup(mknoqual(install_literal("-")));
663 if(pprecedence(ntabpos) < pprecedence(ttabpos))
665 /* (-x)*y ==> -(x*y) */
666 qid lop = ginffun(t);
667 tree arg1 = gnexp(left);
668 tree arg2 = ginfarg2(t);
677 ginfarg1(left) = arg1;
678 ginfarg2(left) = arg2;
684 else if(ttree(left) == infixap)
686 struct infix *ttabpos = infixlookup(ginffun(t));
687 struct infix *lefttabpos = infixlookup(ginffun(left));
689 if(pprecedence(lefttabpos) < pprecedence(ttabpos))
690 rearrangeprec(left,t);
692 else if(pprecedence(lefttabpos) == pprecedence(ttabpos))
694 if(pfixity(lefttabpos) == INFIXR && pfixity(ttabpos) == INFIXR)
695 rearrangeprec(left,t);
697 else if(pfixity(lefttabpos) == INFIXL && pfixity(ttabpos) == INFIXL)
702 char errbuf[ERR_BUF_SIZE];
703 sprintf(errbuf,"Cannot mix %s and %s in the same infix expression",
704 qid_to_string(ginffun(left)), qid_to_string(ginffun(t)));
714 Rearrange a tree to effectively insert an operator in the correct place.
716 x+y*z ==parsed== (x+y)*z ==> x+(y*z)
718 The recursive call to precparse ensures this filters down as necessary.
722 rearrangeprec(tree left, tree t)
724 qid top = ginffun(left);
725 qid lop = ginffun(t);
726 tree arg1 = ginfarg1(left);
727 tree arg2 = ginfarg2(left);
728 tree arg3 = ginfarg2(t);
735 ginfarg1(left) = arg2;
736 ginfarg2(left) = arg3;
743 Check the precedence of a pattern or expression to ensure that
744 sections and function definitions have the correct parse.
748 checkprec(exp,qfn,right)
753 if(ttree(exp) == infixap)
755 struct infix *ftabpos = infixlookup(qfn);
756 struct infix *etabpos = infixlookup(ginffun(exp));
758 if (pprecedence(etabpos) > pprecedence(ftabpos) ||
759 (pprecedence(etabpos) == pprecedence(ftabpos) &&
760 ((pfixity(etabpos) == INFIXR && pfixity(ftabpos) == INFIXR && right) ||
761 ((pfixity(etabpos) == INFIXL && pfixity(ftabpos) == INFIXL && !right)))))
765 char errbuf[ERR_BUF_SIZE];
766 sprintf(errbuf,"Cannot mix %s and %s on a LHS or in a section",
767 qid_to_string(qfn), qid_to_string(ginffun(exp)));
777 /* Reverse a list, in place */
779 list reverse_list( l )
782 list temp, acc = Lnil;
784 while (tlist( l ) != lnil) {