|
Luma 0.1.0
A low-level compiled alternative to C, C++, and more!
|
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"
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. | |
| Stmt * | parse (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. | |
| Expr * | nud (Parser *parser) |
| Null Denotation - handles prefix expressions and primary expressions. | |
| Expr * | led (Parser *parser, Expr *left, BindingPower bp) |
| Left Denotation - handles binary and postfix expressions. | |
| Expr * | parse_expr (Parser *parser, BindingPower bp) |
| Parses an expression using the Pratt parsing algorithm. | |
| Stmt * | parse_stmt (Parser *parser) |
| Parses a single statement. | |
| Type * | parse_type (Parser *parser) |
| Parses a type annotation. | |
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:
| 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.
| kind | The token type to get binding power for |
| 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).
| parser | Pointer to the parser instance |
| left | The left operand expression (already parsed) |
| bp | The current binding power context |
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).
| parser | Pointer to the parser instance |
| 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.
| tks | Growable array containing all tokens from the lexer |
| arena | Arena allocator for memory management during parsing |
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.
| 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.
| parser | Pointer to the parser instance |
| bp | Minimum binding power - only operators with higher binding power will be consumed by this call |
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.
| parser | Pointer to the parser instance |
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.
| parser | Pointer to the parser instance |
| 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.
| psr | Pointer to the parser instance |
| error_type | String describing the type of error (e.g., "SyntaxError") |
| file | Path to the source file where the error occurred |
| msg | Detailed error message describing what went wrong |
| line | Line number where the error occurred (1-based) |
| col | Column number where the error occurred (1-based) |
| tk_length | Length of the token that caused the error |