1 31 package org.jruby.main; 32 33 import java.io.BufferedOutputStream ; 34 import java.io.BufferedReader ; 35 import java.io.File ; 36 import java.io.FileOutputStream ; 37 import java.io.FileReader ; 38 import java.io.IOException ; 39 import java.io.OutputStream ; 40 import java.io.Reader ; 41 import org.ablaf.ast.IAstEncoder; 42 import org.jruby.ast.util.RubyAstMarshal; 43 import org.jruby.common.NullWarnings; 44 import org.jruby.lexer.yacc.LexerSource; 45 import org.jruby.lexer.yacc.SyntaxException; 46 import org.jruby.parser.DefaultRubyParser; 47 import org.jruby.parser.RubyParserConfiguration; 48 import org.jruby.parser.RubyParserPool; 49 import org.jruby.parser.RubyParserResult; 50 51 55 public class ASTSerializer { 56 public ASTSerializer() { 57 super(); 58 } 59 60 public static void serialize(File input, File outputFile) throws IOException { 61 OutputStream output = new BufferedOutputStream (new FileOutputStream (outputFile)); 62 IAstEncoder encoder = RubyAstMarshal.getInstance().openEncoder(output); 63 try { 64 serialize(input, encoder); 65 } finally { 66 encoder.close(); 67 } 68 } 69 70 public static void serialize(File input, IAstEncoder encoder) throws IOException { 71 Reader reader = new BufferedReader (new FileReader (input)); 72 RubyParserConfiguration config = new RubyParserConfiguration(); 73 74 DefaultRubyParser parser = null; 75 RubyParserResult result = null; 76 try { 77 parser = RubyParserPool.getInstance().borrowParser(); 78 parser.setWarnings(new NullWarnings()); 79 result = parser.parse(config, LexerSource.getSource(input.toString(), reader)); 80 } catch (SyntaxException e) { 81 } finally { 83 RubyParserPool.getInstance().returnParser(parser); 84 } 85 reader.close(); 86 87 encoder.writeNode(result.getAST()); 88 } 89 } 90 | Popular Tags |