mirror of
https://github.com/davidhalter/jedi.git
synced 2025-12-18 03:25:55 +08:00
precedence tests and a parse_tree method for Precedence objects
This commit is contained in:
@@ -55,6 +55,18 @@ class Precedence(object):
|
||||
self.operator = operator
|
||||
self.right = right
|
||||
|
||||
def parse_tree(self, strip_literals=False):
|
||||
def process(which):
|
||||
try:
|
||||
which = which.parse_tree
|
||||
except AttributeError:
|
||||
pass
|
||||
if strip_literals and isinstance(which, pr.Literal):
|
||||
which = which.value
|
||||
return which
|
||||
|
||||
return (process(self.left), self.operator, process(self.right))
|
||||
|
||||
def __repr__(self):
|
||||
return '(%s %s %s)' % (self.left, self.operator, self.right)
|
||||
|
||||
@@ -98,17 +110,18 @@ def _check_operator(iterator, priority=PythonGrammar.LOWEST_PRIORITY):
|
||||
break # respect priorities.
|
||||
|
||||
try:
|
||||
match = check.index(el)
|
||||
match_index = check.index(el)
|
||||
except ValueError:
|
||||
continue
|
||||
|
||||
match = check[match_index]
|
||||
if isinstance(match, PythonGrammar.MultiPart):
|
||||
next_tok = next(iterator)
|
||||
if next_tok != match.second:
|
||||
iterator.push_back(next_tok)
|
||||
continue
|
||||
|
||||
operator = check
|
||||
operator = match
|
||||
break
|
||||
|
||||
if operator is None:
|
||||
|
||||
@@ -1,26 +1,26 @@
|
||||
import pytest
|
||||
|
||||
from jedi.parser import Parser
|
||||
from jedi.evaluate import precedence
|
||||
cp = lambda *args: precedence.create_precedence(iter(args))
|
||||
|
||||
|
||||
@pytest.skip('sorry precedence stuff is still not implemented yet')
|
||||
def parse_tree(statement_string):
|
||||
p = Parser(statement_string, no_docstr=True)
|
||||
stmt = p.module.statements[0]
|
||||
iterable = iter(stmt.expression_list())
|
||||
pr = precedence.create_precedence(iterable)
|
||||
if isinstance(pr, precedence.Precedence):
|
||||
return pr.parse_tree(strip_literals=True)
|
||||
else:
|
||||
return pr
|
||||
|
||||
|
||||
def test_simple():
|
||||
p = cp(1, '+', 2)
|
||||
assert p.left == 1
|
||||
assert p.operator == '+'
|
||||
assert p.right == 2
|
||||
|
||||
p = cp('+', 2)
|
||||
assert p.left is None
|
||||
assert p.operator == '+'
|
||||
assert p.right == 2
|
||||
assert parse_tree('1+2') == (1, '+', 2)
|
||||
assert parse_tree('+2') == (None, '+', 2)
|
||||
|
||||
|
||||
@pytest.skip('sorry precedence stuff is still not implemented yet')
|
||||
def test_invalid():
|
||||
"""Should just return a simple operation."""
|
||||
assert cp(1, '+') == 1
|
||||
assert cp('+') is None
|
||||
assert parse_tree('1 +') == 1
|
||||
assert parse_tree('+') is None
|
||||
|
||||
assert cp('*', 1) == 1
|
||||
assert parse_tree('* 1') == 1
|
||||
|
||||
Reference in New Issue
Block a user