package nzdis.lang.fipaacl; /**/ import nzdis.lang.fipaacl.analysis.DepthFirstAdapter; import nzdis.lang.fipaacl.node.*; /** * Simple ACL AST walker. This is simple Abstract Syntax Tree walker * which will visit each of the nodes and print on standard output the * well formed ASCII representation. * *@author Mariusz Nowostawski *@version @version@ $Revision: 1.1 $ */ class Ast2AsciiWalker extends DepthFirstAdapter { int indent = 0; int tabSize = 2; StringBuffer result = new StringBuffer(); boolean spaceNeeded = false; int indentNeeded = 0; public String toString(){ return result.toString(); } void newLine(){ indent = tabSize; result.append("\n").append(" "); spaceNeeded = false; } void indent(){ indent += tabSize; String s = ""; for(int i=0; i < indent; i++) s += " "; result.append("\n").append(s); spaceNeeded = false; } public void defaultIn(Node node) { } public void defaultOut(Node node) { } public void defaultCase(Node node) { } public void inAMessage(AMessage node){ result.append("(").append(node.getIdentifier().getText()); } public void outAMessage(AMessage node){ result.append("\n)"); } public void inAParameter(AParameter node){ newLine(); result.append(":").append(node.getIdentifier().getText()).append(" "); } public void inAComplexContent(AComplexContent node){ if(indentNeeded > 0) { indent(); } else { indent += 10; } result.append("("); indentNeeded++; spaceNeeded = false; } public void outAComplexContent(AComplexContent node){ result.append(")"); indentNeeded--; indent -= tabSize; spaceNeeded = false; } public void inAIdentifierContent(AIdentifierContent node){ if(spaceNeeded) { result.append(" "); } result.append(node.getIdentifier().getText()); spaceNeeded = true; } public void inAExprContent(AExprContent node){ if(spaceNeeded) { result.append(" "); } result.append(node.getLiteral().getText()); spaceNeeded = true; } public void inAPropertyContent(APropertyContent node){ if(spaceNeeded) { result.append(" "); } result.append(":").append(node.getIdentifier().getText()); spaceNeeded = true; } public void inAStringContent(AStringContent node){ if(spaceNeeded) { result.append(" "); } result.append(node.getString().getText()); spaceNeeded = true; } }