⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 astprinter.java

📁 program assegment for compiler
💻 JAVA
字号:
package edu.berkeley.cs164.interp;
import org.eclipse.jdt.core.dom.*;
import org.eclipse.jdt.internal.corext.dom.GenericVisitor;

/** <p>
 * Dump an AST as text. Display nodes with indentation to indicate
 * nesting levels.
 * </p><p>
 * Unlike the other visitors in this project, this one returns true
 * in order to use the automatic traversal implemented by JDT. Also,
 * it uses visitNode to cover most node types. GenericVisitor calls
 * visitNode for visit methods that are not overridden.
 */
public class ASTPrinter extends GenericVisitor {
	public boolean visitNode(ASTNode node) {
		printNode(node);
		System.out.println();
		return true;
	}
	
	public boolean visit(SimpleName node) {
		printNode(node);
		System.out.println(" \"" + node.getIdentifier() + "\"");
		return true;
	}
	
	public boolean visit(NumberLiteral node) {
		printNode(node);
		System.out.println(" \"" + node.getToken() + "\"");
		return true;		
	}
	
	private static void printNode(ASTNode node) {
		// Indent according to nesting depth.
		ASTNode tempNode = node.getParent();
		while (tempNode != null) {
			System.out.print("   ");
			tempNode = tempNode.getParent();
		}
		System.out.print(getShortName(node.getClass()));		
	}
	
	private static String getShortName(Class c) {
		String fullName = c.getName();
		int pos = fullName.lastIndexOf('.');
		if (pos == -1) return fullName;
		return fullName.substring(pos + 1);
	} 
}

⌨️ 快捷键说明

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