Parser

loreline.Parser (Class)

Parser for the Loreline scripting language. Converts a stream of tokens into an Abstract Syntax Tree (AST).

Instance Members

parse(): Script

Main parsing entry point. Parses the entire script into an AST.

Returns Description
Script Root node of the AST

getErrors(): Array<ParseError>

Gets the array of parsing errors encountered.

Returns Description
Array<ParseError> Array of ParseError objects

new(tokens: Tokens, ?context: ParserContext): Void

Creates a new Parser instance.

Name Type Default Description
tokens Tokens Array of tokens to parse
context ParserContext (optional)

Private Members

tokens: Tokens

Array of tokens to parse


current: Int

Current position in the token stream


Collection of parsing errors encountered


pendingComments: Array<Comment>

Comments waiting to be attached to nodes


lastTokenEnd: Position

Position of the last processed token


lastLineBreak: Position

Position of the last encountered line break


lineBreakAfterToken: Bool

Flag indicating if a line break follows the current token


currentNodeId: NodeId

Node id counter, to ensure each parsed node has a unique id


rootBeat: NBeatDecl

Root beat, when adding narrative flow directly at the script root


Context of this parser that contains info like what is the path of the tokens being parsed, as well as a repository of tokens matching file paths that could be imported.


nextNodeId(step: NodeIdStep): NodeId
Name Type
step NodeIdStep
Returns
NodeId

peek(): Token

Looks ahead at the next token without consuming it.

Returns Description
Token The next token in the stream

advance(?advanceLineBreaks: Bool = true): Token

Advances to the next token, handling comments and line breaks.

Name Type Default
advanceLineBreaks Bool true
Returns Description
Token The previous token

Gets the previously consumed token.

Returns Description
Token The previous token

prevWithType(type: Anonymous): Token

Gets the previously consumed token.

Name Type
type Anonymous
Returns Description
Token The previous token

prevIdentifier(): Token

Gets the previously consumed token that was an identifier.

Returns Description
Token The previous token that was an identifier

prevNonWhitespaceOrComment(): Token

Gets the previously consumed token that was not a white space or comment.

Returns Description
Token The previous token that is not a white space or comment

nextNonWhitespaceOrComment(): Token

Gets the next token that is not a white space or comment.

Returns Description
Token The next token that is not a white space or comment

nextNonLineBreakOrComment(): Token

Gets the next token that is not a line break or comment.

Returns Description
Token The next token that is not a line break or comment

currentPos(): Position
Returns
Position

currentTokenTypeToString(): String
Returns
String

check(type: Anonymous): Bool

Checks if the current token matches the expected type.

Name Type Description
type Anonymous TokenType to check against
Returns Description
Bool True if the current token matches

checkString(): Bool

Checks if the current token is a string literal.

Returns Description
Bool True if current token is a string

isAtEnd(): Bool

Checks if we've reached the end of the token stream.

Returns Description
Bool True if at end of input

isComment(type: Anonymous): Bool

Determines if a token type represents a comment.

Name Type Description
type Anonymous TokenType to check
Returns Description
Bool True if token is a comment

parseNode(?topLevel: Bool = false): AstNode

Parses a single node based on the current token.

Name Type Default
topLevel Bool false
Returns Description
AstNode Parsed node

wrapInRootBeat(node: AstNode): Null<NBeatDecl>
Name Type
node AstNode
Returns
Null<NBeatDecl>

parseImport(): NImportStatement

Parses an import statement (import "file.lor")

Returns Description
NImportStatement Import statement node

parseDialogueStatement(): NDialogueStatement

Parses a dialogue statement (character: "text")

Returns Description
NDialogueStatement Dialogue statement node

parseStatementBlock(statements: Array<AstNode>): BlockStyle

Parses a block of statements enclosed in braces.

Name Type
statements Array<AstNode>
Returns Description
BlockStyle Array of parsed statement nodes

parseStateDecl(temporary: Bool): NStateDecl

Parses a state declaration, which can be temporary or permanent.

Name Type Description
temporary Bool Whether this is a temporary state (new state)
Returns Description
NStateDecl State declaration node

parseObjectField(): NObjectField

Parses a single field in an object literal.

Returns Description
NObjectField Object field node

parseBeatDecl(): NBeatDecl

Parses a beat declaration, which represents a story segment.

Returns Description
NBeatDecl Beat declaration node

checkBlockStart(): Bool
Returns
Bool

parseBlockStart(): Token
Returns
Token

parseCharacterDecl(): NCharacterDecl

Parses a character declaration with its fields.

Returns Description
NCharacterDecl Character declaration node

parseTextStatement(): NTextStatement

Parses a text statement (direct string literal).

Returns Description
NTextStatement Text statement node

parseChoiceStatement(): NChoiceStatement

Parses a choice statement with its options.

Returns Description
NChoiceStatement Choice statement node

parseChoiceOption(blockEnd: Anonymous): NChoiceOption

Parses a single choice option with its condition and consequences.

Name Type
blockEnd Anonymous
Returns Description
NChoiceOption Choice option node

isExpressionStart(): Bool

Checks if the current token can start an expression.

Returns Description
Bool True if current token can begin an expression

parseExpressionStatement(): AstNode

Parses an expression statement (expression followed by optional assignment).

Returns Description
AstNode Node representing the expression statement

isKnownNodeStart(): Bool

Checks if the current token starts a known node type.

Returns Description
Bool True if current token begins a known node construct

parseIfStatement(): NIfStatement

Parses an if statement with optional else branch.

Returns Description
NIfStatement If statement node

parseTransition(): NTransition

Parses a transition statement (-> target).

Returns Description
NTransition Transition node

parseInsertion(): NInsertion

Parses an insertion statement (+ target).

Returns Description
NInsertion Insertion node

parseFunction(): NFunctionDecl

Parses a function definition.

Returns Description
NFunctionDecl Function node

parseExpression(): NExpr

Parses an expression, including assignments.

Returns Description
NExpr Expression node

parseLogicalOr(): NExpr

Parses logical OR expressions (expr || expr).

Returns Description
NExpr Expression node

parseLogicalAnd(): NExpr

Parses logical AND expressions (expr && expr).

Returns Description
NExpr Expression node

parseEquality(): NExpr

Parses equality expressions (expr == expr, expr != expr).

Returns Description
NExpr Expression node

parseComparison(): NExpr

Parses comparison expressions (>, >=, <, <=).

Returns Description
NExpr Expression node

parseAdditive(): NExpr

Parses additive expressions (+ and -).

Returns Description
NExpr Expression node

parseMultiplicative(): NExpr

Parses multiplicative expressions (* and /).

Returns Description
NExpr Expression node

parseUnary(): NExpr

Parses unary expressions (!expr, -expr).

Returns Description
NExpr Expression node

parsePrimary(): NExpr

Parses primary expressions (literals, identifiers, parenthesized expressions).

Returns Description
NExpr Expression node

parseStringLiteral(): NStringLiteral

Parses string literals, handling interpolation and tags.

Returns Description
NStringLiteral String literal node

makeStringPartPosition(stringStart: Position, content: String, offset: Int): Position

Creates a Position object for a part of a string literal.

Name Type Description
stringStart Position Starting position of the entire string content
content String String content
offset Int Offset within the string
Returns Description
Position Position object for the string part

makeAccess(pos: Position, target: Null<NExpr>, name: String, namePos: Position): NAccess

Creates an access expression node.

Name Type Description
pos Position Position in source
target Null<NExpr> Target expression (null for simple identifiers)
name String Identifier name
namePos Position
Returns Description
NAccess Access expression node

parseStringInterpolation(braces: Bool, inTag: Bool, tokens: Tokens, start: Int, length: Int, content: String): NStringPart

Parses string interpolation expressions (both simple and complex).

Name Type Description
braces Bool Whether the interpolation uses braces
inTag Bool Whether the interpolation is inside a tag
tokens Tokens Tokens within the interpolation
start Int Start position in source
length Int Length of interpolation
content String Original string content
Returns Description
NStringPart string part representing the interpolation

parseStringTag(closing: Bool, start: Int, length: Int, content: String, quotes: Quotes, attachments: Array<Anonymous>): NStringPart

Parses string tags ( or ).

Name Type Description
closing Bool Whether this is a closing tag
start Int Start position in source
length Int Tag length
content String Original string content
quotes Quotes
attachments Array Array of string attachments
Returns Description
NStringPart StringPart representing the tag

parseIdentifierExpression(startPos: Position, name: String): NExpr

Parses identifier-based expressions (field access, array access, function calls).

Name Type Description
startPos Position Starting position
name String Initial identifier name
Returns Description
NExpr Expression node

parseArrayLiteral(): NExpr

Parses array literals ([expr, expr, ...]).

Returns Description
NExpr Expression node for array literal

parseObjectLiteral(): NExpr

Parses object literals ({key: value, ...}).

Returns Description
NExpr Expression node for object literal

parseCallArguments(): Array<NExpr>

Parses function call arguments.

Returns Description
Array<NExpr> Array of argument expression nodes

parseConditionExpression(): NExpr

Parses a condition expression.

Returns Description
NExpr Expression node

match(type: Anonymous, ?advanceLineBreaks: Bool = true): Bool

Attempts to match and consume a token of the given type.

Name Type Default Description
type Anonymous TokenType to match
advanceLineBreaks Bool true
Returns Description
Bool True if token was matched and consumed

isAtLineBreak(): Bool

Checks if we're at a line break in the source.

Returns Description
Bool True if at a line break

expect(type: Anonymous): Token

Expects a token of the given type, throws error if not found.

Name Type Description
type Anonymous Expected TokenType
Returns Description
Token The consumed token

checkIdentifier(): Bool

Check if next token is an identifier token.

Returns Description
Bool true if the next token is an identifier

expectIdentifier(): String

Expects and consumes an identifier token.

Returns Description
String The identifier name

attachComments(node: attachComments.T): attachComments.T

Attaches pending comments to a node.

Name Type Description
node attachComments.T Node to attach comments to
Returns Description
attachComments.T The node with attached comments

attachElseComments(node: attachElseComments.T, elseToken: Token): attachElseComments.T

Attaches pending comments to an else clause of an if statement.

Name Type Description
node attachElseComments.T If statement node
elseToken Token The else token
Returns Description
attachElseComments.T The node with attached else comments

synchronize(): Void

Synchronizes the parser state after an error. Advances until a safe point is found to continue parsing.


requiresNewLine(): Bool

Checks if the current construct requires a new line.

Returns Description
Bool True if new line is required

addError(error: ParseError): ParseError
Name Type
error ParseError
Returns
ParseError

Metadata

Name Parameters
:hxGen -