This section details the new features of JTB found in versions cumulatively through 1.1pre3.
JTB 1.1pre1 adds a new option in addition to the ones described above. The main change with this version of JTB is the addition of a syntax tree builder for the Scheme programming language. The command-line option -scheme does two things:
//***Note these methods have changed for version 1.1pre3***
public class NodeToken implements Node {
public NodeToken(String s);
public int numSpecials();
public String getSpecialAt(int i);
public void addSpecial(String s);
public void trimSpecials();
public String toString();
public String withSpecials();
public void accept(visitor.Visitor v);
public String tokenImage;
}
// // f0 -> "class" // f1 -> <IDENTIFIER> // f2 -> [ "extends" Name() ] // f3 -> [ "implements" NameList() ] // f4 -> ClassBody() //Looks like this in 1.1pre3 when the -jd flag is used:
| public void flushWriter() | Flushes the OutputStream or Writer that TreeDumper is using to output the syntax tree. |
| public void printSpecials(boolean b) | Allows you to specify whether or not to print special tokens. |
| public void startAtNextToken() | Starts the tree dumper on the line containing the next token visited. For example, if the next token begins on line 50 and the dumper is currently on line 1 of the file, it will set its current line to 50 and continue printing from there, as opposed to printing 49 blank lines and then printing the token. |
| public void resetPosition() | Resets the position of the internal "cursor" to the first line and
column. For example, if the interal cursor was at line twenty and
the next token begins on line twenty one, a single carriage return is output,
then the token. If resetPosition() is called, the interal
cursor will be reset to line 1. Twenty carriage returns would be
output, then the token.
When using a dumper on a syntax tree more than once, you either need to call this method or startAtNextToken() between each dump. |
root.accept(new DepthFirstVisitor() {
public void visit(MethodDeclaration n) {
dumper.startAtNextToken();
n.f0.accept(dumper);
n.f1.accept(dumper);
n.f2.accept(dumper);
n.f3.accept(dumper);
// skip n.f4, the method body
System.out.println();
}
});
| public TreeFormatter(int indentAmt,
int wrapWidth) |
Allows you to specify the number of spaces per indentation level and the number of columns per line, after which tokens are wrapped to the next line (the default constructor assumes an indentAmt of 3 and a wrapWidth of 0, i.e. no line wrapping). |
| protected void add(FormatCommand cmd) | Use this method to add FormatCommands to the command queue to be executed when the next token in the tree is visited. |
| protected FormatCommand force(int i) | A Force command inserts one or more line breaks and indents the next line to the current indentation level. Without an argument, adds just one line break. Use add(force()); |
| protected FormatCommand indent() | An Indent command increases the indentation level by one or more. Without an argument, just adds one indent level. Use add(indent()); |
| protected FormatCommand outdent() | An Outdent command is the reverse of the Indent command: it reduces the indentation level. Use add(outdent()); |
| protected FormatCommand space() | A Space command simply adds one or more spaces between tokens. Without an argument, adds just just one space. Use add(space()); |
| protected void processList(
NodeListInterface n, FormatCommand cmd) |
Visits each element of a NodeList, NodeListOptional, or NodeSequence and inserts an optional FormatCommand between each element (but not after the last one). |
/**
* f0 -> [ PackageDeclaration() ]
* f1 -> ( ImportDeclaration() )*
* f2 -> ( TypeDeclaration() )*
* f3 -> <EOF>
*/
public void visit(CompilationUnit n) {
if ( n.f0.present() ) {
n.f0.accept(this);
add(force(2));
}
if ( n.f1.present() ) {
processList(n.f1, force());
add(force(2));
}
if ( n.f2.present() ) {
processList(n.f2, force(2));
add(force());
}
n.f3.accept(this);
}
Still have questions? Suggestions on improving this document?
Feel free to mail Wanjun Wang or
Jens Palsberg.
| Maintained by Wanjun Wang, wanjun@purdue.edu. | Created January 6, 1999.
Last modified June 26, 1999. |