context

package
v0.2.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 28, 2026 License: MIT Imports: 0 Imported by: 0

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

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) AddError

func (c *Context) AddError(err error)

AddError adds an error to the context.

Parameters:

  • err: The error to add

func (*Context) BindVariable

func (c *Context) BindVariable(name string, typ VarType, offset int)

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

func (c *Context) CurrentScope() *Scope

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) Errors

func (c *Context) Errors() []error

Errors returns all accumulated errors.

Returns the slice of errors.

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

func (c *Context) HasErrors() bool

HasErrors returns true if any errors have been accumulated.

Returns true if there are errors.

func (*Context) InClauseState

func (c *Context) InClauseState() bool

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

func (c *Context) IsVariableDeclared(name string) bool

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

func (c *Context) MarkVariableUsed(name string)

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

func NewScope(parent *Scope) *Scope

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

func (s *Scope) IsBound(name string) bool

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)
}

func (*Scope) MarkUsed

func (s *Scope) MarkUsed(name string)

MarkUsed marks a variable as used in this scope. Note: This only marks variables in the current scope, not parent scopes.

Parameters:

  • name: The variable 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.

type VarType

type VarType int

VarType represents the type of a variable in the Cypher query.

const (
	VarUnknown VarType = iota
	VarNode
	VarRelationship
	VarPath
	VarMap
	VarList
	VarScalar
)

Variable type constants.

func (VarType) String

func (v VarType) String() string

String returns the string representation of the variable type.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL