Lime Parser Generator 0.1.0
Runtime-extensible LALR(1) parser with SIMD tokenization and LLVM JIT
Loading...
Searching...
No Matches
grammar_context.h
Go to the documentation of this file.
1
46#ifndef GRAMMAR_CONTEXT_H
47#define GRAMMAR_CONTEXT_H
48
49#include <stdbool.h>
50#include <stdint.h>
51
52/* ------------------------------------------------------------------ */
53/* Forward declarations */
54/* ------------------------------------------------------------------ */
55
56typedef struct ParserSnapshot ParserSnapshot;
57
58/* ------------------------------------------------------------------ */
63/* ------------------------------------------------------------------ */
64
77
/* end grammar_modes */
79
80/* ------------------------------------------------------------------ */
85/* ------------------------------------------------------------------ */
86
97
/* end ctx_entry */
99
100/* ------------------------------------------------------------------ */
105/* ------------------------------------------------------------------ */
106
138
/* end mode_info */
140
141/* ------------------------------------------------------------------ */
146/* ------------------------------------------------------------------ */
147
150
/* end ctx_stack */
152
153/* ------------------------------------------------------------------ */
158/* ------------------------------------------------------------------ */
159
172typedef bool (*ContextSwitchCallback)(GrammarMode prev_mode, GrammarMode new_mode, void *user_data);
173
/* end ctx_switch_cb */
175
176/* ------------------------------------------------------------------ */
181/* ------------------------------------------------------------------ */
182
194
201
213
228bool grammar_context_detect_switch(GrammarContextStack *stack, int token_code, const char *lexeme,
229 uint32_t offset);
230
243
253bool grammar_context_push(GrammarContextStack *stack, GrammarMode mode, uint32_t offset);
254
263
271
279
287
296 void *user_data);
297
307
319
331
/* end grammar_ctx_api */
333
334/* ------------------------------------------------------------------ */
350/* ------------------------------------------------------------------ */
351
360#define MODE_NONE ((GrammarMode)MODE_SQL)
361
388 const char *trigger_lexeme,
389 ParserSnapshot *embedded_snap,
390 const char *mode_name);
391
406
424bool context_switch_needed(const GrammarContextStack *stack, int token_code, const char *lexeme);
425
443bool context_switch_detect_exit(GrammarContextStack *stack, int token_code, const char *lexeme);
444
/* end ctx_switch */
446
447/* ------------------------------------------------------------------ */
460/* ------------------------------------------------------------------ */
461
469
479
/* end grammar_ctx_introspect */
481
482#endif /* GRAMMAR_CONTEXT_H */
struct GrammarContextStack GrammarContextStack
Opaque grammar context stack handle.
bool(* ContextSwitchCallback)(GrammarMode prev_mode, GrammarMode new_mode, void *user_data)
Called when a context switch occurs.
GrammarMode context_switch_classify_lexeme(const GrammarContextStack *stack, const char *lexeme)
Classify a lexeme against the registered triggers.
bool context_switch_register_trigger(GrammarContextStack *stack, const char *trigger_lexeme, ParserSnapshot *embedded_snap, const char *mode_name)
Register a runtime trigger that switches into an embedded grammar.
bool context_switch_detect_exit(GrammarContextStack *stack, int token_code, const char *lexeme)
Detect whether the current embedded context should exit.
bool context_switch_needed(const GrammarContextStack *stack, int token_code, const char *lexeme)
Fast-path predicate: could the current token trigger a switch?
bool grammar_context_pop(GrammarContextStack *stack)
Pop the current grammar context.
GrammarContextStack * grammar_context_create(ParserSnapshot *root_snapshot)
Create a context stack with the given root grammar snapshot.
GrammarMode grammar_context_current_mode(const GrammarContextStack *stack)
Return the current grammar mode.
void grammar_context_destroy(GrammarContextStack *stack)
Destroy the context stack and release all snapshot references.
bool grammar_context_detect_exit(GrammarContextStack *stack, int token_code)
Detect whether the current embedded context should end.
ParserSnapshot * grammar_context_current_snapshot(const GrammarContextStack *stack)
Return the snapshot for the current (topmost) grammar context.
bool grammar_context_is_root_only(const GrammarContextStack *stack)
Check if only the root grammar is active.
bool grammar_context_register_mode(GrammarContextStack *stack, const GrammarModeInfo *info)
Register a grammar mode.
void grammar_context_set_switch_callback(GrammarContextStack *stack, ContextSwitchCallback cb, void *user_data)
Register a callback to be invoked on context switches.
bool grammar_context_push(GrammarContextStack *stack, GrammarMode mode, uint32_t offset)
Explicitly push a grammar mode onto the context stack.
uint32_t grammar_context_depth(const GrammarContextStack *stack)
Return the current nesting depth (0 = root).
bool grammar_context_close_bracket(GrammarContextStack *stack)
Notify the context stack that a bracket/parenthesis was closed.
void grammar_context_open_bracket(GrammarContextStack *stack)
Notify the context stack that a bracket/parenthesis was opened.
bool grammar_context_detect_switch(GrammarContextStack *stack, int token_code, const char *lexeme, uint32_t offset)
Detect whether a context switch should occur.
const GrammarModeInfo * grammar_context_mode_at(const GrammarContextStack *stack, uint32_t i)
Read one entry from the registered-mode table.
uint32_t grammar_context_mode_count(const GrammarContextStack *stack)
Number of registered modes.
GrammarMode
Grammar mode identifiers.
@ MODE_CUSTOM
User-defined / extension grammar.
@ MODE_JSON
JSON embedded literal.
@ MODE_SQL
Standard SQL (root grammar)
@ MODE_COUNT_
Sentinel (number of built-in modes)
@ MODE_XPATH
XPath embedded expression.
@ MODE_XQUERY
XQuery embedded expression.
@ MODE_EDN
EDN (Extensible Data Notation)
One level of the grammar context stack.
int depth
Nesting depth (for bracket matching)
ParserSnapshot * snapshot
Snapshot for this grammar.
const char * mode_name
Human-readable name (borrowed)
GrammarMode mode
Active grammar mode.
uint32_t start_offset
Input offset where mode began.
Describes a grammar mode that can be entered during parsing.
const char * name
Human-readable name.
GrammarMode mode
Which mode this describes.
const char * trigger_lexeme
Entry trigger: lexeme prefix that initiates this mode.
int trigger_token
Entry trigger: token code that initiates this mode.
int exit_token
Exit trigger: token code that terminates this mode.
ParserSnapshot * snapshot
Snapshot to use (reference acquired)
Opaque snapshot handle.
Definition snapshot.h:177