-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatch.py
executable file
·163 lines (147 loc) · 5.71 KB
/
match.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# ExpressionMatcher - check if a collection matches a boolean expression
#
# SPDX-FileCopyrightText: © 2024 Emanuele Aina
#
# SPDX-License-Identifier: MIT
import ast
import sys
class ExpressionMatcher:
class Transformer(ast.NodeTransformer):
def __init__(self, expression: str | None) -> None:
self.expression = expression
def error(self, msg: str, lineno: int, col_offset: int):
raise SyntaxError(
msg,
(None, lineno, col_offset + 1, self.expression),
)
def generic_visit(self, node: ast.AST) -> ast.AST:
items_var = "items"
match node:
case ast.Name(id=id, ctx=ast.Load()):
return ast.Compare(
left=ast.Constant(value=id),
ops=[ast.In()],
comparators=[ast.Name(id=items_var, ctx=ast.Load())],
)
case ast.Constant(value=value):
return ast.Compare(
left=ast.Constant(value=str(value)),
ops=[ast.In()],
comparators=[ast.Name(id=items_var, ctx=ast.Load())],
)
case ast.Call(func=ast.Name(id="empty"), args=[], keywords=[]):
return ast.UnaryOp(
op=ast.Not(),
operand=ast.Name(id=items_var, ctx=ast.Load()),
)
case ast.Call(func=ast.Name(id="anything"), args=[], keywords=[]):
return ast.Constant(value=True)
case (
ast.Expression()
| ast.BoolOp(op=ast.Or() | ast.And())
| ast.Or()
| ast.And()
| ast.UnaryOp(op=ast.Not())
| ast.Not()
):
pass
case ast.Call(func=ast.Name(id="empty" | "anything" as func)):
# point the error to the argument list
self.error(
f"invalid syntax, {func}() does not accept any argument",
node.func.end_lineno or node.lineno,
node.func.end_col_offset or node.col_offset,
)
case ast.Call(func=ast.Name()):
self.error(
"invalid syntax, unknown function",
node.lineno,
node.col_offset,
)
case ast.BinOp(left=ast.Name()):
# point the error to the operator
self.error(
"invalid syntax, unsupported operation",
node.left.end_lineno or node.lineno,
node.left.end_col_offset or node.col_offset,
)
case _:
self.error("invalid syntax", node.lineno, node.col_offset + 1)
return super().generic_visit(node)
def __init__(self, expression: str | None) -> None:
"""Inizialize the ExpressionMatcher
>>> ast.unparse(ExpressionMatcher("foo and bar").ast)
"'foo' in items and 'bar' in items"
>>> ast.unparse(ExpressionMatcher("foo or empty()").ast)
"'foo' in items or not items"
>>> ast.unparse(ExpressionMatcher(None).ast)
'True'
>>> ExpressionMatcher("foo + 1")
Traceback (most recent call last):
...
File "<string>", line 1
foo + 1
^
SyntaxError: invalid syntax, unsupported operation
>>> ExpressionMatcher("foo()")
Traceback (most recent call last):
...
File "<string>", line 1
foo()
^
SyntaxError: invalid syntax, unknown function
>>> ExpressionMatcher("empty(1)")
Traceback (most recent call last):
...
File "<string>", line 1
empty(1)
^
SyntaxError: invalid syntax, empty() does not accept any argument
"""
if not expression:
expression = "anything()"
parsed = ast.parse(expression, mode="eval")
transformed = self.Transformer(expression).visit(parsed)
ast.fix_missing_locations(transformed)
compiled = compile(transformed, filename="<ast>", mode="eval")
def matches(items):
match = eval(compiled, {}, {"items": items})
return match
self.expression = expression
self.ast = transformed
self.matches = matches
def __call__(self, items) -> bool:
"""Test if the expression matches when applied on the `items` collection
>>> matches = ExpressionMatcher("foo and bar")
>>> matches([])
False
>>> matches({"foo"})
False
>>> matches(["foo", "bar"])
True
>>> matches("foobarbaz")
True
>>> matches = ExpressionMatcher("foo or empty()")
>>> matches({"foo"})
True
>>> matches([])
True
>>> matches(["bar"])
False
"""
return self.matches(items)
def __repr__(self) -> str:
"""String representation
>>> repr(ExpressionMatcher("foo and bar"))
"ExpressionMatcher('foo and bar')"
>>> repr(ExpressionMatcher(""))
"ExpressionMatcher('anything()')"
>>> repr(ExpressionMatcher(None))
"ExpressionMatcher('anything()')"
"""
return f"{self.__class__.__qualname__}('{self.expression}')"
if __name__ == "__main__":
matches = ExpressionMatcher(sys.argv[1])
is_match = matches(sys.argv[2:])
print("✅" if is_match else "❌")
sys.exit(0 if is_match else 1)