parser update
This commit is contained in:
@@ -69,6 +69,7 @@ TOKEN_RE = re.compile(
|
||||
\s*(
|
||||
\(|\)|
|
||||
!|
|
||||
\+|\*|
|
||||
AND|OR|
|
||||
0|1|
|
||||
X\d+|
|
||||
@@ -81,8 +82,7 @@ TOKEN_RE = re.compile(
|
||||
def tokenize(s: str) -> List[str]:
|
||||
tokens = TOKEN_RE.findall(s)
|
||||
stripped = re.sub(r"\s+", "", s)
|
||||
joined = "".join(tokens).replace("AND", "AND").replace("OR", "OR")
|
||||
if re.sub(r"\s+", "", joined) != stripped:
|
||||
if "".join(tokens) != stripped:
|
||||
raise ValueError(f"Invalid token near: {s!r}")
|
||||
return tokens
|
||||
|
||||
@@ -128,17 +128,16 @@ class Parser:
|
||||
|
||||
def parse_or(self) -> Poly:
|
||||
left = self.parse_and()
|
||||
while self.peek() == "OR":
|
||||
self.eat("OR")
|
||||
while self.peek() in ("OR", "+"):
|
||||
self.eat()
|
||||
right = self.parse_and()
|
||||
# A OR B = A XOR B XOR (A AND B)
|
||||
left = poly_xor(poly_xor(left, right), poly_and(left, right))
|
||||
return left
|
||||
|
||||
def parse_and(self) -> Poly:
|
||||
left = self.parse_not()
|
||||
while self.peek() == "AND":
|
||||
self.eat("AND")
|
||||
while self.peek() in ("AND", "*"):
|
||||
self.eat()
|
||||
right = self.parse_not()
|
||||
left = poly_and(left, right)
|
||||
return left
|
||||
|
||||
Reference in New Issue
Block a user