KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > python > compiler > ArgListCompiler


1 // Copyright (c) Corporation for National Research Initiatives
2

3 package org.python.compiler;
4
5 import org.python.parser.*;
6 import org.python.parser.ast.*;
7 import java.io.IOException JavaDoc;
8 import java.util.Vector JavaDoc;
9 import java.util.Enumeration JavaDoc;
10
11 public class ArgListCompiler extends Visitor
12     implements PythonGrammarTreeConstants
13 {
14     public boolean arglist, keywordlist;
15     public exprType[] defaults;
16     public Vector JavaDoc names;
17     public Vector JavaDoc fpnames;
18     public Vector JavaDoc init_code;
19
20     public ArgListCompiler() {
21         arglist = keywordlist = false;
22         defaults = null;
23         names = new Vector JavaDoc();
24         fpnames = new Vector JavaDoc();
25         init_code = new Vector JavaDoc();
26     }
27
28     public void reset() {
29         arglist = keywordlist = false;
30         defaults = null;
31         names.removeAllElements();
32         init_code.removeAllElements();
33     }
34
35     public void appendInitCode(Suite node) {
36         int n = node.body.length;
37         stmtType[] newtree = new stmtType[init_code.size() + n];
38         init_code.copyInto(newtree);
39         System.arraycopy(node.body, 0, newtree, init_code.size(), n);
40         node.body = newtree;
41     }
42
43     public exprType[] getDefaults() {
44         return defaults;
45     }
46
47     public void visitArgs(argumentsType args) throws Exception JavaDoc {
48         for (int i = 0; i < args.args.length; i++) {
49             String JavaDoc name = (String JavaDoc) visit(args.args[i]);
50             names.addElement(name);
51             if (args.args[i] instanceof Tuple) {
52                 Assign ass = new Assign(
53                     new exprType[] { args.args[i] },
54                     new Name(name, Name.Load, args.args[i]), args.args[i]);
55                 init_code.addElement(ass);
56             }
57         }
58         if (args.vararg != null) {
59             arglist = true;
60             names.addElement(args.vararg);
61         }
62         if (args.kwarg != null) {
63             keywordlist = true;
64             names.addElement(args.kwarg);
65         }
66         
67         defaults = args.defaults;
68         for (int i = 0; i < defaults.length; i++) {
69             if (defaults[i] == null)
70                 throw new ParseException(
71                     "non-default argument follows default argument",
72                     args.args[args.args.length - defaults.length + i]);
73         }
74     }
75
76     public Object JavaDoc visitName(Name node) throws Exception JavaDoc {
77         if (node.ctx != Name.Store)
78             return null;
79         
80         if (fpnames.contains(node.id)) {
81             throw new ParseException("duplicate argument name found: " +
82                                      node.id, node);
83         }
84         fpnames.addElement(node.id);
85         return node.id;
86     }
87
88     public Object JavaDoc visitTuple(Tuple node) throws Exception JavaDoc {
89         StringBuffer JavaDoc name = new StringBuffer JavaDoc("(");
90         int n = node.elts.length;
91         for (int i = 0; i < n-1; i++) {
92             name.append(visit(node.elts[i]));
93             name.append(", ");
94         }
95         name.append(visit(node.elts[n - 1]));
96         name.append(")");
97         return name.toString();
98     }
99 }
100
Popular Tags