librdesc
Loading...
Searching...
No Matches
rdesc.h
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2025-2026 Metehan Selvi <me@metehanselvi.com>
2//
3// SPDX-License-Identifier: LGPL-2.1-or-later
4
10#ifndef RDESC_H
11#define RDESC_H
12
13#include "detail.h"
14
15#include <stdbool.h>
16#include <stdint.h>
17#include <stddef.h>
18
20#define RDESC_VERSION_MAJOR 0
22#define RDESC_VERSION_MINOR 2
24#define RDESC_VERSION_PATCH 0
25
26
38
40struct rdesc {
43 /* Grammar production rules. */
44 const struct rdesc_grammar *grammar;
45
46 /* Size in bytes allocated for each token's semantic information. */
47 size_t seminfo_size;
48
49 /* - Error Recovery -
50 *
51 * Extra space for holding a token in case of memory allocation error.
52 * Token will be copied to those fields for retry in next pump call.
53 */
54 bool has_saved_tk;
55 uint16_t saved_tk;
56 void *saved_seminfo;
57
58 /* - Navigation - */
59 size_t cur /* (current) Nonterminal being expanded; may not be
60 * the top element. */;
61 uint16_t top_unwind /* Stack's top node's unwind distance. */;
62
63 /* Destructor method for tokens the parser owns. */
64 void (*token_destroyer)(uint16_t, void *);
65
66 /* Token stack used to store tokens temporarily during nonterminal
67 * backtracking. */
68 struct rdesc_stack *token_stack;
69
70 /* Underlying concrete syntax tree. */
71 struct rdesc_stack *cst_stack;
72
74};
75
77struct rdesc_node;
78
79
80#ifdef __cplusplus
81extern "C" {
82#endif
83
85const char *rdesc_version(void);
86
97int rdesc_init(struct rdesc *parser,
98 const struct rdesc_grammar *grammar,
99 size_t seminfo_size,
100 void (*token_destroyer)(uint16_t id, void *seminfo)) _rdesc_wur;
101
105void rdesc_destroy(struct rdesc *parser);
106
112int rdesc_start(struct rdesc *parser, uint16_t start_symbol) _rdesc_wur;
113
117void rdesc_reset(struct rdesc *parser);
118
136enum rdesc_result rdesc_pump(struct rdesc *parser,
137 uint16_t id,
138 void *seminfo) _rdesc_wur;
139
147 enum rdesc_result rdesc_resume(struct rdesc *parser) _rdesc_wur;
148
154struct rdesc_node *rdesc_root(struct rdesc *parser);
155
156#ifdef __cplusplus
157}
158#endif
159
160
161#endif
int rdesc_start(struct rdesc *parser, uint16_t start_symbol) _rdesc_wur
Sets start symbol for the next match.
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_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.
void rdesc_destroy(struct rdesc *parser)
Frees memory allocated by the parser and destroys the parser instance.
const char * rdesc_version(void)
rdesc version
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.
rdesc_result
Parse operation result codes.
Definition rdesc.h:28
@ RDESC_ENOMEM
Definition rdesc.h:30
@ RDESC_READY
Definition rdesc.h:32
@ RDESC_NOMATCH
Definition rdesc.h:36
@ RDESC_CONTINUE
Definition rdesc.h:34
Grammar definition.
Definition grammar.h:50
Default implementation of the stack.
Definition stack.c:36
Recursive descent parser state.
Definition rdesc.h:40