_B='newline'
_A='spacing'
import re
from codecs import BOM_UTF8
from typing import Tuple
from parso.python.tokenize import group
unicode_bom=BOM_UTF8.decode('utf-8')
class PrefixPart:
	def __init__(A,leaf,typ,value,spacing='',start_pos=None):B=start_pos;assert B is not None;A.parent=leaf;A.type=typ;A.value=value;A.spacing=spacing;A.start_pos=B
	@property
	def end_pos(self):
		A=self
		if A.value.endswith('\n')or A.value.endswith('\r'):return A.start_pos[0]+1,0
		if A.value==unicode_bom:return A.start_pos
		return A.start_pos[0],A.start_pos[1]+len(A.value)
	def create_spacing_part(A):B=A.start_pos[1]-len(A.spacing);return PrefixPart(A.parent,_A,A.spacing,start_pos=(A.start_pos[0],B))
	def __repr__(A):return'%s(%s, %s, %s)'%(A.__class__.__name__,A.type,repr(A.value),A.start_pos)
	def search_ancestor(B,*C):
		A=B.parent
		while A is not None:
			if A.type in C:return A
			A=A.parent
_comment='#[^\\n\\r\\f]*'
_backslash='\\\\\\r?\\n|\\\\\\r'
_newline='\\r?\\n|\\r'
_form_feed='\\f'
_only_spacing='$'
_spacing='[ \\t]*'
_bom=unicode_bom
_regex=group(_comment,_backslash,_newline,_form_feed,_only_spacing,_bom,capture=True)
_regex=re.compile(group(_spacing,capture=True)+_regex)
_types={'#':'comment','\\':'backslash','\x0c':'formfeed','\n':_B,'\r':_B,unicode_bom:'bom'}
def split_prefix(leaf,start_pos):
	D=leaf;E,F=start_pos;B=0;A=C='';H=False
	while B!=len(D.prefix):
		G=_regex.match(D.prefix,B);C=G.group(1);A=G.group(2)
		if not A:break
		I=_types[A[0]];yield PrefixPart(D,I,A,C,start_pos=(E,F+B-int(H)+len(C)))
		if I=='bom':H=True
		B=G.end(0)
		if A.endswith('\n')or A.endswith('\r'):E+=1;F=-B
	if A:C=''
	yield PrefixPart(D,_A,C,start_pos=(E,F+B))