polyglot.util
Class OptimalCodeWriter

java.lang.Object
  extended by polyglot.util.CodeWriter
      extended by polyglot.util.OptimalCodeWriter

public class OptimalCodeWriter
extends CodeWriter

The pretty-printing algorithm is loosely based on the Modula-3 pretty-printer, and on notes by Greg Nelson. It was extended to support breaks at multiple levels. OptimalCodeWriter follows the "break from root" rule: if a break is broken, breaks of equal or lower level in all containing blocks must also be broken, and breaks of strictly lower level in the same block must also be broken.


Field Summary
protected  polyglot.util.BlockItem current
           
static boolean debug
           
protected static int format_calls
           
protected  polyglot.util.BlockItem input
           
protected  PrintWriter output
           
static boolean precompute
           
static boolean showInput
           
protected static polyglot.util.Item top
           
protected  int trace_indent
          Amount to indent during tracing.
static boolean visualize
           
protected  int width
           
 
Constructor Summary
OptimalCodeWriter(OutputStream o, int width_)
          Create a OptimalCodeWriter object with output stream o and width width_.
OptimalCodeWriter(PrintWriter o, int width_)
          Create a OptimalCodeWriter object.
OptimalCodeWriter(Writer o, int width_)
          Create a OptimalCodeWriter object.
 
Method Summary
 void allowBreak(int n, int level, String alt, int altlen)
          Insert a break (an optional newline).
 void begin(int n)
          Start a new block with a relative indentation of n characters.
 void close()
          Flush all formatted text, reset formatter state, and close the underlying writer.
 void end()
          Terminate the most recent outstanding begin.
 boolean flush()
          Send out the current batch of text to be formatted.
 boolean flush(boolean format)
          Like flush, but passing format=false causes output to be generated in the fastest way possible, with all breaks broken.
 void newline()
          This method should be used sparingly; usually a call to allowBreak is preferable because forcing a newline also causes all breaks in containing blocks to be broken.
 void newline(int n, int level)
          Like newline(), but forces a newline with a specified indentation.
 String toString()
          Return a readable representation of all the structured input given to the CodeWriter since the last flush.
 void unifiedBreak(int n, int level, String alt, int altlen)
          Insert a unified break.
 void write(String s)
          Print the string s verbatim on the output stream.
 void write(String s, int length)
          Print the string s on the output stream.
 
Methods inherited from class polyglot.util.CodeWriter
allowBreak, allowBreak, newline, unifiedBreak
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

input

protected polyglot.util.BlockItem input

current

protected polyglot.util.BlockItem current

top

protected static polyglot.util.Item top

output

protected PrintWriter output

width

protected int width

format_calls

protected static int format_calls

debug

public static final boolean debug
See Also:
Constant Field Values

showInput

public static final boolean showInput
See Also:
Constant Field Values

visualize

public static final boolean visualize
See Also:
Constant Field Values

precompute

public static final boolean precompute
See Also:
Constant Field Values

trace_indent

protected int trace_indent
Amount to indent during tracing.

Constructor Detail

OptimalCodeWriter

public OptimalCodeWriter(OutputStream o,
                         int width_)
Create a OptimalCodeWriter object with output stream o and width width_.

Parameters:
o - the writer to write to. Must be non-null.
width_ - the formatting width. Must be positive.

OptimalCodeWriter

public OptimalCodeWriter(PrintWriter o,
                         int width_)
Create a OptimalCodeWriter object.

Parameters:
o - the writer to write to. Must be non-null.
width_ - the formatting width. Must be positive.

OptimalCodeWriter

public OptimalCodeWriter(Writer o,
                         int width_)
Create a OptimalCodeWriter object.

Parameters:
o - the writer to write to. Must be non-null.
width_ - the formatting width. Must be positive.
Method Detail

write

public void write(String s)
Description copied from class: CodeWriter
Print the string s verbatim on the output stream.

Specified by:
write in class CodeWriter
Parameters:
s - the string to print.

write

public void write(String s,
                  int length)
Description copied from class: CodeWriter
Print the string s on the output stream. Pretend that it has width length even if it has a different number of characters. This is useful when the string contains escape sequences, HTML character entity references, etc.

Specified by:
write in class CodeWriter

begin

public void begin(int n)
Start a new block with a relative indentation of n characters.

A block is a formatting unit. The formatting algorithm will try to put the whole block in one line unless

If either of the two conditions is satisfied, the formatting algorithm will break the block into lines by generating newlines for some of the inserted breaks. The first line is printed at the current cursor position pos, all the following lines are printed at the position pos+n.

Specified by:
begin in class CodeWriter
Parameters:
n - the number of characters increased on indentation (relative to the current position) for all lines in the block. Requires: n >= 0.

end

public void end()
Terminate the most recent outstanding begin.

Specified by:
end in class CodeWriter

allowBreak

public void allowBreak(int n,
                       int level,
                       String alt,
                       int altlen)
Description copied from class: CodeWriter
Insert a break (an optional newline). Indentation will be preserved. Every break has a level. A level 0 break is always broken to form a newline. The codewriter tries to avoid breaking higher-level breaks, and the higher the level, the harder it tries.

Specified by:
allowBreak in class CodeWriter
Parameters:
n - indentation relative to the current block if the newline is inserted. Requires: n >= 0
level - the level of the break. Requires: level >= 0
alt - if no newline is inserted, the string alt is output instead. Requires: alt != null

unifiedBreak

public void unifiedBreak(int n,
                         int level,
                         String alt,
                         int altlen)
Description copied from class: CodeWriter
Insert a unified break. Unified breaks act like the breaks inserted by allowBreak, but unified breaks should also break if any break of the same level in the same block is broken, whereas ordinary breaks do not necessarily break in this case. That is, unified breaks act as if they were slightly lower level than other breaks of the same level (including other unified breaks!).

Specified by:
unifiedBreak in class CodeWriter
Parameters:
n - the relative indentation
level - the level of the break
alt - the alternative text
See Also:
CodeWriter.unifiedBreak(int, int, java.lang.String, int)

newline

public void newline()
This method should be used sparingly; usually a call to allowBreak is preferable because forcing a newline also causes all breaks in containing blocks to be broken.

Overrides:
newline in class CodeWriter

newline

public void newline(int n,
                    int level)
Like newline(), but forces a newline with a specified indentation.

Specified by:
newline in class CodeWriter

flush

public boolean flush()
              throws IOException
Send out the current batch of text to be formatted. All outstanding begin's are closed and the current indentation level is reset to 0. Returns true if formatting was completely successful (the margins were obeyed).

Specified by:
flush in class CodeWriter
Throws:
IOException

flush

public boolean flush(boolean format)
              throws IOException
Like flush, but passing format=false causes output to be generated in the fastest way possible, with all breaks broken.

Specified by:
flush in class CodeWriter
Parameters:
format - whether to pretty-print the output
Returns:
whether formatting was completely successful.
Throws:
IOException

close

public void close()
           throws IOException
Description copied from class: CodeWriter
Flush all formatted text, reset formatter state, and close the underlying writer.

Specified by:
close in class CodeWriter
Throws:
IOException

toString

public String toString()
Return a readable representation of all the structured input given to the CodeWriter since the last flush.

Specified by:
toString in class CodeWriter