beanmachine.ppl.compiler.patterns module

A pattern matching engine

class beanmachine.ppl.compiler.patterns.AnyPattern

Bases: beanmachine.ppl.compiler.patterns.PatternBase

The pattern that matches anything; it always succeeds.

match(test: Any) beanmachine.ppl.compiler.patterns.MatchResult
class beanmachine.ppl.compiler.patterns.AtomicPattern(value: Optional[Union[bool, int, float, str]])

Bases: beanmachine.ppl.compiler.patterns.PatternBase

An atomic pattern matches against a single specific value, such as a specific integer, Boolean, string, and so on.

match(test: Any) beanmachine.ppl.compiler.patterns.MatchResult
value: Optional[Union[bool, int, float, str]]
class beanmachine.ppl.compiler.patterns.AttributeSubpattern(name: str, subpattern: Optional[Union[beanmachine.ppl.compiler.patterns.PatternBase, int, str, float, type, list]])

Bases: beanmachine.ppl.compiler.patterns.PatternBase

Sometimes we want to check to see if an attribute of a value matches a pattern. This class represents such patterns. It takes a subpattern and an attribute name. When match is called, it runs the subpattern on the attribute of the given value.

match(test: Any) beanmachine.ppl.compiler.patterns.MatchResult
name: str
subpattern: Optional[Union[beanmachine.ppl.compiler.patterns.PatternBase, int, str, float, type, list]]
class beanmachine.ppl.compiler.patterns.BoolPattern(value: bool)

Bases: beanmachine.ppl.compiler.patterns.AtomicPattern

The pattern that matches a specific Boolean value.

value: Optional[Union[bool, int, float, str]]
class beanmachine.ppl.compiler.patterns.EmptyListPattern(name: str = 'empty_list')

Bases: beanmachine.ppl.compiler.patterns.PatternBase

This pattern matches an empty list.

match(test: Any) beanmachine.ppl.compiler.patterns.MatchResult
name: str
class beanmachine.ppl.compiler.patterns.Fail(test: Optional[Any] = None, submatches: Optional[Dict[str, beanmachine.ppl.compiler.patterns.MatchResult]] = None)

Bases: beanmachine.ppl.compiler.patterns.MatchResult

A pattern that always fails.

is_fail() bool
is_success() bool
submatches: Dict[str, beanmachine.ppl.compiler.patterns.MatchResult]
test: Any
class beanmachine.ppl.compiler.patterns.FailPattern

Bases: beanmachine.ppl.compiler.patterns.PatternBase

The pattern that matches nothing; it always fails.

match(test: Any) beanmachine.ppl.compiler.patterns.MatchResult
class beanmachine.ppl.compiler.patterns.FloatPattern(value: float)

Bases: beanmachine.ppl.compiler.patterns.AtomicPattern

The pattern that matches a specific float value.

value: Optional[Union[bool, int, float, str]]
class beanmachine.ppl.compiler.patterns.HeadTail(head: Optional[Union[beanmachine.ppl.compiler.patterns.PatternBase, int, str, float, type, list]] = <beanmachine.ppl.compiler.patterns.AnyPattern object>, tail: Optional[Union[beanmachine.ppl.compiler.patterns.PatternBase, int, str, float, type, list]] = <beanmachine.ppl.compiler.patterns.AnyPattern object>, name: str = 'head_tail')

Bases: beanmachine.ppl.compiler.patterns.PatternBase

This combinator takes a pattern to match the head of a list and a pattern to match the tail. If the list is empty, it automatically fails; otherwise both patterns must match. The tail pattern is not attempted if the head pattern fails.

head: Optional[Union[beanmachine.ppl.compiler.patterns.PatternBase, int, str, float, type, list]]
match(test: Any) beanmachine.ppl.compiler.patterns.MatchResult
name: str
tail: Optional[Union[beanmachine.ppl.compiler.patterns.PatternBase, int, str, float, type, list]]
class beanmachine.ppl.compiler.patterns.IntPattern(value: int)

Bases: beanmachine.ppl.compiler.patterns.AtomicPattern

The pattern that matches a specific integer value.

value: Optional[Union[bool, int, float, str]]
class beanmachine.ppl.compiler.patterns.ListAll(pattern: Optional[Union[beanmachine.ppl.compiler.patterns.PatternBase, int, str, float, type, list]], name: str = 'list_all')

Bases: beanmachine.ppl.compiler.patterns.PatternBase

Matches a list where all elements of the list match the pattern.

match(test: Any) beanmachine.ppl.compiler.patterns.MatchResult
name: str
pattern: Optional[Union[beanmachine.ppl.compiler.patterns.PatternBase, int, str, float, type, list]]
class beanmachine.ppl.compiler.patterns.ListAny(pattern: Optional[Union[beanmachine.ppl.compiler.patterns.PatternBase, int, str, float, type, list]], name: str = 'list_any')

Bases: beanmachine.ppl.compiler.patterns.PatternBase

Matches a list where one or more elements of the list match the pattern.

match(test: Any) beanmachine.ppl.compiler.patterns.MatchResult
name: str
pattern: Optional[Union[beanmachine.ppl.compiler.patterns.PatternBase, int, str, float, type, list]]
class beanmachine.ppl.compiler.patterns.ListPattern(patterns: List[Optional[Union[beanmachine.ppl.compiler.patterns.PatternBase, int, str, float, type, list]]], name: str = 'list_pattern')

Bases: beanmachine.ppl.compiler.patterns.PatternBase

This pattern matches a list of patterns to a list.

match(test: Any) beanmachine.ppl.compiler.patterns.MatchResult
name: str
patterns: List[Optional[Union[beanmachine.ppl.compiler.patterns.PatternBase, int, str, float, type, list]]]
class beanmachine.ppl.compiler.patterns.MatchAny(*patterns: Optional[Union[beanmachine.ppl.compiler.patterns.PatternBase, int, str, float, type, list]])

Bases: beanmachine.ppl.compiler.patterns.PatternBase

The pattern that succeeds if any pattern in the list succeeds. It will stop trying to match patterns after the first success. If there are no patterns in the list then it fails.

match(test: Any) beanmachine.ppl.compiler.patterns.MatchResult
patterns: List[Optional[Union[beanmachine.ppl.compiler.patterns.PatternBase, int, str, float, type, list]]]
class beanmachine.ppl.compiler.patterns.MatchEvery(*patterns: Optional[Union[beanmachine.ppl.compiler.patterns.PatternBase, int, str, float, type, list]])

Bases: beanmachine.ppl.compiler.patterns.PatternBase

The pattern that succeeds if every pattern in the list succeeds. It will stop trying to match patterns after the first failure. If there are no patterns in the list then it succeeds.

match(test: Any) beanmachine.ppl.compiler.patterns.MatchResult
patterns: List[Optional[Union[beanmachine.ppl.compiler.patterns.PatternBase, int, str, float, type, list]]]
class beanmachine.ppl.compiler.patterns.MatchResult(test: Any, submatches: Optional[Dict[str, beanmachine.ppl.compiler.patterns.MatchResult]] = None)

Bases: abc.ABC

The result of a pattern match; either success or failure.

abstract is_fail() bool
abstract is_success() bool
submatches: Dict[str, beanmachine.ppl.compiler.patterns.MatchResult]
test: Any
class beanmachine.ppl.compiler.patterns.Negate(pattern: Optional[Union[beanmachine.ppl.compiler.patterns.PatternBase, int, str, float, type, list]])

Bases: beanmachine.ppl.compiler.patterns.PatternBase

Negates a pattern; if the underlying pattern succeeds, this fails, and vice versa.

match(test: Any) beanmachine.ppl.compiler.patterns.MatchResult
pattern: Optional[Union[beanmachine.ppl.compiler.patterns.PatternBase, int, str, float, type, list]]
class beanmachine.ppl.compiler.patterns.NonePattern

Bases: beanmachine.ppl.compiler.patterns.AtomicPattern

The pattern that matches None.

value: Optional[Union[bool, int, float, str]]
class beanmachine.ppl.compiler.patterns.PatternBase

Bases: abc.ABC

abstract match(test: Any) beanmachine.ppl.compiler.patterns.MatchResult
class beanmachine.ppl.compiler.patterns.PredicatePattern(predicate: Callable[[Any], bool], name: str = 'if')

Bases: beanmachine.ppl.compiler.patterns.PatternBase

A pattern is logically a predicate; this pattern just encapsulates any predicate that returns a match result.

match(test: Any) beanmachine.ppl.compiler.patterns.MatchResult
name: str
predicate: Callable[[Any], bool]
class beanmachine.ppl.compiler.patterns.StringPattern(value: str)

Bases: beanmachine.ppl.compiler.patterns.AtomicPattern

The pattern that matches a specific string value.

value: Optional[Union[bool, int, float, str]]
class beanmachine.ppl.compiler.patterns.Subpattern(name: str, subpattern: Optional[Union[beanmachine.ppl.compiler.patterns.PatternBase, int, str, float, type, list]], get_subtest: Callable[[Any], Any])

Bases: beanmachine.ppl.compiler.patterns.PatternBase

Sometimes we want to check to see if a given value matches the pattern whereby some projected value matches a pattern. This class represents such patterns. It takes a subpattern and a projection; when match is called, it projects the value and runs the subpattern on the projected value.

get_subtest: Callable[[Any], Any]
match(test: Any) beanmachine.ppl.compiler.patterns.MatchResult
name: str
subpattern: Optional[Union[beanmachine.ppl.compiler.patterns.PatternBase, int, str, float, type, list]]
class beanmachine.ppl.compiler.patterns.Success(test: Optional[Any] = None, submatches: Optional[Dict[str, beanmachine.ppl.compiler.patterns.MatchResult]] = None)

Bases: beanmachine.ppl.compiler.patterns.MatchResult

A pattern that always succeeds.

is_fail() bool
is_success() bool
submatches: Dict[str, beanmachine.ppl.compiler.patterns.MatchResult]
test: Any
class beanmachine.ppl.compiler.patterns.TypePattern(typ: type)

Bases: beanmachine.ppl.compiler.patterns.PatternBase

The pattern that matches if the value is an instance of the given type.

match(test: Any) beanmachine.ppl.compiler.patterns.MatchResult
typ: type
beanmachine.ppl.compiler.patterns.attribute(name: str, subpattern: Optional[Union[beanmachine.ppl.compiler.patterns.PatternBase, int, str, float, type, list]]) Optional[Union[beanmachine.ppl.compiler.patterns.PatternBase, int, str, float, type, list]]
beanmachine.ppl.compiler.patterns.is_any(pattern: Optional[Union[beanmachine.ppl.compiler.patterns.PatternBase, int, str, float, type, list]]) bool
beanmachine.ppl.compiler.patterns.match(pattern: Optional[Union[beanmachine.ppl.compiler.patterns.PatternBase, int, str, float, type, list]], test: Any) beanmachine.ppl.compiler.patterns.MatchResult
beanmachine.ppl.compiler.patterns.match_any(*patterns: Optional[Union[beanmachine.ppl.compiler.patterns.PatternBase, int, str, float, type, list]]) Optional[Union[beanmachine.ppl.compiler.patterns.PatternBase, int, str, float, type, list]]
beanmachine.ppl.compiler.patterns.match_every(*patterns: Optional[Union[beanmachine.ppl.compiler.patterns.PatternBase, int, str, float, type, list]]) Optional[Union[beanmachine.ppl.compiler.patterns.PatternBase, int, str, float, type, list]]
beanmachine.ppl.compiler.patterns.negate(pattern: Optional[Union[beanmachine.ppl.compiler.patterns.PatternBase, int, str, float, type, list]]) Optional[Union[beanmachine.ppl.compiler.patterns.PatternBase, int, str, float, type, list]]

Produces the negation of a given pattern.

beanmachine.ppl.compiler.patterns.to_pattern(pattern: Optional[Union[beanmachine.ppl.compiler.patterns.PatternBase, int, str, float, type, list]]) beanmachine.ppl.compiler.patterns.PatternBase

Takes any value that can be used as a pattern, and returns an object that derives from PatternBase that has the same semantics.

beanmachine.ppl.compiler.patterns.type_and_attributes(typ: type, patterns: Dict[str, Optional[Union[beanmachine.ppl.compiler.patterns.PatternBase, int, str, float, type, list]]]) Optional[Union[beanmachine.ppl.compiler.patterns.PatternBase, int, str, float, type, list]]