Lexer
loreline.Lexer (Class)
The lexical analyzer for the Loreline language.
Converts source code text into a sequence of tokens.
Static Members
hasNonSpecialChar_specialChars: String
Instance Members
Resets the lexer to the beginning of the input.
Converts the entire input into an array of tokens.
| Returns |
Description |
| Tokens |
Array of tokens |
Reads and returns the next token from the input.
| Returns |
Description |
| Token |
The next token |
Gets the array of lexing errors encountered.
Creates a new lexer for the given input.
| Name |
Type |
Description |
input |
String |
The source code to lex |
Private Members
Mapping of keywords to their corresponding token types.
The input source code being lexed.
Current position in the input.
Length of the input string.
Current line number.
Current column number.
Starting line of the current token.
Starting column of the current token.
Previous token that was read.
Current token lists during tokenization
A stack to keep track of whether we are inside a beat or a state/character block.
Depending on that, the rules for reading unquoted string tokens are different.
The token type that will be added to the stack
next time we find a LBrace token
When last value is true, that means only strictly
correct expressions are accepted, thus
unquoted strings will be disabled. Mostly
used to handle interpolated values in strings
or object/array literals in strict expressions that loosen the rules
Current indentation level (number of spaces/tabs)
Stack of indentation levels
Queue of generated indentation tokens
The indentation size (e.g., 4 spaces or 1 tab)
Whether tabs are allowed for indentation
Errors collected during lexing, if any
compareAttachments(l: Anonymous, r: Anonymous): Int
Compares two string attachments by their starting position.
| Name |
Type |
Description |
l |
Anonymous |
First attachment |
r |
Anonymous |
Second attachment |
| Returns |
Description |
| Int |
Negative if l comes before r, positive if r comes before l, 0 if equal |
parentBlockType(): Anonymous
Returns the token type of the parent block.
| Returns |
Description |
| Anonymous |
The token type of the parent block or KwBeat if at top level |
Checks if currently in a beat block.
| Returns |
Description |
| Bool |
True if inside a beat block, false otherwise |
inStateOrCharacter(): Bool
Checks if currently in a state or character block.
| Returns |
Description |
| Bool |
True if inside a state or character block, false otherwise |
Reads a line break token (newline or carriage return + newline).
| Returns |
Description |
| Token |
Line break token |
Helper function to check if a character is whitespace.
| Name |
Type |
Description |
c |
Int |
The character code to check |
| Returns |
Description |
| Bool |
True if the character is whitespace, false otherwise |
Helper function to match an identifier at the given position.
| Name |
Type |
Description |
pos |
Int |
The position to start matching from |
| Returns |
Description |
| Null<String> |
The matched identifier, or null if not matching |
Helper function to skip whitespace and comments
| Name |
Type |
Default |
pos |
Int |
|
stopNextLine |
Bool |
false |
Returns whether the input at the given position is the start of an if condition.
| Name |
Type |
Description |
pos |
Int |
Position to check from |
| Returns |
Description |
| Bool |
True if an if condition starts at the position, false otherwise |
isIdentifierExpressionStart(pos: Int, lowercaseIdentOnly: Bool): Bool
Returns whether the input at the given position is the start of an identifier expression.
| Name |
Type |
Description |
pos |
Int |
Position to check from |
lowercaseIdentOnly |
Bool |
|
| Returns |
Description |
| Bool |
True if an identifier expression starts at the position, false otherwise |
Returns whether the input at the given position is a valid transition start.
A valid transition consists of "->" followed by an identifier, with optional
whitespace and comments in between. Nothing but whitespace and comments can
follow the identifier.
| Name |
Type |
Description |
pos |
Int |
Position to check from |
| Returns |
Description |
| Bool |
True if a valid transition starts at the position, false otherwise |
Returns whether the input at the given position begins with a label pattern (identifier:).
| Name |
Type |
Description |
pos |
Int |
Position to check from |
| Returns |
Description |
| Bool |
True if a label starts at the position, false otherwise |
Returns whether the input at the given position is the start of a function or method call.
| Name |
Type |
Description |
pos |
Int |
Position to check from |
| Returns |
Description |
| Bool |
True if a function call starts at the position, false otherwise |
Returns whether the input at the given position is the start of an assignment.
| Name |
Type |
Description |
pos |
Int |
Position to check from |
strict |
Bool |
|
| Returns |
Description |
| Bool |
True if an assignment starts at the position, false otherwise |
Returns whether the next token is a colon.
| Name |
Type |
Default |
Description |
pos |
Int |
|
Position to check from |
skipWhitespaces |
Bool |
true |
Whether to skip whitespace before checking |
| Returns |
Description |
| Bool |
True if next token is a colon, false otherwise |
afterLabelIdentifierToken(?inSameLine: Bool = true): Int
If we are currently right after a label, return the related identifier token index (before colon),
or -1 if not after a label.
| Name |
Type |
Default |
inSameLine |
Bool |
true |
isStartingMultilineDialogue(labelIdentifierTokenIndex: Int): Int
Checks if a dialogue statement continues over multiple lines.
| Name |
Type |
labelIdentifierTokenIndex |
Int |
| Returns |
Description |
| Int |
The indentation of the multiline dialogue or -1 |
isContinuingMultilineText(pos: Int, indent: Int): Bool
Returns whether we are currently inside brackets [ ].
| Returns |
Description |
| Bool |
True if inside brackets, false otherwise |
followsAssignStart(): Bool
Returns whether the resolved tokens in the current line are only white spaces,
a single label someLabel:, or comments.
| Returns |
Description |
| Bool |
True if line only contains label or comments, false otherwise |
Returns whether the resolved tokens in the current line are only white spaces or comments.
| Returns |
Description |
| Bool |
True if line only contains whitespace or comments, false otherwise |
Returns whether the given string has a non special character that could be
considered as actual text.
| Name |
Type |
Description |
str |
String |
String to check |
| Returns |
Description |
| Bool |
True if string contains non-special characters, false otherwise |
Returns whether the given input is a valid number.
| Name |
Type |
Description |
value |
String |
String to check |
| Returns |
Description |
| Bool |
True if string represents a valid number, false otherwise |
makeLooseOrStrictAfterBracket(): Void
makeStrictIfFollowingCallable(): Void
followsCallableOrIndexable(): Bool
Tries to read an unquoted string literal from the current position.
Returns null if the current position cannot start an unquoted string.
| Returns |
Description |
| Null<Token> |
Token if an unquoted string was read, null otherwise |
Reads a string literal, handling escape sequences, interpolation, and tags.
| Name |
Type |
Description |
stringStart |
Position |
Starting position of the string |
| Returns |
Description |
| Token |
String literal token |
Reads a complex interpolation expression inside ${...}.
| Name |
Type |
Description |
interpStart |
Position |
Starting position of the interpolation |
| Returns |
Description |
| Tokens |
Array of tokens making up the interpolation expression |
Reads a token with proper position tracking in a nested context.
| Name |
Type |
Description |
startPos |
Position |
Starting position for position calculation |
| Returns |
Description |
| Token |
Token with adjusted position |
Reads a simple field access interpolation (e.g. $foo.bar[baz].qux).
| Name |
Type |
Description |
interpStart |
Position |
Starting position of the interpolation |
| Returns |
Description |
| Tokens |
Array of tokens making up the field access |
Reads an identifier token in a string interpolation context.
| Name |
Type |
Description |
stringStart |
Position |
Starting position for position calculation |
| Returns |
Description |
| Token |
Identifier token |
Makes position relative to the given string start position.
| Name |
Type |
Description |
stringStart |
Position |
Starting position for calculation |
| Returns |
Description |
| Position |
Position object relative to string start |
Reads a single-line comment.
| Returns |
Description |
| Token |
Comment token |
Reads a multi-line comment, handling nested comments.
| Returns |
Description |
| Token |
Comment token |
Reads a numeric literal.
| Returns |
Description |
| Token |
Number token |
Reads an identifier or keyword.
| Returns |
Description |
| Token |
Identifier or keyword token |
Creates a new position at the current location.
| Returns |
Description |
| Position |
Position object |
Creates a new token with the given type and optional position.
| Name |
Type |
Default |
Description |
type |
Anonymous |
|
Token type |
position |
Position |
(optional) |
Optional position (defaults to current position) |
| Returns |
Description |
| Token |
Created token |
Advances the lexer position by the given number of characters.
| Name |
Type |
Default |
Description |
count |
Int |
1 |
Number of characters to advance (default 1) |
Looks ahead in the input without advancing the position.
| Name |
Type |
Default |
Description |
offset |
Int |
1 |
Number of characters to look ahead (default 1) |
| Returns |
Description |
| Int |
Character code at the offset position, or 0 if beyond input length |
Creates and throws a lexer error.
| Name |
Type |
Description |
message |
String |
Error message |
fatal |
Bool |
|
Skips whitespace characters (space and tab).
Checks if a character is a digit (0-9).
@*
| Name |
Type |
Description |
c |
Int |
Character code to check |
| Returns |
Description |
| Bool |
Whether the character is a digit |
Checks if a character is valid as the start of an identifier.
Valid identifier starts are letters and underscore.
| Name |
Type |
Description |
c |
Int |
Character code to check |
| Returns |
Description |
| Bool |
Whether the character can start an identifier |
Checks if a character is valid as part of an identifier.
Valid identifier parts are letters, numbers, and underscore.
| Name |
Type |
Description |
c |
Int |
Character code to check |
| Returns |
Description |
| Bool |
Whether the character can be part of an identifier |