mirror of
https://github.com/davidhalter/parso.git
synced 2025-12-08 13:45:01 +08:00
Add a prefix splitting.
This commit is contained in:
50
parso/python/prefix.py
Normal file
50
parso/python/prefix.py
Normal 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
|
||||
Reference in New Issue
Block a user