
    FeZ<                       d Z ddlmZ ddlZddlmZ ddlmZmZm	Z	 ddl
mZ ddlmZ ddlmZ erdd	lmZ dd
lmZ  ej(                  e      Ze	egef   Z ej2                  d      Z ej2                  d      Z ej2                  dej8                        Z ej2                  d      Z ej2                  d      Z ej2                  d      Z  ej2                  d      Z! ej2                  dej8                        Z" ej2                  dej8                        Z# ej2                  d      Z$ ej2                  dej8                        Z%d2dZ& G d de'      Z( G d d      Z) G d de)      Z* G d de*      Z+ G d  d!e)      Z, G d" d#e*      Z- G d$ d%e*      Z. G d& d'e*      Z/ G d( d)e)      Z0 G d* d+e)      Z1 G d, d-e'      Z2 G d. d/e'      Z3 G d0 d1      Z4y)3z2Utility functions common to the C and C++ domains.    )annotationsNdeepcopy)TYPE_CHECKINGAnyCallable)nodes)addnodes)logging)TextElement)Configz\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})
      ))
    )'
c                (    | dvrt        d| z        y )N)
lastIsName
noneIsNamemarkTypemarkNameparamudlz!Description mode '%s' is invalid.)	Exception)modes    5/usr/lib/python3/dist-packages/sphinx/util/cfamily.pyverify_description_moder   N   s"    WW;dBCC X    c                      e Zd Zy)NoOldIdErrorN__name__
__module____qualname__ r   r   r   r   S   s    r   r   c                  @    e Zd ZddZdZd	dZd
dZddZddZddZ	y)ASTBaseBasec                    t        |       t        |      ury	 | j                  j                         D ]  \  }}|t        ||      k7  s y 	 y# t        $ r Y yw xY w)NFT)type__dict__itemsgetattrAttributeError)selfotherkeyvalues       r   __eq__zASTBaseBase.__eq__Y   se    :T%[(	"mm113 !
UGE3// !
   		s   /A 	A A 	AANc                    t        |       S Nr   r*   s    r   clonezASTBaseBase.cloneg   s    ~r   c                *    t        t        |             r0   NotImplementedErrorreprr*   	transforms     r   
_stringifyzASTBaseBase._stringifyj       !$t*--r   c                &    | j                  d       S )Nc                    t        |       S r0   )strasts    r   <lambda>z%ASTBaseBase.__str__.<locals>.<lambda>n   s
    3s8 r   r9   r1   s    r   __str__zASTBaseBase.__str__m   s    344r   c                &    | j                  d       S )Nc                "    | j                         S r0   )get_display_stringr>   s    r   r@   z0ASTBaseBase.get_display_string.<locals>.<lambda>q   s    3+A+A+C r   rA   r1   s    r   rE   zASTBaseBase.get_display_stringp   s    CDDr   c                4    d| j                   j                  z  S )Nz<%s>)	__class__r   r1   s    r   __repr__zASTBaseBase.__repr__s   s    ////r   )r+   r   returnbool)rI   r   r8   StringifyTransformrI   r=   rI   r=   )
r   r   r    r.   __hash__r2   r9   rB   rE   rH   r!   r   r   r#   r#   X   s(    	 H.5E0r   r#   c                      e Zd ZddZy)ASTAttributec                *    t        t        |             r0   r4   r*   signodes     r   describe_signaturezASTAttribute.describe_signature|   r:   r   NrS   r   rI   None)r   r   r    rT   r!   r   r   rP   rP   {   s    .r   rP   c                  $    e Zd ZddZddZddZy)ASTCPPAttributec                    || _         y r0   arg)r*   r[   s     r   __init__zASTCPPAttribute.__init__   s	    r   c                &    d| j                   z   dz   S Nz[[z]]rZ   r7   s     r   r9   zASTCPPAttribute._stringify   s    dhh%%r   c                    |j                  t        j                  dd             |j                  t        j                  | j
                               |j                  t        j                  dd             y r^   )appendr
   desc_sig_punctuationr	   Textr[   rR   s     r   rT   z"ASTCPPAttribute.describe_signature   sM    x44T4@Auzz$((+,x44T4@Ar   N)r[   r=   rI   rV   rK   rU   r   r   r    r\   r9   rT   r!   r   r   rX   rX      s    &Br   rX   c                      e Zd ZddZddZy)ASTGnuAttributec                     || _         || _        y r0   )nameargs)r*   rg   rh   s      r   r\   zASTGnuAttribute.__init__   s    		r   c                    | j                   g}| j                  r!|j                   || j                               dj                  |      S N )rg   rh   r`   join)r*   r8   ress      r   r9   zASTGnuAttribute._stringify   s7    yyk99JJy+,wws|r   N)rg   r=   rh   ASTBaseParenExprList | NonerI   rV   rK   )r   r   r    r\   r9   r!   r   r   re   re      s    r   re   c                  $    e Zd ZddZddZddZy)ASTGnuAttributeListc                    || _         y r0   attrsr*   rs   s     r   r\   zASTGnuAttributeList.__init__   	    
r   c                    dg}d}| j                   D ].  }|s|j                  d       d}|j                   ||             0 |j                  d       dj                  |      S )Nz__attribute__((Tz, Fz))rk   )rs   r`   rl   )r*   r8   rm   firstattrs        r   r9   zASTGnuAttributeList._stringify   sb     !JJ 	(D

4 EJJy'		(
 	

4wws|r   c                b    t        |       }|j                  t        j                  |             y r0   r=   r`   r	   rb   r*   rS   txts      r   rT   z&ASTGnuAttributeList.describe_signature       $iuzz#'r   N)rs   zlist[ASTGnuAttribute]rI   rV   rK   rU   rc   r!   r   r   rp   rp      s    	(r   rp   c                  (    e Zd ZdZddZddZddZy)	ASTIdAttributez*For simple attributes defined by the user.c                    || _         y r0   id)r*   r   s     r   r\   zASTIdAttribute.__init__   s	    r   c                    | j                   S r0   r   r7   s     r   r9   zASTIdAttribute._stringify   s    wwr   c                `    |j                  t        j                  | j                               y r0   )r`   r	   rb   r   rR   s     r   rT   z!ASTIdAttribute.describe_signature   s    uzz$''*+r   N)r   r=   rI   rV   rK   rU   r   r   r    __doc__r\   r9   rT   r!   r   r   r   r      s    4,r   r   c                  (    e Zd ZdZddZddZddZy)	ASTParenAttributez)For paren attributes defined by the user.c                     || _         || _        y r0   r   r[   )r*   r   r[   s      r   r\   zASTParenAttribute.__init__   s    r   c                @    | j                   dz   | j                  z   dz   S )N()r   r7   s     r   r9   zASTParenAttribute._stringify   s    ww}txx'#--r   c                b    t        |       }|j                  t        j                  |             y r0   rz   r{   s      r   rT   z$ASTParenAttribute.describe_signature   r}   r   N)r   r=   r[   r=   rI   rV   rK   rU   r   r!   r   r   r   r      s    3.(r   r   c                  4    e Zd ZddZddZd	dZd
dZddZy)ASTAttributeListc                    || _         y r0   rr   rt   s     r   r\   zASTAttributeList.__init__   ru   r   c                ,    t        | j                        S r0   )lenrs   r1   s    r   __len__zASTAttributeList.__len__   s    4::r   c                F    t        | j                  |j                  z         S r0   )r   rs   )r*   r+   s     r   __add__zASTAttributeList.__add__   s    

U[[ 899r   c                L    dj                  fd| j                  D              S )N c              3  .   K   | ]  } |        y wr0   r!   ).0rx   r8   s     r   	<genexpr>z.ASTAttributeList._stringify.<locals>.<genexpr>   s     ?D	$?s   )rl   rs   r7   s    `r   r9   zASTAttributeList._stringify   s    xx?DJJ???r   c                4   t        | j                        dk(  ry | j                  d   j                  |       t        | j                        dk(  ry | j                  dd  D ]6  }|j                  t	        j
                                |j                  |       8 y )Nr      )r   rs   rT   r`   r
   desc_sig_space)r*   rS   rx   s      r   rT   z#ASTAttributeList.describe_signature   sz    tzz?a

1((1tzz?aJJqrN 	-DNN82245##G,	-r   N)rs   zlist[ASTAttribute]rI   rV   )rI   int)r+   r   rI   r   rK   rU   )r   r   r    r\   r   r   r9   rT   r!   r   r   r   r      s    :@-r   r   c                      e Zd Zy)ASTBaseParenExprListNr   r!   r   r   r   r          r   r   c                      e Zd Zy)$UnsupportedMultiCharacterCharLiteralNr   r!   r   r   r   r      r   r   r   c                      e Zd Zy)DefinitionErrorNr   r!   r   r   r   r      r   r   r   c                     e Zd Z	 	 	 	 	 	 ddZddZedd       ZddZddZddZ	ddZ
d dZd!d	Zd"d
Zd!dZd dZed"d       Zedd       Zedd       ZddZddd#dZed        Zed        Zd$dZd%dZd&dZd'dZy)(
BaseParserc                   |j                         | _        || _        || _        d| _        t        | j                        | _        d | _        d| _        g | _	        d| _
        y )Nr   )r   NT)strip
definitionlocationconfigposr   end
last_match_previous_stateotherErrorsallowFallbackExpressionParsing)r*   r   r   r   s       r   r\   zBaseParser.__init__   sZ     %**, t'04AJ24 /3+r   c                l   t        |      dk(  rHt        |      dkD  r t        |dz   t        |d   d         z         S t        t        |d   d               S |dg}|D ]  }t        |d         dkD  rd}|j                  |d          |j                  d       t        |d         j	                  d      D ]D  }t        |      dk(  r|j                  |       |j                  |       |j                  d       F |j                  t        |d                 t        dj                  |            S )Nr   r   
z  z:
rk   )r   r   r=   r`   splitrl   )r*   errorsheaderresulteindentlines          r   _make_multi_errorzBaseParser._make_multi_error  s   v;!6{Q&v}s6!9Q<7H'HII&s6!9Q<'899$ 	)A1Q4y1}ad#e$!IOOD1 (D4yA~ MM&)MM$'MM$'( c!A$i(	) rwwv//r   c                    t         r0   r5   r1   s    r   languagezBaseParser.language      !!r   c                v    d| j                   z  dz   }t        j                  | d| j                   d|        y )N-^r   )r   loggerdebugr   )r*   msg	indicators      r   statuszBaseParser.status  s6    $((NS(	uBt/r)=>r   c                ,   g }d| j                   z  dz   }t        d| j                  || j                   | j                  |fz        }|j	                  |df       | j
                  D ]  }|j	                  |df        g | _        | j                  |d      )Nr   r   z2Invalid %s declaration: %s [error at %d]
  %s
  %sz
Main errorzPotential other errorrk   )r   r   r   r   r`   r   r   )r*   r   r   r   exMainerrs         r   failzBaseParser.fail!  s    $((NS(	 B]]C4??IFGH 	v|,-## 	:CMM3 789	:$$VR00r   c                F    t         j                  || j                         y )N)r   )r   warningr   )r*   r   s     r   warnzBaseParser.warn-  s    sT]]3r   c                    |j                  | j                  | j                        }|:| j                  | j                  f| _        |j                         | _        || _        yyNTF)matchr   r   r   r   r   )r*   regexr   s      r   r   zBaseParser.match0  sO    DOOTXX6$(HHdoo#>D yy{DH#DOr   c                    t        |      }| j                  | j                  | j                  |z    |k(  r| xj                  |z  c_        yyr   )r   r   r   )r*   stringstrlens      r   skip_stringzBaseParser.skip_string9  s?    V??488DHHv$56&@HHHr   c                v    | j                  t        j                  dt        j                  |      z              S )Nz\b%s\b)r   recompileescaper*   words     r   	skip_wordzBaseParser.skip_word@  s'    zz"**Y4%@ABBr   c                ,    | j                  t              S r0   )r   _whitespace_rer1   s    r   skip_wszBaseParser.skip_wsC  s    zz.))r   c                H    | j                  |      r| j                          yyr   )r   r   r   s     r   skip_word_and_wszBaseParser.skip_word_and_wsF  s    >>$LLNr   c                H    | j                  |      r| j                          yyr   )r   r   )r*   r   s     r   skip_string_and_wszBaseParser.skip_string_and_wsL  s    F#LLNr   c                4    | j                   | j                  k\  S r0   )r   r   r1   s    r   eofzBaseParser.eofR  s    xx488##r   c                T    	 | j                   | j                     S # t        $ r Y yw xY w)NEOF)r   r   
IndexErrorr1   s    r   current_charzBaseParser.current_charV  s,    	??488,, 		s    	''c                P    | j                   | j                   j                         S yrj   )r   groupr1   s    r   matched_textzBaseParser.matched_text]  s"    ??&??((**r   c                Z    | j                   | j                  d  }| j                  | _        |S r0   )r   r   r   )r*   rvs     r   	read_restzBaseParser.read_restc  s&    __TXXY'88	r   F)allowSemicolonc                   | j                          |r<| j                  s/| j                  | j                  d  dk7  r| j	                  d       y y y | j                  s| j	                  d       y y )N;z Expected end of definition or ;.zExpected end of definition.)r   r   r   r   r   )r*   r   s     r   
assert_endzBaseParser.assert_endh  s[    88	 :c A		<= !B8 88		78 r   c                    t         r0   r   r1   s    r   id_attributeszBaseParser.id_attributess  r   r   c                    t         r0   r   r1   s    r   paren_attributeszBaseParser.paren_attributesw  r   r   c                F   dddd}| j                   }g }| j                  st        |      dk(  r| j                  |v rn| j                  |v r|j	                  || j                            n]t        |      dkD  r#| j                  |d   k(  r|j                          n,| j                  dv r| j                  d| j                  z         | xj                   d	z  c_         | j                  s| j                  r| j                  d
|z         | j                  || j                    S )Nr   ]})r   [{r   z)]}z&Unexpected '%s' in balanced-token-seq.r   z8Could not find end of balanced-token-seq starting at %d.)r   r   r   r   r`   popr   r   )r*   r   bracketsstartPossymbolss        r   _parse_balanced_token_seqz$BaseParser._parse_balanced_token_seq{  s    3S188((7|q T%6%6#%=  H,x(9(9:;W!d&7&772;&F""e+		BTEVEVVWHHMH (( 88IIP ! "x11r   c                   | j                          | j                  }| j                  d      r{| j                  d      s|| _        nb| j	                  dg      }| j                  d      s| j                  d       | j                  d      s| j                  d       t        |      S | j                  d      r| j                  d      s| j                  d       | j                  d      s| j                  d	       g }	 | j                  t              r7| j                  }| j                         }|j                  t        ||             | j                  d
      r_| j                  d      rn| j                  d       | j                  d      s| j                  d       t        |      S | j                  D ]   }| j                  |      st!        |      c S  | j"                  D ]x  }| j                  |      s| j                  d      s| j                  d       | j	                  dg      }| j                  d      s| j                  d       t%        ||      c S  y )Nr   r   )r   z!Expected ']' in end of attribute.z-Expected ']' in end of attribute after [[...]__attribute__r   z#Expected '(' after '__attribute__'.z$Expected '(' after '__attribute__('.,r   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  r   rX   r   r   identifier_rer   _parse_paren_expression_listr`   re   rp   r   r   r   r   )r*   r  r[   rs   rg   exprsr   s          r   _parse_attributezBaseParser._parse_attribute  s&   88""3'##C(# 44#4?..s3IIAB..s3IIMN&s++   1**3/		?@**3/		@AE::m,,,D ==?ELLu!=>**3/**3/		NO  **3/		DE&u-- $$ 	*B$$R(%b))	*
 '' 	.B**2.##C(		LM00cU0;C##C(		MN$R--	. r   c                h    g }	 | j                         }|	 t        |      S |j                  |       0r0   )r  r`   r   )r*   rm   rx   s      r   _parse_attribute_listz BaseParser._parse_attribute_list  s>    ((*D|$$ JJt	 r   c                    t         r0   r   r1   s    r   r
  z'BaseParser._parse_paren_expression_list  s    !!r   N)r   r=   r   z"nodes.Node | tuple[str, int] | strr   r   rI   rV   )r   z	list[Any]r   r=   rI   r   rM   )r   r=   rI   rV   )r   zre.Pattern[str]rI   rJ   )r   r=   rI   rJ   )r   r=   rI   rJ   )rI   rJ   )r   rJ   rI   rV   )r   z	list[str]rI   r=   )rI   zASTAttribute | None)rI   r   )rI   rn   )r   r   r    r\   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   r      s    3=33$(3 0, " "?

14C* $ $    

 49 9 " " " "2(5n%"r   r   )r   r=   rI   rV   )5r   
__future__r   r   copyr   typingr   r   r   docutilsr	   sphinxr
   sphinx.utilr   docutils.nodesr   sphinx.configr   	getLoggerr   r   r=   rL   r   r   anon_identifier_reVERBOSEr	  integer_literal_reoctal_literal_rehex_literal_rebinary_literal_reintegers_literal_suffix_refloat_literal_refloat_literal_suffix_rechar_literal_rer   r   r   r#   rP   rX   re   rp   r   r   r   r   r   r   r   r!   r   r   <module>r$     s   8 " 	  / /   *$			8	$seSj)  F#RZZ AB 

  ::  RZZ 9: 2::34 ABBJJ67 'RZZ 	) ::	  2::  ::  %"**[1 "**  ::D
	9 	
0 0F.; .

Bl 
B	k 	(, ((
,\ 
,( (-{ -4	; 		9 		i 	_" _"r   