polyglot.util
Class CodeWriter

java.lang.Object
  extended by polyglot.util.CodeWriter
Direct Known Subclasses:
OptimalCodeWriter, SimpleCodeWriter

public abstract class CodeWriter
extends 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 "miser mode" layout.


Constructor Summary
CodeWriter()
           
 
Method Summary
 void allowBreak(int n)
          The most common use of "allowBreak": level 1, with an alternative of a single space.
abstract  void allowBreak(int n, int level, String alt, int altlen)
          Insert a break (an optional newline).
 void allowBreak(int n, String alt)
           
abstract  void begin(int n)
          Start a new block with a relative indentation of n characters.
abstract  void close()
          Flush all formatted text, reset formatter state, and close the underlying writer.
abstract  void end()
          Terminate the most recent outstanding begin.
abstract  boolean flush()
          Flush all formatted text to the underlying writer.
abstract  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.
abstract  void newline(int n, int level)
          newline with a specified indentation and level.
abstract  String toString()
          Return a readable representation of all the structured input given to the CodeWriter since the last flush.
 void unifiedBreak(int n)
          The most common use of "unifiedBreak": level 1, with an alternative of a single space.
abstract  void unifiedBreak(int n, int level, String alt, int altlen)
          Insert a unified break.
abstract  void write(String s)
          Print the string s verbatim on the output stream.
abstract  void write(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
 

Constructor Detail

CodeWriter

public CodeWriter()
Method Detail

write

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

Parameters:
s - the string to print.

write

public abstract void write(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 abstract void begin(int n)
Start a new block with a relative indentation of n characters.


end

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


allowBreak

public abstract void allowBreak(int n,
                                int level,
                                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.

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 abstract void unifiedBreak(int n,
                                  int level,
                                  String alt,
                                  int altlen)
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!).

Parameters:
n - the relative indentation
level - the level of the break
alt - the alternative text
See Also:
allowBreak(int, int, java.lang.String, int)

unifiedBreak

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

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

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,
                       String alt)

newline

public void newline()
Force a newline. Indentation will be preserved.


newline

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


newline

public abstract void newline(int n,
                             int level)
newline with a specified indentation and level.


flush

public abstract boolean flush()
                       throws IOException
Flush all formatted text to the underlying writer. Returns true if formatting was completely successful (the margins were obeyed).

Throws:
IOException

close

public abstract void close()
                    throws IOException
Flush all formatted text, reset formatter state, and close the underlying writer.

Throws:
IOException

flush

public abstract 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.

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

toString

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

Overrides:
toString in class Object