/* * xtc - The eXTensible Compiler * Copyright (C) 2011 Robert Grimm * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * version 2 as published by the Free Software Foundation. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, * USA. */ package oop; import java.io.File; import java.io.IOException; import java.io.Reader; import xtc.parser.ParseException; import xtc.parser.Result; import xtc.tree.GNode; import xtc.tree.Node; import xtc.tree.Visitor; import xtc.lang.JavaFiveParser; /** * A translator from (a subset of) Java to (a subset of) C++. * * @author Robert Grimm * @version $Revision$ */ public class ASTGenerator extends xtc.util.Tool { Node AST; /** Create a new translator. */ public ASTGenerator() { } public String getName() { return "ASTGenerator for JavaGrinder hMaster"; } public String getVersion(){ return "1.0"; } public String getCopy() { return "JavaGrinder Group"; } public void init() { super.init(); runtime. bool("printJavaAST", "printJavaAST", false, "Print Java AST."). bool("returnJavaAST", "returnJavaAST", false, "Return an AST"); } public Node parse(Reader in, File file) throws IOException, ParseException { JavaFiveParser parser = new JavaFiveParser(in, file.toString(), (int)file.length()); Result result = parser.pCompilationUnit(0); return (Node)parser.value(result); } public void process(Node node) { //runtime.console().format(node).pln().flush(); this.AST = node; } public Node generateAST(String arg){ //System.out.println("ready"); try { process(arg); } catch (IOException e) { e.printStackTrace(); } catch (ParseException e) { e.printStackTrace(); } return AST; } }