Printer
loreline.Printer (Class)
A code printer that converts AST nodes back into formatted Loreline source code.
Handles indentation, newlines, and pretty-printing of all node types.
Respects AST style metadata (BlockStyle, ConditionStyle, Quotes, operator forms).
Instance Members
Set to false to ignore comments.
Resets the printer to its initial state.
Increases the current indentation level by one.
Decreases the current indentation level by one.
Writes a string to the output buffer, handling indentation if at start of line.
| Name |
Type |
Description |
s |
String |
String to write |
| Returns |
Description |
| Printer |
This printer instance for chaining |
Writes a line break to the output.
| Returns |
Description |
| Printer |
This printer instance for chaining |
Adds a newline to the output, limiting consecutive blank lines to 1.
| Returns |
Description |
| Printer |
This printer instance for chaining |
Adds indentation at the current level to the output.
| Returns |
Description |
| Printer |
This printer instance for chaining |
Alias for newline().
| Returns |
Description |
| Printer |
This printer instance for chaining |
Returns the current content of the output buffer.
| Returns |
Description |
| String |
Generated source code |
Renders a string literal as it would appear in source code, but without comments
and with real newlines replaced by \n escapes. Preserves original quoting style.
Used for the reference text after // in translation files.
printStringLiteralAsText(str: NStringLiteral): String
Renders a string literal as plain text (no surrounding quotes, no comments),
with real newlines replaced by \n escapes. Used for translation placeholder text.
Main entry point for printing an AST node to source code.
| Name |
Type |
Description |
node |
Node |
Root node to print |
| Returns |
Description |
| String |
Generated source code as string |
Creates a new code printer with customizable formatting options.
| Name |
Type |
Default |
Description |
indent |
String |
" " |
String used for each level of indentation (default: 4 spaces) |
newline |
String |
"\n" |
String used for line breaks (default: \n) |
Private Members
Dispatches a node to its appropriate printing function based on type.
| Name |
Type |
Default |
Description |
node |
Node |
|
Node to print |
sameLine |
Bool |
false |
|
Prints any leading comments attached to a node.
| Name |
Type |
Description |
node |
AstNode |
Node with potential comments |
Prints any trailing comments attached to a node.
| Name |
Type |
Description |
node |
AstNode |
Node with potential comments |
Prints an import statement.
Prints a complete Loreline script node.
| Name |
Type |
Description |
script |
Script |
Script node to print |
Prints a state declaration node.
| Name |
Type |
Description |
state |
NStateDecl |
State declaration to print |
Prints a character declaration node.
| Name |
Type |
Description |
char |
NCharacterDecl |
Character declaration to print |
Prints a beat declaration node.
| Name |
Type |
Description |
beat |
NBeatDecl |
Beat declaration to print |
Prints a function declaration node.
| Name |
Type |
Description |
func |
NFunctionDecl |
Function declaration to print |
printTextStatement(text: NTextStatement): Void
Prints a text statement node.
Prints a dialogue statement node.
Prints a choice statement node.
Prints an import statement node.
Prints a choice option node.
| Name |
Type |
Description |
option |
NChoiceOption |
Choice option to print |
Prints an if statement node, handling both simple if and if-else structures.
| Name |
Type |
Default |
Description |
ifStmt |
NIfStatement |
|
If statement to print |
isElseIf |
Bool |
false |
Whether this is part of an else-if chain |
Prints a transition node.
Prints an insertion node.
| Name |
Type |
Description |
insert |
NInsertion |
Insertion to print |
Prints a dynamic beat call statement node.
| Name |
Type |
Description |
call |
NBeatCall |
Beat call to print |
Prints the dynamic target expression and/or argument list of a
transition or insertion, comma separated, with comments suppressed
(they should be attached to the parent node).
Prints a string literal node, handling both plain strings and strings with quotes.
Writes a Raw text part for an unquoted string literal.
Escape sequences are preserved as-is from the AST (already in correct form for unquoted context).
Handles multiline continuation by stripping source indentation and re-indenting at current level.
Writes a Raw text part for a double-quoted string literal.
Escape sequences are preserved as-is from the AST (already in correct form for quoted context).
Content inside quotes is written verbatim, no indentation is added for continuation lines.
Writes raw text, handling embedded real newlines.
| Name |
Type |
Description |
text |
String |
|
stripIndent |
Bool |
If true, strips leading whitespace from continuation lines (for unquoted strings where source indentation is in the raw text). |
Prints a literal value node (numbers, booleans, arrays, objects).
| Name |
Type |
Description |
lit |
NLiteral |
Literal node to print |
Prints a field access expression (object.field).
| Name |
Type |
Description |
access |
NAccess |
Field access node to print |
Prints an array access expression (array[index]).
| Name |
Type |
Description |
access |
NArrayAccess |
Array access node to print |
Prints a function call expression.
| Name |
Type |
Description |
call |
NCall |
Function call node to print |
Prints a binary operation expression (a + b, a && b, etc).
Handles operator precedence with parentheses when needed.
| Name |
Type |
Default |
Description |
binary |
NBinary |
|
Binary operation node to print |
skipParen |
Bool |
false |
|
Prints a unary operation expression (!x, -x).
| Name |
Type |
Description |
unary |
NUnary |
Unary operation node to print |
Prints a ternary expression (condition ? trueExpr : falseExpr).
| Name |
Type |
Description |
ternary |
NTernary |
Ternary node to print |
Prints an assignment expression (a = b, a += b).
| Name |
Type |
Description |
assign |
NAssign |
Assignment node to print |
Prints an in-line expression optionally wrapped in parentheses.
Comments on the expression are suppressed (they should be attached to the parent node).
| Name |
Type |
Description |
expr |
NExpr |
Expression to wrap in parentheses |
parens |
Bool |
|
Checks if an expression can be printed as simple interpolation ($var, $var.field, $var[idx], $var.call()).
Simple interpolation is a chain of NAccess, NArrayAccess, and NCall rooted at a plain NAccess (no target).
Checks if a string literal contains real newlines in its raw parts.
Real newlines indicate multiline text from the source (as opposed to escape sequences).
getOperator(op: Anonymous): String
Converts a token type to its corresponding operator string representation.
| Name |
Type |
Description |
op |
Anonymous |
Token type to convert |
| Returns |
Description |
| String |
String representation of the operator |