|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES All Classes | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectpolyglot.util.CodeWriter
polyglot.util.OptimalCodeWriter
public class OptimalCodeWriter
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 |
---|
protected polyglot.util.BlockItem input
protected polyglot.util.BlockItem current
protected static polyglot.util.Item top
protected PrintWriter output
protected int width
protected static int format_calls
public static final boolean debug
public static final boolean showInput
public static final boolean visualize
public static final boolean precompute
protected int trace_indent
Constructor Detail |
---|
public OptimalCodeWriter(OutputStream o, int width_)
o
and width width_
.
o
- the writer to write to. Must be non-null.width_
- the formatting width. Must be positive.public OptimalCodeWriter(PrintWriter o, int width_)
o
- the writer to write to. Must be non-null.width_
- the formatting width. Must be positive.public OptimalCodeWriter(Writer o, int width_)
o
- the writer to write to. Must be non-null.width_
- the formatting width. Must be positive.Method Detail |
---|
public void write(String s)
CodeWriter
s
verbatim on the output stream.
write
in class CodeWriter
s
- the string to print.public void write(String s, int length)
CodeWriter
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.
write
in class CodeWriter
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
.
begin
in class CodeWriter
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
.
end
in class CodeWriter
public void allowBreak(int n, int level, String alt, int altlen)
CodeWriter
allowBreak
in class CodeWriter
n
- indentation relative to the current block if the newline is
inserted. Requires: n >= 0level
- the level of the break. Requires: level >= 0alt
- if no newline is inserted, the string alt
is
output instead. Requires: alt != nullpublic void unifiedBreak(int n, int level, String alt, int altlen)
CodeWriter
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!).
unifiedBreak
in class CodeWriter
n
- the relative indentationlevel
- the level of the breakalt
- the alternative textCodeWriter.unifiedBreak(int, int, java.lang.String, int)
public void newline()
allowBreak
is preferable because forcing a newline also
causes all breaks in containing blocks to be broken.
newline
in class CodeWriter
public void newline(int n, int level)
newline
in class CodeWriter
public boolean flush() throws 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).
flush
in class CodeWriter
IOException
public boolean flush(boolean format) throws IOException
flush
, but passing format=false
causes output to be generated in the fastest way possible, with
all breaks broken.
flush
in class CodeWriter
format
- whether to pretty-print the output
IOException
public void close() throws IOException
CodeWriter
close
in class CodeWriter
IOException
public String toString()
toString
in class CodeWriter
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES All Classes | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |