Trying to change the prefix parsing a bit.

This commit is contained in:
Dave Halter
2017-06-29 22:47:31 +02:00
parent 063d4b052e
commit 7a7ad2038d
3 changed files with 114 additions and 73 deletions

View File

@@ -4,10 +4,12 @@ from parso.tokenize import group
class PrefixPart(object):
def __init__(self, leaf, typ, value, start_pos):
def __init__(self, leaf, typ, value, spacing='', start_pos=None):
assert start_pos is not None
self.parent = leaf
self.type = typ
self.value = value
self.spacing = spacing
self.start_pos = start_pos
@property
@@ -16,6 +18,13 @@ class PrefixPart(object):
return self.start_pos[0] + 1, 0
return self.start_pos[0], self.start_pos[1] + len(self.value)
def create_spacing_part(self):
column = self.start_pos[1] - len(self.spacing)
return PrefixPart(
self.parent, 'spacing', self.spacing,
start_pos=(self.start_pos[0], column)
)
def __repr__(self):
return '%s(%s, %s, %s)' % (
self.__class__.__name__,
@@ -27,35 +36,53 @@ class PrefixPart(object):
_comment = r'#[^\n\r\f]*'
_backslash = r'\\\r?\n'
_indentation = r'[ \t]+'
_newline = r'\r?\n'
_form_feed = r'\f'
_only_spacing = '$'
_spacing = r'[ \t]*'
_regex = group(_comment, _backslash, _indentation, _newline, _form_feed)
_regex = re.compile(_regex)
_regex = group(
_comment, _backslash, _newline, _form_feed, _only_spacing,
capture=True
)
_regex = re.compile(group(_spacing, capture=True) + _regex)
_types = {
' ': 'indentation',
'#': 'comment',
'\\': 'backslash',
'\f': 'formfeed',
'\n': 'newline',
'\r': 'newline',
'\t': 'indentation',
}
def split_prefix(leaf, start_pos):
line, column = start_pos
start = 0
value = spacing = ''
while start != len(leaf.prefix):
match =_regex.match(leaf.prefix, start)
value = match.group(0)
typ = _types[value[0]]
yield PrefixPart(leaf, typ, value, (line, column + start))
spacing = match.group(1)
value = match.group(2)
if not value:
break
type_ = _types[value[0]]
print(repr(spacing), repr(value), column)
yield PrefixPart(
leaf, type_, value, spacing,
start_pos=(line, column + start + len(spacing))
)
start = match.end(0)
if value.endswith('\n'):
line += 1
column = -start
print('x', repr(value), repr(spacing))
if value:
spacing = ''
yield PrefixPart(
leaf, 'spacing', spacing,
start_pos=(line, column + start)
)