Add a prefix splitting.

This commit is contained in:
Dave Halter
2017-05-30 02:00:08 +02:00
parent a276c0b42d
commit 6640308d15
5 changed files with 87 additions and 0 deletions

50
parso/python/prefix.py Normal file
View File

@@ -0,0 +1,50 @@
import re
from parso.tokenize import group
class PrefixToken(object):
def __init__(self, value, start_pos):
self.value = value
self.start_pos = start_pos
@property
def end_pos(self):
if '\n' in self.value:
return self.start_pos[0] + 1, 0
return self.end_pos[1]
_comment = r'#[^\n\r\f]*'
_backslash = r'\\\r?\n?'
_whitespace = r' +'
_newline = r'\r?\n'
_form_feed = r'\f'
_regex = group(_comment, _backslash, _whitespace, _newline, _form_feed)
_regex = re.compile(_regex)
_types = {
' ': 'spaces',
'#': 'comment',
'\\': 'backslash',
'\f': 'form_feed',
'\n': 'newline',
'\r': 'newline'
}
def split_prefix(prefix, start_pos):
line, column = start_pos
start = 0
while start != len(prefix):
match =_regex.match(prefix, start)
value = match.group(0)
yield PrefixToken(value, (line, column + start))
start = match.end(0)
print(start)
if '\n' in value:
line += 1
column = -start