mirror of
https://github.com/davidhalter/parso.git
synced 2025-12-15 00:47:11 +08:00
Deal with indents in diff parser more explicitly
This commit is contained in:
@@ -42,6 +42,10 @@ def _get_next_leaf_if_indentation(leaf):
|
|||||||
return leaf
|
return leaf
|
||||||
|
|
||||||
|
|
||||||
|
def _get_suite_indentation(tree_node):
|
||||||
|
return tree_node.children[1].start_pos[1]
|
||||||
|
|
||||||
|
|
||||||
def _assert_valid_graph(node):
|
def _assert_valid_graph(node):
|
||||||
"""
|
"""
|
||||||
Checks if the parent/children relationship is correct.
|
Checks if the parent/children relationship is correct.
|
||||||
@@ -459,11 +463,12 @@ class _NodesTreeNode(object):
|
|||||||
'_ChildrenGroup',
|
'_ChildrenGroup',
|
||||||
'prefix children line_offset last_line_offset_leaf add_error_leaf')
|
'prefix children line_offset last_line_offset_leaf add_error_leaf')
|
||||||
|
|
||||||
def __init__(self, tree_node, parent=None):
|
def __init__(self, tree_node, indents, parent=None):
|
||||||
self.tree_node = tree_node
|
self.tree_node = tree_node
|
||||||
self._children_groups = []
|
self._children_groups = []
|
||||||
self.parent = parent
|
self.parent = parent
|
||||||
self._node_children = []
|
self._node_children = []
|
||||||
|
self.indents = indents
|
||||||
|
|
||||||
def finish(self):
|
def finish(self):
|
||||||
children = []
|
children = []
|
||||||
@@ -545,7 +550,7 @@ class _NodesTreeNode(object):
|
|||||||
|
|
||||||
class _NodesTree(object):
|
class _NodesTree(object):
|
||||||
def __init__(self, module):
|
def __init__(self, module):
|
||||||
self._base_node = _NodesTreeNode(module)
|
self._base_node = _NodesTreeNode(module, indents=[0])
|
||||||
self._working_stack = [self._base_node]
|
self._working_stack = [self._base_node]
|
||||||
self._module = module
|
self._module = module
|
||||||
self._prefix_remainder = ''
|
self._prefix_remainder = ''
|
||||||
@@ -553,9 +558,9 @@ class _NodesTree(object):
|
|||||||
|
|
||||||
def get_indents(self, indentation):
|
def get_indents(self, indentation):
|
||||||
for node in self._working_stack:
|
for node in self._working_stack:
|
||||||
first_indentation = node.get_first_indentation()
|
for i in node.indents:
|
||||||
yield first_indentation
|
yield i
|
||||||
if indentation <= first_indentation:
|
if indentation <= i:
|
||||||
break
|
break
|
||||||
|
|
||||||
@property
|
@property
|
||||||
@@ -616,19 +621,21 @@ class _NodesTree(object):
|
|||||||
assert node.tree_node.type in ('suite', 'file_input')
|
assert node.tree_node.type in ('suite', 'file_input')
|
||||||
node.add_tree_nodes(old_prefix, tree_nodes)
|
node.add_tree_nodes(old_prefix, tree_nodes)
|
||||||
# tos = Top of stack
|
# tos = Top of stack
|
||||||
self._update_tos(tree_nodes[-1])
|
self._update_parsed_node_tos(tree_nodes[-1])
|
||||||
|
|
||||||
def _update_tos(self, tree_node):
|
def _update_parsed_node_tos(self, tree_node):
|
||||||
if tree_node.type in ('suite', 'file_input'):
|
if tree_node.type == 'suite':
|
||||||
new_tos = _NodesTreeNode(tree_node)
|
indent = _get_suite_indentation(tree_node)
|
||||||
|
|
||||||
|
new_tos = _NodesTreeNode(tree_node, indents=[indent])
|
||||||
new_tos.add_tree_nodes('', list(tree_node.children))
|
new_tos.add_tree_nodes('', list(tree_node.children))
|
||||||
|
|
||||||
self._working_stack[-1].add_child_node(new_tos)
|
self._working_stack[-1].add_child_node(new_tos)
|
||||||
self._working_stack.append(new_tos)
|
self._working_stack.append(new_tos)
|
||||||
|
|
||||||
self._update_tos(tree_node.children[-1])
|
self._update_parsed_node_tos(tree_node.children[-1])
|
||||||
elif _func_or_class_has_suite(tree_node):
|
elif _func_or_class_has_suite(tree_node):
|
||||||
self._update_tos(tree_node.children[-1])
|
self._update_parsed_node_tos(tree_node.children[-1])
|
||||||
|
|
||||||
def _remove_endmarker(self, tree_nodes):
|
def _remove_endmarker(self, tree_nodes):
|
||||||
"""
|
"""
|
||||||
@@ -728,7 +735,8 @@ class _NodesTree(object):
|
|||||||
while suite.type != 'suite':
|
while suite.type != 'suite':
|
||||||
suite = suite.children[-1]
|
suite = suite.children[-1]
|
||||||
|
|
||||||
suite_tos = _NodesTreeNode(suite)
|
indent = _get_suite_indentation(suite)
|
||||||
|
suite_tos = _NodesTreeNode(suite, indents=[indent])
|
||||||
# Don't need to pass line_offset here, it's already done by the
|
# Don't need to pass line_offset here, it's already done by the
|
||||||
# parent.
|
# parent.
|
||||||
suite_nodes, new_working_stack, new_prefix = self._copy_nodes(
|
suite_nodes, new_working_stack, new_prefix = self._copy_nodes(
|
||||||
|
|||||||
@@ -259,7 +259,10 @@ xfail_py2 = dict(marks=[pytest.mark.xfail(sys.version_info[0] == 2, reason='Pyth
|
|||||||
c
|
c
|
||||||
'''), [NAME, NAME, OP, NEWLINE, INDENT, NAME, NEWLINE,
|
'''), [NAME, NAME, OP, NEWLINE, INDENT, NAME, NEWLINE,
|
||||||
ERROR_DEDENT, NAME, NEWLINE, INDENT, NAME, NEWLINE, DEDENT,
|
ERROR_DEDENT, NAME, NEWLINE, INDENT, NAME, NEWLINE, DEDENT,
|
||||||
NAME, NEWLINE, INDENT, NAME, NEWLINE, DEDENT, DEDENT])
|
NAME, NEWLINE, INDENT, NAME, NEWLINE, DEDENT, DEDENT]),
|
||||||
|
(' )\n foo', [INDENT, OP, NEWLINE, ERROR_DEDENT, NAME, DEDENT]),
|
||||||
|
('a\n b\n )\n c', [NAME, NEWLINE, INDENT, NAME, NEWLINE, INDENT, OP,
|
||||||
|
NEWLINE, DEDENT, NAME, DEDENT]),
|
||||||
]
|
]
|
||||||
)
|
)
|
||||||
def test_token_types(code, types):
|
def test_token_types(code, types):
|
||||||
|
|||||||
Reference in New Issue
Block a user