MultiPart string precedences seem to be working now

This commit is contained in:
Dave Halter
2014-03-08 02:52:16 +01:00
parent 0dcc924cf8
commit 0a253b4651
2 changed files with 26 additions and 5 deletions

View File

@@ -16,9 +16,10 @@ class PythonGrammar(object):
def __new__(cls, first, second): def __new__(cls, first, second):
self = str.__new__(cls, first) self = str.__new__(cls, first)
self.second = second self.second = second
return self
def __str__(self): def __str__(self):
return self.__str__() + ' ' + self.second return str.__str__(self) + ' ' + self.second
FACTOR = '+', '-', '~' FACTOR = '+', '-', '~'
POWER = '**', POWER = '**',
@@ -31,7 +32,7 @@ class PythonGrammar(object):
EXPR = '|', EXPR = '|',
COMPARISON = ('<', '>', '==', '>=', '<=', '!=', 'in', COMPARISON = ('<', '>', '==', '>=', '<=', '!=', 'in',
MultiPart('not', 'in'), MultiPart('is', 'not')), 'is' MultiPart('not', 'in'), MultiPart('is', 'not'), 'is')
NOT_TEST = 'not', NOT_TEST = 'not',
AND_TEST = 'and', AND_TEST = 'and',
@@ -58,7 +59,7 @@ class Precedence(object):
def parse_tree(self, strip_literals=False): def parse_tree(self, strip_literals=False):
def process(which): def process(which):
try: try:
which = which.parse_tree() which = which.parse_tree(strip_literals)
except AttributeError: except AttributeError:
pass pass
if strip_literals and isinstance(which, pr.Literal): if strip_literals and isinstance(which, pr.Literal):
@@ -124,7 +125,10 @@ def _check_operator(iterator, priority=PythonGrammar.LOWEST_PRIORITY):
next_tok = next(iterator) next_tok = next(iterator)
if next_tok != match.second: if next_tok != match.second:
iterator.push_back(next_tok) iterator.push_back(next_tok)
continue if el == 'is': # `is not` special case
match = 'is'
else:
continue
operator = match operator = match
break break
@@ -132,7 +136,8 @@ def _check_operator(iterator, priority=PythonGrammar.LOWEST_PRIORITY):
if operator is None: if operator is None:
_syntax_error(el) _syntax_error(el)
continue continue
right = _check_operator(iterator, check_prio) right = _check_operator(iterator, check_prio)
if right is not None: if right is not None:
left = Precedence(left, operator, right) left = Precedence(left, str(operator), right)
return left return left

View File

@@ -21,9 +21,25 @@ def test_simple():
assert parse_tree('+2') == (None, '+', 2) assert parse_tree('+2') == (None, '+', 2)
def test_prefixed():
assert parse_tree('--2') == (None, '-', (None, '-', 2))
assert parse_tree('1 and not - 2') == (1, 'and', (None, '-', 2))
def test_invalid(): def test_invalid():
"""Should just return a simple operation.""" """Should just return a simple operation."""
assert parse_tree('1 +') == 1 assert parse_tree('1 +') == 1
assert parse_tree('+') is None assert parse_tree('+') is None
assert parse_tree('* 1') == 1 assert parse_tree('* 1') == 1
assert parse_tree('1 * * 1') == (1, '*', 1)
# invalid operator
assert parse_tree('1 not - 1') == (1, '-', 1)
assert parse_tree('1 - not ~1') == (1, '-', (None, '~', 1))
def test_multi_part():
assert parse_tree('1 not in 2') == (1, 'not in', 2)
assert parse_tree('1 is not -1') == (1, 'is not', (None, '-', 1))
assert parse_tree('1 is 1') == (1, 'is', 1)