librdesc
Loading...
Searching...
No Matches
Classes | Macros | Typedefs | Enumerations | Functions
rdesc.h File Reference

The Right-Recursive Descent Parser. More...

#include <stdint.h>

Go to the source code of this file.

Classes

struct  rdesc
 Right-recursive descent parser. More...
 
struct  rdesc_token
 terminal (token) object for context-free grammar More...
 
struct  rdesc_nonterminal
 nonterminal (syntatic variable) object for context-free grammar More...
 
struct  rdesc_node
 A node in the CST. More...
 

Macros

#define RDESC_VERSION_MAJOR   0
 major version
 
#define RDESC_VERSION_MINOR   1
 minor version
 
#define RDESC_VERSION_PATCH   0
 patch version
 
#define RDESC_VERSION_PRE_RELEASE   ""
 prerelase identifier
 

Typedefs

typedef void(* rdesc_tk_destroyer_func) (struct rdesc_token *)
 Function pointer type for freeing tokens.
 

Enumerations

enum  rdesc_result { RDESC_READY , RDESC_CONTINUE , RDESC_NOMATCH }
 parsing status More...
 

Functions

void rdesc_init (struct rdesc *p, const struct rdesc_cfg *cfg)
 Initializes a new parser.
 
void rdesc_destroy (struct rdesc *p)
 Frees memory allocated by the parser and destroys the parser instance.
 
void rdesc_start (struct rdesc *p, int start_symbol)
 Sets start symbol for the next match.
 
void rdesc_reset (struct rdesc *p, rdesc_tk_destroyer_func free_tk)
 Resets parser and its state.
 
enum rdesc_result rdesc_pump (struct rdesc *p, struct rdesc_node **out, struct rdesc_token *incoming_tk)
 Drives the parsing process, The Pump.
 
void rdesc_node_destroy (struct rdesc_node *n, rdesc_tk_destroyer_func free_tk)
 Recursively destroys the node and its children.
 

Detailed Description

The Right-Recursive Descent Parser.

Enumeration Type Documentation

◆ rdesc_result

parsing status

Enumerator
RDESC_READY 

a tree is ready for consumption

RDESC_CONTINUE 

new tokens should be provided

RDESC_NOMATCH 

provided tokens do not match with any rule

Function Documentation

◆ rdesc_destroy()

void rdesc_destroy ( struct rdesc p)

Frees memory allocated by the parser and destroys the parser instance.

Warning
The token stack must be empty and no parse in progress before calling this function to prevent memory leaks. Raises an assertion error if the token stack is not empty or the root is not null.

◆ rdesc_node_destroy()

void rdesc_node_destroy ( struct rdesc_node n,
rdesc_tk_destroyer_func  free_tk 
)

Recursively destroys the node and its children.

Note
seminfo field in struct rdesc_token are not freed unless free_tk is set.

◆ rdesc_pump()

enum rdesc_result rdesc_pump ( struct rdesc p,
struct rdesc_node **  out,
struct rdesc_token incoming_tk 
)

Drives the parsing process, The Pump.

As the central engine of the parser, it consumes tokens from either the internal backtracking stack or the provided incoming_tk.

Parameters
pPointer to the parser instance.
outOutput pointer for the resulting CST node (if READY).
incoming_tkPointer to the next token to consume.
  • Optional: Pass NULL to resume parsing using only the tokens currently in the backtracking stack.
  • Storage: The pointer is used solely to make the argument nullable (optional). It does not imply that the token must be heap-allocated. Passing a pointer to a stack-allocated (automatic) variable is valid and expected, as the parser copies the token data internally.
Returns
The current status of the parse operation.

◆ rdesc_reset()

void rdesc_reset ( struct rdesc p,
rdesc_tk_destroyer_func  free_tk 
)

Resets parser and its state.

Note
seminfo field in struct rdesc_token are not freed unless free_tk is set.