KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > soot > jimple > parser > Parse


1 /* Soot - a J*va Optimization Framework
2  * Copyright (C) 2000 Patrice Pominville
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */

19
20 /*
21  * Modified by the Sable Research Group and others 1997-1999.
22  * See the 'credits' file distributed with Soot for the complete list of
23  * contributors. (Soot is distributed at http://www.sable.mcgill.ca/soot)
24  */

25
26
27 package soot.jimple.parser;
28
29 import soot.jimple.parser.parser.*;
30 import soot.jimple.parser.lexer.*;
31 import soot.jimple.parser.node.*;
32 import soot.jimple.parser.analysis.*;
33 import java.io.*;
34 import soot.util.*;
35 import java.util.*;
36
37 import soot.*;
38
39 /** Provides a test-driver for the Jimple parser. */
40 public class Parse
41 {
42     private static final String JavaDoc EXT = ".jimple";
43     
44     private static final String JavaDoc USAGE = "usage: java Parse [options] " +
45         "jimple_file [jimple_file ...]";
46
47
48     /*
49       Parses a jimple input stream.
50       If you just want to get the method bodies for a SootClass, pass as the second
51       argument the SootClass you want fill it's method bodies.
52       If you want to create a SootClass for the inputStream set the 2nd arg to null.
53     */

54     static public SootClass parse(InputStream istream, SootClass sc)
55     {
56         Start tree = null;
57         
58         Parser p =
59                 new Parser(new Lexer(
60                       new PushbackReader(new EscapedReader(new BufferedReader(
61                               new InputStreamReader(istream))), 1024)));
62
63         try {
64             tree = p.parse();
65         } catch(ParserException e) {
66             throw new RuntimeException JavaDoc("Parser exception occurred: " + e);
67         } catch(LexerException e) {
68             throw new RuntimeException JavaDoc("Lexer exception occurred: " + e);
69         } catch(IOException e) {
70             throw new RuntimeException JavaDoc("IOException occurred: " + e);
71         }
72         
73         Walker w;
74         if(sc == null)
75             w = new Walker(null);
76         else {
77             w = new BodyExtractorWalker(sc, null, new HashMap());
78         }
79         
80         tree.apply(w);
81         return w.getSootClass();
82     }
83
84
85     public static void main(String JavaDoc args[])
86         throws java.lang.Exception JavaDoc
87               
88
89     {
90     boolean debug = false;
91     boolean verbose = false;
92         InputStream inFile;
93         
94         // check arguments
95
if (args.length < 1) {
96             G.v().out.println(USAGE);
97             System.exit(0);
98         }
99
100
101         Scene.v().setPhantomRefs(true);
102
103         for (int i = 0; i < args.length; i++) {
104             String JavaDoc arg = args[i];
105             if (arg.startsWith("-")) {
106                 arg = arg.substring(1);
107                 if (arg.equals("d"))
108                     debug = true;
109                 else if (arg.equals("v"))
110                     verbose = true;
111             }
112             else {
113
114                
115                 try {
116                     if (verbose)
117                         G.v().out.println(" ... looking for " + arg);
118                     inFile = new FileInputStream(arg);
119                 } catch (FileNotFoundException e) {
120                     if (arg.endsWith(EXT)) {
121                         G.v().out.println(" *** can't find " + arg);
122                         continue;
123                     }
124                     arg = arg + EXT;
125                     try {
126                         if (verbose)
127                             G.v().out.println(" ... looking for " + arg);
128                         inFile = new BufferedInputStream(new FileInputStream(arg));
129                     } catch (FileNotFoundException ee) {
130                         G.v().out.println(" *** can't find " + arg);
131                         continue;
132                     }
133                 }
134                
135                 Parser p =
136                     new Parser(
137                                new Lexer(
138                                          new PushbackReader(
139                                                             new InputStreamReader(inFile), 1024)));
140
141                 Start tree = p.parse();
142                     
143                 tree.apply(new Walker(null));
144             }
145         }
146     } // main
147
} // Parse
148

149
150
151
152
153
Popular Tags