annotate COBRAxy/utils/rule_parsing.py @ 7:515cd9e4f518 draft

Uploaded
author luca_milaz
date Wed, 18 Sep 2024 12:09:06 +0000
parents 41f35c2f0c7b
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
4
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
1 from enum import Enum
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
2 import utils.general_utils as utils
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
3 from typing import List, Union, Optional
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
4
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
5 class RuleErr(utils.CustomErr):
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
6 """
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
7 CustomErr subclass for rule syntax errors.
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
8 """
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
9 errName = "Rule Syntax Error"
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
10 def __init__(self, rule :str, msg = "no further details provided") -> None:
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
11 super().__init__(
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
12 f"rule \"{rule}\" is malformed, {msg}",
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
13 "please verify your input follows the validity guidelines")
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
14
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
15 class RuleOp(Enum):
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
16 """
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
17 Encodes all operators valid in gene rules.
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
18 """
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
19 OR = "or"
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
20 AND = "and"
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
21
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
22 @classmethod
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
23 def isOperator(cls, op :str) -> bool:
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
24 return op.upper() in cls.__members__
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
25
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
26 def __str__(self) -> str: return self.value
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
27
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
28 class OpList(List[Union[str, "OpList"]]):
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
29 """
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
30 Represents a parsed rule and each of its nesting levels, including the operator that level uses.
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
31 """
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
32 def __init__(self, op :Optional[RuleOp] = None) -> None:
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
33 """
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
34 (Private) Initializes an instance of OpList.
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
35
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
36 Args:
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
37 op (str): Operator to be assigned to the OpList. Defaults to "".
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
38
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
39 Returns:
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
40 None : practically, an OpList instance.
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
41 """
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
42 self.op = op
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
43
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
44 def setOpIfMissing(self, op :RuleOp) -> None:
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
45 """
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
46 Sets the operator of the OpList if it's missing.
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
47
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
48 Args:
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
49 op (str): Operator to be assigned to the OpList.
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
50
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
51 Returns:
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
52 None
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
53 """
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
54 if not self.op: self.op = op
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
55
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
56 def __repr__(self, indent = "") -> str:
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
57 """
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
58 (Private) Returns a string representation of the current OpList instance.
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
59
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
60 Args:
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
61 indent (str): Indentation level . Defaults to "".
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
62
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
63 Returns:
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
64 str: A string representation of the current OpList instance.
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
65 """
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
66 nextIndent = indent + " "
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
67 return f"<{self.op}>[\n" + ",\n".join([
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
68 f"{nextIndent}{item.__repr__(nextIndent) if isinstance(item, OpList) else item}"
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
69 for item in self ]) + f"\n{indent}]"
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
70
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
71 class RuleStack:
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
72 """
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
73 FILO stack structure to save the intermediate representation of a Rule during parsing, with the
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
74 current nesting level at the top of the stack.
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
75 """
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
76 def __init__(self) -> None:
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
77 """
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
78 (Private) initializes an instance of RuleStack.
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
79
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
80 Returns:
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
81 None : practically, a RuleStack instance.
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
82 """
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
83 self.__stack = [OpList()] # the stack starts out with the result list already allocated
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
84 self.__updateCurrent()
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
85
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
86 def pop(self) -> None:
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
87 """
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
88 Removes the OpList on top of the stack, also flattening it once when possible.
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
89
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
90 Side Effects:
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
91 self : mut
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
92
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
93 Returns:
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
94 None
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
95 """
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
96 oldTop = self.__stack.pop()
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
97 if len(oldTop) == 1 and isinstance(oldTop[0], OpList): self.__stack[-1][-1] = oldTop[0]
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
98 self.__updateCurrent()
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
99
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
100 def push(self, operator = "") -> None:
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
101 """
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
102 Adds a new nesting level, in the form of a new OpList on top of the stack.
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
103
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
104 Args:
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
105 operator : the operator assigned to the new OpList.
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
106
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
107 Side Effects:
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
108 self : mut
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
109
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
110 Returns:
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
111 None
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
112 """
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
113 newLevel = OpList(operator)
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
114 self.current.append(newLevel)
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
115 self.__stack.append(newLevel)
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
116 self.__updateCurrent()
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
117
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
118 def popForward(self) -> None:
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
119 """
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
120 Moves the last "actual" item from the 2nd to last list to the beginning of the top list, as per
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
121 the example below:
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
122 stack : [list_a, list_b]
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
123 list_a : [item1, item2, list_b] --> [item1, list_b]
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
124 list_b : [item3, item4] --> [item2, item3, item4]
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
125
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
126 This is essentially a "give back as needed" operation.
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
127
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
128 Side Effects:
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
129 self : mut
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
130
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
131 Returns:
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
132 None
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
133 """
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
134 self.current.insert(0, self.__stack[-2].pop(-2))
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
135
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
136 def currentIsAnd(self) -> bool:
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
137 """
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
138 Checks if the current OpList's assigned operator is "and".
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
139
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
140 Returns:
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
141 bool : True if the current OpList's assigned operator is "and", False otherwise.
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
142 """
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
143 return self.current.op is RuleOp.AND
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
144
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
145 def obtain(self, err :Optional[utils.CustomErr] = None) -> Optional[OpList]:
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
146 """
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
147 Obtains the first OpList on the stack, only if it's the only element.
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
148
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
149 Args:
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
150 err : The error to raise if obtaining the result is not possible.
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
151
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
152 Side Effects:
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
153 self : mut
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
154
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
155 Raises:
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
156 err: If given, otherwise None is returned.
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
157
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
158 Returns:
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
159 Optional[OpList]: The first OpList on the stack, only if it's the only element.
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
160 """
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
161
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
162 if len(self.__stack) == 1: return self.__stack.pop()
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
163 if err: raise err
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
164 return None
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
165
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
166 def __updateCurrent(self) -> None:
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
167 """
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
168 (Private) Updates the current OpList to the one on top of the stack.
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
169
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
170 Side Effects:
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
171 self : mut
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
172
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
173 Returns:
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
174 None
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
175 """
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
176 self.current = self.__stack[-1]
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
177
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
178 def parseRuleToNestedList(rule :str) -> OpList:
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
179 """
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
180 Parse a single rule from its string representation to an OpList, making all priority explicit
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
181 through nesting levels.
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
182
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
183 Args:
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
184 rule : the string representation of a rule to be parsed.
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
185
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
186 Raises:
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
187 RuleErr : whenever something goes wrong during parsing.
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
188
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
189 Returns:
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
190 OpList : the parsed rule.
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
191 """
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
192 source = iter(rule
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
193 .replace("(", "( ").replace(")", " )") # Single out parens as words
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
194 .strip() # remove whitespace at extremities
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
195 .split()) # split by spaces
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
196
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
197 stack = RuleStack()
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
198 nestingErr = RuleErr(rule, "mismatch between open and closed parentheses")
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
199 try:
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
200 while True: # keep reading until source ends
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
201 while True:
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
202 operand = next(source, None) # expected name or rule opening
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
203 if operand is None: raise RuleErr(rule, "found trailing open parentheses")
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
204 if operand == "and" or operand == "or" or operand == ")": # found operator instead, panic
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
205 raise RuleErr(rule, f"found \"{operand}\" in unexpected position")
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
206
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
207 if operand != "(": break # found name
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
208
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
209 # found rule opening, we add new nesting level but don't know the operator
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
210 stack.push()
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
211
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
212 stack.current.append(operand)
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
213
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
214 while True: # keep reading until operator is found or source ends
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
215 operator = next(source, None) # expected operator or rule closing
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
216 if operator and operator != ")": break # found operator
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
217
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
218 if stack.currentIsAnd(): stack.pop() # we close the "and" chain
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
219
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
220 if not operator: break
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
221 stack.pop() # we close the parentheses
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
222
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
223 # we proceed with operator:
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
224 if not operator: break # there is no such thing as a double loop break.. yet
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
225
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
226 if not RuleOp.isOperator(operator): raise RuleErr(
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
227 rule, f"found \"{operator}\" in unexpected position, expected operator")
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
228
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
229 operator = RuleOp(operator)
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
230 if operator is RuleOp.OR and stack.currentIsAnd():
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
231 stack.pop()
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
232
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
233 elif operator is RuleOp.AND and not stack.currentIsAnd():
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
234 stack.push(operator)
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
235 stack.popForward()
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
236
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
237 stack.current.setOpIfMissing(operator) # buffer now knows what operator its data had
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
238
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
239 except RuleErr as err: raise err # bubble up proper errors
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
240 except: raise nestingErr # everything else is interpreted as a nesting error.
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
241
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
242 parsedRule = stack.obtain(nestingErr)
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
243 return parsedRule[0] if len(parsedRule) == 1 and isinstance(parsedRule[0], list) else parsedRule