public class OptimalCodeWriter extends CodeWriter
Modifier and Type | Field and Description |
---|---|
protected polyglot.util.BlockItem |
current |
static boolean |
debug |
protected static int |
format_calls |
protected polyglot.util.BlockItem |
input |
protected java.io.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 and Description |
---|
OptimalCodeWriter(java.io.OutputStream o,
int width_)
Create a OptimalCodeWriter object with output stream
o
and width width_ . |
OptimalCodeWriter(java.io.PrintWriter o,
int width_)
Create a OptimalCodeWriter object.
|
OptimalCodeWriter(java.io.Writer o,
int width_)
Create a OptimalCodeWriter object.
|
Modifier and Type | Method and Description |
---|---|
void |
allowBreak(int n,
int level,
java.lang.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.
|
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. |
allowBreak, allowBreak, newline, unifiedBreak
protected polyglot.util.BlockItem input
protected polyglot.util.BlockItem current
protected static polyglot.util.Item top
protected java.io.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
public OptimalCodeWriter(java.io.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(java.io.PrintWriter o, int width_)
o
- the writer to write to. Must be non-null.width_
- the formatting width. Must be positive.public OptimalCodeWriter(java.io.Writer o, int width_)
o
- the writer to write to. Must be non-null.width_
- the formatting width. Must be positive.public void write(java.lang.String s)
CodeWriter
s
verbatim on the output stream.write
in class CodeWriter
s
- the string to print.public void write(java.lang.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, java.lang.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 != nullaltlen
- the length of 'alt' in characterspublic void unifiedBreak(int n, int level, java.lang.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 textaltlen
- the length in characters that 'alt' will be treated as taking up.CodeWriter.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 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).flush
in class CodeWriter
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.flush
in class CodeWriter
format
- whether to pretty-print the outputjava.io.IOException
public void close() throws java.io.IOException
CodeWriter
close
in interface java.lang.AutoCloseable
close
in class CodeWriter
java.io.IOException
public java.lang.String toString()
toString
in class CodeWriter