L ikEHddlZddlZddlZddlZddlZddlmZddlmZmZm Z m Z m Z m Z m Z mZddlmZddlmZmZmZmZmZmZmZmZmZerddlmZmZGdd ej: ZGd d eZGd deZ dZ!ejDdddk(r e!dz Z!e!dz Z!ddZ#y)N)Module)IterableIteratorListOptionalTupleAnycast TYPE_CHECKING) LineNumbers) Token match_tokenis_non_coding_tokenpatched_generate_tokens last_stmtannotate_fstring_nodesgenerate_tokens is_moduleis_stmt)AstNode TokenInfocTeZdZdededdfdZej dZd dZd dZ y) ASTTextBase source_textfilenamereturnNcV||_t|}||_t||_yN) _filenamestr_textr _line_numbers)selfrrs Y/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/asttokens/asttokens.py__init__zASTTextBase.__init__"s)DN k"KDJ$[1Dct) Returns two ``(lineno, col_offset)`` tuples for the start and end of the given node. If the positions can't be determined, or the nodes don't correspond to any particular text, returns ``(1, 0)`` for both. ``padded`` corresponds to the ``padded`` argument to ``ast.get_source_segment()``. This means that if ``padded`` is True, the start position will be adjusted to include leading whitespace if ``node`` is a multiline statement. )NotImplementedErrorr$nodepaddeds r%get_text_positionszASTTextBase.get_text_positions-s  r'c|j||\}}|jj||jj|fS)a Returns the (startpos, endpos) positions in source text corresponding to the given node. Returns (0, 0) for nodes (like `Load`) that don't correspond to any particular text. See ``get_text_positions()`` for details on the ``padded`` argument. )r.r#line_to_offsetr$r,r-startends r%get_text_rangezASTTextBase.get_text_range;sP((v6JE3'd''/'d''- r'cJ|j||\}}|j||S)z Returns the text corresponding to the given node. Returns '' for nodes (like `Load`) that don't correspond to any particular text. See ``get_text_positions()`` for details on the ``padded`` argument. )r4r"r1s r%get_textzASTTextBase.get_textIs+$$T62JE3 ::eS !!r')T) __name__ __module__ __qualname__r!r&abcabstractmethodr.r4r6r'r%rr!s@ 2# 2 2 2    "r'r) metaclassceZdZdZdfd ZdZdZedZedZ edZ edZ d Z d Z d Zdd Zdd ZddZ ddZddZdZxZS) ASTTokensa ASTTokens maintains the text of Python code in several forms: as a string, as line numbers, and as tokens, and is used to mark and access token and position information. ``source_text`` must be a unicode or UTF8-encoded string. If you pass in UTF8 bytes, remember that all offsets you'll get are to the unicode text, which is available as the ``.text`` property. If ``parse`` is set, the ``source_text`` will be parsed with ``ast.parse()``, and the resulting tree marked with token info and made available as the ``.tree`` property. If ``tree`` is given, it will be marked and made available as the ``.tree`` property. In addition to the trees produced by the ``ast`` module, ASTTokens will also mark trees produced using ``astroid`` library . If only ``source_text`` is given, you may use ``.mark_tokens(tree)`` to mark the nodes of an AST tree created separately. ctt| |||rtj||n||_|t |j}t|j||_ |jDcgc]}|jc}|_ |j r|j|j yycc}wr)superr?r&astparse_treerr"list_translate_tokens_tokensstartpos_token_offsets mark_tokens)r$rrCtreertokenstok __class__s r%r&zASTTokens.__init__is )T#K:5:;1DJ~tzz*f..v67DL48<<@C3<<@D zz tzz"As9B=c>ddlm}||j|y)aq Given the root of the AST or Astroid tree produced from source_text, visits all nodes marking them with token and position information by adding ``.first_token`` and ``.last_token`` attributes. This is done automatically in the constructor when ``parse`` or ``tree`` arguments are set, but may be used manually with a separate AST or Astroid tree. r ) MarkTokensN)rJrP visit_tree)r$ root_noderPs r%rJzASTTokens.mark_tokenszs(t *r'c#Ktt|D]a\}}|\}}}}}t|||||||jj |d|d|jj |d|dcyw)zS Translates the given standard library tokens into our own representation. rr N) enumeraterrr#r0) r$original_tokensindexrMtok_typetok_strr2r3lines r%rFzASTTokens._translate_tokenss  7 HIE s,/)hT (GUCu$$33E!HeAhG$$33CFCFC EEEsA:A<c|jS)z,The source code passed into the constructor.)r"r$s r%textzASTTokens.text ::r'c|jS)zIThe list of tokens corresponding to the source code from the constructor.)rGr[s r%rLzASTTokens.tokenss <<r'c|jS)zTThe root of the AST tree passed into the constructor or parsed from the source code.)rDr[s r%rKzASTTokens.treer]r'c|jS)zThe filename that was parsed)r r[s r%rzASTTokens.filenames >>r'cb|jtj|j|dz S)z Returns the token containing the given character offset (0-based position in source text), or the preceeding token if the position is between tokens. r )rGbisectrI)r$offsets r%get_token_from_offsetzASTTokens.get_token_from_offsets) << d&9&96BQF GGr'cX|j|jj||S)z Returns the token containing the given (lineno, col_offset) position, or the preceeding token if the position is between tokens. )rdr#r0r$lineno col_offsets r% get_tokenzASTTokens.get_tokens)  % %d&8&8&G&GPZ&[ \\r'cZ|j||jj||S)zd Same as get_token(), but interprets col_offset as a UTF8 offset, which is what `ast` uses. )rir# from_utf8_colrfs r%get_token_from_utf8zASTTokens.get_token_from_utf8s( >>&$"4"4"B"B6:"V WWr'c|jdz}|sJt|j|jr(|dz }t|j|jr(|j|S)z Returns the next token after the given one. If include_extra is True, includes non-coding tokens from the tokenize module, such as NL and COMMENT. r rVrrGtyper$rM include_extrais r% next_tokenzASTTokens.next_tokenY A A   Q 4 4 5 Q  Q 4 4 5 <<?r'c|jdz }|sJt|j|jr(|dz}t|j|jr(|j|S)z Returns the previous token before the given one. If include_extra is True, includes non-coding tokens from the tokenize module, such as NL and COMMENT. r rnrps r% prev_tokenzASTTokens.prev_tokenrtr'c|}|r |jn |j}t|||sVtj|j s7||d}t|||s tj|j s7|S)z Looks for the first token, starting at start_token, that matches tok_type and, if given, the token string. Searches backwards if reverse is True. Returns ENDMARKER token if not found (you can check it with `token.ISEOF(t.type)`). Trq)rvrsrtokenISEOFro)r$ start_tokenrWrXreversetadvances r% find_tokenzASTTokens.find_tokensc A!(doodooG!Xw/ AFF8K !4 (a!Xw/ AFF8K Hr'c#Kt|j|jdzD]8}|s#t|j|jr(|j|:yw)z Yields all tokens in order from first_token through and including last_token. If include_extra is True, includes non-coding tokens such as tokenize.NL and .COMMENT. r N)rangerVrrGro)r$ first_token last_tokenrqrrs r% token_rangezASTTokens.token_rangesV;$$j&6&6&: ; 1$,,q/2F2FGll1os A A"A"cR|j|j|j|S)z Yields all tokens making up the given node. If include_extra is True, includes non-coding tokens such as tokenize.NL and .COMMENT. rx)rrr)r$r,rqs r% get_tokenszASTTokens.get_tokenss&   D,,doo]  [[r'ct|dsy|jj}|jj}|r(t d|j |Dr|ddf}||fS)r)rr rrc3PK|]}t|tj ywr)rryNEWLINE).0r}s r% z/ASTTokens.get_text_positions..sSk!U]]3Ss$&r)hasattrrr2rr3anyrr1s r%r.zASTTokens.get_text_positionssa 4 '     " "E //  C #ST__T=RSSQxme #:r')FN N)F)NF)r7r8r9__doc__r&rJrFpropertyr\rLrKrrdrirlrsrvrrrr. __classcell__rNs@r%r?r?Us&#" + E            H ]X    !& \r'r?cPeZdZdZdfd ZedZedZdZdZ xZ S)ASTTextaF Supports the same ``get_text*`` methods as ``ASTTokens``, but uses the AST to determine the text positions instead of tokens. This is faster than ``ASTTokens`` as it requires less setup work. It also (sometimes) supports nodes inside f-strings, which ``ASTTokens`` doesn't. Some node types and/or Python versions are not supported. In these cases the ``get_text*`` methods will fall back to using ``ASTTokens`` which incurs the usual setup cost the first time. If you want to avoid this, check ``supports_tokenless(node)`` before calling ``get_text*`` methods. ctt| ||||_|jt |jd|_yr)rArr&rDr _asttokens)r$rrKrrNs r%r&zASTText.__init__"s8 '4!+x8DJ zzTZZ(DOr'c|jDtj|j|j|_t |j|jSr)rDrBrCr"r rr[s r%rKz ASTText.tree,s> zz99TZZ8djTZZ( ::r'c|j1t|j|j|j|_|jS)N)rKr)rr?r"rKr r[s r% asttokenszASTText.asttokens4s; ! **yy>>do ??r'ct|r0d|jjt|jfSt |ddy|sJt |dg}|st |dd}t |dg}|r|d}n|}|j }t|}|r9||j k7s'||jk7rt |d drt|rd}n&|jj||j}||f} tt|j} tt|j} |jj| | } | | f} | | fS) zF Version of ``get_text_positions()`` that doesn't use tokens. rrgNrdecorator_list decoratorsnodesrdoc_node)rr#offset_to_linelenr"getattrrgr end_linenorrkrhr intend_col_offset) r$r,r-rdecorators_node start_node start_linenoend_nodestart_col_offsetr2rrr3s r%_get_text_positions_tokenlessz%ASTText._get_text_positions_tokenless?sU  T''66s4::G GGtXt$,  K4/4J lD9o?GR8ja=jj$$LH' DOO +j$/ ++99, H]H]^ + ,E c8../J#x667N''55j.QN ~ &C #:r'ct|ddryt|r|j||S|jj ||S)r)_broken_positionsNr)rsupports_tokenlessrrr.r+s r%r.zASTText.get_text_positionssFt($/ $  / /f == >> , ,T6 ::r')Nr) r7r8r9rr&rrKrrr.rrs@r%rrsA     @D;r'r) arguments Argumentswithitem))argStarred)SliceExtSliceIndexkeywordc>t|jtvxrt|tj  xrA|duxr;t|jdk(xr!t|j jdv xr dtjjvS)av Returns True if the Python version and the node (if given) are supported by the ``get_text*`` methods of ``ASTText`` without falling back to ``ASTTokens``. See ``ASTText`` for why this matters. The following cases are not supported: - PyPy - ``ast.arguments`` / ``astroid.Arguments`` - ``ast.withitem`` - ``astroid.Comprehension`` - ``astroid.AssignName`` inside ``astroid.Arguments`` or ``astroid.ExceptHandler`` - The following nodes in Python 3.8 only: - ``ast.arg`` - ``ast.Starred`` - ``ast.Slice`` - ``ast.ExtSlice`` - ``ast.Index`` - ``ast.keyword`` N AssignName)r ExceptHandlerpypy) ror7_unsupported_tokenless_types isinstancerBASTparentsysversionlower)r,s r%rrs. 4j!== , tSWW %% $d*: J  < / MT[[!**.LL   ,  ))+ + r'r)$r:rBrbrryrtypingrrrrrr r r line_numbersr utilrrrrrrrrrrrABCMetarr?rr version_inforr<r'r%rs VVV% &1"CKK1"h} }@~;k~;F FBQ6!"44"KK"r'