Many changes
[IRC.git] / Robust / src / IR / Tree / BuildIR.java
1 package IR.Tree;
2 import IR.*;
3 import java.util.*;
4
5
6 public class BuildIR {
7     State state;
8     public BuildIR(State state) {
9         this.state=state;
10     }
11     public void buildtree() {
12         for(Iterator it=state.parsetrees.iterator();it.hasNext();) {
13             ParseNode pn=(ParseNode)it.next();
14             parseFile(pn);
15         }
16     }
17
18     /** Parse the classes in this file */
19     public void parseFile(ParseNode pn) {
20         ParseNode tpn=pn.getChild("type_declaration_list");
21         if (tpn!=null) {
22             ParseNodeVector pnv=tpn.getChildren();
23             for(int i=0;i<pnv.size();i++) {
24                 ParseNode type_pn=pnv.elementAt(i);
25                 if (isEmpty(type_pn)) /* Skip the semicolon */
26                     continue;
27                 if (isNode(type_pn,"class_declaration")) {
28                     ClassDescriptor cn=parseTypeDecl(type_pn);
29                     state.addClass(cn);
30                 } else if (isNode(type_pn,"task_declaration")) {
31                     TaskDescriptor td=parseTaskDecl(type_pn);
32                     state.addTask(td);
33                 } throw new Error();
34             }
35         }
36     }
37
38     public TaskDescriptor parseTaskDecl(ParseNode pn) {
39         TaskDescriptor td=new TaskDescriptor(pn.getChild("name").getTerminal());
40         ParseNode bodyn=pn.getChild("body");
41         BlockNode bn=parseBlock(bodyn);
42         parseParameterList(td, pn);
43         state.addTreeCode(td,bn);
44         if (pn.getChild("flag_effects_list")!=null)
45             td.addFlagEffects(parseFlagEffects(pn.getChild("flag_effects_list")));
46         return td;
47     }
48
49     public FlagEffects parseFlagEffects(ParseNode pn) {
50         return null;
51     }
52
53     public FlagExpressionNode parseFlagExpression(ParseNode pn) {
54         if (pn.getChild("or")!=null) {
55             ParseNodeVector pnv=pn.getChild("or").getChildren();
56             ParseNode left=pnv.elementAt(0);
57             ParseNode right=pnv.elementAt(1);
58             return new FlagOpNode(parseFlagExpression(left), parseFlagExpression(right), new Operation(Operation.LOGIC_OR));
59         } else if (pn.getChild("and")!=null) {
60             ParseNodeVector pnv=pn.getChild("and").getChildren();
61             ParseNode left=pnv.elementAt(0);
62             ParseNode right=pnv.elementAt(1);
63             return new FlagOpNode(parseFlagExpression(left), parseFlagExpression(right), new Operation(Operation.LOGIC_AND));
64         } else if (pn.getChild("not")!=null) {
65             ParseNodeVector pnv=pn.getChild("not").getChildren();
66             ParseNode left=pnv.elementAt(0);
67             return new FlagOpNode(parseFlagExpression(left), new Operation(Operation.LOGIC_NOT));           
68
69         } else if (pn.getChild("name")!=null) {
70             return new FlagNode(pn.getChild("name").getTerminal());
71         } else throw new Error();
72     }
73
74     public void parseParameterList(TaskDescriptor td, ParseNode pn) {
75         ParseNode paramlist=pn.getChild("task_parameter_list");
76         if (paramlist==null)
77             return;
78          ParseNodeVector pnv=paramlist.getChildren();
79          for(int i=0;i<pnv.size();i++) {
80              ParseNode paramn=pnv.elementAt(i);
81              TypeDescriptor type=parseTypeDescriptor(paramn);
82
83              ParseNode tmp=paramn;
84              while (tmp.getChild("single")==null) {
85                  type=type.makeArray(state);
86                  tmp=tmp.getChild("array");
87              }
88              String paramname=tmp.getChild("single").getTerminal();
89              FlagExpressionNode fen=parseFlagExpression(paramn.getChild("flag"));
90              
91              td.addParameter(type,paramname,fen);
92          }
93     }
94
95     public ClassDescriptor parseTypeDecl(ParseNode pn) {
96         ClassDescriptor cn=new ClassDescriptor(pn.getChild("name").getTerminal());
97         if (!isEmpty(pn.getChild("super").getTerminal())) {
98             /* parse superclass name */
99             ParseNode snn=pn.getChild("super").getChild("type").getChild("class").getChild("name");
100             NameDescriptor nd=parseName(snn);
101             cn.setSuper(nd.toString());
102         } else {
103             if (!cn.getSymbol().equals(TypeUtil.ObjectClass))
104                 cn.setSuper(TypeUtil.ObjectClass);
105         }
106         cn.setModifiers(parseModifiersList(pn.getChild("modifiers")));
107         parseClassBody(cn, pn.getChild("classbody"));
108         return cn;
109     }
110
111     private void parseClassBody(ClassDescriptor cn, ParseNode pn) {
112         ParseNode decls=pn.getChild("class_body_declaration_list");
113         if (decls!=null) {
114             ParseNodeVector pnv=decls.getChildren();
115             for(int i=0;i<pnv.size();i++) {
116                 ParseNode decl=pnv.elementAt(i);
117                 if (isNode(decl,"member")) {
118                     parseClassMember(cn,decl);
119                 } else if (isNode(decl,"constructor")) {
120                     parseConstructorDecl(cn,decl.getChild("constructor_declaration"));
121                 } else if (isNode(decl,"block")) {
122                 } else throw new Error();
123             }
124         }
125     }
126
127     private void parseClassMember(ClassDescriptor cn, ParseNode pn) {
128         ParseNode fieldnode=pn.getChild("field");
129
130         if (fieldnode!=null) {
131             parseFieldDecl(cn,fieldnode.getChild("field_declaration"));
132             return;
133         }
134         ParseNode methodnode=pn.getChild("method");
135         if (methodnode!=null) {
136             parseMethodDecl(cn,methodnode.getChild("method_declaration"));
137             return;
138         }
139         throw new Error();
140     }
141
142     private TypeDescriptor parseTypeDescriptor(ParseNode pn) {
143         ParseNode tn=pn.getChild("type");
144         String type_st=tn.getTerminal();
145         if(type_st.equals("byte")) {
146             return state.getTypeDescriptor(TypeDescriptor.BYTE);
147         } else if(type_st.equals("short")) {
148             return state.getTypeDescriptor(TypeDescriptor.SHORT);
149         } else if(type_st.equals("boolean")) {
150             return state.getTypeDescriptor(TypeDescriptor.BOOLEAN);
151         } else if(type_st.equals("int")) {
152             return state.getTypeDescriptor(TypeDescriptor.INT);
153         } else if(type_st.equals("long")) {
154             return state.getTypeDescriptor(TypeDescriptor.LONG);
155         } else if(type_st.equals("char")) {
156             return state.getTypeDescriptor(TypeDescriptor.CHAR);
157         } else if(type_st.equals("float")) {
158             return state.getTypeDescriptor(TypeDescriptor.FLOAT);
159         } else if(type_st.equals("double")) {
160             return state.getTypeDescriptor(TypeDescriptor.DOUBLE);
161         } else if(type_st.equals("class")) {
162             ParseNode nn=tn.getChild("class");
163             return state.getTypeDescriptor(parseName(nn.getChild("name")));
164         } else if(type_st.equals("array")) {
165             ParseNode nn=tn.getChild("array");
166             TypeDescriptor td=parseTypeDescriptor(nn.getChild("basetype"));
167             Integer numdims=(Integer)nn.getChild("dims").getLiteral();
168             for(int i=0;i<numdims.intValue();i++)
169                 td=td.makeArray(state);
170             return td;
171         } else {
172             throw new Error();
173         }
174     }
175
176     private NameDescriptor parseName(ParseNode nn) {
177         ParseNode base=nn.getChild("base");
178         ParseNode id=nn.getChild("identifier");
179         if (base==null)
180             return new NameDescriptor(id.getTerminal());
181         return new NameDescriptor(parseName(base.getChild("name")),id.getTerminal());
182         
183     }
184
185     private void parseFieldDecl(ClassDescriptor cn,ParseNode pn) {
186         ParseNode mn=pn.getChild("modifier");
187         Modifiers m=parseModifiersList(mn);
188
189         ParseNode tn=pn.getChild("type");
190         TypeDescriptor t=parseTypeDescriptor(tn);
191         ParseNode vn=pn.getChild("variables").getChild("variable_declarators_list");
192         ParseNodeVector pnv=vn.getChildren();
193         for(int i=0;i<pnv.size();i++) {
194             ParseNode vardecl=pnv.elementAt(i);
195
196             
197             ParseNode tmp=vardecl;
198             TypeDescriptor arrayt=t;
199             while (tmp.getChild("single")==null) {
200                 arrayt=arrayt.makeArray(state);
201                 tmp=tmp.getChild("array");
202             }
203             String identifier=tmp.getChild("single").getTerminal();
204
205             ParseNode epn=vardecl.getChild("initializer");
206             
207             ExpressionNode en=null;
208             if (epn!=null)
209                 en=parseExpression(epn.getFirstChild());
210   
211             cn.addField(new FieldDescriptor(m,arrayt,identifier, en));
212         }
213         
214     }
215
216     private ExpressionNode parseExpression(ParseNode pn) {
217         if (isNode(pn,"assignment"))
218             return parseAssignmentExpression(pn);
219         else if (isNode(pn,"logical_or")||isNode(pn,"logical_and")||
220                  isNode(pn,"bitwise_or")||isNode(pn,"bitwise_xor")||
221                  isNode(pn,"bitwise_and")||isNode(pn,"equal")||
222                  isNode(pn,"not_equal")||isNode(pn,"comp_lt")||
223                  isNode(pn,"comp_lte")||isNode(pn,"comp_gt")||
224                  isNode(pn,"comp_gte")||isNode(pn,"leftshift")||
225                  isNode(pn,"rightshift")||isNode(pn,"sub")||
226                  isNode(pn,"add")||isNode(pn,"mult")||
227                  isNode(pn,"div")||isNode(pn,"mod")) {
228             ParseNodeVector pnv=pn.getChildren();
229             ParseNode left=pnv.elementAt(0);
230             ParseNode right=pnv.elementAt(1);
231             Operation op=new Operation(pn.getLabel());
232             return new OpNode(parseExpression(left),parseExpression(right),op);
233         } else if (isNode(pn,"unaryplus")||
234                    isNode(pn,"unaryminus")||
235                    isNode(pn,"postinc")||
236                    isNode(pn,"postdec")||
237                    isNode(pn,"preinc")||
238                    isNode(pn,"not")||
239                    isNode(pn,"predec")) {
240             ParseNode left=pn.getFirstChild();
241             Operation op=new Operation(pn.getLabel());
242             return new OpNode(parseExpression(left),op);
243         } else if (isNode(pn,"literal")) {
244             String literaltype=pn.getTerminal();
245             ParseNode literalnode=pn.getChild(literaltype);
246             Object literal_obj=literalnode.getLiteral();
247             return new LiteralNode(literaltype, literal_obj);
248         } else if (isNode(pn,"createobject")) {
249             TypeDescriptor td=parseTypeDescriptor(pn);
250             Vector args=parseArgumentList(pn);
251             CreateObjectNode con=new CreateObjectNode(td);
252             for(int i=0;i<args.size();i++) {
253                 con.addArgument((ExpressionNode)args.get(i));
254             }
255             return con;
256         } else if (isNode(pn,"createarray")) {
257             System.out.println(pn.PPrint(3,true));
258             TypeDescriptor td=parseTypeDescriptor(pn);
259             Vector args=parseDimExprs(pn);
260             int num=0;
261             if (pn.getChild("dims_opt").getLiteral()!=null)
262                 num=((Integer)pn.getChild("dims_opt").getLiteral()).intValue();
263             for(int i=0;i<(args.size()+num);i++)
264                 td=td.makeArray(state);
265             CreateObjectNode con=new CreateObjectNode(td);
266             for(int i=0;i<args.size();i++) {
267                 con.addArgument((ExpressionNode)args.get(i));
268             }
269             return con;
270         } else if (isNode(pn,"name")) {
271             NameDescriptor nd=parseName(pn);
272             return new NameNode(nd);
273         } else if (isNode(pn,"this")) {
274             NameDescriptor nd=new NameDescriptor("this");
275             return new NameNode(nd);
276         } else if (isNode(pn,"methodinvoke1")) {
277             NameDescriptor nd=parseName(pn.getChild("name"));
278             Vector args=parseArgumentList(pn);
279             MethodInvokeNode min=new MethodInvokeNode(nd);
280             for(int i=0;i<args.size();i++) {
281                 min.addArgument((ExpressionNode)args.get(i));
282             }
283             return min;
284         } else if (isNode(pn,"methodinvoke2")) {
285             String methodid=pn.getChild("id").getTerminal();
286             ExpressionNode exp=parseExpression(pn.getChild("base").getFirstChild());
287             Vector args=parseArgumentList(pn);
288             MethodInvokeNode min=new MethodInvokeNode(methodid,exp);
289             for(int i=0;i<args.size();i++) {
290                 min.addArgument((ExpressionNode)args.get(i));
291             }
292             return min;
293         } else if (isNode(pn,"fieldaccess")) { 
294             ExpressionNode en=parseExpression(pn.getChild("base").getFirstChild());         String fieldname=pn.getChild("field").getTerminal();
295             return new FieldAccessNode(en,fieldname);
296         } else if (isNode(pn,"arrayaccess")) { 
297             ExpressionNode en=parseExpression(pn.getChild("base").getFirstChild());
298             ExpressionNode index=parseExpression(pn.getChild("index").getFirstChild());
299             return new ArrayAccessNode(en,index);
300         } else if (isNode(pn,"cast1")) { 
301             return new CastNode(parseTypeDescriptor(pn.getChild("type")),parseExpression(pn.getChild("exp").getFirstChild()));
302         } else if (isNode(pn,"cast2")) { 
303             return new CastNode(parseExpression(pn.getChild("type").getFirstChild()),parseExpression(pn.getChild("exp").getFirstChild()));
304         } else {
305             System.out.println("---------------------");
306             System.out.println(pn.PPrint(3,true));
307             throw new Error();
308         }
309     }
310
311     private Vector parseDimExprs(ParseNode pn) {
312         Vector arglist=new Vector();
313         ParseNode an=pn.getChild("dim_exprs");
314         if (an==null)   /* No argument list */
315             return arglist;
316         ParseNodeVector anv=an.getChildren();
317         for(int i=0;i<anv.size();i++) {
318             arglist.add(parseExpression(anv.elementAt(i)));
319         }
320         return arglist;
321     }
322
323     private Vector parseArgumentList(ParseNode pn) {
324         Vector arglist=new Vector();
325         ParseNode an=pn.getChild("argument_list");
326         if (an==null)   /* No argument list */
327             return arglist;
328         ParseNodeVector anv=an.getChildren();
329         for(int i=0;i<anv.size();i++) {
330             arglist.add(parseExpression(anv.elementAt(i)));
331         }
332         return arglist;
333     }
334
335     private ExpressionNode parseAssignmentExpression(ParseNode pn) {
336         AssignOperation ao=new AssignOperation(pn.getChild("op").getTerminal());
337         ParseNodeVector pnv=pn.getChild("args").getChildren();
338         
339         AssignmentNode an=new AssignmentNode(parseExpression(pnv.elementAt(0)),parseExpression(pnv.elementAt(1)),ao);
340         return an;
341     }
342
343
344     private void parseMethodDecl(ClassDescriptor cn, ParseNode pn) {
345         ParseNode headern=pn.getChild("method_header");
346         ParseNode bodyn=pn.getChild("body");
347         MethodDescriptor md=parseMethodHeader(headern);
348         BlockNode bn=parseBlock(bodyn);
349         cn.addMethod(md);
350         state.addTreeCode(md,bn);
351     }
352
353     private void parseConstructorDecl(ClassDescriptor cn, ParseNode pn) {
354         ParseNode mn=pn.getChild("modifiers");
355         Modifiers m=parseModifiersList(mn);
356         ParseNode cdecl=pn.getChild("constructor_declarator");
357         String name=cdecl.getChild("name").getChild("identifier").getTerminal();
358         MethodDescriptor md=new MethodDescriptor(m, name);
359         ParseNode paramnode=cdecl.getChild("parameters");
360         parseParameterList(md,paramnode);
361         ParseNode bodyn0=pn.getChild("body");
362         ParseNode bodyn=bodyn0.getChild("constructor_body");
363         cn.addMethod(md);
364         if (bodyn!=null) {
365             BlockNode bn=parseBlock(bodyn);
366             state.addTreeCode(md,bn);
367         }
368     }
369
370     public BlockNode parseBlock(ParseNode pn) {
371         if (isEmpty(pn.getTerminal()))
372             return new BlockNode();
373         ParseNode bsn=pn.getChild("block_statement_list");
374         return parseBlockHelper(bsn);
375     }
376     
377     private BlockNode parseBlockHelper(ParseNode pn) {
378         ParseNodeVector pnv=pn.getChildren();
379         BlockNode bn=new BlockNode();
380         for(int i=0;i<pnv.size();i++) {
381             Vector bsv=parseBlockStatement(pnv.elementAt(i));
382             for(int j=0;j<bsv.size();j++) {
383                 bn.addBlockStatement((BlockStatementNode)bsv.get(j));
384             }
385         }
386         return bn;
387     }
388
389     public BlockNode parseSingleBlock(ParseNode pn) {
390         BlockNode bn=new BlockNode();
391         Vector bsv=parseBlockStatement(pn);
392         for(int j=0;j<bsv.size();j++) {
393             bn.addBlockStatement((BlockStatementNode)bsv.get(j));
394         }
395         bn.setStyle(BlockNode.NOBRACES);
396         return bn;
397     }
398
399     public Vector parseBlockStatement(ParseNode pn) {
400         Vector blockstatements=new Vector();
401         if (isNode(pn,"local_variable_declaration")) {
402             TypeDescriptor t=parseTypeDescriptor(pn);
403             ParseNode vn=pn.getChild("variable_declarators_list");
404             ParseNodeVector pnv=vn.getChildren();
405             for(int i=0;i<pnv.size();i++) {
406                 ParseNode vardecl=pnv.elementAt(i);
407
408             
409                 ParseNode tmp=vardecl;
410                 TypeDescriptor arrayt=t;
411                 while (tmp.getChild("single")==null) {
412                     arrayt=arrayt.makeArray(state);
413                     tmp=tmp.getChild("array");
414                 }
415                 String identifier=tmp.getChild("single").getTerminal();
416                 
417                 ParseNode epn=vardecl.getChild("initializer");
418             
419
420                 ExpressionNode en=null;
421                 if (epn!=null)
422                     en=parseExpression(epn.getFirstChild());
423                 
424                 blockstatements.add(new DeclarationNode(new VarDescriptor(arrayt, identifier),en));
425             }
426         } else if (isNode(pn,"nop")) {
427             /* Do Nothing */
428         } else if (isNode(pn,"expression")) {
429             blockstatements.add(new BlockExpressionNode(parseExpression(pn.getFirstChild())));
430         } else if (isNode(pn,"ifstatement")) {
431             blockstatements.add(new IfStatementNode(parseExpression(pn.getChild("condition").getFirstChild()),
432                                        parseSingleBlock(pn.getChild("statement").getFirstChild()),
433                                        pn.getChild("else_statement")!=null?parseSingleBlock(pn.getChild("else_statement").getFirstChild()):null));
434         } else if (isNode(pn,"taskexit")) {
435             FlagEffects fe=null;
436             if (pn.getChild("flag_effects_list")!=null)
437                 fe=parseFlagEffects(pn.getChild("flag_effects_list"));
438             blockstatements.add(new TaskExitNode(fe));
439         } else if (isNode(pn,"return")) {
440             if (isEmpty(pn.getTerminal()))
441                 blockstatements.add(new ReturnNode());
442             else {
443                 ExpressionNode en=parseExpression(pn.getFirstChild());
444                 blockstatements.add(new ReturnNode(en));
445             }
446         } else if (isNode(pn,"block_statement_list")) {
447             BlockNode bn=parseBlockHelper(pn);
448             blockstatements.add(new SubBlockNode(bn));
449         } else if (isNode(pn,"empty")) {
450             /* nop */
451         } else if (isNode(pn,"statement_expression_list")) {
452             ParseNodeVector pnv=pn.getChildren();
453             BlockNode bn=new BlockNode();
454             for(int i=0;i<pnv.size();i++) {
455                 ExpressionNode en=parseExpression(pnv.elementAt(i));
456                 blockstatements.add(new BlockExpressionNode(en));
457             }
458             bn.setStyle(BlockNode.EXPRLIST);
459         } else if (isNode(pn,"forstatement")) {
460             BlockNode init=parseSingleBlock(pn.getChild("initializer").getFirstChild());
461             BlockNode update=parseSingleBlock(pn.getChild("update").getFirstChild());
462             ExpressionNode condition=parseExpression(pn.getChild("condition").getFirstChild());
463             BlockNode body=parseSingleBlock(pn.getChild("statement").getFirstChild());
464             blockstatements.add(new LoopNode(init,condition,update,body));
465         } else if (isNode(pn,"whilestatement")) {
466             ExpressionNode condition=parseExpression(pn.getChild("condition").getFirstChild());
467             BlockNode body=parseSingleBlock(pn.getChild("statement").getFirstChild());
468             blockstatements.add(new LoopNode(condition,body,LoopNode.WHILELOOP));
469         } else if (isNode(pn,"dowhilestatement")) {
470             ExpressionNode condition=parseExpression(pn.getChild("condition").getFirstChild());
471             BlockNode body=parseSingleBlock(pn.getChild("statement").getFirstChild());
472             blockstatements.add(new LoopNode(condition,body,LoopNode.DOWHILELOOP));
473         } else {
474             System.out.println("---------------");
475             System.out.println(pn.PPrint(3,true));
476             throw new Error();
477         }
478         return blockstatements;
479     }
480
481     public MethodDescriptor parseMethodHeader(ParseNode pn) {
482         ParseNode mn=pn.getChild("modifiers");
483         Modifiers m=parseModifiersList(mn);
484         
485         ParseNode tn=pn.getChild("returntype");
486         TypeDescriptor returntype;
487         if (tn!=null) 
488             returntype=parseTypeDescriptor(tn);
489         else
490             returntype=new TypeDescriptor(TypeDescriptor.VOID);
491
492         ParseNode pmd=pn.getChild("method_declarator");
493         String name=pmd.getChild("name").getTerminal();
494         MethodDescriptor md=new MethodDescriptor(m, returntype, name);
495        
496         ParseNode paramnode=pmd.getChild("parameters");
497         parseParameterList(md,paramnode);
498         return md;
499     }
500
501     public void parseParameterList(MethodDescriptor md, ParseNode pn) {
502         ParseNode paramlist=pn.getChild("formal_parameter_list");
503         if (paramlist==null)
504             return;
505          ParseNodeVector pnv=paramlist.getChildren();
506          for(int i=0;i<pnv.size();i++) {
507              ParseNode paramn=pnv.elementAt(i);
508              TypeDescriptor type=parseTypeDescriptor(paramn);
509
510              ParseNode tmp=paramn;
511              while (tmp.getChild("single")==null) {
512                  type=type.makeArray(state);
513                  tmp=tmp.getChild("array");
514              }
515              String paramname=tmp.getChild("single").getTerminal();
516             
517              md.addParameter(type,paramname);
518          }
519     }
520
521     public Modifiers parseModifiersList(ParseNode pn) {
522         Modifiers m=new Modifiers();
523         ParseNode modlist=pn.getChild("modifier_list");
524         if (modlist!=null) {
525             ParseNodeVector pnv=modlist.getChildren();
526             for(int i=0;i<pnv.size();i++) {
527                 ParseNode modn=pnv.elementAt(i);
528                 if (isNode(modn,"public"))
529                     m.addModifier(Modifiers.PUBLIC);
530                 if (isNode(modn,"protected"))
531                     m.addModifier(Modifiers.PROTECTED);
532                 if (isNode(modn,"private"))
533                     m.addModifier(Modifiers.PRIVATE);
534                 if (isNode(modn,"static"))
535                     m.addModifier(Modifiers.STATIC);
536                 if (isNode(modn,"final"))
537                     m.addModifier(Modifiers.FINAL);
538                 if (isNode(modn,"native"))
539                     m.addModifier(Modifiers.NATIVE);
540             }
541         }
542         return m;
543     }
544
545     private boolean isNode(ParseNode pn, String label) {
546         if (pn.getLabel().equals(label))
547             return true;
548         else return false;
549     }
550
551     private static boolean isEmpty(ParseNode pn) {
552         if (pn.getLabel().equals("empty"))
553             return true;
554         else
555             return false;
556     }
557
558     private static boolean isEmpty(String s) {
559         if (s.equals("empty"))
560             return true;
561         else
562             return false;
563     }
564
565     /** Throw an exception if something is unexpected */
566     private void check(ParseNode pn, String label) {
567         if (pn == null) {
568             throw new Error(pn+ "IE: Expected '" + label + "', got null");
569         }
570         if (! pn.getLabel().equals(label)) {
571             throw new Error(pn+ "IE: Expected '" + label + "', got '"+pn.getLabel()+"'");
572         }
573     }
574 }