B
    d;                 @   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markNameparamZudlz!Description mode '%s' is invalid.)	Exception)r    r   `/work/yifan.wang/ringdown/master-ringdown-env/lib/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   r1   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+   r0   r1   r3   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"   r9   r   r   r   describe_signaturev   s    zASTAttribute.describe_signature)r   r   r   r   r:   r   r   r   r   r8   u   s   r8   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)r9   r   c             C   s:   | tdd | t| j | tdd d S )Nz[[z]])appendr   Zdesc_sig_punctuationr   Textr<   )r"   r9   r   r   r   r:      s    z"ASTCPPAttribute.describe_signature)	r   r   r   r,   r=   r7   r+   r   r:   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)rB   rC   )r"   rB   rC   r   r   r   r=      s    zASTGnuAttribute.__init__)r(   r   c             C   s(   | j g}| jr||| j d|S )N )rB   rC   r>   join)r"   r(   resr   r   r   r+      s    zASTGnuAttribute._stringify)r   r   r   r,   r   r=   r7   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)rH   )r"   rH   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))rD   )rH   r>   rE   )r"   r(   rF   firstattrr   r   r   r+      s    

zASTGnuAttributeList._stringify)r9   r   c             C   s   t | }|t| d S )N)r,   r>   r   r?   )r"   r9   txtr   r   r   r:      s    z&ASTGnuAttributeList.describe_signature)r   r   r   r   r@   r=   r7   r,   r+   r   r:   r   r   r   r   rG      s   rG   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)rM   )r"   rM   r   r   r   r=      s    zASTIdAttribute.__init__)r(   r   c             C   s   | j S )N)rM   )r"   r(   r   r   r   r+      s    zASTIdAttribute._stringify)r9   r   c             C   s   | t| j d S )N)r>   r   r?   rM   )r"   r9   r   r   r   r:      s    z!ASTIdAttribute.describe_signature)
r   r   r   __doc__r,   r=   r7   r+   r   r:   r   r   r   r   rL      s   rL   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)rM   r<   r   c             C   s   || _ || _d S )N)rM   r<   )r"   rM   r<   r   r   r   r=      s    zASTParenAttribute.__init__)r(   r   c             C   s   | j d | j d S )N())rM   r<   )r"   r(   r   r   r   r+      s    zASTParenAttribute._stringify)r9   r   c             C   s   t | }|t| d S )N)r,   r>   r   r?   )r"   r9   rK   r   r   r   r:      s    z$ASTParenAttribute.describe_signature)
r   r   r   rN   r,   r=   r7   r+   r   r:   r   r   r   r   rO      s   rO   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)rH   r   c             C   s
   || _ d S )N)rH   )r"   rH   r   r   r   r=      s    zASTAttributeList.__init__)r   c             C   s
   t | jS )N)lenrH   )r"   r   r   r   __len__   s    zASTAttributeList.__len__)r   r   c             C   s   t | j|j S )N)rR   rH   )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   ).0rJ   )r(   r   r   	<genexpr>   s    z.ASTAttributeList._stringify.<locals>.<genexpr>)rE   rH   )r"   r(   r   )r(   r   r+      s    zASTAttributeList._stringify)r9   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      )rS   rH   r:   r>   r   Zdesc_sig_space)r"   r9   rJ   r   r   r   r:      s    z#ASTAttributeList.describe_signature)r   r   r   r   r8   r=   r5   rT   rU   r7   r,   r+   r   r:   r   r   r   r   rR      s
   rR   c               @   s   e Zd ZdS )rA   N)r   r   r   r   r   r   r   rA      s   rA   c               @   s   e Zd ZdS )$UnsupportedMultiCharacterCharLiteralN)r   r   r   r   r   r   r   rZ      s   rZ   c               @   s   e Zd ZdS )DefinitionErrorN)r   r   r   r   r   r   r   r[      s   r[   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_   posrS   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 )NrY   r   
z  z:
rD   )rS   r[   r,   r>   splitrE   )r"   rf   rg   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)ra   printr]   )r"   rp   	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 )Nrq   rr   z2Invalid %s declaration: %s [error at %d]
  %s
  %sz
Main errorzPotential other errorrD   )ra   r[   ro   r]   r>   re   rn   )r"   rp   rf   rt   ZexMainerrr   r   r   fail  s    zBaseParser.failc             C   s   t j|| jd d S )N)r^   )loggerwarningr^   )r"   rp   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]   ra   rc   rd   rb   )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 )NTF)rS   r]   ra   )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)r|   recompileescape)r"   r   r   r   r   	skip_word:  s    zBaseParser.skip_wordc             C   s
   |  tS )N)r|   _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)ra   rb   )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]   ra   
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)rc   group)r"   r   r   r   matched_textW  s    

zBaseParser.matched_textc             C   s   | j | jd  }| j| _|S )N)r]   ra   rb   )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]   ra   rw   )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)rb   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 )NrQ   ]})rP   [{r   z)]}z&Unexpected '%s' in balanced-token-seq.rY   z8Could not find end of balanced-token-seq starting at %d.)ra   r   rS   r   r>   poprw   r]   )r"   rb   ZbracketsstartPos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   )rb   z!Expected ']' in end of attribute.z-Expected ']' in end of attribute after [[...]Z__attribute__rP   z#Expected '(' after '__attribute__'.z$Expected '(' after '__attribute__('.,rQ   z2Expected identifier, ')', or ',' in __attribute__.z(Expected ')' after '__attribute__((...)'z0Expected '(' after user-defined paren-attribute.z1Expected ')' to end user-defined paren-attribute.)r   ra   r   r~   r   rw   r;   r   r|   identifier_rer   _parse_paren_expression_listr>   r@   rG   r   rL   r   rO   )r"   r   r<   rH   rB   exprsrM   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>   rR   )r"   rF   rJ   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   Noder	   r5   r=   r   r   r[   rn   propertyro   ru   rw   rz   r   r4   r|   r~   r   r   r   r   r   r   r   r   r   r   r   r   r   r8   r   rR   r   rA   r   r   r   r   r   r\      s:   	8	r\   )7rN   r   copyr   typingr   r   r   r   r   r   r	   r
   Zdocutilsr   Zdocutils.nodesr   Zsphinxr   Zsphinx.configr   Zsphinx.utilr   	getLoggerr   rx   r,   r7   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   r8   r;   r@   rG   rL   rO   rR   rA   rZ   r[   r\   r   r   r   r   <module>   sN   (






	
"