|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object polyglot.util.CodeWriter
public class CodeWriter
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 |
---|
public static final boolean debug
public static final boolean precompute
Constructor Detail |
---|
public CodeWriter(java.io.OutputStream o, int width_)
o
and width width_
.
public CodeWriter(java.io.PrintWriter o, int width_)
w
- the output to write to. Must be non-null.width
- the formatting width. Must be positive.public CodeWriter(java.io.Writer o, int width_)
w
- the output to write to. Must be non-null.width
- the formatting width. Must be positive.Method Detail |
---|
public void write(java.lang.String s)
s
verbatim on the output stream.
the
- string to print.public void write(java.lang.String s, int length)
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.
s
- length
- public void begin(int n)
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
.
n
- the number of characters increased on indentation (relative
to the current position) for all lines in the block.
Requires: n >= 0.public void end()
begin
.
public void allowBreak(int n, int level, java.lang.String alt, int altlen)
n
- indentation relative to the current block if the newline is
inserted. Requires: n >= 0alt
- if no newline is inserted, the string alt
is
output instead. Requires: alt != nulllevel
- the level of the break. Requires: level >= 0public void unifiedBreak(int n, int level, java.lang.String alt, int altlen)
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!).
n
- the relative indentationlevel
- the level of the breakalt
- the alternative textallowBreak
public void allowBreak(int n)
n
- the indentation relative to the current block.public void allowBreak(int n, java.lang.String alt)
public void newline()
allowBreak
is preferable
because forcing a newline also causes all breaks in containing blocks to
be broken.
public void newline(int n)
public boolean flush() throws java.io.IOException
begin
's are closed and the current indentation level is
reset to 0. Returns true if formatting was completely successful (the
margins were obeyed).
java.io.IOException
public boolean flush(boolean format) throws java.io.IOException
flush
, but passing format=false
causes output to be generated in the fastest way possible, with
all breaks broken.
format
- whether to pretty-print the output
java.io.IOException
public java.lang.String toString()
toString
in class java.lang.Object
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |