Lime Parser Generator 0.1.0
Runtime-extensible LALR(1) parser with SIMD tokenization and LLVM JIT
Loading...
Searching...
No Matches
grammar_context.h File Reference

Grammar Context – language switching for embedded grammars. More...

#include <stdbool.h>
#include <stdint.h>

Go to the source code of this file.

Data Structures

struct  GrammarContextEntry
 One level of the grammar context stack. More...
 
struct  GrammarModeInfo
 Describes a grammar mode that can be entered during parsing. More...
 

Typedefs

typedef bool(* ContextSwitchCallback) (GrammarMode prev_mode, GrammarMode new_mode, void *user_data)
 Called when a context switch occurs.
 
typedef struct GrammarContextStack GrammarContextStack
 Opaque grammar context stack handle.
 

Enumerations

enum  GrammarMode {
  MODE_SQL = 0 , MODE_XQUERY , MODE_XPATH , MODE_EDN ,
  MODE_JSON , MODE_CUSTOM , MODE_COUNT_
}
 Grammar mode identifiers. More...
 

Functions

bool grammar_context_close_bracket (GrammarContextStack *stack)
 Notify the context stack that a bracket/parenthesis was closed.
 
GrammarContextStackgrammar_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.
 
ParserSnapshotgrammar_context_current_snapshot (const GrammarContextStack *stack)
 Return the snapshot for the current (topmost) grammar context.
 
uint32_t grammar_context_depth (const GrammarContextStack *stack)
 Return the current nesting depth (0 = root).
 
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.
 
bool grammar_context_detect_switch (GrammarContextStack *stack, int token_code, const char *lexeme, uint32_t offset)
 Detect whether a context switch should occur.
 
bool grammar_context_is_root_only (const GrammarContextStack *stack)
 Check if only the root grammar is active.
 
void grammar_context_open_bracket (GrammarContextStack *stack)
 Notify the context stack that a bracket/parenthesis was opened.
 
bool grammar_context_pop (GrammarContextStack *stack)
 Pop the current grammar context.
 
bool grammar_context_push (GrammarContextStack *stack, GrammarMode mode, uint32_t offset)
 Explicitly push a grammar mode onto the context stack.
 
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.
 

Detailed Description

Grammar Context – language switching for embedded grammars.

Manages a stack of grammar contexts to support parsing inputs that mix multiple languages (e.g., SQL with embedded XQuery expressions or EDN literals).

Each context represents an active grammar identified by a name and backed by a ParserSnapshot. When the parser encounters a boundary token (e.g., "xmlquery(", "xpath(", "{:"), it pushes a new context for the embedded language. When the embedded region ends, the context is popped and parsing resumes with the previous grammar.

Supported Modes
Mode Description
MODE_SQL Standard SQL (default / root)
MODE_XQUERY XQuery embedded in xmlquery(...)
MODE_XPATH XPath embedded in xpath(...)
MODE_EDN EDN embedded in {: ... :}
MODE_JSON JSON embedded literal
MODE_CUSTOM User-defined grammar mode
Usage Example
GrammarModeInfo xq_info = {
.mode = MODE_XQUERY, .name = "xquery",
.snapshot = xq_snap, .trigger_token = TK_XMLQUERY,
.exit_token = -1,
};
// During lexing:
if (grammar_context_detect_switch(stack, token, lexeme, offset)) {
// context was pushed, new snapshot is active
}
struct GrammarContextStack GrammarContextStack
Opaque grammar context stack handle.
GrammarContextStack * grammar_context_create(ParserSnapshot *root_snapshot)
Create a context stack with the given root grammar snapshot.
void grammar_context_destroy(GrammarContextStack *stack)
Destroy the context stack and release all snapshot references.
bool grammar_context_register_mode(GrammarContextStack *stack, const GrammarModeInfo *info)
Register a grammar mode.
bool grammar_context_detect_switch(GrammarContextStack *stack, int token_code, const char *lexeme, uint32_t offset)
Detect whether a context switch should occur.
@ MODE_XQUERY
XQuery embedded expression.
Describes a grammar mode that can be entered during parsing.
GrammarMode mode
Which mode this describes.
See also
parser.h for snapshot reference management.

Definition in file grammar_context.h.