Debian Codegraph @

thirdparty/folly/build/fbcode_builder/getdeps/expr.py

raw
1 # Copyright (c) Meta Platforms, Inc. and affiliates.
2 #
3 # This source code is licensed under the MIT license found in the
4 # LICENSE file in the root directory of this source tree.
5
6 import re
7 import shlex
8
9
10 def parse_expr(expr_text, valid_variables):
11 """parses the simple criteria expression syntax used in
12 dependency specifications.
13 Returns an ExprNode instance that can be evaluated like this:
14
15 ```
16 expr = parse_expr("os=windows")
17 ok = expr.eval({
18 "os": "windows"
19 })
20 ```
21
22 Whitespace is allowed between tokens. The following terms
23 are recognized:
24
25 KEY = VALUE # Evaluates to True if ctx[KEY] == VALUE
26 not(EXPR) # Evaluates to True if EXPR evaluates to False
27 # and vice versa
28 all(EXPR1, EXPR2, ...) # Evaluates True if all of the supplied
29 # EXPR's also evaluate True
30 any(EXPR1, EXPR2, ...) # Evaluates True if any of the supplied
31 # EXPR's also evaluate True, False if
32 # none of them evaluated true.
33 """
34
35 p = Parser(expr_text, valid_variables)
36 return p.parse()
37
38
39 class ExprNode(object):
40 def eval(self, ctx) -> bool:
41 return False
42
43
44 class TrueExpr(ExprNode):
45 def eval(self, ctx) -> bool:
46 return True
47
48 def __str__(self) -> str:
49 return "true"
50
51
52 class NotExpr(ExprNode):
53 def __init__(self, node) -> None:
54 self._node = node
55
56 def eval(self, ctx) -> bool:
57 return not self._node.eval(ctx)
58
59 def __str__(self) -> str:
60 return "not(%s)" % self._node
61
62
63 class AllExpr(ExprNode):
64 def __init__(self, nodes) -> None:
65 self._nodes = nodes
66
67 def eval(self, ctx) -> bool:
68 for node in self._nodes:
69 if not node.eval(ctx):
70 return False
71 return True
72
73 def __str__(self) -> str:
74 items = []
75 for node in self._nodes:
76 items.append(str(node))
77 return "all(%s)" % ",".join(items)
78
79
80 class AnyExpr(ExprNode):
81 def __init__(self, nodes) -> None:
82 self._nodes = nodes
83
84 def eval(self, ctx) -> bool:
85 for node in self._nodes:
86 if node.eval(ctx):
87 return True
88 return False
89
90 def __str__(self) -> str:
91 items = []
92 for node in self._nodes:
93 items.append(str(node))
94 return "any(%s)" % ",".join(items)
95
96
97 class EqualExpr(ExprNode):
98 def __init__(self, key, value) -> None:
99 self._key = key
100 self._value = value
101
102 def eval(self, ctx):
103 return ctx.get(self._key) == self._value
104
105 def __str__(self) -> str:
106 return "%s=%s" % (self._key, self._value)
107
108
109 class Parser(object):
110 def __init__(self, text, valid_variables) -> None:
111 self.text = text
112 self.lex = shlex.shlex(text)
113 self.valid_variables = valid_variables
114
115 def parse(self):
116 expr = self.top()
117 garbage = self.lex.get_token()
118 if garbage != "":
119 raise Exception(
120 "Unexpected token %s after EqualExpr in %s" % (garbage, self.text)
121 )
122 return expr
123
124 def top(self):
125 name = self.ident()
126 op = self.lex.get_token()
127
128 if op == "(":
129 parsers = {
130 "not": self.parse_not,
131 "any": self.parse_any,
132 "all": self.parse_all,
133 }
134 func = parsers.get(name)
135 if not func:
136 raise Exception("invalid term %s in %s" % (name, self.text))
137 return func()
138
139 if op == "=":
140 if name not in self.valid_variables:
141 raise Exception("unknown variable %r in expression" % (name,))
142 # remove shell quote from value so can test things with period in them, e.g "18.04"
143 unquoted = " ".join(shlex.split(self.lex.get_token()))
144 return EqualExpr(name, unquoted)
145
146 raise Exception(
147 "Unexpected token sequence '%s %s' in %s" % (name, op, self.text)
148 )
149
150 def ident(self) -> str:
151 ident = self.lex.get_token()
152 if not re.match("[a-zA-Z]+", ident):
153 raise Exception("expected identifier found %s" % ident)
154 return ident
155
156 def parse_not(self) -> NotExpr:
157 node = self.top()
158 expr = NotExpr(node)
159 tok = self.lex.get_token()
160 if tok != ")":
161 raise Exception("expected ')' found %s" % tok)
162 return expr
163
164 def parse_any(self) -> AnyExpr:
165 nodes = []
166 while True:
167 nodes.append(self.top())
168 tok = self.lex.get_token()
169 if tok == ")":
170 break
171 if tok != ",":
172 raise Exception("expected ',' or ')' but found %s" % tok)
173 return AnyExpr(nodes)
174
175 def parse_all(self) -> AllExpr:
176 nodes = []
177 while True:
178 nodes.append(self.top())
179 tok = self.lex.get_token()
180 if tok == ")":
181 break
182 if tok != ",":
183 raise Exception("expected ',' or ')' but found %s" % tok)
184 return AllExpr(nodes)