Lime Parser Generator 0.1.0
Runtime-extensible LALR(1) parser with SIMD tokenization and LLVM JIT
Loading...
Searching...
No Matches
snapshot.h
1/*
2** Snapshot system core data structures for the extensible SQL parser.
3**
4** A ParserSnapshot captures the complete state of a parser's tables at a
5** point in time. Snapshots use atomic reference counting so they can be
6** safely shared across threads: readers acquire a reference before use and
7** release it when done. When the last reference is released the snapshot
8** and all of its owned memory are freed.
9*/
10#ifndef SNAPSHOT_H
11#define SNAPSHOT_H
12
13#include <stdint.h>
14#include <stdbool.h>
15#include <stdatomic.h>
16
17/* Forward declarations for Lemon grammar structures.
18** The actual definitions live in lemon.c; snapshot consumers only
19** need pointers to these types. */
20struct symbol;
21struct rule;
22struct state;
23
24/* ------------------------------------------------------------------ */
25/* Semantic versioning */
26/* ------------------------------------------------------------------ */
27
35typedef struct SemVer {
36 uint32_t major;
37 uint32_t minor;
38 uint32_t patch;
39 char *prerelease;
40} SemVer;
41
45typedef enum VersionOp {
46 VERSION_OP_EQ = 0, /* == exact match */
47 VERSION_OP_GTE, /* >= greater-than-or-equal */
48 VERSION_OP_LTE, /* <= less-than-or-equal */
49 VERSION_OP_GT, /* > strictly greater */
50 VERSION_OP_LT, /* < strictly less */
51 VERSION_OP_CARET, /* ^ compatible (same major) */
52 VERSION_OP_TILDE, /* ~ approximately (same major.minor) */
53} VersionOp;
54
58typedef struct VersionConstraint {
59 VersionOp op;
62
63/* ------------------------------------------------------------------ */
64/* Module dependency metadata */
65/* ------------------------------------------------------------------ */
66
82
103
104/* ------------------------------------------------------------------ */
105/* Snapshot */
106/* ------------------------------------------------------------------ */
107
108/*
109** A ParserSnapshot holds a frozen copy of every table the generated parser
110** needs at runtime. Fields fall into three groups:
111**
112** 1. Bookkeeping - version, refcount, timestamps
113** 2. Grammar data - symbols, rules, states (deep copies)
114** 3. Action tables - the compact arrays that drive the parse engine
115** 4. Module data - optional module identity and content hash
116*/
117typedef struct ParserSnapshot {
121 uint64_t version;
122
126 atomic_uint_fast32_t refcount;
127
128 /* --- Grammar data (deep-copied, owned by this snapshot) ----------- */
129
130 struct symbol **symbols;
131 uint32_t nsymbol;
132 uint32_t nterminal;
134 struct rule *rules;
135 uint32_t nrule;
137 struct state **states;
138 uint32_t nstate;
140 /* --- Compact action tables (heap-allocated, owned) ---------------- */
141
142 uint16_t *yy_action;
143 uint16_t *yy_lookahead;
144 int16_t *yy_shift_ofst;
145 int16_t *yy_reduce_ofst;
146 uint16_t *yy_default;
147 uint32_t action_count;
150 /* --- Module identity (optional, NULL when not part of a module) --- */
151
152 uint8_t merkle_root[32];
153 ParserModule *module;
157
160 void *jit_ctx;
162
163/*
164** Acquire a reference to an existing snapshot. The caller must eventually
165** call snapshot_release() to avoid leaking the snapshot.
166**
167** Returns the same pointer that was passed in, for convenience:
168** ParserSnapshot *my_ref = snapshot_acquire(shared_snap);
169**
170** Passing NULL is safe and returns NULL.
171*/
172ParserSnapshot *snapshot_acquire(ParserSnapshot *snap);
173
174/*
175** Release a reference previously obtained via snapshot_acquire() or
176** create_base_snapshot(). When the last reference is released the
177** snapshot and all memory it owns are freed.
178**
179** Passing NULL is safe and does nothing.
180*/
181void snapshot_release(ParserSnapshot *snap);
182
183/*
184** Create a base snapshot by parsing *grammar_file* through the Lemon
185** parser generator. On success a new snapshot with refcount == 1 is
186** returned and *error is set to NULL. On failure NULL is returned and
187** *error points to a malloc'd error message that the caller must free.
188**
189** NOTE: This is currently a stub. Full implementation comes when Lemon
190** is modified to emit dynamic tables (Task #3).
191*/
192ParserSnapshot *create_base_snapshot(const char *grammar_file, char **error);
193
194/* ------------------------------------------------------------------ */
195/* SemVer utilities */
196/* ------------------------------------------------------------------ */
197
198/*
199** Parse a semantic version string ("1.2.3" or "1.2.3-beta.1") into a
200** SemVer struct. Returns true on success. On failure *out is zeroed
201** and the function returns false.
202*/
203bool semver_parse(const char *str, SemVer *out);
204
205/*
206** Compare two semantic versions. Returns <0, 0, or >0 following the
207** same convention as strcmp. Prerelease versions sort before their
208** release counterpart (e.g. 1.0.0-alpha < 1.0.0).
209*/
210int semver_compare(const SemVer *a, const SemVer *b);
211
212/*
213** Check whether *ver* satisfies *constraint*.
214*/
215bool semver_satisfies(const SemVer *ver, const VersionConstraint *constraint);
216
217/*
218** Free resources owned by a SemVer (just the prerelease string).
219** Does not free the SemVer struct itself.
220*/
221void semver_destroy(SemVer *v);
222
223/* ------------------------------------------------------------------ */
224/* Module lifecycle helpers */
225/* ------------------------------------------------------------------ */
226
227/*
228** Deep-free a ParserModule and all memory it owns (name, version,
229** dependencies, exports, imports). Does not free the pointer itself
230** unless *mod* was heap-allocated by the caller.
231*/
232void parser_module_destroy_contents(ParserModule *mod);
233
234/*
235** Deep-free a ParserDependency's owned memory.
236*/
237void parser_dependency_destroy_contents(ParserDependency *dep);
238
239#endif /* SNAPSHOT_H */
A dependency declaration from one module to another.
Definition snapshot.h:75
bool optional
If true, unsatisfied is not an error.
Definition snapshot.h:80
char * module_name
Target module name (owned)
Definition snapshot.h:76
uint32_t nconstraints
Number of entries in constraints.
Definition snapshot.h:79
VersionConstraint * constraints
Array of version constraints.
Definition snapshot.h:78
uint8_t merkle_root[32]
Expected content hash (zero = any)
Definition snapshot.h:77
A parser module: a named, versioned unit of grammar with explicit dependency, export,...
Definition snapshot.h:90
char ** exports
Symbol names exported by this module.
Definition snapshot.h:97
char * name
Unique module name (owned)
Definition snapshot.h:91
char ** imports
Symbol names imported from other modules.
Definition snapshot.h:100
ParserDependency * dependencies
Array of dependencies (owned)
Definition snapshot.h:94
SemVer version
Module version.
Definition snapshot.h:92
uint32_t nimports
Length of imports.
Definition snapshot.h:101
uint32_t nexports
Length of exports.
Definition snapshot.h:98
uint32_t ndependencies
Length of Dependencies.
Definition snapshot.h:95
Opaque snapshot handle.
Definition snapshot.h:117
uint32_t lookahead_count
Number of entries in yy_lookahead.
Definition snapshot.h:148
struct symbol ** symbols
Array of pointers to symbol structs.
Definition snapshot.h:130
uint16_t * yy_default
Default action for each state.
Definition snapshot.h:146
uint64_t version
Monotonically increasing version number.
Definition snapshot.h:121
void * jit_ctx
Reserved for a future JIT compilation context that can cache machine code generated from the action t...
Definition snapshot.h:160
atomic_uint_fast32_t refcount
Atomic reference count.
Definition snapshot.h:126
int16_t * yy_shift_ofst
Per-state offset into yy_action for shifts.
Definition snapshot.h:144
uint32_t nterminal
Number of terminal symbols.
Definition snapshot.h:132
uint32_t nsymbol
Total number of symbols.
Definition snapshot.h:131
uint32_t nrule
Total number of rules.
Definition snapshot.h:135
uint8_t merkle_root[32]
Content hash of grammar data.
Definition snapshot.h:152
struct state ** states
Array of pointers to state structs.
Definition snapshot.h:137
uint32_t action_count
Number of entries in yy_action.
Definition snapshot.h:147
uint32_t nstate
Total number of parser states.
Definition snapshot.h:138
struct rule * rules
Linked list of production rules.
Definition snapshot.h:134
uint16_t * yy_lookahead
Lookahead values parallel to yy_action.
Definition snapshot.h:143
ParserModule *uint64_t create_time_ns
< Owning module metadata, or NULL
Definition snapshot.h:156
uint16_t * yy_action
Combined shift+reduce action array.
Definition snapshot.h:142
int16_t * yy_reduce_ofst
Per-state offset into yy_action for reduces.
Definition snapshot.h:145
A parsed semantic version: major.minor.patch with optional prerelease label (e.g.
Definition snapshot.h:35
uint32_t major
Major version component.
Definition snapshot.h:36
uint32_t minor
Minor version component.
Definition snapshot.h:37
uint32_t patch
Patch version component.
Definition snapshot.h:38
char * prerelease
Prerelease label (malloc'd; NULL if absent)
Definition snapshot.h:39
A single version constraint on a dependency, e.g.
Definition snapshot.h:58
SemVer version
Reference version for the operator.
Definition snapshot.h:60
VersionOp op
Constraint operator.
Definition snapshot.h:59