annotate cobraxy-9688ad27287b/COBRAxy/utils/rule_parsing.py @ 90:a48b2e06ebe7 draft

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