Fix newline stuff for empty parsers.

This commit is contained in:
Dave Halter
2015-01-27 01:15:39 +01:00
parent 39e869d146
commit 88a3e25814
3 changed files with 12 additions and 5 deletions

View File

@@ -312,7 +312,6 @@ class Parser(object):
def remove_last_newline(self):
endmarker = self.module.children[-1]
print('ENDNL', self.module.children, repr(endmarker.prefix))
# The newline is either in the endmarker as a prefix or the previous
# leaf as a newline token.
if endmarker.prefix.endswith('\n'):
@@ -320,7 +319,10 @@ class Parser(object):
last_line = re.sub('.*\n', '', endmarker.prefix)
endmarker.start_pos = endmarker.start_pos[0] - 1, len(last_line)
else:
newline = endmarker.get_previous()
try:
newline = endmarker.get_previous()
except IndexError:
return # This means that the parser is empty.
while True:
if newline.value == '':
# Must be a DEDENT, just continue.

View File

@@ -233,9 +233,6 @@ class LeafWithNewLines(Leaf):
"""
end_pos_line, end_pos_col = self.start_pos
lines = self.value.split('\n')
if self.value.endswith('\n'):
lines = lines[:-1]
lines[-1] += '\n'
end_pos_line += len(lines) - 1
# Check for multiline token
if self.start_pos[0] == end_pos_line:

View File

@@ -155,3 +155,11 @@ def test_error_correction_with():
assert len(comps) > 30
# `open` completions have a closed attribute.
assert [1 for c in comps if c.name == 'closed']
def test_newline_positions():
endmarker = Parser(load_grammar(), 'a\n').module.children[-1]
assert endmarker.end_pos == (2, 0)
new_line = endmarker.get_previous()
assert new_line.start_pos == (1, 1)
assert new_line.end_pos == (2, 0)