U
    \Sh<                     @  s  d Z ddlmZ ddlZddlmZ ddlmZm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eZeegef ZedZedZejdejdZedZedZedZedZ ejdejdZ!ejdejdZ"edZ#ejdejdZ$dddddZ%G dd de&Z'G dd dZ(G d d! d!e(Z)G d"d# d#e)Z*G d$d% d%e(Z+G d&d' d'e)Z,G d(d) d)e)Z-G d*d+ d+e)Z.G d,d- d-e(Z/G d.d/ d/e(Z0G d0d1 d1e&Z1G d2d3 d3e&Z2G d4d5 d5Z3dS )6z2Utility functions common to the C and C++ domains.    )annotationsNdeepcopy)AnyCallable)nodes)TextElement)addnodes)Config)loggingz\s+z(@[a-zA-Z0-9_])[a-zA-Z0-9_]*\ba  
    (   # This 'extends' _anon_identifier_re with the ordinary identifiers,
        # make sure they are in sync.
        (~?\b[a-zA-Z_])  # ordinary identifiers
    |   (@[a-zA-Z0-9_])  # our extension for names of anonymous entities
    )
    [a-zA-Z0-9_]*\b
)flagsz[1-9][0-9]*(\'[0-9]+)*z0[0-7]*(\'[0-7]+)*z"0[xX][0-9a-fA-F]+(\'[0-9a-fA-F]+)*z0[bB][01]+(\'[01]+)*a!  
    # unsigned and/or (long) long, in any order, but at least one of them
    (
        ([uU]    ([lL]  |  (ll)  |  (LL))?)
        |
        (([lL]  |  (ll)  |  (LL))    [uU]?)
    )\b
    # the ending word boundary is important for distinguishing
    # between suffixes and UDLs in C++
a
  
    [+-]?(
    # decimal
      ([0-9]+(\'[0-9]+)*[eE][+-]?[0-9]+(\'[0-9]+)*)
    | (([0-9]+(\'[0-9]+)*)?\.[0-9]+(\'[0-9]+)*([eE][+-]?[0-9]+(\'[0-9]+)*)?)
    | ([0-9]+(\'[0-9]+)*\.([eE][+-]?[0-9]+(\'[0-9]+)*)?)
    # hex
    | (0[xX][0-9a-fA-F]+(\'[0-9a-fA-F]+)*[pP][+-]?[0-9a-fA-F]+(\'[0-9a-fA-F]+)*)
    | (0[xX]([0-9a-fA-F]+(\'[0-9a-fA-F]+)*)?\.
        [0-9a-fA-F]+(\'[0-9a-fA-F]+)*([pP][+-]?[0-9a-fA-F]+(\'[0-9a-fA-F]+)*)?)
    | (0[xX][0-9a-fA-F]+(\'[0-9a-fA-F]+)*\.([pP][+-]?[0-9a-fA-F]+(\'[0-9a-fA-F]+)*)?)
    )
z[fFlL]\bz
    ((?:u8)|u|U|L)?
    '(
      (?:[^\\'])
    | (\\(
        (?:['"?\\abfnrtv])
      | (?:[0-7]{1,3})
      | (?:x[0-9a-fA-F]{2})
      | (?:u[0-9a-fA-F]{4})
      | (?:U[0-9a-fA-F]{8})
      ))
    )'
strNone)modereturnc                 C  s   | dkrt d|  d S )N)Z
lastIsNameZ
noneIsNameZmarkTypeZmarkNameparamZudlz!Description mode '%s' is invalid.)	Exception)r    r   F/root/rtd-docs/venv/lib/python3.8/site-packages/sphinx/util/cfamily.pyverify_description_modeK   s    r   c                   @  s   e Zd ZdS )NoOldIdErrorN__name__
__module____qualname__r   r   r   r   r   P   s   r   c                   @  sh   e Zd ZdddddZdZdddd	Zd
ddddZddddZddddZddddZ	dS )ASTBaseBaser   boolotherr   c                 C  s^   t | t |k	rdS z.| j D ]\}}|t||kr  W dS q W n tk
rX   Y dS X dS )NFT)type__dict__itemsgetattrAttributeError)selfr   keyvaluer   r   r   __eq__V   s    zASTBaseBase.__eq__Nr   c                 C  s   t | S Nr   r$   r   r   r   cloned   s    zASTBaseBase.cloneStringifyTransformr   	transformr   c                 C  s   t t| d S r)   NotImplementedErrorreprr$   r.   r   r   r   
_stringifyg   s    zASTBaseBase._stringifyc                 C  s   |  dd S )Nc                 S  s   t | S r)   )r   astr   r   r   <lambda>k       z%ASTBaseBase.__str__.<locals>.<lambda>r3   r*   r   r   r   __str__j   s    zASTBaseBase.__str__c                 C  s   |  dd S )Nc                 S  s   |   S r)   )get_display_stringr4   r   r   r   r6   n   r7   z0ASTBaseBase.get_display_string.<locals>.<lambda>r8   r*   r   r   r   r:   m   s    zASTBaseBase.get_display_stringc                 C  s   d| j j S )Nz<%s>)	__class__r   r*   r   r   r   __repr__p   s    zASTBaseBase.__repr__)
r   r   r   r'   __hash__r+   r3   r9   r:   r<   r   r   r   r   r   U   s   r   c                   @  s   e Zd ZdddddZdS )ASTAttributer   r   signoder   c                 C  s   t t| d S r)   r/   r$   r@   r   r   r   describe_signaturey   s    zASTAttribute.describe_signatureN)r   r   r   rB   r   r   r   r   r>   x   s   r>   c                   @  s<   e Zd ZdddddZddddd	Zd
ddddZdS )ASTCPPAttributer   r   )argr   c                 C  s
   || _ d S r)   rD   )r$   rD   r   r   r   __init__~   s    zASTCPPAttribute.__init__r,   r-   c                 C  s   d| j  d S Nz[[z]]rE   r2   r   r   r   r3      s    zASTCPPAttribute._stringifyr   r?   c                 C  s:   | tdd | t| j | tdd d S rG   )appendr	   Zdesc_sig_punctuationr   TextrD   rA   r   r   r   rB      s    z"ASTCPPAttribute.describe_signatureNr   r   r   rF   r3   rB   r   r   r   r   rC   }   s   rC   c                   @  s.   e Zd ZddddddZdddd	d
ZdS )ASTGnuAttributer   ASTBaseParenExprList | Noner   )nameargsr   c                 C  s   || _ || _d S r)   )rM   rN   )r$   rM   rN   r   r   r   rF      s    zASTGnuAttribute.__init__r,   r-   c                 C  s(   | j g}| jr||| j d|S N )rM   rN   rH   join)r$   r.   resr   r   r   r3      s    zASTGnuAttribute._stringifyN)r   r   r   rF   r3   r   r   r   r   rK      s   rK   c                   @  s<   e Zd ZdddddZdddd	d
ZdddddZdS )ASTGnuAttributeListzlist[ASTGnuAttribute]r   attrsr   c                 C  s
   || _ d S r)   rU   r$   rU   r   r   r   rF      s    zASTGnuAttributeList.__init__r,   r   r-   c                 C  sJ   dg}d}| j D ]$}|s"|d d}||| q|d d|S )Nz__attribute__((Tz, Fz))rP   )rU   rH   rQ   )r$   r.   rR   firstattrr   r   r   r3      s    


zASTGnuAttributeList._stringifyr   r?   c                 C  s   t | }|t| d S r)   r   rH   r   rI   r$   r@   txtr   r   r   rB      s    z&ASTGnuAttributeList.describe_signatureNrJ   r   r   r   r   rS      s   rS   c                   @  s@   e Zd ZdZdddddZdddd	d
ZdddddZdS )ASTIdAttributez*For simple attributes defined by the user.r   r   )idr   c                 C  s
   || _ d S r)   r^   )r$   r^   r   r   r   rF      s    zASTIdAttribute.__init__r,   r-   c                 C  s   | j S r)   r_   r2   r   r   r   r3      s    zASTIdAttribute._stringifyr   r?   c                 C  s   | t| j d S r)   )rH   r   rI   r^   rA   r   r   r   rB      s    z!ASTIdAttribute.describe_signatureNr   r   r   __doc__rF   r3   rB   r   r   r   r   r]      s   r]   c                   @  sB   e Zd ZdZddddddZdddd	d
ZdddddZdS )ASTParenAttributez)For paren attributes defined by the user.r   r   )r^   rD   r   c                 C  s   || _ || _d S r)   r^   rD   )r$   r^   rD   r   r   r   rF      s    zASTParenAttribute.__init__r,   r-   c                 C  s   | j d | j d S )N()rc   r2   r   r   r   r3      s    zASTParenAttribute._stringifyr   r?   c                 C  s   t | }|t| d S r)   rZ   r[   r   r   r   rB      s    z$ASTParenAttribute.describe_signatureNr`   r   r   r   r   rb      s   rb   c                   @  sZ   e Zd ZdddddZdddd	Zd d d
ddZdddddZdddddZdS )ASTAttributeListzlist[ASTAttribute]r   rT   c                 C  s
   || _ d S r)   rV   rW   r   r   r   rF      s    zASTAttributeList.__init__intr(   c                 C  s
   t | jS r)   )lenrU   r*   r   r   r   __len__   s    zASTAttributeList.__len__r   c                 C  s   t | j|j S r)   )rf   rU   )r$   r   r   r   r   __add__   s    zASTAttributeList.__add__r,   r   r-   c                   s   d  fdd| jD S )N c                 3  s   | ]} |V  qd S r)   r   ).0rY   r.   r   r   	<genexpr>   s     z.ASTAttributeList._stringify.<locals>.<genexpr>)rQ   rU   r2   r   rm   r   r3      s    zASTAttributeList._stringifyr   r?   c                 C  sd   t | jdkrd S | jd | t | jdkr4d S | jdd  D ]}|t  || qBd S )Nr      )rh   rU   rB   rH   r	   Zdesc_sig_space)r$   r@   rY   r   r   r   rB      s    z#ASTAttributeList.describe_signatureN)r   r   r   rF   ri   rj   r3   rB   r   r   r   r   rf      s
   rf   c                   @  s   e Zd ZdS )ASTBaseParenExprListNr   r   r   r   r   rp      s   rp   c                   @  s   e Zd ZdS )$UnsupportedMultiCharacterCharLiteralNr   r   r   r   r   rq      s   rq   c                   @  s   e Zd ZdS )DefinitionErrorNr   r   r   r   r   rr      s   rr   c                   @  s~  e Zd ZdddddddZddd	d
ddZeddddZdddddZdddddZdddddZ	dddddZ
dddddZdddd d!Zddd"d#Zdddd$d%Zdddd&d'Zeddd(d)Zeddd*d+Zeddd,d-Zddd.d/Zd0d1ddd2d3d4Zed5d6 Zed7d8 Zd9dd:d;d<Zd=dd>d?Zd@ddAdBZdCddDdEZdFS )G
BaseParserr   z"nodes.Node | tuple[str, int] | strr
   r   )
definitionlocationconfigr   c                C  sD   |  | _|| _|| _d| _t| j| _d | _d| _g | _	d| _
d S )Nr   )r   NT)striprt   ru   rv   posrh   end
last_match_previous_stateotherErrorsZallowFallbackExpressionParsing)r$   rt   ru   rv   r   r   r   rF      s    
zBaseParser.__init__z	list[Any]rr   )errorsheaderr   c                 C  s   t |dkrHt |dkr4t|d t|d d  S tt|d d S |dg}|D ]}t |d dkrd}||d  |d t|d dD ]0}t |dkrq|| || |d qqT|t|d  qTtd|S )Nro   r   
z  z:
rP   )rh   rr   r   rH   splitrQ   )r$   r}   r~   resulteindentliner   r   r   _make_multi_error   s$    


zBaseParser._make_multi_errorr(   c                 C  s   t d S r)   r0   r*   r   r   r   language  s    zBaseParser.language)msgr   c                 C  s,   d| j  d }t| d| j d|  d S )N-^r   )rx   printrt   )r$   r   	indicatorr   r   r   status  s    zBaseParser.statusc                 C  sl   g }d| j  d }td| j|| j | j|f }||df | jD ]}||df qBg | _| |dd S )Nr   r   z2Invalid %s declaration: %s [error at %d]
  %s
  %sz
Main errorzPotential other errorrP   )rx   rr   r   rt   rH   r|   r   )r$   r   r}   r   ZexMainerrr   r   r   fail  s    
zBaseParser.failc                 C  s   t j|| jd d S )N)ru   )loggerwarningru   )r$   r   r   r   r   warn*  s    zBaseParser.warnzre.Pattern[str]r   )regexr   c                 C  s>   | | j| j}|d k	r:| j| jf| _| | _|| _dS dS NTF)matchrt   rx   rz   r{   ry   )r$   r   r   r   r   r   r   -  s    
zBaseParser.match)stringr   c                 C  s8   t |}| j| j| j|  |kr4|  j|7  _dS dS r   )rh   rt   rx   )r$   r   Zstrlenr   r   r   skip_string6  s
    zBaseParser.skip_string)wordr   c                 C  s   |  tdt| S )Nz\b%s\b)r   recompileescaper$   r   r   r   r   	skip_word=  s    zBaseParser.skip_wordc                 C  s
   |  tS r)   )r   _whitespace_rer*   r   r   r   skip_ws@  s    zBaseParser.skip_wsc                 C  s   |  |r|   dS dS r   )r   r   r   r   r   r   skip_word_and_wsC  s    
zBaseParser.skip_word_and_wsc                 C  s   |  |r|   dS dS r   )r   r   )r$   r   r   r   r   skip_string_and_wsI  s    
zBaseParser.skip_string_and_wsc                 C  s   | j | jkS r)   )rx   ry   r*   r   r   r   eofO  s    zBaseParser.eofc                 C  s*   z| j | j W S  tk
r$   Y dS X d S )NZEOF)rt   rx   
IndexErrorr*   r   r   r   current_charS  s    zBaseParser.current_charc                 C  s   | j d k	r| j  S dS rO   )rz   groupr*   r   r   r   matched_textZ  s    

zBaseParser.matched_textc                 C  s   | j | jd  }| j| _|S r)   )rt   rx   ry   )r$   rvr   r   r   	read_rest`  s    zBaseParser.read_restF)allowSemicolon)r   r   c                C  sF   |    |r2| jsB| j| jd  dkrB| d n| jsB| d d S )N;z Expected end of definition or ;.zExpected end of definition.)r   r   rt   rx   r   )r$   r   r   r   r   
assert_ende  s    zBaseParser.assert_endc                 C  s   t d S r)   r   r*   r   r   r   id_attributesp  s    zBaseParser.id_attributesc                 C  s   t d S r)   r   r*   r   r   r   paren_attributest  s    zBaseParser.paren_attributesz	list[str])ry   r   c                 C  s   dddd}| j }g }| jst|dkr4| j|kr4q| j|krP||| j  n>t|dkrt| j|d krt|  n| jdkr| d| j  |  j d	7  _ q| jr| d
|  | j|| j  S )Nre   ]})rd   [{r   z)]}z&Unexpected '%s' in balanced-token-seq.ro   z8Could not find end of balanced-token-seq starting at %d.)rx   r   rh   r   rH   popr   rt   )r$   ry   ZbracketsstartPossymbolsr   r   r   _parse_balanced_token_seqx  s$    


z$BaseParser._parse_balanced_token_seqzASTAttribute | Nonec                 C  s  |    | j}| drh| ds*|| _n>| jdgd}| dsL| d | ds`| d t|S | dr
| ds| d | ds| d	 g }| t	r| j
}|  }|t|| | d
rq| drq| d q| ds| d t|S | jD ]}| |rt|  S q| jD ]Z}| |sJq6| ds`| d | jdgd}| ds| d t||  S d S )Nr   r   )ry   z!Expected ']' in end of attribute.z-Expected ']' in end of attribute after [[...]Z__attribute__rd   z#Expected '(' after '__attribute__'.z$Expected '(' after '__attribute__('.,re   z2Expected identifier, ')', or ',' in __attribute__.z(Expected ')' after '__attribute__((...)'z0Expected '(' after user-defined paren-attribute.z1Expected ')' to end user-defined paren-attribute.)r   rx   r   r   r   r   rC   r   r   identifier_rer   _parse_paren_expression_listrH   rK   rS   r   r]   r   rb   )r$   r   rD   rU   rM   Zexprsr^   r   r   r   _parse_attribute  sT    

















zBaseParser._parse_attributerf   c                 C  s*   g }|   }|d krq"|| qt|S r)   )r   rH   rf   )r$   rR   rY   r   r   r   _parse_attribute_list  s    z BaseParser._parse_attribute_listrL   c                 C  s   t d S r)   r   r*   r   r   r   r     s    z'BaseParser._parse_paren_expression_listN)r   r   r   rF   r   propertyr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rs      s:   	

7	rs   )4ra   
__future__r   r   copyr   typingr   r   Zdocutilsr   Zdocutils.nodesr   Zsphinxr	   Zsphinx.configr
   Zsphinx.utilr   	getLoggerr   r   r   r,   r   r   Zanon_identifier_reVERBOSEr   Zinteger_literal_reZoctal_literal_reZhex_literal_reZbinary_literal_reZintegers_literal_suffix_reZfloat_literal_reZfloat_literal_suffix_reZchar_literal_rer   r   r   r   r>   rC   rK   rS   r]   rb   rf   rp   rq   rr   rs   r   r   r   r   <module>   sX   






	

#