AstUtils

loreline.AstUtils (Class)

Static utility functions for programmatic AST formatting transformations. Usable as static extensions on AstNode via using loreline.AstUtils.

Static Members

addQuotes(node: AstNode): Void

Add double quotes to all unquoted string literals in the AST.

Name Type
node AstNode

removeQuotes(node: AstNode): Void

Remove double quotes from string literals that don't strictly need them.

Name Type
node AstNode

toBraceStyle(node: AstNode): Void

Convert all blocks to brace style.

Name Type
node AstNode

toPlainStyle(node: AstNode): Void

Convert all blocks to plain/indent style.

Name Type
node AstNode

useWordOperators(node: AstNode): Void

Convert &&/|| operators to and/or word form.

Name Type
node AstNode

useSymbolOperators(node: AstNode): Void

Convert and/or operators to &&/|| symbol form.

Name Type
node AstNode

useParenConditions(node: AstNode): Void

Convert all if conditions to use parentheses.

Name Type
node AstNode

usePlainConditions(node: AstNode): Void

Convert all if conditions to plain style (no parentheses).

Name Type
node AstNode

reindentFunctions(node: AstNode, oldIndent: String, newIndent: String): Void

Re-indent function body raw code strings.

Name Type
node AstNode
oldIndent String
newIndent String

insertLocalizationKeys(content: String, node: AstNode, ?includeImports: Bool = true, ?reservedIds: Map, ?rng: Random): String

Insert localization key hash comments directly into source text. Returns the modified source content. Does NOT use the Printer, so all existing content (comments, formatting, test blocks) is preserved.

When includeImports is false and node is a Script, imported scripts are skipped. Their byte offsets refer to other files' contents and would land at wrong positions in content. Use this for per-file rewrites that walk imports externally.

When reservedIds is provided, it's used (and mutated) as the shared existing-IDs set across calls. Useful for project-wide coordination: pass the same map to every per-file call so file B's generation avoids every ID already in file A (both pre-existing AND just-generated). Existing IDs found in this file are added to the map; newly-generated IDs are added too.

Name Type Default
content String
node AstNode
includeImports Bool true
reservedIds Map (optional)
rng Random (optional)
Returns
String

removeLocalizationKeys(content: String, node: AstNode): String

Remove all #hash localization keys from source content, using AST positions.

Name Type
content String
node AstNode
Returns
String

extractTranslations(node: AstNode): Map

Extract translations from a parsed translation file. Returns a map of localization key -> NStringLiteral. Looks for hash comments (#key) on text/dialogue nodes.

Name Type
node AstNode
Returns
Map

extractTranslatableEntries(node: AstNode): Array<AnonStruct>

Collects all translatable entries with #id from a source script, in source order. Returns pairs of {id, str} for NTextStatement, NDialogueStatement, and NChoiceOption.

Name Type
node AstNode
Returns
Array

hasUntaggedTranslatableStrings(node: AstNode): Bool

Returns true iff node contains at least one translatable string (NTextStatement / NDialogueStatement / NChoiceOption text) that has no #id hash comment. Used by tooling to gate "add tags?" prompts without doing any AST or content mutation. Stops at the first untagged occurrence.

Name Type
node AstNode
Returns
Bool

collectHashIds(content: String, ?out: Map): Map

Lex-only scan of content for every hash-comment identifier (#xxxx). Cheap compared to a full parse. Runs the lexer, walks tokens, pulls out every CommentHash payload. No AST is built, no imports are resolved.

Use for project-wide reserved-IDs registries: scanning every .lor file in a project to collect every hash ID currently in use, so a subsequent insertLocalizationKeys call can be passed the union as its reservedIds and avoid generating any of them.

Fills + returns out; allocates a fresh map when out is null.

Name Type Default
content String
out Map (optional)
Returns
Map

generateTranslationFile(sourceScript: AstNode, existingTranslations: Null<Map>, printer: Printer): String

Generates a translation file from a source script. Each entry has: #id // original-text-as-in-source (with quotes preserved), followed by translated text or original as placeholder.

Name Type
sourceScript AstNode
existingTranslations Null<Map>
printer Printer
Returns
String

findHashComment(node: AstNode, ?str: NStringLiteral): Null<String>

Helper: find the first hash comment on a node and return its content (the localization key). Returns null if no hash comment is found.

Name Type Default
node AstNode
str NStringLiteral (optional)
Returns
Null<String>

Private Members

lineHasHashComment(line: String): Bool

Check if a source line contains a hash comment (# followed by word chars, not preceded by ).

Name Type
line String
Returns
Bool

unquotedRawToQuotedRaw(text: String): String

Transform a Raw text part from unquoted to quoted context.

  • Remove unquoted-only escapes: = -> =, { -> {, \X -> X (for X not in {n,t,r,,<})
  • Add quoted-only escapes: " -> "
  • Keep shared escapes: \n, \t, \r, \, <, $$
Name Type
text String
Returns
String

quotedRawToUnquotedRaw(text: String): String

Transform a Raw text part from quoted to unquoted context.

  • Remove quoted-only escapes: " -> "
  • Keep shared escapes: \n, \t, \r, \, <, $$
Name Type
text String
Returns
String

canSafelyRemoveQuotes(text: String): Bool

Checks if removing quotes from this text would be safe (re-parseable as unquoted). Conservative: rejects if any condition makes the text ambiguous as unquoted.

Name Type
text String
Returns
Bool

isNumberLiteral(text: String): Bool
Name Type
text String
Returns
Bool

reindentCode(code: String, oldIndent: String, newIndent: String): String

Re-indent code by replacing leading old-indent units with new-indent units.

Name Type
code String
oldIndent String
newIndent String
Returns
String

getStringEndLine(str: NStringLiteral): Int

Find the last line (1-based) occupied by a string literal, accounting for multiline raw parts.

Name Type
str NStringLiteral
Returns
Int

randomId(rng: Random, length: Int): String

Generate a random base36 ID of the given length, guaranteed to not be a valid hex string.

Name Type
rng Random
length Int
Returns
String

Metadata

Name Parameters
:hxGen -