dumpastvisitor.java

来自「SRI international 发布的OAA框架软件」· Java 代码 · 共 67 行

JAVA
67
字号
package antlr_oaa;

/* ANTLR Translator Generator
 * Project led by Terence Parr at http://www.jGuru.com
 * Software rights: http://www.antlr.org/RIGHTS.html
 *
 * $Id: DumpASTVisitor.java,v 1.1 2002/11/08 17:38:06 agno Exp $
 */

import java.io.*;
import antlr_oaa.collections.AST;

/** Simple class to dump the contents of an AST to the output */
public class DumpASTVisitor implements ASTVisitor {
    protected int level = 0;


    private void tabs() {
	for (int i = 0; i < level; i++) {
	    System.out.print("   ");
	}
    }

    public void visit(AST node) {
	// Flatten this level of the tree if it has no children
	boolean flatten = /*true*/ false;
	AST node2;
	for (node2 = node; node2 != null ; node2 = node2.getNextSibling()) {
	    if (node2.getFirstChild() != null) {
		flatten = false;
		break;
	    }
	}

	for (node2 = node; node2 != null; node2 = node2.getNextSibling()) {
	    if (!flatten || node2 == node) {
		tabs();
	    }
	    if ( node2.getText()==null ) {
		System.out.print("nil");
	    }
	    else {
		System.out.print(node2.getText());
	    }

	    System.out.print(" [" + node2.getType() + "] ");

	    if (flatten) {
		System.out.print(" ");
	    }
	    else {
		System.out.println("");
	    }

	    if ( node2.getFirstChild() != null ) {
		level++;
		visit(node2.getFirstChild());
		level--;
	    }
	}

	if (flatten) {
	    System.out.println("");
	}
    }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?