An 'ASCII art' AST printer for debugging ANTLR grammars.
| Method from org.hibernate.hql.ast.util.ASTPrinter Detail: |
public static void appendEscapedMultibyteChars(String text,
StringBuffer buf) {
char[] chars = text.toCharArray();
for (int i = 0; i < chars.length; i++) {
char aChar = chars[i];
if (aChar > 256) {
buf.append("\\u");
buf.append(Integer.toHexString(aChar));
}
else
buf.append(aChar);
}
}
|
public static String escapeMultibyteChars(String text) {
StringBuffer buf = new StringBuffer();
appendEscapedMultibyteChars(text,buf);
return buf.toString();
}
|
public static String getConstantName(Class tokenTypeConstants,
int type) {
String tokenTypeName = null;
if ( tokenTypeConstants != null ) {
Field[] fields = tokenTypeConstants.getFields();
for ( int i = 0; i < fields.length; i++ ) {
Field field = fields[i];
tokenTypeName = getTokenTypeName( field, type, true );
if ( tokenTypeName != null ) {
break; // Stop if found.
}
} // for
} // if type constants were provided
// Use the integer value if no token type name was found
if ( tokenTypeName == null ) {
tokenTypeName = Integer.toString( type );
}
return tokenTypeName;
}
Get a single token type name in the specified set of token type constants (interface). |
public boolean isShowClassNames() {
return showClassNames;
}
Returns true if the node class names will be displayed. |
public String nodeToString(AST ast,
boolean showClassName) {
if ( ast == null ) {
return "{null}";
}
StringBuffer buf = new StringBuffer();
buf.append( "[" ).append( getTokenTypeName( ast.getType() ) ).append( "] " );
if ( showClassName ) {
buf.append( StringHelper.unqualify( ast.getClass().getName() ) ).append( ": " );
}
buf.append( "'" );
String text = ast.getText();
appendEscapedMultibyteChars(text, buf);
buf.append( "'" );
if ( ast instanceof DisplayableNode ) {
DisplayableNode displayableNode = ( DisplayableNode ) ast;
// Add a space before the display text.
buf.append( " " ).append( displayableNode.getDisplayText() );
}
String s = buf.toString();
return s;
}
|
public void setShowClassNames(boolean showClassNames) {
this.showClassNames = showClassNames;
}
Enables or disables AST node class name display. |
public String showAsString(AST ast,
String header) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
PrintStream ps = new PrintStream( baos );
ps.println( header );
showAst( ast, ps );
ps.flush();
return new String( baos.toByteArray() );
}
Prints the AST in 'ASCII art' tree form into a string. |
public void showAst(AST ast,
PrintWriter pw) {
ArrayList parents = new ArrayList();
showAst( parents, pw, ast );
pw.flush();
}
Prints the AST in 'ASCII art' tree form to the specified print writer. |