polyglot.util
Class CodeWriter

java.lang.Object
  extended by polyglot.util.CodeWriter
Direct Known Subclasses:
StringPrettyPrinter.StringCodeWriter

public class CodeWriter
extends java.lang.Object

A CodeWriter is a pretty-printing engine. It formats structured text onto an output stream o in the minimum number of lines, while keeping the width of the output within width characters if possible. Newlines occur in the output only at places where a line break is permitted by the use of allowBreak and unifiedBreak. Line breaks can have different levels, which is useful for implementing things like Scheme's "miser mode".


Field Summary
static boolean debug
           
static boolean precompute
           
 
Constructor Summary
CodeWriter(java.io.OutputStream o, int width_)
          Create a CodeWriter object with output stream o and width width_.
CodeWriter(java.io.PrintWriter o, int width_)
          Create a CodeWriter object.
CodeWriter(java.io.Writer o, int width_)
          Create a CodeWriter object.
 
Method Summary
 void allowBreak(int n)
          The most common use of "allowBreak": level 1, with an alternative of a single space.
 void allowBreak(int n, int level, java.lang.String alt, int altlen)
          Insert a break (an optional newline).
 void allowBreak(int n, java.lang.String alt)
           
 void begin(int n)
          Start a new block with a relative indentation of n characters.
 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()
          Force a newline.
 void newline(int n)
          Like newline(), but forces a newline with a specified indentation.
 java.lang.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, java.lang.String alt, int altlen)
          Insert a unified break.
 void write(java.lang.String s)
          Print the string s verbatim on the output stream.
 void write(java.lang.String s, int length)
          Print the string s on the output stream.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

debug

public static final boolean debug
See Also:
Constant Field Values

precompute

public static final boolean precompute
See Also:
Constant Field Values
Constructor Detail

CodeWriter

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


CodeWriter

public CodeWriter(java.io.PrintWriter o,
                  int width_)
Create a CodeWriter object.

Parameters:
w - the output to write to. Must be non-null.
width - the formatting width. Must be positive.

CodeWriter

public CodeWriter(java.io.Writer o,
                  int width_)
Create a CodeWriter object.

Parameters:
w - the output to write to. Must be non-null.
width - the formatting width. Must be positive.
Method Detail

write

public void write(java.lang.String s)
Print the string s verbatim on the output stream.

Parameters:
the - string to print.

write

public void write(java.lang.String s,
                  int length)
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.

Parameters:
s -
length -

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.

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.


allowBreak

public void allowBreak(int n,
                       int level,
                       java.lang.String alt,
                       int altlen)
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. It 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.

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

unifiedBreak

public void unifiedBreak(int n,
                         int level,
                         java.lang.String alt,
                         int altlen)
Insert a unified break. Unified breaks act like the breaks inserted by allowBreak, but unified breaks 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 that other breaks of the same level (including other unified breaks!).

Parameters:
n - the relative indentation
level - the level of the break
alt - the alternative text
See Also:
allowBreak

allowBreak

public void allowBreak(int n)
The most common use of "allowBreak": level 1, with an alternative of a single space.

Parameters:
n - the indentation relative to the current block.

allowBreak

public void allowBreak(int n,
                       java.lang.String alt)

newline

public void newline()
Force a newline. Indentation will be preserved. 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.


newline

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


flush

public boolean flush()
              throws java.io.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).

Throws:
java.io.IOException

flush

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

Parameters:
format - whether to pretty-print the output
Returns:
whether formatting was completely successful.
Throws:
java.io.IOException

toString

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

Overrides:
toString in class java.lang.Object