match

package
v2.0.0+incompatible Latest Latest
Warning

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

Go to latest
Published: Apr 15, 2019 License: Apache-2.0 Imports: 2 Imported by: 9

Documentation

Overview

Package match implements the core pattern matcher.

Index

Constants

This section is empty.

Variables

View Source
var DefaultMatcher = &Matcher{
	AllowPropertyVariables:       true,
	CheckForBadPropertyVariables: true,
	Inequalities:                 true,
}

DefaultMatcher is just that.

Change it if you want.

The Match() function uses this value.

Functions

This section is empty.

Types

type Bindings

type Bindings map[string]interface{}

Bindings is a map from variables (strings starting with a '?') to their values.

func Match

func Match(pattern interface{}, fact interface{}, bindings Bindings) ([]Bindings, error)

Match calls DefaultMatcher.Match.

func NewBindings

func NewBindings() Bindings

NewBindings returns empty Bindings.

func (Bindings) Copy

func (bs Bindings) Copy() Bindings

Copy makes a shallow copy of the Bindings.

func (Bindings) DeleteExcept

func (bs Bindings) DeleteExcept(keeps ...string) Bindings

DeleteExcept removes all but the given properties.

Does not copy.

This method is handy in some actions.

func (Bindings) Extend

func (bs Bindings) Extend(p string, v interface{}) Bindings

Extends adds the property; modifies and returns the Bindings.

The Bindings are modified.

func (Bindings) Extendm

func (bs Bindings) Extendm(pairs ...interface{}) (Bindings, error)

Extends adds the properties; modifies and returns the Bindings.

The Bindings are modified.

func (Bindings) Remove

func (bs Bindings) Remove(ps ...string) Bindings

Remove removes the given keys.

The Bindings are modified.

type Matcher

type Matcher struct {
	// AllowPropertyVariables enables the experimental support for
	// a single property variable in a map pattern that has no
	// other properties.
	AllowPropertyVariables bool

	// CheckForBadPropertyVariables runs a test to verify that a
	// pattern does not contain a property variable along with
	// other properties.
	//
	// This check might not be necessary because the other code
	// will report an error if a bad property variable is actually
	// enountered during matching.  The interesting twist is that
	// if a match fails before encountering the bad property
	// variable, then that code will not report the problem.  In
	// order to report the problem consistently, turn on this
	// switch.  Performance will suffer, but any bad property
	// variable will be caught.
	CheckForBadPropertyVariables bool

	// Inequalities is a switch to turn on experimental binding
	// inequality support.
	//
	// With this feature, pattern matching supports numeric
	// inequalities in addition to the standard equality
	// predicate.  The input bindings should include a binding for
	// a variable with a name that contains either "<", ">", "<=",
	// ">=", or "!=" immediately after the leading "?".  The input
	// pattern can then use that variable.  When matching, a value
	// X will match that variable only if the binding Y for that
	// variable satisfies the inequality with X and Y (in that
	// order). In this case, the output bindings will include a
	// new binding for a variable with the same name as the
	// inequality variable but without the actual inequality.
	//
	// For example, given input bindings {"?<n":10}, pattern
	// {"n":"?<n"}, and message {"n":3}, the match will succeed
	// with bindings {"?<n":10,"?n":3}.
	//
	// See match_test.json for several examples. (Search for
	// "inequality".)
	//
	// For now at least, the inequalities only work for numeric
	// values.  We might support strings later.
	//
	// Yes, such a feature makes us stare down a slippery slope.
	// However, we are brave, and we do not shy away from even
	// grave danger.
	//
	//   "[In Sheens] we live exactly as we please, and yet are
	//   just as ready to encounter every legitimate danger."
	//
	//   --Pericles
	//
	// The immediate motivation for this feature was to support
	// timer fallbacks.  For example, using the Goja interpreter,
	// an action could establish a binding with a value that's a
	// number representing a future time in UNIX milliseconds.
	// Then a branch pattern can check for a message containing
	// the current time which is greater than that number.  When a
	// machine is loaded, a system could send the machine a
	// "current time" message, which could advance the machine
	// according to such a branch.  Using this technique, a
	// machine that creates a timer that somehow gets lots could
	// regain at least some sense of what's going on.  Similarly,
	// a system that doesn't have asynchronous, timer-driven
	// messaging support could simply send messages every second
	// (or at whatever internal) to provide timer-like
	// functionality (albeit with inefficiencies and without the
	// message-oriented timer protocol that's been offered
	// elsewhere).
	Inequalities bool
}

func (*Matcher) IsAnonymousVariable

func (m *Matcher) IsAnonymousVariable(s string) bool

IsAnonymousVariable detects a variable of the form '?'. An binding for an anonymous variable shouldn't ever make it into bindins.

func (*Matcher) IsConstant

func (m *Matcher) IsConstant(s string) bool

IsConstant reports if the string represents a constant (and not a pattern variable).

func (*Matcher) IsOptionalVariable

func (m *Matcher) IsOptionalVariable(x interface{}) bool

IsOptionalVariable reports whether the value is a variable that starts with two "?" instead of a single "?". Example: "??x".

func (*Matcher) IsVariable

func (m *Matcher) IsVariable(s string) bool

IsVariable reports if the string represents a pattern variable.

All pattern variables start with a '?".

func (*Matcher) Match

func (m *Matcher) Match(pattern interface{}, fact interface{}, bindings Bindings) ([]Bindings, error)

Match is a verion of 'Matches' that takes initial bindings.

Those initial bindings are not modified.

func (*Matcher) Matches

func (m *Matcher) Matches(pattern interface{}, fact interface{}) ([]Bindings, error)

Matches attempts to match the given fact with the given pattern. Returns an array of 'Bindings'. Each Bindings is just a map from variables to their values.

Note that this function returns multiple (sets of) bindings. This possibility is introduced when a pattern contains an array that contains a variable.

type UnknownPatternType

type UnknownPatternType struct {
	Pattern interface{}
}

UnknownPatternType is an error that includes the thing that's causing the trouble.

func (*UnknownPatternType) Error

func (e *UnknownPatternType) Error() string

Jump to

Keyboard shortcuts

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