interpreter

package
v1.11.1 Latest Latest
Warning

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

Go to latest
Published: Oct 18, 2024 License: MIT Imports: 41 Imported by: 0

Documentation

Index

Constants

View Source
const (
	DIRECTORTYPE_RANDOM   = "random"
	DIRECTORTYPE_FALLBACK = "fallback"
	DIRECTORTYPE_HASH     = "hash"
	DIRECTORTYPE_CLIENT   = "client"
	DIRECTORTYPE_CHASH    = "chash"
	DIRECTORTYPE_SHIELD   = "shield"
)
View Source
const HTTPS_SCHEME = "https"

Variables

View Source
var (
	ErrQuorumWeightNotReached = errors.New("Quorum weight not reached")
	ErrAllBackendsFailed      = errors.New("All backend failed")
)

Functions

This section is empty.

Types

type DebugState

type DebugState int
const (
	DebugPass DebugState = iota
	DebugStepIn
	DebugStepOver
	DebugStepOut
)

type Debugger

type Debugger interface {
	Run(ast.Node) DebugState
	Message(string)
}

type DefaultDebugger

type DefaultDebugger struct{}

Default debugger, simply output message to stdout

func (DefaultDebugger) Message

func (d DefaultDebugger) Message(msg string)

func (DefaultDebugger) Run

func (d DefaultDebugger) Run(node ast.Node) DebugState

type Interpreter

type Interpreter struct {
	Debugger      Debugger
	IdentResolver func(v string) value.Value

	TestingState State
	// contains filtered or unexported fields
}

func New

func New(options ...context.Option) *Interpreter

func (*Interpreter) ConsoleProcessInit added in v1.6.0

func (i *Interpreter) ConsoleProcessInit() error

func (*Interpreter) IdentValue

func (i *Interpreter) IdentValue(val string, withCondition bool) (value.Value, error)

func (*Interpreter) InjectEdgeDictionaryItem added in v1.8.0

func (i *Interpreter) InjectEdgeDictionaryItem(table *ast.TableDeclaration, dict config.EdgeDictionary) error

Inject edge dictionary item from configuration. Edge dictionary value is managed in Fastly could so typically items are readonly. However we need to set some items in local simulator, particularly write-only edge dictionary So the interpreter can inject virtual value from falco coniguration.

func (*Interpreter) ProcessAddStatement

func (i *Interpreter) ProcessAddStatement(stmt *ast.AddStatement) error

func (*Interpreter) ProcessBackends added in v1.2.0

func (i *Interpreter) ProcessBackends(statements []ast.Statement) error

func (*Interpreter) ProcessBlockStatement

func (i *Interpreter) ProcessBlockStatement(
	statements []ast.Statement,
	ds DebugState,

	isReturnAsValue bool,
) (value.Value, State, DebugState, error)

nolint: gocognit

func (*Interpreter) ProcessCallStatement

func (i *Interpreter) ProcessCallStatement(stmt *ast.CallStatement, ds DebugState) (State, error)

func (*Interpreter) ProcessCaseStatement added in v1.4.0

func (i *Interpreter) ProcessCaseStatement(
	stmt *ast.SwitchStatement,
	offset int,
	control value.Value,
	isFallthrough bool,
	ds DebugState,
	isReturnAsValue bool,
) (value.Value, State, bool, error)

func (*Interpreter) ProcessDeclarations

func (i *Interpreter) ProcessDeclarations(statements []ast.Statement) error

func (*Interpreter) ProcessDeclareStatement

func (i *Interpreter) ProcessDeclareStatement(stmt *ast.DeclareStatement) error

func (*Interpreter) ProcessDeliver

func (i *Interpreter) ProcessDeliver() error

func (*Interpreter) ProcessError

func (i *Interpreter) ProcessError() error

func (*Interpreter) ProcessErrorStatement

func (i *Interpreter) ProcessErrorStatement(stmt *ast.ErrorStatement) error

func (*Interpreter) ProcessEsiStatement

func (i *Interpreter) ProcessEsiStatement(stmt *ast.EsiStatement) error

func (*Interpreter) ProcessExpression

func (i *Interpreter) ProcessExpression(exp ast.Expression, withCondition bool) (value.Value, error)

Evaluate expression withCondition boolean is special flag for evaluating expression, used for if condition, parenthesis wrapped expression. On if condition, prefix expression could use "!" prefix operator for null value.

For example:

withCondition: true  -> if (!req.http.Foo) { ... } // Valid, req.http.Foo is nullable string but can be inverse as false
withCondition: false -> set var.bool = (!req.http.Foo); // Complicated but valid, "!" prefix operator could  use for right expression
withCondition: false -> set var.bool = !req.http.Foo;   // Invalid, bare "!" prefix operator could not use for right expression

func (*Interpreter) ProcessExpressionReturnStatement added in v1.4.0

func (i *Interpreter) ProcessExpressionReturnStatement(stmt *ast.ReturnStatement) (value.Value, State, error)

func (*Interpreter) ProcessFetch

func (i *Interpreter) ProcessFetch() error

func (*Interpreter) ProcessFunctionCallExpression

func (i *Interpreter) ProcessFunctionCallExpression(exp *ast.FunctionCallExpression, withCondition bool) (value.Value, error)

func (*Interpreter) ProcessFunctionCallStatement

func (i *Interpreter) ProcessFunctionCallStatement(stmt *ast.FunctionCallStatement, ds DebugState) (State, error)

func (*Interpreter) ProcessFunctionSubroutine

func (i *Interpreter) ProcessFunctionSubroutine(sub *ast.SubroutineDeclaration, ds DebugState) (value.Value, State, error)

nolint: gocognit

func (*Interpreter) ProcessGroupedExpression

func (i *Interpreter) ProcessGroupedExpression(exp *ast.GroupedExpression) (value.Value, error)

func (*Interpreter) ProcessHash

func (i *Interpreter) ProcessHash() error

func (*Interpreter) ProcessHit

func (i *Interpreter) ProcessHit() error

func (*Interpreter) ProcessIfExpression

func (i *Interpreter) ProcessIfExpression(exp *ast.IfExpression) (value.Value, error)

func (*Interpreter) ProcessIfStatement

func (i *Interpreter) ProcessIfStatement(
	stmt *ast.IfStatement,
	ds DebugState,
	isReturnAsValue bool,
) (value.Value, State, error)

nolint:gocognit

func (*Interpreter) ProcessInfixExpression

func (i *Interpreter) ProcessInfixExpression(exp *ast.InfixExpression, withCondition bool) (value.Value, error)

func (*Interpreter) ProcessInit

func (i *Interpreter) ProcessInit(r *http.Request) error

func (*Interpreter) ProcessLog

func (i *Interpreter) ProcessLog() error

func (*Interpreter) ProcessLogStatement

func (i *Interpreter) ProcessLogStatement(stmt *ast.LogStatement) error

func (*Interpreter) ProcessMiss

func (i *Interpreter) ProcessMiss() error

func (*Interpreter) ProcessPass

func (i *Interpreter) ProcessPass() error

func (*Interpreter) ProcessPostfixExpression added in v1.5.0

func (i *Interpreter) ProcessPostfixExpression(exp *ast.PostfixExpression) (value.Value, error)

func (*Interpreter) ProcessPrefixExpression

func (i *Interpreter) ProcessPrefixExpression(exp *ast.PrefixExpression, withCondition bool) (value.Value, error)

func (*Interpreter) ProcessRecv

func (i *Interpreter) ProcessRecv() error

func (*Interpreter) ProcessRemoveStatement

func (i *Interpreter) ProcessRemoveStatement(stmt *ast.RemoveStatement) error

func (*Interpreter) ProcessReturnStatement

func (i *Interpreter) ProcessReturnStatement(stmt *ast.ReturnStatement) State

func (*Interpreter) ProcessSetStatement

func (i *Interpreter) ProcessSetStatement(stmt *ast.SetStatement) error

func (*Interpreter) ProcessStringConcatInfixExpression added in v1.11.0

func (i *Interpreter) ProcessStringConcatInfixExpression(exp *ast.InfixExpression) (value.Value, error)

InfixExpression process, but special case for string concatenation. string cocatenation has special type checking rule. left and right expression must be following expressions: - STRING - both literal and ident - IDENT - common variables like req.*, var.*, now, etc - PrefixExpression - the operator must be "+" - InfixExpression(nested) - same rule for left and right expression, and operator must be "+" - FunctionCallExpression - return value must be STRING - IfExpression - return value must be STRING

Other expressions (e.g GroupedExpression) is invalid. For example: set req.http.SomeHeader = "a" + "b" + "c"; // valid set req.http.SomeHeader = "a" + ("b" + "c"); // invalid

And we need to consider about "time calculation" - calculate between TIME and RTIME - as following rules: 1. RTIME literal with explicit plus or minus sign should be accepted for concatenating to previous TIME expression 2. And previous TIME expression must be an IDENT - invalid for return type of FunctionCallExpression like std.time()

Above two rules are satisfied, if should be "time calculation", add RTIME duration to the TIME. For example syntaxes: set req.http.Foo = now + 5m; // valid, time calculation(plus) between now(TIME) and 5m(RTIME literal) set req.http.Foo = now - 5m; // valid, time calculation(minus) between now(TIME) and 5m(RTIME literal) set req.http.Foo = now + var.someRTime; // invalid, plus sign will be time calculation but accept only literal set req.http.Foo = now var.someRTime; // valid, string concatenation set req.http.Foo = now 5m; // invalid, time calculate syntax error set req.http.Foo = std.time("xxx", now) + 5m; // invalid, left expression is TIME type but not an ident set req.http.Foo = std.time("xxx", now) + var.someRTime; // valid, string concatenation

See Fastly fiddle: https://fiddle.fastly.dev/fiddle/befec89e nolint: gocognit

func (*Interpreter) ProcessSubroutine

func (i *Interpreter) ProcessSubroutine(sub *ast.SubroutineDeclaration, ds DebugState) (State, error)

func (*Interpreter) ProcessSwitchStatement added in v1.4.0

func (i *Interpreter) ProcessSwitchStatement(
	stmt *ast.SwitchStatement,
	ds DebugState,
	isReturnAsValue bool,
) (value.Value, State, error)

func (*Interpreter) ProcessSyntheticBase64Statement

func (i *Interpreter) ProcessSyntheticBase64Statement(stmt *ast.SyntheticBase64Statement) error

func (*Interpreter) ProcessSyntheticStatement

func (i *Interpreter) ProcessSyntheticStatement(stmt *ast.SyntheticStatement) error

func (*Interpreter) ProcessTestSubroutine

func (i *Interpreter) ProcessTestSubroutine(scope icontext.Scope, sub *ast.SubroutineDeclaration) error

func (*Interpreter) ProcessUnsetStatement

func (i *Interpreter) ProcessUnsetStatement(stmt *ast.UnsetStatement) error

func (*Interpreter) ServeHTTP

func (i *Interpreter) ServeHTTP(w http.ResponseWriter, r *http.Request)

Implements http.Handler

func (*Interpreter) SetScope

func (i *Interpreter) SetScope(scope context.Scope)

func (*Interpreter) TestProcessInit

func (i *Interpreter) TestProcessInit(r *http.Request) error

type State

type State string
const (
	NONE           State = ""
	LOOKUP         State = "lookup"
	PASS           State = "pass"
	HASH           State = "hash"
	ERROR          State = "error"
	RESTART        State = "restart"
	DELIVER        State = "deliver"
	FETCH          State = "fetch"
	DELIVER_STALE  State = "deliver_stale"
	LOG            State = "log"
	END            State = "end"
	INTERNAL_ERROR State = "_internal_error_"
	BARE_RETURN    State = "_bare_return_"
)

func StateFromString added in v1.3.0

func StateFromString(s string) State

func (State) String

func (s State) String() string

Directories

Path Synopsis
Falco's interpreter cacheing is simply in-memory
Falco's interpreter cacheing is simply in-memory

Jump to

Keyboard shortcuts

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