Luma 0.1.0
A low-level compiled alternative to C, C++, and more!
Loading...
Searching...
No Matches
Classes | Macros | Enumerations | Functions
parser.h File Reference

Recursive descent and Pratt parser for the Zura language. More...

#include <stddef.h>
#include "../ast/ast.h"
#include "../c_libs/memory/memory.h"
#include "../helper/help.h"
#include "../lexer/lexer.h"
Include dependency graph for parser.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

struct  Parser
 Parser state holding token stream and current position. More...
 

Macros

#define CURRENT_TOKEN_LENGTH(parser)   ((int)p_current(parser).length)
 Get the length of the current token's lexeme.
 
#define CURRENT_TOKEN_VALUE(parser)   (p_current(parser).value)
 Get the value string of the current token.
 
#define MAX_STMT   1024
 Maximum allowed size for statements, expressions, and types.
 
#define MAX_EXPR   1024
 
#define MAX_TYPE   1024
 

Enumerations

enum  BindingPower {
  BP_NONE = 0 , BP_LOWEST , BP_ASSIGN , BP_TERNARY ,
  BP_LOGICAL_OR , BP_LOGICAL_AND , BP_BITWISE_OR , BP_BITWISE_XOR ,
  BP_BITWISE_AND , BP_EQUALITY , BP_RELATIONAL , BP_RANGE ,
  BP_SHIFT , BP_SUM , BP_PRODUCT , BP_EXPONENT ,
  BP_UNARY , BP_POSTFIX , BP_CALL , BP_PRIMARY
}
 Binding power (precedence) levels for expression parsing. More...
 

Functions

void parser_error (Parser *psr, const char *error_type, const char *file, const char *msg, int line, int col, int tk_length)
 Report a parser error with detailed location info.
 
char * collect_doc_comments (Parser *parser)
 Collects consecutive documentation comments before a declaration.
 
void consume_doc_comments (Parser *parser)
 
bool p_has_tokens (Parser *psr)
 Checks if there are more tokens available for parsing.
 
Token p_peek (Parser *psr, size_t offset)
 Peeks at a token at the specified offset from current position.
 
Token p_current (Parser *psr)
 Gets the current token without advancing the parser position.
 
Token p_advance (Parser *psr)
 Advances to the next token and returns the current token.
 
Token p_consume (Parser *psr, LumaTokenType type, const char *error_msg)
 Consumes a token of the expected type or reports an error.
 
char * get_name (Parser *psr)
 Extracts and duplicates the current token's string value.
 
Stmtparse (GrowableArray *tks, ArenaAllocator *arena, BuildConfig *config)
 Parses a full program from tokens into an AST of statements.
 
Exprparse_expr (Parser *parser, BindingPower bp)
 Parses an expression using the Pratt parsing algorithm.
 
Stmtparse_stmt (Parser *parser)
 Parses a single statement.
 
Typeparse_type (Parser *parser)
 Parses a type annotation.
 
bool init_parser_arrays (Parser *parser, GrowableArray *stmts, GrowableArray *modules)
 
const char * parse_module_declaration (Parser *parser, char **out_module_doc)
 
Exprnud (Parser *parser)
 Null Denotation - handles prefix expressions and primary expressions.
 
Exprled (Parser *parser, Expr *left, BindingPower bp)
 Pratt parser function for left denotation (infix/postfix parsing).
 
BindingPower get_bp (LumaTokenType kind)
 Gets the binding power (precedence) for a given token type.
 
Exprprimary (Parser *parser)
 
Exprunary (Parser *parser)
 
Exprgrouping (Parser *parser)
 
Exprbinary (Parser *parser, Expr *left, BindingPower bp)
 
Exprcall_expr (Parser *parser, Expr *left, BindingPower bp)
 
Exprassign_expr (Parser *parser, Expr *left, BindingPower bp)
 
Exprprefix_expr (Parser *parser, Expr *left, BindingPower bp)
 
Exprarray_expr (Parser *parser)
 
Exprindex_expr (Parser *parser)
 
Exprderef_expr (Parser *parser)
 
Expraddr_expr (Parser *parser)
 
Expralloc_expr (Parser *parser)
 
Exprfree_expr (Parser *parser)
 
Exprcast_expr (Parser *parser)
 
Exprinput_expr (Parser *parser)
 
Exprsystem_expr (Parser *parser)
 
Exprsyscall_expr (Parser *parser)
 
Exprsizeof_expr (Parser *parser)
 
Exprstruct_expr (Parser *parser)
 
Exprnamed_struct_expr (Parser *parser, Expr *left, BindingPower bp)
 
Typetnud (Parser *parser)
 
Typetled (Parser *parser, Type *left, BindingPower bp)
 
BindingPower tget_bp (Parser *parser, LumaTokenType kind)
 
Typepointer (Parser *parser)
 
Typearray_type (Parser *parser)
 
Typefunction_type (Parser *parser, Type *return_type)
 
Stmtuse_stmt (Parser *parser)
 
Stmtos_stmt (Parser *parser)
 
Stmtlink_stmt (Parser *parser)
 
Stmtexpr_stmt (Parser *parser)
 Parses an expression statement.
 
Stmtvar_stmt (Parser *parser, bool is_public)
 Parses a variable declaration statement.
 
Stmtconst_stmt (Parser *parser, bool is_public, bool returns_ownership, bool takes_ownership)
 Parses a constant declaration statement.
 
Stmtfn_stmt (Parser *parser, const char *name, bool is_public, bool is_static, bool returns_ownership, bool takes_ownership)
 Parses a function declaration statement.
 
Stmtenum_stmt (Parser *parser, const char *name, bool is_public)
 Parses an enumeration declaration statement.
 
Stmtstruct_stmt (Parser *parser, const char *name, bool is_public)
 Parses a structure declaration statement.
 
Stmtprint_stmt (Parser *parser, bool ln)
 Parses print/println statements.
 
Stmtreturn_stmt (Parser *parser)
 Parses a return statement.
 
Stmtblock_stmt (Parser *parser)
 Parses a block statement.
 
Stmtinfinite_loop_stmt (Parser *parser, int line, int col)
 Parses an infinite loop statement.
 
Stmtfor_loop_stmt (Parser *parser, int line, int col)
 Parses a for loop statement.
 
Stmtloop_stmt (Parser *parser)
 Parses loop statements (infinite, while, or for loops)
 
Stmtif_stmt (Parser *parser)
 Parses if/elif/else conditional statements.
 
Stmtbreak_continue_stmt (Parser *parser, bool is_continue)
 Parses break and continue statements.
 
Stmtdefer_stmt (Parser *parser)
 
Stmtswitch_stmt (Parser *parser)
 
Stmtimpl_stmt (Parser *parser)
 

Detailed Description

Recursive descent and Pratt parser for the Zura language.

This module implements parsing of tokens into an Abstract Syntax Tree (AST), handling expressions, statements, types, and error reporting. The parser uses binding power (precedence) to correctly parse expressions.

The parser supports:

Author
Connor Harris
Date
2025

Macro Definition Documentation

◆ CURRENT_TOKEN_LENGTH

#define CURRENT_TOKEN_LENGTH (   parser)    ((int)p_current(parser).length)

Get the length of the current token's lexeme.

◆ CURRENT_TOKEN_VALUE

#define CURRENT_TOKEN_VALUE (   parser)    (p_current(parser).value)

Get the value string of the current token.

◆ MAX_EXPR

#define MAX_EXPR   1024

◆ MAX_STMT

#define MAX_STMT   1024

Maximum allowed size for statements, expressions, and types.

◆ MAX_TYPE

#define MAX_TYPE   1024

Enumeration Type Documentation

◆ BindingPower

Binding power (precedence) levels for expression parsing.

Used to control operator precedence and associativity in Pratt parsing.

Enumerator
BP_NONE 

No binding power

BP_LOWEST 

Lowest binding power

BP_ASSIGN 

Assignment operators (=, +=, etc.)

BP_TERNARY 

Ternary conditional operator (? :)

BP_LOGICAL_OR 

Logical OR operator (||)

BP_LOGICAL_AND 

Logical AND operator (&&)

BP_BITWISE_OR 

Bitwise OR operator (|)

BP_BITWISE_XOR 

Bitwise XOR operator (^)

BP_BITWISE_AND 

Bitwise AND operator (&)

BP_EQUALITY 

Equality operators (==, !=)

BP_RELATIONAL 

Relational operators (<, >, <=, >=)

BP_RANGE 

Range operations (..)

BP_SHIFT 

Shift operators (<<, >>)

BP_SUM 

Addition and subtraction (+, -)

BP_PRODUCT 

Multiplication, division, modulo (*, /, %)

BP_EXPONENT 

Exponentiation operator (**)

BP_UNARY 

Unary operators (!, ~, +, -, prefix ++/–)

BP_POSTFIX 

Postfix operators (++/– postfix)

BP_CALL 

Function call or indexing

BP_PRIMARY 

Primary expressions (literals, variables)

Function Documentation

◆ addr_expr()

Expr * addr_expr ( Parser parser)

◆ alloc_expr()

Expr * alloc_expr ( Parser parser)

◆ array_expr()

Expr * array_expr ( Parser parser)

◆ array_type()

Type * array_type ( Parser parser)

◆ assign_expr()

Expr * assign_expr ( Parser parser,
Expr left,
BindingPower  bp 
)

◆ binary()

Expr * binary ( Parser parser,
Expr left,
BindingPower  bp 
)

◆ block_stmt()

Stmt * block_stmt ( Parser parser)

Parses a block statement.

Handles block statements with the syntax: { statement1; statement2; ... }

Parameters
parserPointer to the parser instance
Returns
Pointer to the block statement AST node, or NULL on failure
Note
Empty blocks are allowed and create a valid block statement with 0 statements
Each statement in the block is parsed recursively using parse_stmt()
Handles memory allocation for the statement array using growable arrays
Continues parsing on individual statement failures (for error recovery)
See also
parse_stmt(), create_block_stmt()

◆ break_continue_stmt()

Stmt * break_continue_stmt ( Parser parser,
bool  is_continue 
)

Parses break and continue statements.

Handles loop control statements with the syntax:

  • break; - Exit the current loop
  • continue; - Skip to the next iteration of the current loop
Parameters
parserPointer to the parser instance
is_continueWhether this is a continue (true) or break (false) statement
Returns
Pointer to the break/continue statement AST node, or NULL on failure
Note
Both statements require semicolon terminators
These statements are only valid within loop contexts (enforced at semantic analysis)
Break exits the innermost enclosing loop
Continue skips to the next iteration of the innermost enclosing loop
See also
create_break_continue_stmt()

◆ call_expr()

Expr * call_expr ( Parser parser,
Expr left,
BindingPower  bp 
)

◆ cast_expr()

Expr * cast_expr ( Parser parser)

◆ collect_doc_comments()

char * collect_doc_comments ( Parser parser)

Collects consecutive documentation comments before a declaration.

Accumulates all doc comments (/// or //!) that appear immediately before the current token. Returns them as a single string with newlines preserved.

Parameters
parserPointer to the parser instance
Returns
Concatenated doc comment string, or NULL if no doc comments found

◆ const_stmt()

Stmt * const_stmt ( Parser parser,
bool  is_public,
bool  returns_ownership,
bool  takes_ownership 
)

Parses a constant declaration statement.

Handles multiple forms of constant declarations:

  • const name: Type = value; - Explicit type annotation
  • const name = fn ... - Function declaration
  • const name = struct ... - Struct declaration
  • const name = enum ... - Enum declaration
Parameters
parserPointer to the parser instance
is_publicWhether this declaration has public visibility
Returns
Pointer to the appropriate declaration statement AST node, or NULL on failure
Note
For simple constants, creates a variable declaration with immutable flag
For complex types (functions, structs, enums), delegates to specialized parsers
If no type is specified, it defaults to the type of the value
See also
fn_stmt(), struct_stmt(), enum_stmt(), create_var_decl_stmt()

◆ consume_doc_comments()

void consume_doc_comments ( Parser parser)

◆ defer_stmt()

Stmt * defer_stmt ( Parser parser)

◆ deref_expr()

Expr * deref_expr ( Parser parser)

◆ enum_stmt()

Stmt * enum_stmt ( Parser parser,
const char *  name,
bool  is_public 
)

Parses an enumeration declaration statement.

Handles enum declarations with the syntax: enum { member1, member2, member3, ... };

Parameters
parserPointer to the parser instance
nameEnum name (already parsed by caller)
is_publicWhether this enum has public visibility
Returns
Pointer to the enum declaration AST node, or NULL on failure
Note
Enum members are identifiers separated by commas
Trailing commas are allowed but not required
Requires a semicolon terminator after the closing brace
See also
create_enum_decl_stmt()

◆ expr_stmt()

Stmt * expr_stmt ( Parser parser)

Parses an expression statement.

An expression statement consists of any expression followed by a semicolon. This is used for statements that evaluate an expression for its side effects, such as function calls or assignment expressions.

Parameters
parserPointer to the parser instance
Returns
Pointer to the created expression statement AST node, or NULL on failure
Note
Captures line and column information at the start of parsing
Requires a semicolon terminator
See also
parse_expr(), create_expr_stmt()

◆ fn_stmt()

Stmt * fn_stmt ( Parser parser,
const char *  name,
bool  is_public,
bool  is_static,
bool  returns_ownership,
bool  takes_ownership 
)

Parses a function declaration statement.

Handles function declarations with the syntax: fn(param1: Type1, param2: Type2, ...) ReturnType { body }

Parameters
parserPointer to the parser instance
nameFunction name (already parsed by caller)
is_publicWhether this function has public visibility
Returns
Pointer to the function declaration AST node, or NULL on failure
Note
Function parameters are stored as parallel arrays of names and types
Return type is required and parsed after the parameter list
Function body must be a block statement
Memory for parameter arrays is allocated using the arena allocator
See also
parse_type(), block_stmt(), create_func_decl_stmt()

◆ for_loop_stmt()

Stmt * for_loop_stmt ( Parser parser,
int  line,
int  col 
)

Parses a for loop statement.

Handles for loops with the syntax:

loop [i: int = 0, j: int = 1](condition) { ... }
loop [i: int = 0, j: int = 1](condition) : (optional_condition) { ... }
Parameters
parserPointer to the parser instance
lineLine number where the loop statement starts
colColumn number where the loop statement starts
Returns
Pointer to the for loop statement AST node, or NULL on failure
Note
Supports multiple initializer variables separated by commas
Main condition is required and parenthesized
Optional secondary condition can be provided after a colon
Loop body must be a block statement
See also
loop_init(), parse_expr(), block_stmt(), create_for_loop_stmt()

◆ free_expr()

Expr * free_expr ( Parser parser)

◆ function_type()

Type * function_type ( Parser parser,
Type return_type 
)

◆ get_bp()

BindingPower get_bp ( LumaTokenType  kind)

Gets the binding power (precedence) for a given token type.

This function is crucial for the Pratt parser implementation. It returns the binding power (precedence level) for different operators, which determines the order of operations during expression parsing.

Higher binding power values indicate higher precedence operators.

Parameters
kindThe token type to get binding power for
Returns
BindingPower enumeration value representing the precedence level Returns BP_NONE for tokens that don't have binding power
Note
Precedence levels (highest to lowest):
  • BP_CALL: Function calls, member access, indexing
  • BP_POSTFIX: Postfix increment/decrement
  • BP_PRODUCT: Multiplication, division
  • BP_SUM: Addition, subtraction
  • BP_RELATIONAL: Comparison operators
  • BP_EQUALITY: Equality and inequality
  • BP_BITWISE_AND, BP_BITWISE_XOR, BP_BITWISE_OR: Bitwise operations
  • BP_LOGICAL_AND, BP_LOGICAL_OR: Logical operations
  • BP_TERNARY: Ternary conditional operator
  • BP_ASSIGN: Assignment operators
See also
BindingPower, LumaTokenType

◆ get_name()

char * get_name ( Parser psr)

Extracts and duplicates the current token's string value.

This function creates a null-terminated string copy of the current token's value using the arena allocator. It's primarily used for extracting identifier names, string literals, and other textual token content that needs to be preserved in the AST.

Parameters
psrPointer to the parser instance
Returns
Pointer to a null-terminated string containing the current token's value, allocated using the parser's arena allocator
Note
The returned string is allocated using arena_alloc() for automatic memory management
The string is properly null-terminated for safe use with standard string functions
The original token value is copied, so the returned string is independent
Memory is automatically managed by the arena - no manual deallocation needed
Warning
This function assumes the current token has a valid value field
Should only be called when you know the current token contains string data
See also
CURRENT_TOKEN_VALUE(), CURRENT_TOKEN_LENGTH(), arena_alloc()

◆ grouping()

Expr * grouping ( Parser parser)

◆ if_stmt()

Stmt * if_stmt ( Parser parser)

Parses if/elif/else conditional statements.

Handles complex conditional statements with multiple branches:

if (condition1) { ... }
elif (condition2) { ... }
elif (condition3) { ... }
else { ... }
Parameters
parserPointer to the parser instance
Returns
Pointer to the if statement AST node, or NULL on failure
Note
Supports multiple elif clauses
Else clause is optional
Each condition must be parenthesized
Each branch must be a block statement
Elif statements are collected in an array rather than nested recursively
See also
parse_expr(), block_stmt(), create_if_stmt()

◆ impl_stmt()

Stmt * impl_stmt ( Parser parser)

◆ index_expr()

Expr * index_expr ( Parser parser)

◆ infinite_loop_stmt()

Stmt * infinite_loop_stmt ( Parser parser,
int  line,
int  col 
)

Parses an infinite loop statement.

Handles infinite loops with the syntax: loop { ... }

Parameters
parserPointer to the parser instance
lineLine number where the loop statement starts
colColumn number where the loop statement starts
Returns
Pointer to the infinite loop statement AST node, or NULL on failure
Note
The loop body must be a block statement
This creates a loop that runs indefinitely unless broken by break/return
See also
block_stmt(), create_infinite_loop_stmt()

◆ init_parser_arrays()

bool init_parser_arrays ( Parser parser,
GrowableArray stmts,
GrowableArray modules 
)

◆ input_expr()

Expr * input_expr ( Parser parser)

◆ led()

Expr * led ( Parser parser,
Expr left,
BindingPower  bp 
)

Pratt parser function for left denotation (infix/postfix parsing).

Parameters
parserParser pointer.
leftLeft expression.
bpBinding power.
Returns
Parsed expression node.

Pratt parser function for left denotation (infix/postfix parsing).

This is part of the Pratt parser implementation. The "led" function handles tokens that can appear after an expression has been parsed (binary operators and postfix operators).

Parameters
parserPointer to the parser instance
leftThe left operand expression (already parsed)
bpThe current binding power context
Returns
Pointer to the parsed expression AST node incorporating the left operand, or the original left expression if no valid LED is found
Note
Handles:
  • Binary arithmetic and logical operators: +, -, *, /, ==, !=, etc.
  • Function calls: function(args)
  • Assignment: variable = value
  • Member access: object.member
  • Postfix operators: variable++, variable–
  • Array indexing: array[index]
See also
nud(), parse_expr(), binary(), call_expr(), assign_expr(), prefix_expr()

◆ link_stmt()

Stmt * link_stmt ( Parser parser)

◆ loop_stmt()

Stmt * loop_stmt ( Parser parser)

Parses loop statements (infinite, while, or for loops)

Dispatcher function that determines the type of loop based on the following tokens and delegates to the appropriate specialized parser:

  • loop { ... } → infinite loop
  • loop [initializers](...) { ... } → for loop
  • loop (condition) { ... } → while loop
  • loop (condition) : (optional_condition) { ... } → while loop with secondary condition
Parameters
parserPointer to the parser instance
Returns
Pointer to the appropriate loop statement AST node, or NULL on failure
Note
The specific loop type is determined by the token following 'loop'
All loop bodies must be block statements
See also
infinite_loop_stmt(), for_loop_stmt(), parse_expr(), block_stmt(), create_loop_stmt()

◆ named_struct_expr()

Expr * named_struct_expr ( Parser parser,
Expr left,
BindingPower  bp 
)

◆ nud()

Expr * nud ( Parser parser)

Null Denotation - handles prefix expressions and primary expressions.

This is part of the Pratt parser implementation. The "nud" function handles tokens that can appear at the beginning of an expression (prefix operators and primary expressions like literals and identifiers).

Parameters
parserPointer to the parser instance
Returns
Pointer to the parsed expression AST node, or NULL if parsing fails
Note
Handles:
  • Primary expressions: numbers, strings, identifiers
  • Prefix unary operators: -, +, !, ++, –
  • Grouped expressions: (expression)
  • Array literals: [expression, ...]
See also
led(), parse_expr(), primary(), unary(), grouping(), array_expr()

◆ os_stmt()

Stmt * os_stmt ( Parser parser)

◆ p_advance()

Token p_advance ( Parser psr)

Advances to the next token and returns the current token.

This function moves the parser position forward by one token and returns the token that was current before advancing. This is the primary mechanism for consuming tokens during parsing.

Parameters
psrPointer to the parser instance
Returns
The token that was current before advancing, or EOF token if already at the end of the token stream
Note
The parser position is incremented only if there are tokens available
Safe to call at end of stream - returns EOF token and doesn't advance
This is a mutating operation that changes parser state
Warning
After calling this function, subsequent calls to p_current() will return the next token in the stream
See also
p_current(), p_has_tokens(), p_consume()

◆ p_consume()

Token p_consume ( Parser psr,
LumaTokenType  type,
const char *  error_msg 
)

Consumes a token of the expected type or reports an error.

This function is used when the parser expects a specific token type at the current position. If the current token matches the expected type, it advances and returns the token. If not, it reports a syntax error with the provided error message.

Parameters
psrPointer to the parser instance
typeThe expected token type that should be at the current position
error_msgError message to display if the token doesn't match
Returns
The consumed token if it matches the expected type, or an EOF token if there's a mismatch (indicating an error)
Note
This function combines token validation and consumption in one operation
Error reporting includes current line, column, and token length information
The parser position advances only on successful token match
On error, the parser position remains unchanged
Warning
Always check the return value's type if you need to handle parsing errors
See also
p_current(), p_advance(), parser_error(), LumaTokenType

◆ p_current()

Token p_current ( Parser psr)

Gets the current token without advancing the parser position.

This function returns the token at the current parser position without modifying the parser state. It's the most frequently used function for examining the current token during parsing.

Parameters
psrPointer to the parser instance
Returns
Current token, or an EOF token if at the end of the token stream
Note
This function is safe to call even when at the end of the token stream
The returned token contains all token information: type, value, position, etc.
Does not modify parser state - can be called multiple times safely
See also
p_advance(), p_peek(), Token

◆ p_has_tokens()

bool p_has_tokens ( Parser psr)

Checks if there are more tokens available for parsing.

This function determines whether the parser has reached the end of the token stream. It's used throughout the parser to control parsing loops and prevent buffer overruns when accessing tokens.

Parameters
psrPointer to the parser instance
Returns
true if there are more tokens to process (and current token is not EOF), false if at end of stream or current token is EOF
Note
This function performs two checks:
  1. Position is within the token array bounds
  2. Current token is not TOK_EOF (which marks end of input)
See also
p_current(), p_advance()

◆ p_peek()

Token p_peek ( Parser psr,
size_t  offset 
)

Peeks at a token at the specified offset from current position.

This function allows looking ahead in the token stream without advancing the current position. It's useful for making parsing decisions based on upcoming tokens (lookahead parsing).

Parameters
psrPointer to the parser instance
offsetNumber of positions ahead to look (0 = current, 1 = next, etc.)
Returns
Token at the specified offset, or an EOF token if the offset goes beyond the end of the token stream
Note
Common usage patterns:
  • p_peek(parser, 0) is equivalent to p_current(parser)
  • p_peek(parser, 1) looks at the next token
  • Safe to use with any offset; returns EOF token for out-of-bounds access
See also
p_current(), Token, TOK_EOF

◆ parse()

Stmt * parse ( GrowableArray tks,
ArenaAllocator arena,
BuildConfig config 
)

Parses a full program from tokens into an AST of statements.

Parameters
tksGrowableArray containing tokens.
arenaMemory arena for allocations.
Returns
Pointer to the root AST statement node (program).

Parses a full program from tokens into an AST of statements.

This is the entry point for the parser. It takes a growable array of tokens and converts them into a complete program AST node containing all parsed statements.

Parameters
tksGrowable array containing all tokens from the lexer
arenaArena allocator for memory management during parsing
Returns
Pointer to the root AST node (Program node) containing all parsed statements, or NULL if parsing fails
Note
The function estimates the initial capacity for statements based on token count
All memory allocations use the provided arena allocator
See also
Parser, parse_stmt(), create_program_node()

Main parsing function that converts tokens into an AST

This is the entry point for the parser. It takes a growable array of tokens and converts them into a complete program AST node containing all parsed statements.

◆ parse_expr()

Expr * parse_expr ( Parser parser,
BindingPower  bp 
)

Parses an expression using the Pratt parsing algorithm.

This is the core expression parsing function that implements the Pratt parser algorithm. It handles operator precedence and associativity automatically through the binding power mechanism.

Parameters
parserPointer to the parser instance
bpMinimum binding power - only operators with higher binding power will be consumed by this call
Returns
Pointer to the parsed expression AST node, or NULL if parsing fails
Note
The algorithm works by:
  1. Getting the left expression using nud()
  2. While the next operator has higher binding power than bp:
    • Use led() to extend the expression with the operator
  3. Return the final expression
See also
nud(), led(), get_bp(), BindingPower

◆ parse_module_declaration()

const char * parse_module_declaration ( Parser parser,
char **  out_module_doc 
)

◆ parse_stmt()

Stmt * parse_stmt ( Parser parser)

Parses a single statement.

This function dispatches to the appropriate statement parsing function based on the current token. It also handles visibility modifiers (public/private) that can appear before certain statement types.

Parameters
parserPointer to the parser instance
Returns
Pointer to the parsed statement AST node, or NULL if parsing fails
Note
Handles:
  • Variable declarations: const, var
  • Control flow: return, if, loop, break, continue
  • Block statements: { ... }
  • Print statements: print, println
  • Expression statements: any expression followed by semicolon
  • Visibility modifiers: public, private (applied to declarations)
See also
const_stmt(), var_stmt(), return_stmt(), block_stmt(), if_stmt(), loop_stmt(), print_stmt(), break_continue_stmt(), expr_stmt()

◆ parse_type()

Type * parse_type ( Parser parser)

Parses a type annotation.

This function parses type expressions used in variable declarations, function parameters, return types, etc. It handles primitive types, pointer types, array types, and user-defined types.

Parameters
parserPointer to the parser instance
Returns
Pointer to the parsed Type AST node, or NULL if parsing fails
Note
Handles:
  • Primitive types: int, uint, float, bool, string, void, char
  • Pointer types: *type
  • Array types: [size]type or []type
  • User-defined types: identified by TOK_IDENTIFIER
Warning
Prints error message to stderr for unexpected tokens
See also
tnud(), tled(), LumaTokenType

◆ parser_error()

void parser_error ( Parser psr,
const char *  error_type,
const char *  file,
const char *  msg,
int  line,
int  col,
int  tk_length 
)

Report a parser error with detailed location info.

Parameters
psrPointer to the parser.
error_typeType/category of the error.
fileSource file where error occurred.
msgError message.
lineLine number of error.
colColumn number of error.
tk_lengthLength of the token causing the error.

Report a parser error with detailed location info.

Creates and adds an error to the global error system with information about where the error occurred in the source code, including line and column information.

Parameters
psrPointer to the parser instance
error_typeString describing the type of error (e.g., "SyntaxError")
filePath to the source file where the error occurred
msgDetailed error message describing what went wrong
lineLine number where the error occurred (1-based)
colColumn number where the error occurred (1-based)
tk_lengthLength of the token that caused the error
Note
This function uses the arena allocator to duplicate the line text
See also
ErrorInformation, error_add()

◆ pointer()

Type * pointer ( Parser parser)

◆ prefix_expr()

Expr * prefix_expr ( Parser parser,
Expr left,
BindingPower  bp 
)

◆ primary()

Expr * primary ( Parser parser)

◆ print_stmt()

Stmt * print_stmt ( Parser parser,
bool  ln 
)

Parses print/println statements.

Handles output statements with the syntax:

  • print(expr1, expr2, ...);
  • println(expr1, expr2, ...);
Parameters
parserPointer to the parser instance
lnWhether this is a println (true) or print (false) statement
Returns
Pointer to the print statement AST node, or NULL on failure
Note
Supports multiple expressions separated by commas
println automatically adds a newline after output
Empty argument lists are allowed: print();
All expressions are evaluated and their values are printed
See also
parse_expr(), create_print_stmt()

◆ return_stmt()

Stmt * return_stmt ( Parser parser)

Parses a return statement.

Handles return statements with optional return values:

  • return; - Return with no value (void return)
  • return expression; - Return with a value
Parameters
parserPointer to the parser instance
Returns
Pointer to the return statement AST node, or NULL on failure
Note
The return value is optional; if not present, creates a void return
Requires a semicolon terminator
See also
parse_expr(), create_return_stmt()

◆ sizeof_expr()

Expr * sizeof_expr ( Parser parser)

◆ struct_expr()

Expr * struct_expr ( Parser parser)

◆ struct_stmt()

Stmt * struct_stmt ( Parser parser,
const char *  name,
bool  is_public 
)

Parses a structure declaration statement.

Handles struct declarations with public/private member visibility:

struct {
public:
field1: Type1,
method1 = fn() Type { ... }
private:
field2: Type2,
};
Definition ast.h:142
Parameters
parserPointer to the parser instance
nameStruct name (already parsed by caller)
is_publicWhether this struct has public visibility
Returns
Pointer to the struct declaration AST node, or NULL on failure
Note
Members are separated into public and private arrays
Supports both data fields (name: Type) and methods (name = fn ...)
Visibility defaults to public unless explicitly changed
Visibility changes affect all subsequent members until changed again
See also
fn_stmt(), create_field_decl_stmt(), create_struct_decl_stmt()

◆ switch_stmt()

Stmt * switch_stmt ( Parser parser)

◆ syscall_expr()

Expr * syscall_expr ( Parser parser)

◆ system_expr()

Expr * system_expr ( Parser parser)

◆ tget_bp()

BindingPower tget_bp ( Parser parser,
LumaTokenType  kind 
)

◆ tled()

Type * tled ( Parser parser,
Type left,
BindingPower  bp 
)

◆ tnud()

Type * tnud ( Parser parser)

◆ unary()

Expr * unary ( Parser parser)

◆ use_stmt()

Stmt * use_stmt ( Parser parser)

◆ var_stmt()

Stmt * var_stmt ( Parser parser,
bool  is_public 
)

Parses a variable declaration statement.

Handles variable declarations with the syntax: var name: Type = value;

Parameters
parserPointer to the parser instance
is_publicWhether this variable has public visibility
Returns
Pointer to the variable declaration AST node, or NULL on failure
Note
Variables are mutable by default (unlike constants)
Type annotation is required
Initial value assignment is required
Creates a variable declaration with is_mutable set to true
See also
parse_type(), parse_expr(), create_var_decl_stmt()