to the power of precedences

This commit is contained in:
Dave Halter
2014-03-08 03:05:38 +01:00
parent 0a253b4651
commit 3559dba7ea
2 changed files with 13 additions and 1 deletions

View File

@@ -113,7 +113,9 @@ def _check_operator(iterator, priority=PythonGrammar.LOWEST_PRIORITY):
operator = None operator = None
for check_prio, check in enumerate(PythonGrammar.ORDER): for check_prio, check in enumerate(PythonGrammar.ORDER):
if check_prio >= priority: if check_prio >= priority:
break # respect priorities. # respect priorities.
iterator.push_back(el)
return left
try: try:
match_index = check.index(el) match_index = check.index(el)
@@ -137,6 +139,8 @@ def _check_operator(iterator, priority=PythonGrammar.LOWEST_PRIORITY):
_syntax_error(el) _syntax_error(el)
continue continue
if operator == '**':
check_prio += 1 # to the power of is right-associative
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, str(operator), right) left = Precedence(left, str(operator), right)

View File

@@ -19,6 +19,7 @@ def parse_tree(statement_string):
def test_simple(): def test_simple():
assert parse_tree('1+2') == (1, '+', 2) assert parse_tree('1+2') == (1, '+', 2)
assert parse_tree('+2') == (None, '+', 2) assert parse_tree('+2') == (None, '+', 2)
assert parse_tree('1+2-3') == ((1, '+', 2), '-', 3)
def test_prefixed(): def test_prefixed():
@@ -38,8 +39,15 @@ def test_invalid():
assert parse_tree('1 not - 1') == (1, '-', 1) assert parse_tree('1 not - 1') == (1, '-', 1)
assert parse_tree('1 - not ~1') == (1, '-', (None, '~', 1)) assert parse_tree('1 - not ~1') == (1, '-', (None, '~', 1))
# not not allowed
assert parse_tree('1 is not not 1') == (1, 'is not', 1)
def test_multi_part(): def test_multi_part():
assert parse_tree('1 not in 2') == (1, 'not in', 2) 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 not -1') == (1, 'is not', (None, '-', 1))
assert parse_tree('1 is 1') == (1, 'is', 1) assert parse_tree('1 is 1') == (1, 'is', 1)
def test_power():
assert parse_tree('2 ** 3 ** 4') == (2, '**', (3, '**', 4))