# simple-prettyprinter.api
#
# A fast, simple prettyprinter. Its major advantage is that
# the complete implementation is only 300 lines of code -- you
# can read and understand it completely in an hour.
#
# This api is used heavily in:
#
src/lib/compiler/back/low/tools/adl-syntax/adl-raw-syntax-unparser.pkg#
# Compare to:
#
src/lib/prettyprint/big/src/standard-prettyprinter.api# Compiled by:
#
src/lib/std/standard.lib# This api is implemented in:
#
src/lib/prettyprint/simple/simple-prettyprinter.pkgapi Simple_Prettyprinter {
#
Prettyprint_Expression # Holds a collection of pretty-printed text.
#
# Leaf values for prettyprint expression trees:
#
= INT Int # 31-bit signed integer.
| INT1 one_word_int::Int
# 32-bit signed integer.
| INTEGER multiword_int::Int
# Indefinition-precision signed integer
| UNT Unt
# 31-bit unsigned integer.
| UNT1 one_word_unt::Unt
# 32-bit unsigned integer.
| FLOAT Float
# 64-bit floating-point number.
| BOOL Bool
#
| CHAR Char
#
| STRING String
#
| NOP
# Placeholder; produces no text output whatever.
# Leaf values containing identifiers like "foo" or "=".
#
# The critical difference between 'alphabetic' and 'punctuation'
# is just that 'alphabetic' will automatically insert a leading blank
# if it follows an alphabetic, number or string, but 'punctuation'
# does no such automatic blank insertion. (Sometimes we use
# 'alphabetic' to force blank insertion even on non-alphabetic
# identifiers like "=".)
#
| ALPHABETIC String
# Append a blank if preceding token was an alphabetic, number or string token, then append given string.
| PUNCTUATION String
# Append given string to buffer.
# Explicit whitespace:
#
| MAYBE_BLANK
# Insert a blank, except do nothing if previous token was a blank or newline. Was 'sp'
| NEWLINE
# Start new line, set 'current column' to zero.
# Concatenating multiple expressions
# to make a single expression:
#
| CONS (Prettyprint_Expression, Prettyprint_Expression)
# Print two expressions in given order. Clients typically assign the infix op ++ as a synonym for this.
| CAT List( Prettyprint_Expression )
# Print list of expressions in given order.
# Indented blocks:
#
| ENTER_INDENTED_BLOCK
# Start block indented four spaces relative to enclosing block.
| ENTER_DEEPLY_INDENTED_BLOCK
# Start block indented to current column. was enter_intented_block'
| LEAVE_INDENTED_BLOCK
# Exit block started by either of above two commands.
| INDENT
# Space over to column for innermost indented block.
| INDENT_OFFSET Int
# Space over to column for innermost indented block, plus 'Int' (indent_offset). was indent'
| SET_WRAP_COLUMN Int
# Defaults to 80.
| INDENTED_BLOCK Prettyprint_Expression
# == ENTER_INDENTED_BLOCK ++ prettyprint_expression ++ LEAVE_INDENTED_BLOCK;
| INDENTED_LINE Prettyprint_Expression
# == INDENT ++ prettyprint_expression ++ NEWLINE;
#
| MAYBE_LINEWRAP
# If current column + right_margin > wrap column, insert newline and space over to current indent level + indent_offset.
{ right_margin: Int,
indent_offset: Int
}
# User-defined modes. The modestack
# starts out as ["default"] and is entirely
# for client use; the internal prettyprinter
# package code makes no use of it. This is
# currently heavily used (only) in
#
src/lib/compiler/back/low/tools/adl-syntax/adl-raw-syntax-unparser.pkg # where we print items differently
# in "code" vs "default" modes:
#
| PUSH_MODE String
# Push arbitrary user string on user-controlled mode stack.
| POP_MODE
# Pop top entry from modestack; raises exception DIE exception if modestack is empty.
| PER_MODE (String -> Prettyprint_Expression)
# User-supplied function will select prettyprint expression based on current mode (i.e., top string on modestack).
# PER_MODE raises exception DIE if modestack is empty at rendering time.
# Random convenience functions:
#
| IN_PARENTHESES Prettyprint_Expression
# == PUNCTUATION "(" ++ prettyprint_expression ++ PUNCTUATION ")";
#
| LIST
# Format a list with given bracket and separator strings, e.g. ["foo","bar"] as "[foo,bar]"
{ leftbracket: Prettyprint_Expression,
separator: Prettyprint_Expression,
rightbracket: Prettyprint_Expression,
elements: List( Prettyprint_Expression )
}
# Print a construct like
#
# {
# ...
# }
#
| BRACKETED_BLOCK
{ leftbracket: String, # Opening bracket for block. Printed using 'punctuation'.
body: Prettyprint_Expression, # Body of block, indented between brackets.
rightbracket: String # Closing bracket for block.
};
prettyprint_expression_to_string: Prettyprint_Expression -> String; # Convert Prettyprint_Expression to indented text.
longest_line_in_prettyprint_expression: Prettyprint_Expression -> Int; # Useful when doing a trial prettyprint to see if it fits within the margins.
};