expression#

class TokenType(*values)#

Bases: Enum

LPAREN = 'left parenthesis'#
RPAREN = 'right parenthesis'#
OR = 'or'#
AND = 'and'#
NOT = 'not'#
WILDCARD = 'wildcard'#
IDENT = 'identifier'#
EOF = 'end of input'#
class Token(type: _canary.expression.TokenType, value: str, pos: int)#

Bases: object

type: TokenType#
value: str#
pos: int#
exception ParseError(column: int, message: str)#

Bases: Exception

The expression contains invalid syntax.

Parameters:
  • column – The column in the line where the error occurred (1-based).

  • message – A description of the error.

class Scanner(input: str)#

Bases: object

ident_regex = '(:?\\w|:|\\+|-|\\.|\\[|\\]|\\\\|/)+'#
tokens#
current#
lex(input: str) Iterator[Token]#
accept(type: TokenType, *, reject: bool = False) Token | None#
reject(expected: Sequence[TokenType]) NoReturn#
class WildcardScanner(input: str)#

Bases: Scanner

ident_regex = '(:?\\w|:|\\*|\\?|\\+|-|\\.|\\[|\\]|\\\\|/)+'#
tokens#
current#
expression(s: Scanner) Expression#
expr(s: Scanner) expr#
and_expr(s: Scanner) expr#
not_expr(s: Scanner) expr#
class MatcherAdapter(matcher: Callable[[str], bool])#

Bases: Mapping[str, bool]

Adapts a matcher function to a locals mapping as required by eval().

class Expression(code: CodeType, string: str)#

Bases: object

Evaluate match expressions, as used by -k and -m.

The grammar is:

expression: expr? EOF
expr:       and_expr ('or' and_expr)*
and_expr:   not_expr ('and' not_expr)*
not_expr:   'not' not_expr | '(' expr ')' | ident
ident:      (\w|:|\+|-|\.|\[|\]|\\|/)+

The semantics are:

  • Empty expression evaluates to False.

  • ident evaluates to True of False according to a provided matcher function.

  • or/and/not evaluate according to the usual boolean semantics.

The expression can be evaluated against different matchers.

code#
string#
classmethod compile(input: str, allow_wildcards: bool = False) Expression#

Compile a match expression.

Parameters:

input – The input expression - one line.

evaluate(matcher: Callable[[str], bool]) bool#

Evaluate the match expression.

Parameters:

matcher – Given an identifier, should return whether it matches or not. Should be prepared to handle arbitrary strings as input.

Returns:

Whether the expression matches or not.

class ParameterExpression(string: str)#

Bases: object

Evaluate match expressions, as used by -p

The grammar is:

expression: expr? EOF expr: ident ident: ([a-zA-Z_]+\w(=|>=|>|<|<=|==|!=).*

The semantics are:

  • Empty expression evaluates to False.

  • ident evaluates to True of False according to a provided matcher function.

  • or/and/not evaluate according to the usual boolean semantics.

static parse_expr(expr: str) str#
evaluate(parameters: dict[str, Any]) bool#
class NotDefined(names: list[str])#

Bases: object

class Defined(names: list[str])#

Bases: object

get_tokens(code)#