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