Lime Parser Generator 0.1.0
Runtime-extensible LALR(1) parser with SIMD tokenization and LLVM JIT
Loading...
Searching...
No Matches
glr.h
Go to the documentation of this file.
1
29#ifndef LIME_GLR_H
30#define LIME_GLR_H
31
32#include <stdint.h>
33#include <stdbool.h>
34#include <stddef.h>
35
36#ifdef __cplusplus
37extern "C" {
38#endif
39
40/* Forward declarations */
41typedef struct LimeArena LimeArena;
42typedef struct ParserSnapshot ParserSnapshot;
43
51typedef struct GSSNode {
52 uint32_t state;
54 union {
55 int ival;
56 void *pval;
57 double dval;
60 uint32_t npred;
61 uint32_t pred_capacity;
62 uint32_t refcount;
63#ifdef YYLOCATIONTYPE
65 struct {
66 uint32_t first_line, first_column, last_line, last_column;
67 } location;
68#endif
69} GSSNode;
70
78typedef int (*GLRDisambiguateFn)(uint32_t rule1_index, uint32_t rule2_index, void *user_data);
79
97
105GLRParser *glr_parser_create(uint32_t initial_state, size_t arena_size);
106
113
138int glr_parser_feed(GLRParser *parser, const ParserSnapshot *snap, uint16_t token);
139
147void glr_parser_set_disambiguate(GLRParser *parser, GLRDisambiguateFn fn, void *user_data);
148
156uint32_t glr_parser_head_count(const GLRParser *parser);
157
165bool glr_parser_accepted(const GLRParser *parser, uint16_t accept_action);
166
167/* GSSNode management (exposed for testing and embedding integrations). */
168GSSNode *gss_node_create(LimeArena *arena, uint32_t state);
169void gss_node_add_predecessor(GSSNode *node, GSSNode *pred);
170GSSNode *gss_node_acquire(GSSNode *node);
171void gss_node_release(GSSNode *node);
172
173/* ----------------------------------------------------------------------
174 * Public GLR API (parse_glr.c).
175 *
176 * These are the user-facing entry points that lift the GLR engine
177 * above into the same conceptual world as the LALR push parser.
178 * The LALR fast path (parse_token()) is intentionally untouched;
179 * users who never call lime_parse_glr() pay zero cost for GLR.
180 *
181 * Lifecycle:
182 * ParseContext *ctx = parse_begin(snap);
183 * if (lime_parse_glr(ctx, my_disambig, my_data) != 0) ...
184 * while (have_more_tokens) {
185 * int rc = lime_parse_glr_feed(ctx, token);
186 * if (rc < 0) break;
187 * }
188 * bool ok = lime_parse_glr_accepted(ctx);
189 * lime_parse_glr_end(ctx);
190 * parse_end(ctx);
191 * ---------------------------------------------------------------------- */
192
193/* Forward declaration so the prototypes below don't pull in parser.h. */
194struct ParseContext;
195
205 GLRDisambiguateFn disambig,
206 void *user_data);
207
215int lime_parse_glr_feed(struct ParseContext *ctx, uint16_t token);
216
221
226
231
232#ifdef __cplusplus
233}
234#endif
235
236#endif /* LIME_GLR_H */
bool glr_parser_accepted(const GLRParser *parser, uint16_t accept_action)
Check if the parse completed successfully.
int lime_parse_glr_feed(struct ParseContext *ctx, uint16_t token)
Feed one token into the GLR engine.
void lime_parse_glr_end(struct ParseContext *ctx)
Tear down GLR-mode state on the ParseContext.
bool lime_parse_glr_accepted(struct ParseContext *ctx)
Check whether the GLR parse accepted the input.
int lime_parse_glr(struct ParseContext *ctx, GLRDisambiguateFn disambig, void *user_data)
Enter GLR mode for an existing ParseContext.
uint32_t glr_parser_head_count(const GLRParser *parser)
Return the number of active parse heads.
void glr_parser_destroy(GLRParser *parser)
Destroy a GLR parser and free all resources.
int(* GLRDisambiguateFn)(uint32_t rule1_index, uint32_t rule2_index, void *user_data)
User-provided disambiguation callback.
Definition glr.h:78
int glr_parser_feed(GLRParser *parser, const ParserSnapshot *snap, uint16_t token)
Feed a token to the GLR parser.
uint32_t lime_parse_glr_head_count(struct ParseContext *ctx)
Number of active parse heads.
void glr_parser_set_disambiguate(GLRParser *parser, GLRDisambiguateFn fn, void *user_data)
Set the disambiguation callback.
GLRParser * glr_parser_create(uint32_t initial_state, size_t arena_size)
Create a GLR parser.
GLR parser context, wrapping an underlying LALR(1) parser.
Definition glr.h:86
bool has_ambiguity
True if unresolved ambiguity detected.
Definition glr.h:95
void * disambiguate_data
User data for callback.
Definition glr.h:92
LimeArena * arena
Arena for GSSNode allocation.
Definition glr.h:90
uint32_t total_merges
Statistics: total merges.
Definition glr.h:94
uint32_t nheads
Number of active heads.
Definition glr.h:88
uint32_t max_heads
Capacity of active_heads.
Definition glr.h:89
GSSNode ** active_heads
Array of active stack top nodes.
Definition glr.h:87
GLRDisambiguateFn disambiguate
User disambiguation callback.
Definition glr.h:91
uint32_t total_forks
Statistics: total forks.
Definition glr.h:93
Graph-Structured Stack (GSS) node for GLR parsing.
Definition glr.h:51
uint32_t npred
Number of predecessors.
Definition glr.h:60
uint32_t refcount
Reference count.
Definition glr.h:62
struct GSSNode ** predecessors
Array of predecessor nodes.
Definition glr.h:59
uint32_t state
Parser state number.
Definition glr.h:52
uint32_t pred_capacity
Allocated predecessor slots.
Definition glr.h:61
union GSSNode::@0 value
Semantic value (one of int / pointer / double, by convention).
Arena allocator for AST nodes.
Definition lime_ast.h:17
Per-parse-session state.
Opaque snapshot handle.
Definition snapshot.h:177