KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > java > source > script > ScriptHandler


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.java.source.script;
21
22 import com.sun.tools.javac.util.*;
23 import com.sun.tools.javac.tree.*;
24 import com.sun.tools.javac.tree.JCTree.*;
25 import com.sun.tools.javac.parser.*;
26
27 import java.io.*;
28 import java.util.Map JavaDoc;
29 import java.util.HashMap JavaDoc;
30
31 import static com.sun.tools.javac.parser.Token.*;
32
33 public class ScriptHandler extends TransformParser {
34     public ScriptHandler(String JavaDoc fn, String JavaDoc classpath) throws IOException {
35     this(new BufferedReader(new FileReader(fn)), classpath);
36     fileName = fn;
37     }
38     private ScriptHandler(Reader in, String JavaDoc classpath) {
39     super(in, classpath);
40     applyName = names.fromString("apply");
41     methodName = names.fromString("method");
42     clazzName = names.fromString("clazz");
43     noteName = names.fromString("note");
44     }
45     private List<JCTree> parseScript() {
46     if(scanner.token()==EOF) return List.<JCTree>nil();
47     JCTree t = statement();
48     if(!hasErrors())
49         return parseScript().prepend(t);
50     return List.<JCTree>nil();
51     }
52     private PluginCompiler pc;
53
54     public boolean isName(JCTree t, Name name) {
55     return t!=null && t instanceof JCIdent && ((JCIdent)t).name==name;
56     }
57
58     public Class JavaDoc compile() throws IOException {
59     List<JCTree> statements = parseScript();
60     System.err.println("JCTree="+statements);
61     if(statements.isEmpty()) return null;
62     pc = new PluginCompiler();
63     if(pc.needsGeneration(fileName, 0L, true)) {
64         pc.startGeneration();
65         pc.write("import com.sun.tools.javac.code.Symbol;\n");
66         pc.write("import com.sun.tools.javac.parser.*;\n");
67         pc.write("import com.sun.tools.javac.tree.*;\n");
68         pc.write("import com.sun.tools.javac.tree.JCTree.*;\n");
69         pc.write("import com.sun.tools.javac.util.*;\n");
70         pc.write("public class ");
71         pc.writeClassName();
72         pc.write(" extends GeneratedScript {\n");
73         pc.write("public void execute() {\n");
74         
75         pc.write(" }\n");
76         for(List<JCTree> t = statements; t.nonEmpty(); t = t.tail) {
77         if(t.head instanceof JCIf) {
78             final JCIf st = (JCIf) t.head;
79             st.thenpart.accept(new Visitor () {
80                 final boolean isMethod = isName(st.cond, methodName);
81                 final boolean isClass = isName(st.cond, clazzName);
82                 final String JavaDoc mname = isMethod ? "MethodDef"
83                                 : isClass ? "ClassDef"
84                                           : "ERROR";
85                 {
86                 pc.write(" public void visit(");
87                 pc.write(mname);
88                 pc.write(" node) {\n");
89                 if(!isMethod && !isClass)
90                     log.error(st.pos,"illegal condition");
91                 }
92                 public void visitTree(JCTree t) {
93                 log.error(t.pos,"Missing code generator");
94                 }
95                 public void visitTopLevel(JCCompilationUnit tree) { visitTree(tree); }
96                 public void visitImport(JCImport tree) { visitTree(tree); }
97                 public void visitClassDef(JCClassDecl tree) { visitTree(tree); }
98                 public void visitMethodDef(JCMethodDecl tree) { visitTree(tree); }
99                 public void visitVarDef(JCVariableDecl tree) { visitTree(tree); }
100                 public void visitSkip(JCSkip tree) { }
101                 public void visitBlock(JCBlock tree) {
102                 for(List<JCStatement> t = tree.stats; t.nonEmpty(); t = t.tail)
103                     t.head.accept(this);
104                 }
105                 public void visitDoLoop(JCDoWhileLoop tree) { visitTree(tree); }
106                 public void visitWhileLoop(JCWhileLoop tree) { visitTree(tree); }
107                 public void visitForLoop(JCForLoop tree) { visitTree(tree); }
108                 public void visitLabelled(JCLabeledStatement tree) { visitTree(tree); }
109                 public void visitSwitch(JCSwitch tree) { visitTree(tree); }
110                 public void visitCase(JCCase tree) { visitTree(tree); }
111                 public void visitSynchronized(JCSynchronized tree) { visitTree(tree); }
112                 public void visitTry(JCTry tree) { visitTree(tree); }
113                 public void visitCatch(JCCatch tree) { visitTree(tree); }
114                 public void visitConditional(JCConditional tree) { visitTree(tree); }
115                 public void visitIf(JCIf tree) { visitTree(tree); }
116                 public void visitExec(JCExpressionStatement tree) {
117                 tree.expr.accept(this);
118                 }
119                 public void visitBreak(JCBreak tree) { visitTree(tree); }
120                 public void visitContinue(JCContinue tree) { visitTree(tree); }
121                 public void visitReturn(JCReturn tree) { visitTree(tree); }
122                 public void visitThrow(JCThrow tree) { visitTree(tree); }
123                 public void visitAssert(JCAssert tree) { visitTree(tree); }
124                 public void visitApply(JCMethodInvocation tree) {
125                 if(isName(tree.meth,noteName)) {
126                     write("\tSystem.err.println(\"NOTE!! \"+node);\n");
127                     write("\taddResult(node,null,");
128                     writeString(tree.args);
129                     write(");\n");
130                 } else log.error(tree.pos,"Unknown method");
131                 }
132                 public void visitNewClass(JCNewClass tree) { visitTree(tree); }
133                 public void visitNewArray(JCNewArray tree) { visitTree(tree); }
134                 public void visitParens(JCParens tree) {
135                 tree.expr.accept(this);
136                 }
137                 public void visitAssign(JCAssign tree) { visitTree(tree); }
138                 public void visitAssignop(JCAssignOp tree) { visitTree(tree); }
139                 public void visitUnary(JCUnary tree) { visitTree(tree); }
140                 public void visitBinary(JCBinary tree) { visitTree(tree); }
141                 public void visitTypeCast(JCTypeCast tree) { visitTree(tree); }
142                 public void visitTypeTest(JCInstanceOf tree) { visitTree(tree); }
143                 public void visitIndexed(JCArrayAccess tree) { visitTree(tree); }
144                 public void visitSelect(JCFieldAccess tree) { visitTree(tree); }
145                 public void visitIdent(JCIdent tree) { visitTree(tree); }
146                 public void visitLiteral(JCLiteral tree) { visitTree(tree); }
147                 public void visitTypeIdent(JCPrimitiveTypeTree tree) { visitTree(tree); }
148                 public void visitTypeArray(JCArrayTypeTree tree) { visitTree(tree); }
149                 public void visitTypeApply(JCTypeApply tree) { visitTree(tree); }
150                 public void visitTypeParameter(JCTypeParameter tree) { visitTree(tree); }
151                 public void visitErroneous(JCErroneous tree) { visitTree(tree); }
152
153                 private void write(String JavaDoc s) {
154                 try {
155                     pc.write(s);
156                 } catch(IOException ioe){}
157                 }
158                 private void writeQuoted(String JavaDoc s) {
159                 try {
160                     pc.writeQuoted(s);
161                 } catch(IOException ioe){}
162                 }
163                 private void writeString(List<? extends JCTree> t) {
164                 boolean first = true;
165                 while(t.nonEmpty()) {
166                     if(first) first = false;
167                     else write("+");
168                     writeString(t.head);
169                     t = t.tail;
170                 }
171                 }
172                 private void writeString(JCTree t) {
173                 if(t==null) writeQuoted("");
174                 else switch(t.tag) {
175                 case JCTree.LITERAL:
176                     Object JavaDoc v = ((JCLiteral)t).value;
177                     writeQuoted(v==null ? "NULL" : v.toString());
178                     break;
179                 case JCTree.PLUS:
180                     JCBinary b = (JCBinary) t;
181                     writeString(b.lhs);
182                     write("+");
183                     writeString(b.rhs);
184                     break;
185                 default: log.error(t.pos,"Unsupported construct");
186                 }
187                 }
188             });
189             pc.write(" }\n");
190         } else {
191             log.error(t.head.pos, "illegal root statement");
192         }
193         }
194         dumpNameConsts(pc);
195         pc.write("}\n");
196         if(hasErrors())
197         return null;
198     }
199     return pc.loadClass(javacpath);
200     }
201     
202     private static class Factory extends Parser.Factory {
203         public static Factory instance(Context context) {
204         Factory instance = (Factory)context.get(parserFactoryKey);
205         if (instance == null)
206         instance = new Factory(context);
207         return instance;
208         }
209         protected Factory(Context context) {
210             super(context);
211         }
212         public Parser newParser(final Scanner S, final ScriptHandler sh) {
213             return new Parser(this, S, false) {
214         protected JCTree checkExprStat(JCTree t) { // be more forgiving
215
return t;
216         }
217         public void accept(Token token) { //allow statements to be ended by =>
218
if(token!=SEMI || (!sh.isToken(sh.impliesToken) && !sh.isToken(sh.suchthatToken)))
219             super.accept(token);
220         }
221         protected JCExpression term3() {
222             JCExpression ret = super.term3();
223             if(ret instanceof JCFieldAccess && sh.scanner.token()==LBRACE) {
224             Name nm = ((JCFieldAccess) ret).name;
225             if(nm==sh.applyName) { // t.apply{rules}
226
S.nextToken();
227                 Rule rules = sh.parseRules();
228                 accept(RBRACE);
229                 sh.attachMap.put(ret,rules);
230             }
231             }
232             return ret;
233         }
234             };
235         }
236     }
237     public Parser makeParser() {
238     return Factory.instance(context).newParser(scanner, this);
239     }
240     
241     private final Map JavaDoc<JCTree,Object JavaDoc> attachMap = new HashMap JavaDoc<JCTree,Object JavaDoc>();
242     private Name applyName, methodName, noteName, clazzName;
243 }
244
Popular Tags