|
Luma 0.1.0
A low-level compiled alternative to C, C++, and more!
|
Utility functions for the parser module. More...

Functions | |
| 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. | |
| bool | init_parser_arrays (Parser *parser, GrowableArray *stmts, GrowableArray *modules) |
| const char * | parse_module_declaration (Parser *parser, char **out_module_doc) |
| char * | collect_doc_comments (Parser *parser) |
| Collects consecutive documentation comments before a declaration. | |
| void | consume_doc_comments (Parser *parser) |
Utility functions for the parser module.
This file contains essential utility functions that provide the core infrastructure for token navigation and manipulation during parsing. These functions abstract the low-level details of token stream management and provide a clean interface for parser operations.
The utilities include:
All functions are designed to be safe and handle edge cases like end-of-stream conditions gracefully by returning EOF tokens.
| 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.
| parser | Pointer to the parser instance |
| void consume_doc_comments | ( | Parser * | parser | ) |
| 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.
| psr | Pointer to the parser instance |
| bool init_parser_arrays | ( | Parser * | parser, |
| GrowableArray * | stmts, | ||
| GrowableArray * | modules | ||
| ) |
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.
| psr | Pointer to the parser instance |
| 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.
| psr | Pointer to the parser instance |
| type | The expected token type that should be at the current position |
| error_msg | Error message to display if the token doesn't match |
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.
| psr | Pointer to the parser instance |
| 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.
| psr | Pointer to the parser instance |
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).
| psr | Pointer to the parser instance |
| offset | Number of positions ahead to look (0 = current, 1 = next, etc.) |
p_peek(parser, 0) is equivalent to p_current(parser)p_peek(parser, 1) looks at the next token| const char * parse_module_declaration | ( | Parser * | parser, |
| char ** | out_module_doc | ||
| ) |