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

Implementation of the parser module for the programming language compiler. More...

#include <stdalign.h>
#include <stdio.h>
#include "../ast/ast.h"
#include "../c_libs/error/error.h"
#include "../c_libs/memory/memory.h"
#include "../helper/help.h"
#include "parser.h"
Include dependency graph for parser.c:

Functions

void parser_error (Parser *psr, const char *error_type, const char *file, const char *msg, int line, int col, int tk_length)
 Reports a parser error with detailed location information.
 
Stmtparse (GrowableArray *tks, ArenaAllocator *arena, BuildConfig *config)
 Main parsing function that converts tokens into an AST.
 
BindingPower get_bp (LumaTokenType kind)
 Gets the binding power (precedence) for a given token type.
 
Exprnud (Parser *parser)
 Null Denotation - handles prefix expressions and primary expressions.
 
Exprled (Parser *parser, Expr *left, BindingPower bp)
 Left Denotation - handles binary and postfix expressions.
 
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.
 

Detailed Description

Implementation of the parser module for the programming language compiler.

This file contains the core parsing functionality that converts a stream of tokens into an Abstract Syntax Tree (AST). The parser uses a Pratt parser approach for handling operator precedence and associativity in expressions.

The parser supports:

Author
Connor Harris
Date
2025
Version
1.0

Function Documentation

◆ 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

◆ led()

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

Left Denotation - handles binary and postfix expressions.

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()

◆ 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()

◆ parse()

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

Main parsing function that converts tokens into an AST.

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_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 
)

Reports a parser error with detailed location information.

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()