- def fromstring(cls, string):
- string = string.strip()
- string = re.sub(r'^\{\s*|\s*\}$', '', string)
- string = re.sub(r'([^<=>])=([^<=>])', r'\1==\2', string)
- string = re.sub(r'(\d+|\))\s*([^\W\d_]\w*|\()', r'\1*\2', string)
- equalities = []
- inequalities = []
- for cstr in re.split(r',|;|and|&&|/\\|∧', string, flags=re.I):
- tree = ast.parse(cstr.strip(), 'eval')
- if not isinstance(tree, ast.Module) or len(tree.body) != 1:
- raise SyntaxError('invalid syntax')
- node = tree.body[0]
- if not isinstance(node, ast.Expr):
- raise SyntaxError('invalid syntax')
- node = node.value
- if not isinstance(node, ast.Compare):
- raise SyntaxError('invalid syntax')
+ def _fromast(cls, node):
+ if isinstance(node, ast.Module) and len(node.body) == 1:
+ return cls._fromast(node.body[0])
+ elif isinstance(node, ast.Expr):
+ return cls._fromast(node.value)
+ elif isinstance(node, ast.BinOp) and isinstance(node.op, ast.BitAnd):
+ equalities1, inequalities1 = cls._fromast(node.left)
+ equalities2, inequalities2 = cls._fromast(node.right)
+ equalities = equalities1 + equalities2
+ inequalities = inequalities1 + inequalities2
+ return equalities, inequalities
+ elif isinstance(node, ast.Compare):
+ equalities = []
+ inequalities = []