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

The deterministic recursive descent parser. More...

#include "detail.h"
#include <stdbool.h>
#include <stdint.h>
#include <stddef.h>
Include dependency graph for rdesc.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

struct  rdesc
 Recursive descent parser state. More...
 

Macros

#define RDESC_VERSION_MAJOR   0
 Major version.
 
#define RDESC_VERSION_MINOR   2
 Minor version.
 
#define RDESC_VERSION_PATCH   0
 Patch version.
 
#define RDESC_VERSION_PRE_RELEASE   "rc.2"
 Prerelease identifier.
 

Enumerations

enum  rdesc_result { RDESC_ENOMEM = -1 , RDESC_READY = 0 , RDESC_CONTINUE = 1 , RDESC_NOMATCH = 2 }
 Parse operation result codes. More...
 

Functions

const char * rdesc_version (void)
 rdesc version
 
int rdesc_init (struct rdesc *parser, const struct rdesc_grammar *grammar, size_t seminfo_size, void(*token_destroyer)(uint16_t id, void *seminfo)) _rdesc_wur
 Initializes a new parser.
 
void rdesc_destroy (struct rdesc *parser)
 Frees memory allocated by the parser and destroys the parser instance.
 
int rdesc_start (struct rdesc *parser, uint16_t start_symbol) _rdesc_wur
 Sets start symbol for the next match.
 
void rdesc_reset (struct rdesc *parser)
 Resets the parser to its initial state.
 
enum rdesc_result rdesc_pump (struct rdesc *parser, uint16_t id, void *seminfo) _rdesc_wur
 Drives the parsing process, the pump.
 
enum rdesc_result rdesc_resume (struct rdesc *parser) _rdesc_wur
 Resume parsing without providing a new token.
 
struct rdesc_node * rdesc_root (struct rdesc *parser)
 Returns the root of the CST.
 

Detailed Description

The deterministic recursive descent parser.

Enumeration Type Documentation

◆ rdesc_result

Parse operation result codes.

Enumerator
RDESC_ENOMEM 

Memory allocation failed.

RDESC_READY 

A CST is ready for consumption.

RDESC_CONTINUE 

New tokens should be provided.

RDESC_NOMATCH 

No grammar rule matches the provided tokens.

Function Documentation

◆ rdesc_init()

int rdesc_init ( struct rdesc parser,
const struct rdesc_grammar grammar,
size_t  seminfo_size,
void(*)(uint16_t id, void *seminfo)  token_destroyer 
)

Initializes a new parser.

Parameters
parserParser instance to initialize.
grammarGrammar defining production rules (must outlive parser).
seminfo_sizeSize in bytes of token semantic information.
token_destroyerOptional callback to free token seminfo (can be NULL).
Returns
Non-zero value if memory allocation fails.

◆ rdesc_pump()

enum rdesc_result rdesc_pump ( struct rdesc parser,
uint16_t  id,
void *  seminfo 
)

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 id.

Parameters
parserPointer to the parser instance.
idIdentifier of the next token to consume.
seminfoExtra semantic information for the token.
  • Semantic information pointer. The parser copies this data internally, so passing a pointer to stack-allocated data is valid. NULL is acceptable.
Returns
The current status of the parse operation.
Warning
Raises an error if the parser is not started.

◆ rdesc_resume()

enum rdesc_result rdesc_resume ( struct rdesc parser)

Resume parsing without providing a new token.

Resumes using either:

  • The saved token from a previous ENOMEM error, or
  • A token from the backtrack stack

◆ rdesc_root()

struct rdesc_node * rdesc_root ( struct rdesc parser)

Returns the root of the CST.

Note
Returns NULL if no CST has been created yet.

◆ rdesc_start()

int rdesc_start ( struct rdesc parser,
uint16_t  start_symbol 
)

Sets start symbol for the next match.

Returns
Non-zero value if memory allocation fails.