You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

222 lines
6.2KB

  1. r"""Evaluate match expressions, as used by `-k` and `-m`.
  2. The grammar is:
  3. expression: expr? EOF
  4. expr: and_expr ('or' and_expr)*
  5. and_expr: not_expr ('and' not_expr)*
  6. not_expr: 'not' not_expr | '(' expr ')' | ident
  7. ident: (\w|:|\+|-|\.|\[|\])+
  8. The semantics are:
  9. - Empty expression evaluates to False.
  10. - ident evaluates to True of False according to a provided matcher function.
  11. - or/and/not evaluate according to the usual boolean semantics.
  12. """
  13. import ast
  14. import enum
  15. import re
  16. import types
  17. from typing import Callable
  18. from typing import Iterator
  19. from typing import Mapping
  20. from typing import Optional
  21. from typing import Sequence
  22. from typing import TYPE_CHECKING
  23. import attr
  24. if TYPE_CHECKING:
  25. from typing import NoReturn
  26. __all__ = [
  27. "Expression",
  28. "ParseError",
  29. ]
  30. class TokenType(enum.Enum):
  31. LPAREN = "left parenthesis"
  32. RPAREN = "right parenthesis"
  33. OR = "or"
  34. AND = "and"
  35. NOT = "not"
  36. IDENT = "identifier"
  37. EOF = "end of input"
  38. @attr.s(frozen=True, slots=True)
  39. class Token:
  40. type = attr.ib(type=TokenType)
  41. value = attr.ib(type=str)
  42. pos = attr.ib(type=int)
  43. class ParseError(Exception):
  44. """The expression contains invalid syntax.
  45. :param column: The column in the line where the error occurred (1-based).
  46. :param message: A description of the error.
  47. """
  48. def __init__(self, column: int, message: str) -> None:
  49. self.column = column
  50. self.message = message
  51. def __str__(self) -> str:
  52. return f"at column {self.column}: {self.message}"
  53. class Scanner:
  54. __slots__ = ("tokens", "current")
  55. def __init__(self, input: str) -> None:
  56. self.tokens = self.lex(input)
  57. self.current = next(self.tokens)
  58. def lex(self, input: str) -> Iterator[Token]:
  59. pos = 0
  60. while pos < len(input):
  61. if input[pos] in (" ", "\t"):
  62. pos += 1
  63. elif input[pos] == "(":
  64. yield Token(TokenType.LPAREN, "(", pos)
  65. pos += 1
  66. elif input[pos] == ")":
  67. yield Token(TokenType.RPAREN, ")", pos)
  68. pos += 1
  69. else:
  70. match = re.match(r"(:?\w|:|\+|-|\.|\[|\])+", input[pos:])
  71. if match:
  72. value = match.group(0)
  73. if value == "or":
  74. yield Token(TokenType.OR, value, pos)
  75. elif value == "and":
  76. yield Token(TokenType.AND, value, pos)
  77. elif value == "not":
  78. yield Token(TokenType.NOT, value, pos)
  79. else:
  80. yield Token(TokenType.IDENT, value, pos)
  81. pos += len(value)
  82. else:
  83. raise ParseError(
  84. pos + 1, 'unexpected character "{}"'.format(input[pos]),
  85. )
  86. yield Token(TokenType.EOF, "", pos)
  87. def accept(self, type: TokenType, *, reject: bool = False) -> Optional[Token]:
  88. if self.current.type is type:
  89. token = self.current
  90. if token.type is not TokenType.EOF:
  91. self.current = next(self.tokens)
  92. return token
  93. if reject:
  94. self.reject((type,))
  95. return None
  96. def reject(self, expected: Sequence[TokenType]) -> "NoReturn":
  97. raise ParseError(
  98. self.current.pos + 1,
  99. "expected {}; got {}".format(
  100. " OR ".join(type.value for type in expected), self.current.type.value,
  101. ),
  102. )
  103. # True, False and None are legal match expression identifiers,
  104. # but illegal as Python identifiers. To fix this, this prefix
  105. # is added to identifiers in the conversion to Python AST.
  106. IDENT_PREFIX = "$"
  107. def expression(s: Scanner) -> ast.Expression:
  108. if s.accept(TokenType.EOF):
  109. ret: ast.expr = ast.NameConstant(False)
  110. else:
  111. ret = expr(s)
  112. s.accept(TokenType.EOF, reject=True)
  113. return ast.fix_missing_locations(ast.Expression(ret))
  114. def expr(s: Scanner) -> ast.expr:
  115. ret = and_expr(s)
  116. while s.accept(TokenType.OR):
  117. rhs = and_expr(s)
  118. ret = ast.BoolOp(ast.Or(), [ret, rhs])
  119. return ret
  120. def and_expr(s: Scanner) -> ast.expr:
  121. ret = not_expr(s)
  122. while s.accept(TokenType.AND):
  123. rhs = not_expr(s)
  124. ret = ast.BoolOp(ast.And(), [ret, rhs])
  125. return ret
  126. def not_expr(s: Scanner) -> ast.expr:
  127. if s.accept(TokenType.NOT):
  128. return ast.UnaryOp(ast.Not(), not_expr(s))
  129. if s.accept(TokenType.LPAREN):
  130. ret = expr(s)
  131. s.accept(TokenType.RPAREN, reject=True)
  132. return ret
  133. ident = s.accept(TokenType.IDENT)
  134. if ident:
  135. return ast.Name(IDENT_PREFIX + ident.value, ast.Load())
  136. s.reject((TokenType.NOT, TokenType.LPAREN, TokenType.IDENT))
  137. class MatcherAdapter(Mapping[str, bool]):
  138. """Adapts a matcher function to a locals mapping as required by eval()."""
  139. def __init__(self, matcher: Callable[[str], bool]) -> None:
  140. self.matcher = matcher
  141. def __getitem__(self, key: str) -> bool:
  142. return self.matcher(key[len(IDENT_PREFIX) :])
  143. def __iter__(self) -> Iterator[str]:
  144. raise NotImplementedError()
  145. def __len__(self) -> int:
  146. raise NotImplementedError()
  147. class Expression:
  148. """A compiled match expression as used by -k and -m.
  149. The expression can be evaulated against different matchers.
  150. """
  151. __slots__ = ("code",)
  152. def __init__(self, code: types.CodeType) -> None:
  153. self.code = code
  154. @classmethod
  155. def compile(self, input: str) -> "Expression":
  156. """Compile a match expression.
  157. :param input: The input expression - one line.
  158. """
  159. astexpr = expression(Scanner(input))
  160. code: types.CodeType = compile(
  161. astexpr, filename="<pytest match expression>", mode="eval",
  162. )
  163. return Expression(code)
  164. def evaluate(self, matcher: Callable[[str], bool]) -> bool:
  165. """Evaluate the match expression.
  166. :param matcher:
  167. Given an identifier, should return whether it matches or not.
  168. Should be prepared to handle arbitrary strings as input.
  169. :returns: Whether the expression matches or not.
  170. """
  171. ret: bool = eval(self.code, {"__builtins__": {}}, MatcherAdapter(matcher))
  172. return ret