Documentation
¶
Overview ¶
Package context provides parsing context management for the Cypher query parser. It tracks the current parsing state, variable scopes, and error accumulation during the parsing process.
The context maintains a stack of scopes for variable binding and tracks the current clause being parsed (MATCH, CREATE, WHERE, etc.).
Example:
ctx := context.New()
ctx.EnterScope()
ctx.BindVariable("n", context.VarNode, 0)
if binding := ctx.LookupVariable("n"); binding != nil {
fmt.Printf("Variable n is a %v\n", binding.Type)
}
ctx.ExitScope()
Index ¶
- type Context
- func (c *Context) AddError(err error)
- func (c *Context) BindVariable(name string, typ VarType, offset int)
- func (c *Context) CurrentScope() *Scope
- func (c *Context) EnterScope()
- func (c *Context) Errors() []error
- func (c *Context) ExitScope()
- func (c *Context) GetState() ParseState
- func (c *Context) HasErrors() bool
- func (c *Context) InClauseState() bool
- func (c *Context) IsVariableDeclared(name string) bool
- func (c *Context) LookupVariable(name string) *VarBinding
- func (c *Context) MarkVariableUsed(name string)
- func (c *Context) SetState(state ParseState)
- type ParseState
- type Scope
- type VarBinding
- type VarType
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Context ¶
type Context struct {
// contains filtered or unexported fields
}
Context manages the parsing state and variable scopes.
func New ¶
func New() *Context
New creates a new parsing context with a global scope.
Returns a new Context instance.
Example:
ctx := context.New() ctx.EnterScope()
func (*Context) BindVariable ¶
BindVariable binds a variable in the current scope.
Parameters:
- name: The variable name
- typ: The variable type
- offset: The variable offset (for code generation)
func (*Context) CurrentScope ¶
CurrentScope returns the current scope.
Returns the current scope, or nil if no scopes exist.
func (*Context) EnterScope ¶
func (c *Context) EnterScope()
EnterScope creates and enters a new scope. The new scope becomes the current scope.
Example:
ctx.EnterScope() defer ctx.ExitScope()
func (*Context) ExitScope ¶
func (c *Context) ExitScope()
ExitScope exits the current scope and returns to the parent scope. The global scope cannot be exited.
func (*Context) GetState ¶
func (c *Context) GetState() ParseState
GetState returns the current parse state.
Returns the current parse state.
func (*Context) HasErrors ¶
HasErrors returns true if any errors have been accumulated.
Returns true if there are errors.
func (*Context) InClauseState ¶
InClauseState returns true if the parser is currently in a clause state. This includes MATCH, CREATE, WHERE, RETURN, etc.
Returns true if in a clause state.
func (*Context) IsVariableDeclared ¶
IsVariableDeclared checks if a variable is declared in the current scope.
Parameters:
- name: The variable name
Returns true if the variable is declared.
func (*Context) LookupVariable ¶
func (c *Context) LookupVariable(name string) *VarBinding
LookupVariable looks up a variable in the current scope.
Parameters:
- name: The variable name
Returns the variable binding, or nil if not found.
func (*Context) MarkVariableUsed ¶
MarkVariableUsed marks a variable as used in the current scope.
Parameters:
- name: The variable name
func (*Context) SetState ¶
func (c *Context) SetState(state ParseState)
SetState sets the current parse state.
Parameters:
- state: The new parse state
type ParseState ¶
type ParseState int
ParseState represents the current state of the parser.
const ( StateInitial ParseState = iota StateInMatch StateInCreate StateInMerge StateInWhere StateInReturn StateInWith StateInSet StateInDelete StateInRemove StateInPattern StateInExpression StateInListComprehension StateInPatternComprehension )
Parse state constants.
func (ParseState) String ¶
func (s ParseState) String() string
String returns the string representation of the parse state.
type Scope ¶
type Scope struct {
// contains filtered or unexported fields
}
Scope represents a variable scope in the Cypher query. Scopes can be nested, with child scopes inheriting from parent scopes.
func NewScope ¶
NewScope creates a new scope with the given parent.
Parameters:
- parent: The parent scope, or nil for a global scope
Returns a new Scope instance.
Example:
global := NewScope(nil) local := NewScope(global)
func (*Scope) Bind ¶
func (s *Scope) Bind(name string, typ VarType, offset int) *VarBinding
Bind creates a new variable binding in this scope.
Parameters:
- name: The variable name
- typ: The variable type
- offset: The variable offset
Returns the created VarBinding.
Example:
binding := scope.Bind("n", VarNode, 0)
func (*Scope) IsBound ¶
IsBound checks if a variable is bound in this scope or any parent scope.
Parameters:
- name: The variable name
Returns true if the variable is bound.
func (*Scope) Lookup ¶
func (s *Scope) Lookup(name string) *VarBinding
Lookup searches for a variable in this scope and parent scopes.
Parameters:
- name: The variable name
Returns the VarBinding if found, or nil if not found.
Example:
if binding := scope.Lookup("n"); binding != nil {
fmt.Printf("Found variable: %s\n", binding.Name)
}
type VarBinding ¶
type VarBinding struct {
Name string // Variable name
Type VarType // Variable type
Declared bool // Whether the variable is declared
Used bool // Whether the variable is used
Offset int // Variable offset for code generation
}
VarBinding represents a variable binding in a scope.