gov.llnl.babel.backend.writers

Class PrettyWriter

Known Direct Subclasses:
LanguageWriter

public class PrettyWriter
extends java.lang.Object

The PrettyWriter class formats output for the backend code generators. Formatted output is sent to a PrintWriter class. The pretty writer supports two optional formatting modes. The first mode is "line breaking", in which long lines are broken at a specified column and optional end-of-line or start-of-line continuation strings are printed for the broken lines. For example, Fortran line breaking would set the break column at 72 with a start-of-line string of " &" and a null end-of-line string. Python line breaking might set a line break at column 78 with an end-of-line string of "\" and a null start-of-line string. In all cases, the continued line is indented one tab level. The second mode is "block commenting", in which optional line-start or after-tab strings are printed for comment blocks. For example, a Fortran comment block might set the line-start string to "C" and the after-tab string to null whereas Python would set the line-start string to null and the after-tab string to "#". In all cases, comment block mode supercedes line breaking mode such that lines are not broken in comment blocks. Column numbers start with zero as the first column in a line. Note also that newlines are output as '\n' and not using the print writer default println() command, which adds the extra DOS endo-of-line characters that confuses cygwin utilities like automake.

Constructor Summary

PrettyWriter(PrintWriter writer)
Create a new PrettyWriter instance that will send output to the specified print writer object.

Method Summary

void
backTab()
Decrease the tab level by one so that newlines begin at the previous tab stop.
void
changeTabLevel(int delta)
Change the current tab level at the beginning of a new lines by the specified positive or negative amount.
void
close()
Flush and close the associated print writer stream.
void
defineBlockComment(String line_start, String after_tab)
Define the pretty printer parameters for block comment mode.
void
disableBlockComment()
Disable block comment mode.
void
disableLineBreak()
Disable line breaking mode.
void
enableBlockComment()
Enable block comment mode.
void
enableLineBreak()
Enable line breaking mode.
void
enableLineBreak(int column, String start, String end)
Define the pretty printer parameters for line breaking.
void
flushPrintWriter()
Flush the underlying print writer stream.
PrintWriter
getPrintWriter()
Retrieve the underlying print writer stream.
void
popLineBreak()
Restore the line break status to its previous value.
void
print(String s)
Print the specified string to the print writer output stream.
void
printAligned(String text, int field)
Print the text string in a field of the specified width.
void
printSpaces(int nspaces)
Print the specified number of spaces to the pretty writer.
void
printUnformatted(String s)
Print unformatted text to the print writer stream.
void
println()
Advance the output stream to the next line.
void
println(String s)
Print the specified string followed by a newline to the print writer output stream.
void
printlnUnformatted(String s)
Print unformatted text to the print writer stream.
void
pushLineBreak(boolean linebreak)
Change the line break status to linebreak and save the previous value on a stack.
void
restoreFirstTabStop()
Restore the default first tab stop to its original value before the call to setTemporaryFirstTabStop.
void
setFirstTabStop(int column)
Set the first tab stop for this pretty writer.
void
setLineBreakString(String s)
Set the line breaking characters.
void
setStrictBreaking()
void
setTabSpacing(int spacing)
Set the tab spacing for this pretty writer.
void
setTemporaryFirstTabStop()
Set a temporary tab stop at the current cursor location.
void
tab()
Increase the tab level by one so that newlines begin at the next tab stop.

Constructor Details

PrettyWriter

public PrettyWriter(PrintWriter writer)
Create a new PrettyWriter instance that will send output to the specified print writer object. The first tab stop is set to column zero with a tab spacing of three. Both line breaking and block commenting modes are disabled.

Method Details

backTab

public void backTab()
Decrease the tab level by one so that newlines begin at the previous tab stop.

changeTabLevel

public void changeTabLevel(int delta)
Change the current tab level at the beginning of a new lines by the specified positive or negative amount. If the tab level drops below zero, then it is reset to zero.

close

public void close()
Flush and close the associated print writer stream. No other pretty writer or print writer routines may be called after the print writer stream is closed.

defineBlockComment

public void defineBlockComment(String line_start,
                               String after_tab)
Define the pretty printer parameters for block comment mode. The first argument defines the start-of-line string and the second defines the after-tab string. This routine does not turn on block comment mode.

disableBlockComment

public void disableBlockComment()
Disable block comment mode.

disableLineBreak

public void disableLineBreak()
Disable line breaking mode.

enableBlockComment

public void enableBlockComment()
Enable block comment mode. This mode takes precedence over line breaking so that comment lines are not broken.

enableLineBreak

public void enableLineBreak()
Enable line breaking mode. Note that block comment mode takes precedence over line breaking mode.

enableLineBreak

public void enableLineBreak(int column,
                            String start,
                            String end)
Define the pretty printer parameters for line breaking. The column argument specifies the maximum column number (with the left margin starting at column zero). A column number of 80 means that up to eighty characters can be printed on the line before the line breaking algorithm splits the line. The start and end strings indicate the continuation strings to be output for continuation lines. Either or both of these arguments may be null. For example, in Fortran, the start string will be " &" and the end string null. In most shell languages, the start string will be null and the end string "\". This routine automatically enables line breaking.

flushPrintWriter

public void flushPrintWriter()
Flush the underlying print writer stream.

getPrintWriter

public PrintWriter getPrintWriter()
Retrieve the underlying print writer stream. Note that printing directly to the print writer stream should only be done on newline boundaries since direct output will bypass internal pretty writer buffering.

popLineBreak

public void popLineBreak()
Restore the line break status to its previous value.

print

public void print(String s)
Print the specified string to the print writer output stream. Nothing is done if the string argument is null. If the string contains newlines "\n", then println is called on the substrings between the newlines.

printAligned

public void printAligned(String text,
                         int field)
Print the text string in a field of the specified width. If the text string is less than the field width, the field is filled with spaces (after the text string).

printSpaces

public void printSpaces(int nspaces)
Print the specified number of spaces to the pretty writer. If the number of spaces is zero or negative, then nothing is done.

printUnformatted

public void printUnformatted(String s)
Print unformatted text to the print writer stream. Switching between formatted and unformatted text should only be done on newline boundaries.

println

public void println()
Advance the output stream to the next line. This method flushes the output stream.

println

public void println(String s)
Print the specified string followed by a newline to the print writer output stream. If the argument string is null or empty, then the writer output stream is advanced to the next line. If the string contains newlines "\n", then println is called on the substrings between the newlines. This method flushes the output stream.

printlnUnformatted

public void printlnUnformatted(String s)
Print unformatted text to the print writer stream. Switching between formatted and unformatted text should only be done on newline boundaries.

pushLineBreak

public void pushLineBreak(boolean linebreak)
Change the line break status to linebreak and save the previous value on a stack.

restoreFirstTabStop

public void restoreFirstTabStop()
Restore the default first tab stop to its original value before the call to setTemporaryFirstTabStop.

setFirstTabStop

public void setFirstTabStop(int column)
Set the first tab stop for this pretty writer. The first tab stop must be nonnegative.

setLineBreakString

public void setLineBreakString(String s)
Set the line breaking characters. Lines will be broken using these characters as separators. Each character will be tested in order. The default like breaking characters are ", (". This means that lines will be tested for commas first, then spaces, and finally open parenthesis. If set to null, then line breaking is disabled.

setStrictBreaking

public void setStrictBreaking()

setTabSpacing

public void setTabSpacing(int spacing)
Set the tab spacing for this pretty writer. The tab stop value must be greater than zero.

setTemporaryFirstTabStop

public void setTemporaryFirstTabStop()
Set a temporary tab stop at the current cursor location. This capability is useful for aligning function arguments. The temporary tab stop may be removed by calling the restore first tab stop method.

tab

public void tab()
Increase the tab level by one so that newlines begin at the next tab stop.