precedence working for some simple cases (and invalid statements)

This commit is contained in:
Dave Halter
2014-03-08 02:24:25 +01:00
parent f2e2a684d5
commit 0dcc924cf8
3 changed files with 16 additions and 6 deletions

View File

@@ -58,7 +58,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()
except AttributeError: except AttributeError:
pass pass
if strip_literals and isinstance(which, pr.Literal): if strip_literals and isinstance(which, pr.Literal):
@@ -98,7 +98,12 @@ def _get_number(iterator, priority=PythonGrammar.LOWEST_PRIORITY):
def _check_operator(iterator, priority=PythonGrammar.LOWEST_PRIORITY): def _check_operator(iterator, priority=PythonGrammar.LOWEST_PRIORITY):
""" """
""" """
try:
left = _get_number(iterator, priority) left = _get_number(iterator, priority)
except StopIteration:
_syntax_error(iterator.current, 'SyntaxError operand missing')
return None
for el in iterator: for el in iterator:
if not isinstance(el, pr.Operator): if not isinstance(el, pr.Operator):
_syntax_error(el) _syntax_error(el)
@@ -127,5 +132,7 @@ def _check_operator(iterator, priority=PythonGrammar.LOWEST_PRIORITY):
if operator is None: if operator is None:
_syntax_error(el) _syntax_error(el)
continue continue
left = Precedence(left, operator, _check_operator(iterator, check_prio)) right = _check_operator(iterator, check_prio)
if right is not None:
left = Precedence(left, operator, right)
return left return left

View File

@@ -581,7 +581,7 @@ class Parser(object):
continue continue
# default # default
elif token_type in (tokenize.NAME, tokenize.STRING, elif token_type in (tokenize.NAME, tokenize.STRING,
tokenize.NUMBER) \ tokenize.NUMBER, tokenize.OP) \
or tok_str in statement_toks: or tok_str in statement_toks:
# this is the main part - a name can be a function or a # this is the main part - a name can be a function or a
# normal var, which can follow anything. but this is done # normal var, which can follow anything. but this is done

View File

@@ -10,6 +10,9 @@ def parse_tree(statement_string):
if isinstance(pr, precedence.Precedence): if isinstance(pr, precedence.Precedence):
return pr.parse_tree(strip_literals=True) return pr.parse_tree(strip_literals=True)
else: else:
try:
return pr.value # Literal
except AttributeError:
return pr return pr