libsnobol4 1.0.2
High-performance SNOBOL4-style pattern matching library
Loading...
Searching...
No Matches
parser.h File Reference

Recursive descent parser for SNOBOL pattern syntax. More...

#include "snobol/ast.h"
#include "snobol/lexer.h"
#include <stdbool.h>
#include <stddef.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_error_t

Macros

#define SNOBOL_PARSER_ERROR_MAX   256

Typedefs

typedef struct snobol_parser snobol_parser_t

Enumerations

enum  { SNOBOL_PARSER_MAX_DEPTH = 96 }
enum  { SNOBOL_PARSER_MAX_AST_DEPTH = 1000 }

Functions

snobol_parser_t * snobol_parser_create (void)
ast_node_t * snobol_parser_parse (snobol_parser_t *parser, snobol_lexer_t *lexer)
bool snobol_parser_has_error (snobol_parser_t *parser)
const char * snobol_parser_get_error (snobol_parser_t *parser)
void snobol_parser_get_error_location (snobol_parser_t *parser, size_t *line, size_t *column)
void snobol_parser_clear_error (snobol_parser_t *parser)
void snobol_parser_destroy (snobol_parser_t *parser)

Detailed Description

Recursive descent parser for SNOBOL pattern syntax.

Consumes tokens from lexer and produces AST. Opaque handle pattern - implementation details hidden from callers.

Definition in file parser.h.

Macro Definition Documentation

◆ SNOBOL_PARSER_ERROR_MAX

#define SNOBOL_PARSER_ERROR_MAX   256

Maximum error message length

Definition at line 24 of file parser.h.

Typedef Documentation

◆ snobol_parser_t

typedef struct snobol_parser snobol_parser_t

Opaque parser state

Definition at line 73 of file parser.h.

Enumeration Type Documentation

◆ anonymous enum

anonymous enum

Maximum structural nesting depth of a parsed pattern.

Each parenthesised group, label prefix, capture and function-call argument consumes one recursive-descent level. The parser rejects patterns that exceed this budget with a parse error instead of letting repeated '(' or a chain of nested calls exhaust the C stack.

The cap is sized against the most expensive production, not the cheapest: a table-update level costs about 3.5 KiB in a Debug build (ARBNO about 2.1 KiB, EVAL about 2.8 KiB, a group about 1.3 KiB), so 96 levels stay below 336 KiB and fit the smallest default thread stack (512 KiB on macOS) with room for the caller's frames above the parse entry. Do not raise it without re-measuring the largest production; a level count is only a safe proxy for stack use when it is calibrated this way.

Builder-composed ASTs (snobol_pattern_build_compile) bypass the parser and are not subject to this limit.

Enumerator
SNOBOL_PARSER_MAX_DEPTH 

Definition at line 45 of file parser.h.

◆ anonymous enum

anonymous enum

Maximum height of the AST the parser hands to the compiler.

Flat alternation branches and match-naming operators extend the tree without recursing the descent, so they get a larger budget than SNOBOL_PARSER_MAX_DEPTH. It still bounds the recursive walkers (emit_node_c/emit_alt_c, ast_max_reg_c, snobol_ast_free). Those are cheaper per level than the descent: a 1000-branch alternation compiles inside a 128 KiB thread stack in a Release build and needs about 384 KiB in a Debug build, which still fits the 512 KiB target with margin.

Enumerator
SNOBOL_PARSER_MAX_AST_DEPTH 

Definition at line 58 of file parser.h.

Function Documentation

◆ snobol_parser_clear_error()

void snobol_parser_clear_error ( snobol_parser_t * parser)

Clear parser error state (for reuse)

Parameters
parserParser instance

◆ snobol_parser_create()

snobol_parser_t * snobol_parser_create ( void )

Create a new parser

Returns
New parser (caller owns, must free with snobol_parser_destroy)

◆ snobol_parser_destroy()

void snobol_parser_destroy ( snobol_parser_t * parser)

Destroy parser and free resources

Parameters
parserParser to destroy (NULL is safe)

◆ snobol_parser_get_error()

const char * snobol_parser_get_error ( snobol_parser_t * parser)

Get parser error message

Parameters
parserParser instance
Returns
Error message (valid until parser is destroyed), or NULL if no error

◆ snobol_parser_get_error_location()

void snobol_parser_get_error_location ( snobol_parser_t * parser,
size_t * line,
size_t * column )

Get error location

Parameters
parserParser instance
lineOutput: line number (1-based)
columnOutput: column number (1-based)

◆ snobol_parser_has_error()

bool snobol_parser_has_error ( snobol_parser_t * parser)

Check if parser has an error

Parameters
parserParser instance
Returns
true if error occurred

◆ snobol_parser_parse()

ast_node_t * snobol_parser_parse ( snobol_parser_t * parser,
snobol_lexer_t * lexer )

Parse a complete pattern from tokens

Parameters
parserParser instance
lexerLexer instance (must be created with same source)
Returns
AST root node (caller owns), or NULL on error