a
    
d                     @   s   d Z ddlZddlZddlZddlZddlZddlmZ ddlmZ ddl	m
Z
 ddlmZ ddlmZ ejZdd	d
Zdd ZdS )aP  Simple Python Parser

Parse Python code into a list of logical lines, represented by LogicalLine
objects. This uses Python's tokenizer to generate the tokens. As such, YAPF must
be run with the appropriate Python version---Python >=3.7 for Python 3.7 code,
Python >=3.8 for Python 3.8 code, etc.

This parser uses Python's native "tokenizer" module to generate a list of tokens
for the source code. It then uses Python's native "ast" module to assign
subtypes, calculate split penalties, etc.

A "logical line" produced by Python's "tokenizer" module ends with a
tokenize.NEWLINE, rather than a tokenize.NL, making it easy to separate them
out. Comments all end with a tokentizer.NL, so we need to make sure we don't
errantly pick up non-comment tokens when parsing comment blocks.

  ParseCode(): parse the code producing a list of logical lines.
    N)StringIO)	TokenInfo)split_penalty_visitor)format_token)logical_line	<unknown>c                 C   sv   |  tjs| tj7 } z.t| |}t| t| j}t	|}W n t
yX    Y n0 t|}t|| |S )ac  Parse a string of Python code into logical lines.

  This provides an alternative entry point to YAPF.

  Arguments:
    unformatted_source: (unicode) The code to format.
    filename: (unicode) The name of the file being reformatted.

  Returns:
    A list of LogicalLines.

  Raises:
    An exception is raised if there's an error during AST parsing.
  )endswithoslinesepastparsefix_missing_locationsr   readlinetokenizegenerate_tokens	Exception_CreateLogicalLinesr   ZSplitPenaltyvisit)Zunformatted_sourcefilenameZast_treer   tokenslogical_lines r   _/var/www/html/stable-diffusion-webui/venv/lib/python3.9/site-packages/yapf/pyparser/pyparser.py	ParseCode2   s    


r   c                 C   sJ  g }d}| D ]}t | }|r|j dr|jd |jd k rt td|jd |jd d f|jd |jd d f|jd}|jd |_|jd |_d|_	|
t|d |jd |_|jd |_|j|_	|
t|tj|j  |}qg g  }}d}|D ]}|jtjkr q|jtjkr<|
t|| g }n|jtjkrT|d7 }nn|jtjkrl|d8 }nV|jtjkr|nF|r|jtjks|d jtjkr|
t|| g }|
|  q|D ]z}|j}	|	 r|	gng }
|jdd D ]J}|	|_||	_|}	| r |

| n| r||
d _ |
! |_ qq|S )	zSeparate tokens into logical lines.

  Arguments:
    tokens: (list of tokenizer.TokenInfo) Tokens generated by tokenizer.

  Returns:
    A list of LogicalLines.
  N\r         )typestringstartendlineCONTINUATION)"r   r!   rstripr   r   r"   r    linenocolumnvalueappendr   ZFormatTokenr   tokentok_namer   r   	ENDMARKERNEWLINEr   ZLogicalLineINDENTDEDENTNLCOMMENTfirstZ
OpensScoper   Zprevious_tokenZ
next_tokenZClosesScopeZmatching_bracketpop)r   Zformatted_tokensZprev_toktokZctokr   Zcur_logical_linedepthr!   previousZbracket_stackr   r   r   r   T   st    	





r   )r   )__doc__r   codecsr	   r)   r   ior   r   Zyapf.pyparserr   Zyapf.yapflibr   r   N_TOKENSr"   r   r   r   r   r   r   <module>   s   
"