1 28 29 package org.jruby.ast.visitor.rewriter; 30 31 import java.io.BufferedReader ; 32 import java.io.FileInputStream ; 33 import java.io.FileNotFoundException ; 34 import java.io.IOException ; 35 import java.io.InputStreamReader ; 36 37 import org.jruby.ast.Node; 38 import org.jruby.common.NullWarnings; 39 import org.jruby.lexer.yacc.LexerSource; 40 import org.jruby.parser.DefaultRubyParser; 41 import org.jruby.parser.RubyParserConfiguration; 42 import org.jruby.parser.RubyParserPool; 43 44 public class SourceRewriterMain { 45 46 public static void main(String [] args) { 47 48 if (args.length < 1) { 49 System.err.println("Please specify a sourcefile."); 50 return; 51 } 52 53 DefaultRubyParser parser = RubyParserPool.getInstance().borrowParser(); 54 parser.setWarnings(new NullWarnings()); 55 56 LexerSource lexerSource = null; 57 BufferedReader reader = null; 58 try { 59 reader = new BufferedReader (new InputStreamReader (new FileInputStream (args[0]))); 60 lexerSource = new LexerSource(args[0], reader); 61 } catch (FileNotFoundException e) { 62 System.err.println("Could not find the file:"); 63 System.err.println(args[0]); 64 return; 65 } 66 67 Node rootNode = parser.parse(new RubyParserConfiguration(), lexerSource).getAST(); 68 if (rootNode == null) { 69 System.err.println("Source File seems to be empty."); 70 return; 71 } 72 73 StringBuffer buffer = new StringBuffer (); 74 String line; 75 try { 76 reader = new BufferedReader (new InputStreamReader (new FileInputStream (args[0]))); 77 78 while ((line = reader.readLine()) != null) { 79 buffer.append(line); 80 buffer.append('\n'); 81 } 82 } catch (IOException e) { 83 System.err.println("Could not read the Sourcefile."); 84 } 85 86 ReWriteVisitor visitor = new ReWriteVisitor(System.out, buffer.toString()); 87 rootNode.accept(visitor); 88 visitor.flushStream(); 89 90 System.out.println("\n"); 91 } 92 } 93 | Popular Tags |