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

enableComments: Bool

Set to false to ignore comments.


clear(): Void

Resets the printer to its initial state.


indent(): Void

Increases the current indentation level by one.


unindent(): Void

Decreases the current indentation level by one.


write(s: String): Printer

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

writeln(): Printer

Writes a line break to the output.

Returns Description
Printer This printer instance for chaining

newline(): Printer

Adds a newline to the output, limiting consecutive blank lines to 1.

Returns Description
Printer This printer instance for chaining

tab(): Printer

Adds indentation at the current level to the output.

Returns Description
Printer This printer instance for chaining

line(): Printer

Alias for newline().

Returns Description
Printer This printer instance for chaining

toString(): String

Returns the current content of the output buffer.

Returns Description
String Generated source code

printStringLiteralAsReference(str: NStringLiteral): String

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.

Name Type
str NStringLiteral
Returns
String

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.

Name Type
str NStringLiteral
Returns
String

print(node: Node): String

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

new(?indent: String = " ", ?newline: String = "\n"): Void

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

printNode(node: Node, ?sameLine: Bool = false): Void

Dispatches a node to its appropriate printing function based on type.

Name Type Default Description
node Node Node to print
sameLine Bool false

printLeadingComments(node: AstNode): Void

Prints any leading comments attached to a node.

Name Type Description
node AstNode Node with potential comments

printHashTrailingComments(node: AstNode): Void

Prints any trailing comments attached to a node.

Name Type Description
node AstNode Node with potential comments

printTrailingComments(node: AstNode): Void
Name Type
node AstNode

printImport(imp: NImportStatement): Void

Prints an import statement.

Name Type Description
imp NImportStatement Import statement node

printScript(script: Script): Void

Prints a complete Loreline script node.

Name Type Description
script Script Script node to print

printStateDecl(state: NStateDecl): Void

Prints a state declaration node.

Name Type Description
state NStateDecl State declaration to print

printCharacterDecl(char: NCharacterDecl): Void

Prints a character declaration node.

Name Type Description
char NCharacterDecl Character declaration to print

printBeatDecl(beat: NBeatDecl): Void

Prints a beat declaration node.

Name Type Description
beat NBeatDecl Beat declaration to print

printFunctionDecl(func: NFunctionDecl): Void

Prints a function declaration node.

Name Type Description
func NFunctionDecl Function declaration to print

printTextStatement(text: NTextStatement): Void

Prints a text statement node.

Name Type Description
text NTextStatement Text statement to print

printDialogueStatement(dialogue: NDialogueStatement): Void

Prints a dialogue statement node.

Name Type Description
dialogue NDialogueStatement Dialogue statement to print

printChoiceStatement(choice: NChoiceStatement): Void

Prints a choice statement node.

Name Type Description
choice NChoiceStatement Choice statement to print

printImportStatement(imp: NImportStatement): Void

Prints an import statement node.

Name Type Description
imp NImportStatement Import statement to print

printChoiceOption(option: NChoiceOption): Void

Prints a choice option node.

Name Type Description
option NChoiceOption Choice option to print

printIfStatement(ifStmt: NIfStatement, ?isElseIf: Bool = false): Void

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

printAlternative(alt: NAlternative): Void
Name Type
alt NAlternative

needsEmptyLines(node: AstNode): Bool
Name Type
node AstNode
Returns
Bool

printTransition(trans: NTransition, sameLine: Bool): Void

Prints a transition node.

Name Type Description
trans NTransition Transition to print
sameLine Bool

printInsertion(insert: NInsertion): Void

Prints an insertion node.

Name Type Description
insert NInsertion Insertion to print

printBeatCall(call: NBeatCall): Void

Prints a dynamic beat call statement node.

Name Type Description
call NBeatCall Beat call to print

printTargetExprAndArgs(targetExpr: Null<NExpr>, args: Null<Array<NExpr>>): Void

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).

Name Type
targetExpr Null<NExpr>
args Null<Array<NExpr>>

printStringLiteral(str: NStringLiteral): Void

Prints a string literal node, handling both plain strings and strings with quotes.

Name Type Description
str NStringLiteral String literal to print

writeUnquotedRaw(text: String): Void

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.

Name Type
text String

writeQuotedRaw(text: String): Void

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.

Name Type
text String

writeRawWithContinuation(text: String, stripIndent: Bool): Void

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).

printLiteral(lit: NLiteral): Void

Prints a literal value node (numbers, booleans, arrays, objects).

Name Type Description
lit NLiteral Literal node to print

printAccess(access: NAccess): Void

Prints a field access expression (object.field).

Name Type Description
access NAccess Field access node to print

printArrayAccess(access: NArrayAccess): Void

Prints an array access expression (array[index]).

Name Type Description
access NArrayAccess Array access node to print

printCall(call: NCall): Void

Prints a function call expression.

Name Type Description
call NCall Function call node to print

printBinary(binary: NBinary, ?skipParen: Bool = false): Void

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

printUnary(unary: NUnary): Void

Prints a unary operation expression (!x, -x).

Name Type Description
unary NUnary Unary operation node to print

printTernary(ternary: NTernary): Void

Prints a ternary expression (condition ? trueExpr : falseExpr).

Name Type Description
ternary NTernary Ternary node to print

printAssignment(assign: NAssign): Void

Prints an assignment expression (a = b, a += b).

Name Type Description
assign NAssign Assignment node to print

printInLineExpression(expr: NExpr, parens: Bool): Void

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

isSimpleInterpolationExpr(expr: NExpr): 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).

Name Type
expr NExpr
Returns
Bool

hasRealNewlines(node: Node): Bool

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).

Name Type
node Node
Returns
Bool

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

Metadata

Name Parameters
:hxGen -