ion7-grammar / runtime.context

class

GrammarContext

local gc = Grammar.context()

-- Register live schema
gc:learn_enum("status", { "pending", "active", "closed" })
gc:learn_table("users", { "id", "name", "email" })

-- Compile current grammar (cached until invalidated)
local g = gc:current()

-- Grammar grows with the conversation
gc:learn_table("orders", { "id", "user_id", "total" })

-- Branch and restore
local snap = gc:snapshot()
gc:learn_enum("color", { "red", "blue" })
gc:restore(snap)  -- back to before color

Functions

GrammarContext.new

Create a new GrammarContext.

GrammarContext.new(opts)
optstable?
→ GrammarContext

GrammarContext:_invalidate

GrammarContext:_invalidate()

GrammarContext:learn_enum

Register an enum whitelist (creates or replaces a named rule).

GrammarContext:learn_enum(rule_name, values)
rule_namestringRule name.
valuestableArray of allowed string values.
→ GrammarContextself

GrammarContext:learn_table

Register a database table with its column names. Creates two rules: `

` (the table name) and `
_col` (columns). Also updates combined `table_name` and `column_name` enums.

GrammarContext:learn_table(name, columns)
namestringTable name.
columnstableArray of column name strings.
→ GrammarContextself

GrammarContext:learn_tool

Register a tool definition (for tool-call grammars).

GrammarContext:learn_tool(name, schema)
namestringTool name.
schematable?JSON Schema for arguments.
→ GrammarContextself

GrammarContext:learn_rule

Add or replace a custom rule.

GrammarContext:learn_rule(rule_name, body)
rule_namestringRule name.
bodyanyAST node.
→ GrammarContextself

GrammarContext:forget

Remove a learned rule, enum, table, or tool.

GrammarContext:forget(name)
namestringRule/table/tool name to forget.
→ GrammarContextself

GrammarContext:current

Build and return the current grammar object reflecting all learned knowledge. Result is cached until a learn_* or forget() call invalidates it.

GrammarContext:current()
→ anyGrammar_obj (via _ctx_grammar_mt)

GrammarContext:snapshot

Serialize current state for snapshot/restore.

GrammarContext:snapshot()
→ tableSnapshot of current learned state.

GrammarContext:restore

Restore state from a snapshot.

GrammarContext:restore(snap)
snaptableFrom GrammarContext:snapshot().
→ GrammarContextself

GrammarContext:stats

Return a summary of what this context has learned.

GrammarContext:stats()
→ table{ n_enums, n_tables, n_tools, n_extra }