Parser
loreline.Parser (Class)
Parser for the Loreline scripting language.
Converts a stream of tokens into an Abstract Syntax Tree (AST).
Instance Members
Main parsing entry point. Parses the entire script into an AST.
| Returns |
Description |
| Script |
Root node of the AST |
Gets the array of parsing errors encountered.
Creates a new Parser instance.
| Name |
Type |
Default |
Description |
tokens |
Tokens |
|
Array of tokens to parse |
context |
ParserContext |
(optional) |
|
Private Members
Array of tokens to parse
Current position in the token stream
Collection of parsing errors encountered
Comments waiting to be attached to nodes
Position of the last processed token
Position of the last encountered line break
lineBreakAfterToken: Bool
Flag indicating if a line break follows the current token
Node id counter, to ensure each parsed node has a unique id
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.
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.
| Returns |
Description |
| Token |
The previous token |
Gets the previously consumed token that was an identifier.
| Returns |
Description |
| Token |
The previous token that was an identifier |
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 |
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 |
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 |
currentTokenTypeToString(): 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 |
Checks if the current token is a string literal.
| Returns |
Description |
| Bool |
True if current token is a string |
Checks if we've reached the end of the token stream.
| Returns |
Description |
| Bool |
True if at end of input |
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 |
Parses a single node based on the current token.
| Name |
Type |
Default |
topLevel |
Bool |
false |
| Returns |
Description |
| AstNode |
Parsed node |
Parses an import statement (import "file.lor")
Parses a dialogue statement (character: "text")
Parses a block of statements enclosed in braces.
| Returns |
Description |
| BlockStyle |
Array of parsed statement nodes |
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 |
Parses a single field in an object literal.
Parses a beat declaration, which represents a story segment.
| Returns |
Description |
| NBeatDecl |
Beat declaration node |
Parses a character declaration with its fields.
parseTextStatement(): NTextStatement
Parses a text statement (direct string literal).
Parses a choice statement with its options.
Parses a single choice option with its condition and consequences.
| Name |
Type |
blockEnd |
Anonymous |
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 |
Checks if the current token starts a known node type.
| Returns |
Description |
| Bool |
True if current token begins a known node construct |
Parses an if statement with optional else branch.
Parses a transition statement (-> target).
Parses an insertion statement (+ target).
Parses a function definition.
Parses an expression, including assignments.
| Returns |
Description |
| NExpr |
Expression node |
Parses logical OR expressions (expr || expr).
| Returns |
Description |
| NExpr |
Expression node |
Parses logical AND expressions (expr && expr).
| Returns |
Description |
| NExpr |
Expression node |
Parses equality expressions (expr == expr, expr != expr).
| Returns |
Description |
| NExpr |
Expression node |
Parses comparison expressions (>, >=, <, <=).
| Returns |
Description |
| NExpr |
Expression node |
Parses additive expressions (+ and -).
| Returns |
Description |
| NExpr |
Expression node |
parseMultiplicative(): NExpr
Parses multiplicative expressions (* and /).
| Returns |
Description |
| NExpr |
Expression node |
Parses unary expressions (!expr, -expr).
| Returns |
Description |
| NExpr |
Expression node |
Parses primary expressions (literals, identifiers, parenthesized expressions).
| Returns |
Description |
| NExpr |
Expression node |
Parses string literals, handling interpolation and tags.
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 |
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 |
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 |
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 |
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 |
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 |
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 |
Check if next token is an identifier token.
| Returns |
Description |
| Bool |
true if the next token is an identifier |
Expects and consumes an identifier token.
| Returns |
Description |
| String |
The identifier name |
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 |
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 |
Synchronizes the parser state after an error.
Advances until a safe point is found to continue parsing.
Checks if the current construct requires a new line.
| Returns |
Description |
| Bool |
True if new line is required |