B
    .Kc;                 @   s  d Z ddlZddlmZ ddlmZmZmZmZm	Z	m
Z
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dZedZ edZ!edZ"edZ#edZ$edZ%edZ&edZ'e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,Z0G d&d' d'e,Z1G d(d) d)e+Z2G d*d+ d+e+Z3G d,d- d-e)Z4G d.d/ d/e)Z5G d0d1 d1Z6dS )2z2Utility functions common to the C and C++ domains.    N)deepcopy)AnyCallableListMatchOptionalPatternTupleUnion)nodes)TextElement)addnodes)Config)loggingz(?u)\s+z(@[a-zA-Z0-9_])[a-zA-Z0-9_]*\ba
  (?x)
    (   # 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
z[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%  (?x)
    # 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  (?x)
    [+-]?(
    # 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(?x)
    ((?: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})
      ))
    )'
)modereturnc             C   s   | dkrt d|  d S )N)Z
lastIsNameZ
noneIsNameZmarkTypeZmarkNameZparamZudlz!Description mode '%s' is invalid.)	Exception)r    r   2lib/python3.7/site-packages/sphinx/util/cfamily.pyverify_description_modeI   s    r   c               @   s   e Zd ZdS )NoOldIdErrorN)__name__
__module____qualname__r   r   r   r   r   N   s   r   c               @   sz   e Zd ZU eedddZdZeg ef e	d< edddZ
eed	d
dZedddZedddZedddZdS )ASTBaseBase)otherr   c             C   s\   t | t |k	rdS y.x(| j D ]\}}|t||kr"dS q"W W n tk
rV   dS X dS )NFT)type__dict__itemsgetattrAttributeError)selfr   keyvaluer   r   r   __eq__T   s    zASTBaseBase.__eq__N__hash__)r   c             C   s   t | S )N)r   )r!   r   r   r   clonea   s    zASTBaseBase.clone)	transformr   c             C   s   t t| d S )N)NotImplementedErrorrepr)r!   r'   r   r   r   
_stringifyd   s    zASTBaseBase._stringifyc             C   s   |  dd S )Nc             S   s   t | S )N)str)astr   r   r   <lambda>h       z%ASTBaseBase.__str__.<locals>.<lambda>)r*   )r!   r   r   r   __str__g   s    zASTBaseBase.__str__c             C   s   |  dd S )Nc             S   s   |   S )N)get_display_string)r,   r   r   r   r-   k   r.   z0ASTBaseBase.get_display_string.<locals>.<lambda>)r*   )r!   r   r   r   r0   j   s    zASTBaseBase.get_display_stringc             C   s   d| j j S )Nz<%s>)	__class__r   )r!   r   r   r   __repr__m   s    zASTBaseBase.__repr__)r   r   r   r   boolr$   r%   r   int__annotations__r&   StringifyTransformr+   r*   r/   r0   r2   r   r   r   r   r   S   s   
r   c               @   s   e Zd ZeddddZdS )ASTAttributeN)signoder   c             C   s   t t| d S )N)r(   r)   )r!   r8   r   r   r   describe_signaturev   s    zASTAttribute.describe_signature)r   r   r   r   r9   r   r   r   r   r7   u   s   r7   c               @   s<   e Zd ZeddddZeedddZeddd	d
ZdS )ASTCPPAttributeN)argr   c             C   s
   || _ d S )N)r;   )r!   r;   r   r   r   __init__{   s    zASTCPPAttribute.__init__)r'   r   c             C   s   d| j  d S )Nz[[z]])r;   )r!   r'   r   r   r   r*   ~   s    zASTCPPAttribute._stringify)r8   r   c             C   s:   | tdd | t| j | tdd d S )Nz[[z]])appendr   Zdesc_sig_punctuationr   Textr;   )r!   r8   r   r   r   r9      s    z"ASTCPPAttribute.describe_signature)	r   r   r   r+   r<   r6   r*   r   r9   r   r   r   r   r:   z   s   r:   c               @   s2   e Zd Zeed ddddZeedddZdS )	ASTGnuAttributeASTBaseParenExprListN)nameargsr   c             C   s   || _ || _d S )N)rA   rB   )r!   rA   rB   r   r   r   r<      s    zASTGnuAttribute.__init__)r'   r   c             C   s(   | j g}| jr||| j d|S )N )rA   rB   r=   join)r!   r'   resr   r   r   r*      s    zASTGnuAttribute._stringify)r   r   r   r+   r   r<   r6   r*   r   r   r   r   r?      s   r?   c               @   s@   e Zd Zee ddddZeedddZe	ddd	d
Z
dS )ASTGnuAttributeListN)attrsr   c             C   s
   || _ d S )N)rG   )r!   rG   r   r   r   r<      s    zASTGnuAttributeList.__init__)r'   r   c             C   sN   dg}d}x.| j D ]$}|s$|d d}||| qW |d d|S )Nz__attribute__((Tz, Fz))rC   )rG   r=   rD   )r!   r'   rE   firstattrr   r   r   r*      s    

zASTGnuAttributeList._stringify)r8   r   c             C   s   t | }|t| d S )N)r+   r=   r   r>   )r!   r8   txtr   r   r   r9      s    z&ASTGnuAttributeList.describe_signature)r   r   r   r   r?   r<   r6   r+   r*   r   r9   r   r   r   r   rF      s   rF   c               @   s@   e Zd ZdZeddddZeedddZedd	d
dZ	dS )ASTIdAttributez*For simple attributes defined by the user.N)idr   c             C   s
   || _ d S )N)rL   )r!   rL   r   r   r   r<      s    zASTIdAttribute.__init__)r'   r   c             C   s   | j S )N)rL   )r!   r'   r   r   r   r*      s    zASTIdAttribute._stringify)r8   r   c             C   s   | t| j d S )N)r=   r   r>   rL   )r!   r8   r   r   r   r9      s    z!ASTIdAttribute.describe_signature)
r   r   r   __doc__r+   r<   r6   r*   r   r9   r   r   r   r   rK      s   rK   c               @   sB   e Zd ZdZeeddddZeedddZedd	d
dZ	dS )ASTParenAttributez)For paren attributes defined by the user.N)rL   r;   r   c             C   s   || _ || _d S )N)rL   r;   )r!   rL   r;   r   r   r   r<      s    zASTParenAttribute.__init__)r'   r   c             C   s   | j d | j d S )N())rL   r;   )r!   r'   r   r   r   r*      s    zASTParenAttribute._stringify)r8   r   c             C   s   t | }|t| d S )N)r+   r=   r   r>   )r!   r8   rJ   r   r   r   r9      s    z$ASTParenAttribute.describe_signature)
r   r   r   rM   r+   r<   r6   r*   r   r9   r   r   r   r   rN      s   rN   c               @   s^   e Zd Zee ddddZedddZd d dd	d
Ze	e
dddZeddddZdS )ASTAttributeListN)rG   r   c             C   s
   || _ d S )N)rG   )r!   rG   r   r   r   r<      s    zASTAttributeList.__init__)r   c             C   s
   t | jS )N)lenrG   )r!   r   r   r   __len__   s    zASTAttributeList.__len__)r   r   c             C   s   t | j|j S )N)rQ   rG   )r!   r   r   r   r   __add__   s    zASTAttributeList.__add__)r'   r   c                s   d  fdd| jD S )N c             3   s   | ]} |V  qd S )Nr   ).0rI   )r'   r   r   	<genexpr>   s    z.ASTAttributeList._stringify.<locals>.<genexpr>)rD   rG   )r!   r'   r   )r'   r   r*      s    zASTAttributeList._stringify)r8   r   c             C   sh   t | jdkrd S | jd | t | jdkr4d S x.| jdd  D ]}|t  || qDW d S )Nr      )rR   rG   r9   r=   r   Zdesc_sig_space)r!   r8   rI   r   r   r   r9      s    z#ASTAttributeList.describe_signature)r   r   r   r   r7   r<   r4   rS   rT   r6   r+   r*   r   r9   r   r   r   r   rQ      s
   rQ   c               @   s   e Zd ZdS )r@   N)r   r   r   r   r   r   r   r@      s   r@   c               @   s   e Zd ZdS )$UnsupportedMultiCharacterCharLiteralN)r   r   r   r   r   r   r   rY      s   rY   c               @   s   e Zd ZdS )DefinitionErrorN)r   r   r   r   r   r   r   rZ      s   rZ   c               @   s  e Zd Zeeejeeef ef dddddZ	e
e eedddZeed	d
dZeddddZeddddZeddddZeedddZeedddZeedddZed	ddZeedddZeedd d!Zeed	d"d#Zeed	d$d%Zeed	d&d'Zed	d(d)Zd*d+edd,d-d.Zed/d0 Z ed1d2 Z!e
e ed3d4d5Z"e#e$ d	d6d7Z%e&d	d8d9Z'e(d	d:d;Z)dS )<
BaseParserr   N)
definitionlocationconfigr   c            C   sD   |  | _|| _|| _d| _t| j| _d | _d| _g | _	d| _
d S )Nr   )r   NT)stripr\   r]   r^   posrR   end
last_match_previous_stateotherErrorsZallowFallbackExpressionParsing)r!   r\   r]   r^   r   r   r   r<      s    
zBaseParser.__init__)errorsheaderr   c             C   s   t |dkrHt |dkr4t|d t|d d  S tt|d d S |dg}x|D ]}t |d dkrd}||d  |d xZt|d dD ]0}t |dkrq|| || |d qW qV|t|d  qVW td|S )NrX   r   
z  z:
rC   )rR   rZ   r+   r=   splitrD   )r!   re   rf   resulteindentliner   r   r   _make_multi_error   s$    



zBaseParser._make_multi_error)r   c             C   s   t d S )N)r(   )r!   r   r   r   language  s    zBaseParser.language)msgr   c             C   s&   d| j  d }td|| j|f  d S )N-^z%s
%s
%s)r`   printr\   )r!   ro   	indicatorr   r   r   status  s    zBaseParser.statusc             C   sp   g }d| j  d }td| j|| j | j|f }||df x| jD ]}||df qDW g | _| |dd S )Nrp   rq   z2Invalid %s declaration: %s [error at %d]
  %s
  %sz
Main errorzPotential other errorrC   )r`   rZ   rn   r\   r=   rd   rm   )r!   ro   re   rs   ZexMainerrr   r   r   fail  s    zBaseParser.failc             C   s   t j|| jd d S )N)r]   )loggerZwarningr]   )r!   ro   r   r   r   warn'  s    zBaseParser.warn)regexr   c             C   s>   | | j| j}|d k	r:| j| jf| _| | _|| _dS dS )NTF)matchr\   r`   rb   rc   ra   )r!   ry   rz   r   r   r   rz   *  s    
zBaseParser.match)stringr   c             C   s8   t |}| j| j| j|  |kr4|  j|7  _dS dS )NTF)rR   r\   r`   )r!   r{   Zstrlenr   r   r   skip_string3  s
    zBaseParser.skip_string)wordr   c             C   s   |  tdt| S )Nz\b%s\b)rz   recompileescape)r!   r}   r   r   r   	skip_word:  s    zBaseParser.skip_wordc             C   s
   |  tS )N)rz   _whitespace_re)r!   r   r   r   skip_ws=  s    zBaseParser.skip_wsc             C   s   |  |r|   dS dS )NTF)r   r   )r!   r}   r   r   r   skip_word_and_ws@  s    
zBaseParser.skip_word_and_wsc             C   s   |  |r|   dS dS )NTF)r|   r   )r!   r{   r   r   r   skip_string_and_wsF  s    
zBaseParser.skip_string_and_wsc             C   s   | j | jkS )N)r`   ra   )r!   r   r   r   eofL  s    zBaseParser.eofc             C   s&   y| j | j S  tk
r    dS X d S )NZEOF)r\   r`   
IndexError)r!   r   r   r   current_charP  s    zBaseParser.current_charc             C   s   | j d k	r| j  S d S d S )N)rb   group)r!   r   r   r   matched_textW  s    

zBaseParser.matched_textc             C   s   | j | jd  }| j| _|S )N)r\   r`   ra   )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   r\   r`   rv   )r!   r   r   r   r   
assert_endc  s    zBaseParser.assert_endc             C   s   t d S )N)r(   )r!   r   r   r   id_attributesn  s    zBaseParser.id_attributesc             C   s   t d S )N)r(   )r!   r   r   r   paren_attributesr  s    zBaseParser.paren_attributes)ra   r   c             C   s   dddd}| j }g }x| jst|dkr6| j|kr6P | j|krR||| j  n>t|dkrv| j|d krv|  n| jdkr| d| j  |  j d	7  _ qW | jr| d
|  | j|| j  S )NrP   ]})rO   [{r   z)]}z&Unexpected '%s' in balanced-token-seq.rX   z8Could not find end of balanced-token-seq starting at %d.)r`   r   rR   r   r=   poprv   r\   )r!   ra   ZbracketsstartPosZsymbolsr   r   r   _parse_balanced_token_seqv  s"    


z$BaseParser._parse_balanced_token_seqc             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 }xR| t	r| j
}|  }|t|| | d
rqq| drP q| d qW | ds
| d t|S x$| jD ]}| |rt|S qW x`| jD ]V}| |sTq@| dsj| d | jdgd}| ds| d t||S W d S )Nr   r   )ra   z!Expected ']' in end of attribute.z-Expected ']' in end of attribute after [[...]Z__attribute__rO   z#Expected '(' after '__attribute__'.z$Expected '(' after '__attribute__('.,rP   z2Expected identifier, ')', or ',' in __attribute__.z(Expected ')' after '__attribute__((...)'z0Expected '(' after user-defined paren-attribute.z1Expected ')' to end user-defined paren-attribute.)r   r`   r   r|   r   rv   r:   r   rz   identifier_rer   _parse_paren_expression_listr=   r?   rF   r   rK   r   rN   )r!   r   r;   rG   rA   ZexprsrL   r   r   r   _parse_attribute  sV    















zBaseParser._parse_attributec             C   s.   g }x |   }|d krP || qW t|S )N)r   r=   rQ   )r!   rE   rI   r   r   r   _parse_attribute_list  s    z BaseParser._parse_attribute_listc             C   s   t d S )N)r(   )r!   r   r   r   r     s    z'BaseParser._parse_paren_expression_list)*r   r   r   r+   r
   r   ZNoder	   r4   r<   r   r   rZ   rm   propertyrn   rt   rv   rx   r   r3   rz   r|   r   r   r   r   r   r   r   r   r   r   r   r   r   r7   r   rQ   r   r@   r   r   r   r   r   r[      s:   	8	r[   )7rM   r~   copyr   typingr   r   r   r   r   r   r	   r
   Zdocutilsr   Zdocutils.nodesr   Zsphinxr   Zsphinx.configr   Zsphinx.utilr   Z	getLoggerr   rw   r+   r6   r   r   Zanon_identifier_rer   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   r7   r:   r?   rF   rK   rN   rQ   r@   rY   rZ   r[   r   r   r   r   <module>   sN   (






	
"