Lime Parser Generator 0.1.0
Runtime-extensible LALR(1) parser with SIMD tokenization and LLVM JIT
Loading...
Searching...
No Matches
Grammar Context API

Create, manage, and query the grammar context stack. 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

Create, manage, and query the grammar context stack.

Function Documentation

◆ grammar_context_close_bracket()

bool grammar_context_close_bracket ( GrammarContextStack stack)

#include <include/grammar_context.h>

Notify the context stack that a bracket/parenthesis was closed.

May trigger an automatic context pop if the mode's depth returns to its entry depth.

Parameters
stackThe context stack.
Return values
trueA context pop was triggered by bracket close.
falseNo pop occurred.

◆ grammar_context_create()

GrammarContextStack * grammar_context_create ( ParserSnapshot root_snapshot)

#include <include/grammar_context.h>

Create a context stack with the given root grammar snapshot.

The root mode is MODE_SQL. Acquires a reference to root_snapshot.

Parameters
root_snapshotSnapshot for the root (SQL) grammar.
Returns
New context stack, or NULL on allocation failure.
See also
grammar_context_destroy()

◆ grammar_context_current_mode()

GrammarMode grammar_context_current_mode ( const GrammarContextStack stack)

#include <include/grammar_context.h>

Return the current grammar mode.

Parameters
stackThe context stack.
Returns
Current GrammarMode value.

◆ grammar_context_current_snapshot()

ParserSnapshot * grammar_context_current_snapshot ( const GrammarContextStack stack)

#include <include/grammar_context.h>

Return the snapshot for the current (topmost) grammar context.

Parameters
stackThe context stack.
Returns
Current snapshot. Valid until the next push/pop.

◆ grammar_context_depth()

uint32_t grammar_context_depth ( const GrammarContextStack stack)

#include <include/grammar_context.h>

Return the current nesting depth (0 = root).

Parameters
stackThe context stack.
Returns
Nesting depth.

◆ grammar_context_destroy()

void grammar_context_destroy ( GrammarContextStack stack)

#include <include/grammar_context.h>

Destroy the context stack and release all snapshot references.

Parameters
stackStack to destroy. Passing NULL is safe.

◆ grammar_context_detect_exit()

bool grammar_context_detect_exit ( GrammarContextStack stack,
int  token_code 
)

#include <include/grammar_context.h>

Detect whether the current embedded context should end.

If so, the context is popped and the function returns true. The caller should switch back to grammar_context_current_snapshot().

Parameters
stackThe context stack.
token_codeThe token code just lexed.
Return values
trueContext was popped; previous mode is now active.
falseNo exit detected; continue in the current mode.

◆ grammar_context_detect_switch()

bool grammar_context_detect_switch ( GrammarContextStack stack,
int  token_code,
const char *  lexeme,
uint32_t  offset 
)

#include <include/grammar_context.h>

Detect whether a context switch should occur.

If a switch is detected, the appropriate context is pushed and the function returns true. The caller should then use grammar_context_current_snapshot() for subsequent parsing.

Parameters
stackThe context stack.
token_codeThe token code just lexed.
lexemeThe lexeme text (may be NULL).
offsetCurrent offset in the input stream.
Return values
trueA context switch occurred; new mode is active.
falseNo switch; continue with the current mode.

◆ grammar_context_is_root_only()

bool grammar_context_is_root_only ( const GrammarContextStack stack)

#include <include/grammar_context.h>

Check if only the root grammar is active.

This is the fast-path check: if true, the parser can skip all mode-detection logic.

Parameters
stackThe context stack.
Return values
trueOnly the root grammar is active (no embedded contexts).
falseOne or more embedded contexts are active.

◆ grammar_context_open_bracket()

void grammar_context_open_bracket ( GrammarContextStack stack)

#include <include/grammar_context.h>

Notify the context stack that a bracket/parenthesis was opened.

Used for depth tracking in modes that exit when their enclosing brackets close.

Parameters
stackThe context stack.

◆ grammar_context_pop()

bool grammar_context_pop ( GrammarContextStack stack)

#include <include/grammar_context.h>

Pop the current grammar context.

Parameters
stackThe context stack.
Return values
trueA context was popped.
falseAlready at the root (nothing to pop).

◆ grammar_context_push()

bool grammar_context_push ( GrammarContextStack stack,
GrammarMode  mode,
uint32_t  offset 
)

#include <include/grammar_context.h>

Explicitly push a grammar mode onto the context stack.

Parameters
stackThe context stack.
modeGrammar mode to push.
offsetCurrent input offset.
Return values
truePush succeeded.
falseMode not registered or allocation failed.

◆ grammar_context_register_mode()

bool grammar_context_register_mode ( GrammarContextStack stack,
const GrammarModeInfo info 
)

#include <include/grammar_context.h>

Register a grammar mode.

The stack acquires a reference to the mode's snapshot.

Parameters
stackStack to register the mode with.
infoMode descriptor.
Return values
trueRegistration succeeded.
falseMode already registered or allocation failed.

◆ grammar_context_set_switch_callback()

void grammar_context_set_switch_callback ( GrammarContextStack stack,
ContextSwitchCallback  cb,
void *  user_data 
)

#include <include/grammar_context.h>

Register a callback to be invoked on context switches.

Parameters
stackThe context stack.
cbCallback function (may be NULL to unregister).
user_dataOpaque pointer forwarded to cb.