summaryrefslogtreecommitdiff
path: root/ghc/compiler/parser/syntax.c
blob: 509145360aeb0478b07c7cec4e85f468344ed1ab (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
/**********************************************************************
*                                                                     *
*                                                                     *
*     Syntax-related Utility Functions                                *
*                                                                     *
*                                                                     *
**********************************************************************/

#include <stdio.h>
#include <ctype.h>

#include "hspincl.h"
#include "constants.h"
#include "utils.h"
#include "tree.h"

#include "hsparser.tab.h"

/* Imported values */
extern short icontexts;
extern list Lnil;
extern unsigned endlineno, startlineno;
extern BOOLEAN hashIds, etags;

/* Forward Declarations */

char *ineg		    PROTO((char *));
static tree unparen	    PROTO((tree));
static void is_conapp_patt  PROTO((int, tree, tree));
static void rearrangeprec   PROTO((tree, tree));
static void error_if_expr_wanted PROTO((int, char *));
static void error_if_patt_wanted PROTO((int, char *));

qid	fns[MAX_CONTEXTS] = { NULL };
BOOLEAN samefn[MAX_CONTEXTS] = { FALSE };
tree	prevpatt[MAX_CONTEXTS] = { NULL };

static BOOLEAN	 checkorder2 PROTO((binding, BOOLEAN));
static BOOLEAN	 checksig PROTO((BOOLEAN, binding));

/*
  check infix value in range 0..9
*/


int
checkfixity(vals)
  char *vals;
{
  int value;
  sscanf(vals,"%d",&value);

  if (value < 0 || value > 9)
    {
      int oldvalue = value;
      value = value < 0 ? 0 : 9;
      fprintf(stderr,"Precedence must be between 0 and 9 (value given: %d, changed to %d)\n",
	      oldvalue,value);
    }
  return(value);
}


/*
  Check Previous Pattern usage
*/

void
checksamefn(fn)
  qid fn;
{
  char *this = qid_to_string(fn);
  char *was  = (FN==NULL) ? NULL : qid_to_string(FN);

  SAMEFN = (was != NULL && strcmp(this,was) == 0);

  if(!SAMEFN && etags)
#if 1/*etags*/
    printf("%u\n",startlineno);
#else
    fprintf(stderr,"%u\tchecksamefn:%s\n",startlineno,this);
#endif
}


/* ------------------------------------------------------------------------
*/

void
expORpat(int wanted, tree e)
{
  switch(ttree(e))
    {
      case ident: /* a pattern or expr */
	break;

      case wildp:
	error_if_expr_wanted(wanted, "wildcard in expression");
	break;

      case as:
	error_if_expr_wanted(wanted, "`as'-pattern instead of an expression");
	expORpat(wanted, gase(e));
	break;

      case lazyp:
	error_if_expr_wanted(wanted, "irrefutable pattern instead of an expression");
	expORpat(wanted, glazyp(e));
	break;

      case lit:
	switch (tliteral(glit(e))) {
	  case integer:
	  case intprim:
	  case floatr:
	  case doubleprim:
	  case floatprim:
	  case string:
	  case stringprim:
	  case charr:
	  case charprim:
	    break; /* pattern or expr */

	  case clitlit:
	    error_if_patt_wanted(wanted, "``literal-literal'' in pattern");

	  default: /* the others only occur in pragmas */
	    hsperror("not a valid literal pattern or expression");
	}
	break;

      case negate:
	{ tree sub = gnexp(e);
	  if (ttree(sub) != lit) {
	      error_if_patt_wanted(wanted, "\"-\" applied to a non-literal");
	  } else {
	      literal l = glit(sub);

	      if (tliteral(l) != integer && tliteral(l) != floatr) {
		error_if_patt_wanted(wanted, "\"-\" applied to a non-number");
	      }
	  }
	  expORpat(wanted, sub);
	}
	break;

      case ap:
	{
	  tree f = gfun(e);
	  tree a = garg(e);

	  is_conapp_patt(wanted, f, a); /* does nothing unless wanted == LEGIT_PATT */
	  expORpat(wanted, f);
	  expORpat(wanted, a);
	}
	break;

      case infixap:
	{
	  qid  f  = ginffun ((struct Sinfixap *)e);
	  tree a1 = ginfarg1((struct Sinfixap *)e);
	  tree a2 = ginfarg2((struct Sinfixap *)e);

	  expORpat(wanted, a1);
	  expORpat(wanted, a2);

	  if (wanted == LEGIT_PATT && !isconstr(qid_to_string(f)))
	     hsperror("variable application in pattern");
	}
	break;

      case record:
	{
          list field;
	  for (field = grbinds(e); tlist(field) == lcons; field = ltl(field)) {
	      expORpat(wanted, lhd(field));
	  }
	}
	break;

      case rbind:
	if (tmaybe(grbindexp(e)) == just)
	    expORpat(wanted, gthing(grbindexp(e)));
	break;

      case tuple:
	{
	  list tup;
	  for (tup = gtuplelist(e); tlist(tup) == lcons; tup = ltl(tup)) {
	      expORpat(wanted, lhd(tup));
	  }
	}
	break;

      case llist:
	{
	  list l;
	  for (l = gllist(e); tlist(l) == lcons; l = ltl(l)) {
	      expORpat(wanted, lhd(l));
	  }
	}
	break;

      case par: /* parenthesised */
	expORpat(wanted, gpare(e));
	break;

      case restr:
      case lambda:
      case let:
      case casee:
      case ife:
      case doe:
      case ccall:
      case scc:
      case rupdate:
      case comprh:
      case eenum:
      case lsection:
      case rsection:
	error_if_patt_wanted(wanted, "unexpected construct in a pattern");
	break;

      default:
	hsperror("not a pattern or expression");
      }
}

static void
is_conapp_patt(int wanted, tree f, tree a)
{
  if (wanted == LEGIT_EXPR)
     return; /* that was easy */

  switch(ttree(f))
    {
      case ident:
        if (isconstr(qid_to_string(gident(f))))
	  {
	    expORpat(wanted, a);
	    return;
	  }
	{
	  char errbuf[ERR_BUF_SIZE];
	  sprintf(errbuf,"not a constructor application -- %s",qid_to_string(gident(f)));
	  hsperror(errbuf);
	}

      case ap:
	is_conapp_patt(wanted, gfun(f), garg(f));
	expORpat(wanted, a);
	return;

      case par:
	is_conapp_patt(wanted, gpare(f), a);
	break;

      case tuple:
	{
	   char errbuf[ERR_BUF_SIZE];
	   sprintf(errbuf,"tuple pattern `applied' to arguments (missing comma?)");
	   hsperror(errbuf);
	}
	break;

      default:
	hsperror("not a constructor application");
      }
}

static void
error_if_expr_wanted(int wanted, char *msg)
{
    if (wanted == LEGIT_EXPR)
	hsperror(msg);
}

static void
error_if_patt_wanted(int wanted, char *msg)
{
    if (wanted == LEGIT_PATT)
	hsperror(msg);
}

/* ---------------------------------------------------------------------- */

BOOLEAN /* return TRUE if LHS is a pattern */
lhs_is_patt(tree e)
{
  switch(ttree(e))
    {
      case lit:
	switch (tliteral(glit(e))) {
	  case integer:
	  case intprim:
	  case floatr:
	  case doubleprim:
	  case floatprim:
	  case string:
	  case charr:
	  case charprim:
	  case stringprim:
	    return TRUE;
	  default:
	    hsperror("Literal is not a valid LHS");
	}

      case wildp:
        return TRUE;

      case as:
      case lazyp:
      case llist:
      case tuple:
      case negate:
	expORpat(LEGIT_PATT, e);
	return TRUE;

      case ident:
	return(TRUE);

      case ap:
	{
	  tree f = function(e);
	  tree a = garg(e);       /* do not "unparen", otherwise the error
				       fromInteger ((x,y) {-no comma-} z)
				     will be missed.
			          */

	  /* definitions must have pattern arguments */
	  expORpat(LEGIT_PATT, a);

	  if(ttree(f) == ident)
	    return(isconstr(qid_to_string(gident(f))));

	  else if(ttree(f) == infixap)
	    return(lhs_is_patt(f));

	  else
	    hsperror("Not a legal pattern binding in LHS");
	}

      case infixap:
	{
	  qid  f  = ginffun((struct Sinfixap *)e);
	  tree a1 = unparen(ginfarg1((struct Sinfixap *)e)),
	       a2 = unparen(ginfarg2((struct Sinfixap *)e));

	  /* definitions must have pattern arguments */
	  expORpat(LEGIT_PATT, a1);
	  expORpat(LEGIT_PATT, a2);

	  return(isconstr(qid_to_string(f)));
	}

      case par:
	return(lhs_is_patt(gpare(e)));

      /* Anything else must be an illegal LHS */
      default:
	hsperror("Not a valid LHS");
      }

  abort(); /* should never get here */
  return(FALSE);
}


/*
  Return the function at the root of a series of applications.
*/

tree
function(e)
  tree e;
{
  switch (ttree(e))
    {
      case ap:
        expORpat(LEGIT_PATT, garg(e));
        return(function(gfun(e)));

      case par:
	return(function(gpare(e)));
	
      default:
	return(e);
    }
}


static tree
unparen(e)
  tree e;
{
  while (ttree(e) == par)
      e = gpare(e);

  return(e);
}


/*
  Extend a function by adding a new definition to its list of bindings.
*/

void
extendfn(bind,rule)
binding bind;
binding rule;
{
/*  fprintf(stderr,"extending binding (%d)\n",tbinding(bind));*/
  if(tbinding(bind) == abind)
    bind = gabindsnd(bind);

  if(tbinding(bind) == pbind)
    gpbindl(bind) = lconc(gpbindl(bind), gpbindl(rule));
  else if(tbinding(bind) == fbind)
    gfbindl(bind) = lconc(gfbindl(bind), gfbindl(rule));
  else
    fprintf(stderr,"bind error in decl (%d)\n",tbinding(bind));
}


pbinding
createpat(guards,where)
  pbinding guards;
  binding where;
{
  qid func;

  if(FN != NULL)
    func = FN;
  else
    func = mknoqual(install_literal(""));

  return(mkpgrhs(PREVPATT,guards,where,func,endlineno));
}


char *
ineg(i)
  char *i;
{
  char *p = xmalloc(strlen(i)+2);

  *p = '-';
  strcpy(p+1,i);
  return(p);
}

/*
  Check the ordering of declarations in a cbody.
  All signatures must appear before any declarations.
*/

void
checkorder(decls)
  binding decls;
{
  /* The ordering must be correct for a singleton */
  if(tbinding(decls)!=abind)
    return;

  checkorder2(decls,TRUE);
}

static BOOLEAN
checkorder2(decls,sigs)
  binding decls;
  BOOLEAN sigs;
{
  while(tbinding(decls)==abind)
    {
      /* Perform a left-traversal if necessary */
      binding left = gabindfst(decls);
      if(tbinding(left)==abind)
	sigs = checkorder2(left,sigs);
      else
	sigs = checksig(sigs,left);
      decls = gabindsnd(decls);
    }

  return(checksig(sigs,decls));
}

static BOOLEAN
checksig(sig,decl)
  BOOLEAN sig;
  binding decl;
{
  BOOLEAN issig = tbinding(decl) == sbind || tbinding(decl) == nullbind;
  if(!sig && issig)
    hsperror("Signature appears after definition in class body");

  return(issig);
}


/*
  Check the last expression in a list of do statements.
*/

void
checkdostmts(stmts)
  list stmts;
{
  if (tlist(stmts) == lnil)
      hsperror("do expression with no statements");

  for(; tlist(ltl(stmts)) != lnil; stmts = ltl(stmts))
      ;
  if (ttree(lhd(stmts)) != doexp)
      hsperror("do statements must end with expression");
}


/*
  Checks there are no bangs in a tycon application.
*/

void
checknobangs(app)
  ttype app;
{
  if(tttype(app) == tapp)
    {
      if(tttype(gtarg((struct Stapp *)app)) == tbang)
	hsperror("syntax error: unexpected ! in type");

      checknobangs(gtapp((struct Stapp *)app));
    }	  
}


/*
  Splits a tycon application into its constructor and a list of types.
*/

void
splittyconapp(app, tyc, tys)
  ttype app;
  qid *tyc;
  list *tys;
{
  switch (tttype(app)) {
    case tapp:
      splittyconapp(gtapp((struct Stapp *)app), tyc, tys);
      *tys = lapp(*tys, gtarg((struct Stapp *)app));
      break;

    case tname:
    case namedtvar:
      *tyc = gtypeid((struct Stname *)app);
      *tys = Lnil;
      break;

    default:
      hsperror("panic: splittyconap: bad tycon application (no tycon)");
    }
}


#if 0 

Precedence Parsing Is Now Done In The Compiler !!!

/* 

  Precedence Parser for Haskell.  By default operators are left-associative, 
  so it is only necessary to rearrange the parse tree where the new operator
  has a greater precedence than the existing one, or where two operators have
  the same precedence and are both right-associative. Error conditions are
  handled.

  Note:  Prefix negation has the same precedence as infix minus.
         The algorithm must thus take account of explicit negates.
*/

void
precparse(tree t)
{
  if(ttree(t) == infixap)
    {
      tree left = ginfarg1(t);

      if(ttree(left) == negate)
	{
	  struct infix *ttabpos = infixlookup(ginffun(t));
	  struct infix *ntabpos = infixlookup(mknoqual(install_literal("-")));
	  
	  if(pprecedence(ntabpos) < pprecedence(ttabpos))
	    {
	      /* (-x)*y  ==> -(x*y) */
	      qid  lop  = ginffun(t);
	      tree arg1 = gnexp(left);
	      tree arg2 = ginfarg2(t);

	      t->tag = negate;
	      gnexp(t) = left;
	      gnxxx1(t) = NULL;
	      gnxxx2(t) = NULL;

	      left->tag = infixap;
	      ginffun(left)  = lop;
	      ginfarg1(left) = arg1;
	      ginfarg2(left) = arg2;

	      precparse(left);
	    }
	}

      else if(ttree(left) == infixap)
	{
	  struct infix *ttabpos    = infixlookup(ginffun(t));
	  struct infix *lefttabpos = infixlookup(ginffun(left));

	  if(pprecedence(lefttabpos) < pprecedence(ttabpos))
	    rearrangeprec(left,t);

	  else if(pprecedence(lefttabpos) == pprecedence(ttabpos))
	    {
	      if(pfixity(lefttabpos) == INFIXR && pfixity(ttabpos) == INFIXR)
		rearrangeprec(left,t);

	      else if(pfixity(lefttabpos) == INFIXL && pfixity(ttabpos) == INFIXL)
		/* SKIP */;

	      else
		{
		  char errbuf[ERR_BUF_SIZE];
		  sprintf(errbuf,"Cannot mix %s and %s in the same infix expression", 
			  qid_to_string(ginffun(left)), qid_to_string(ginffun(t)));
		  hsperror(errbuf);
	      }
	    }
	}
    }
}


/*
  Rearrange a tree to effectively insert an operator in the correct place.

  x+y*z ==parsed== (x+y)*z  ==>  x+(y*z)

  The recursive call to precparse ensures this filters down as necessary.
*/

static void
rearrangeprec(tree left, tree t)
{
  qid top  = ginffun(left);
  qid lop  = ginffun(t);
  tree arg1 = ginfarg1(left);
  tree arg2 = ginfarg2(left);
  tree arg3 = ginfarg2(t);

  ginffun(t)  = top;
  ginfarg1(t) = arg1;
  ginfarg2(t) = left;

  ginffun(left)  = lop;
  ginfarg1(left) = arg2;
  ginfarg2(left) = arg3;

  precparse(left);
}


/*
  Check the precedence of a pattern or expression to ensure that
  sections and function definitions have the correct parse.
*/

void
checkprec(exp,qfn,right)
  tree exp;
  qid qfn;
  BOOLEAN right;
{
  if(ttree(exp) == infixap)
    {
      struct infix *ftabpos = infixlookup(qfn);
      struct infix *etabpos = infixlookup(ginffun(exp));

      if (pprecedence(etabpos) > pprecedence(ftabpos) ||
	 (pprecedence(etabpos) == pprecedence(ftabpos) &&
	  ((pfixity(etabpos) == INFIXR && pfixity(ftabpos) == INFIXR && right) ||
	  ((pfixity(etabpos) == INFIXL && pfixity(ftabpos) == INFIXL && !right)))))
	/* SKIP */;
      else
	{
	  char errbuf[ERR_BUF_SIZE];
	  sprintf(errbuf,"Cannot mix %s and %s on a LHS or in a section", 
		  qid_to_string(qfn), qid_to_string(ginffun(exp)));
	  hsperror(errbuf);
	}
    }
}

#endif /* 0 */