o
    :eq                     @  s  d Z ddlmZ ddlZddlZddlZddlZddlm	Z	 ddlm
Z
 ddlmZ ejrNed	Zejd
eje f Zeje Zejejeeje f  ZG dd
 d
ejZd9ddZd:ddZejG dd dZejG dd deZejG dd deZejG dd deZ ejG dd  d eZ!ejG d!d" d"eZ"ejG d#d$ d$eZ#d;d'd(Z$d<d)d*Z%ej&d=d0d1Z'e'j(d>d2d3Z)e'j(d?d4d3Z)e'j(d@d5d3Z)e'j(dAd6d3Z)e'j(dBd7d3Z)e'j(dCd8d3Z)dS )Da  Rust CFG parser.

Rust uses its `cfg()` format in cargo.

This may have the following functions:
 - all()
 - any()
 - not()

And additionally is made up of `identifier [ = str]`. Where the str is optional,
so you could have examples like:
```
[target.`cfg(unix)`.dependencies]
[target.'cfg(target_arch = "x86_64")'.dependencies]
[target.'cfg(all(target_arch = "x86_64", target_arch = "x86"))'.dependencies]
```
    )annotationsN   )builder   )mparser)MesonBugException_T	TokenTypec                   @  sT   e Zd Ze Ze Ze Ze Ze Z	e Z
e Ze Ze ZdS )r	   N)__name__
__module____qualname__enumautoLPARENRPARENSTRING
IDENTIFIERALLANYNOTCOMMAEQUAL r   r   9/usr/lib/python3.10/site-packages/mesonbuild/cargo/cfg.pyr	   (   s    rawstrreturn_LEX_STREAMc                 c  s@   g }d}| D ]}|  s|dv s|dkr|rd|}|  |r)tj|fV  n)|dkr4tjdfV  n|dkr?tjdfV  n|dkrJtjdfV  n|rRtj|fV  |d	kr]tj	dfV  q|d
krhtj
dfV  q|dkrstjdfV  q|dkr~tjdfV  q|  rq|dkr| }q|| q|rtjd|fV  dS dS )ziLex a cfg() expression.

    :param raw: The raw cfg() expression
    :return: An iterable of tokens
    F>   ,)(=" anyNallnotr    r   r   r!   )isspacejoinclearr	   r   r   r   r   r   r   r   r   r   append)r   buffer	is_stringsvalr   r   r   lexer5   sJ   
r/   iterT.Iterator[_T]'T.Iterator[T.Tuple[_T, T.Optional[_T]]]c                 c  sf    zt | }W n
 ty   Y dS w 	 |}zt | }W n ty&   d}Y nw ||fV  |du r2dS q)zGet the current value of the iterable, and the next if possible.

    :param iter: The iterable to look into
    :yield: A tuple of the current value, and, if possible, the next
    :return: nothing
    N)nextStopIteration)r0   Znext_currentr   r   r   	lookaheadd   s"   	
r6   c                   @  s   e Zd ZdZdS )IRzBase IR node for Cargo CFG.N)r
   r   r   __doc__r   r   r   r   r7      s    r7   c                   @     e Zd ZU ded< dS )Stringr   valueNr
   r   r   __annotations__r   r   r   r   r:         
 r:   c                   @  r9   )
Identifierr   r;   Nr<   r   r   r   r   r?      r>   r?   c                   @  s   e Zd ZU ded< ded< dS )Equalr7   lhsrhsNr<   r   r   r   r   r@      s   
 r@   c                   @  r9   )Any
T.List[IR]argsNr<   r   r   r   r   rC      r>   rC   c                   @  r9   )AllrD   rE   Nr<   r   r   r   r   rF      r>   rF   c                   @  r9   )Notr7   r;   Nr<   r   r   r   r   rG      r>   rG   ast_LEX_STREAM_AHc           	      C  s  t | \\}}}|d ur|\}}nd\}}|tju r(|tju r(tt|t| S |tju r1t|S |tju r:t| S |tj	tj
hv r|tj
u rItnt}|tju sRJ t |  g }g }|tjurt | \\}}}|tju r||ttt| |  n|||f |tjus_|r|ttt| ||S |tju rt |  g }|tjurt | \\}}}|||f |tjustttt|S td| )N)NNzUnhandled Cargo token: )r3   r	   r   r   r@   r?   _parser   r:   r   r   rF   rC   r   r   r   r*   r6   r0   r)   r   rG   r   )	rH   tokenr;   Zn_streamZntoken_type_streamrE   r   r   r   rJ      sH   











rJ   c                 C  s   t t| }t|S )zParse the tokenized list into Meson AST.

    :param ast: An iterable of Tokens
    :return: An mparser Node to be used as a conditional
    )r6   r0   rJ   )rH   Zast_ir   r   r   parse   s   rO   irT.Anybuildbuilder.Buildermparser.BaseNodec                 C  s   t N)NotImplementedErrorrP   rR   r   r   r   ir_to_meson   s   rX   c                 C  s   | | jS rU   )stringr;   rW   r   r   r   rL      s   rL   c                 C  s\   | d}| jdkr|d|S | jdv r|d|S | jdkr&|d|S td| j )	Nhost_machineZtarget_archZ
cpu_family>   Z	target_osZtarget_familysystemZtarget_endianZendianzUnhandled Cargo identifier: )
identifierr;   methodr   )rP   rR   rZ   r   r   r   rL      s   



c                 C  s   | t| j|t| j|S rU   )equalrX   rA   rB   rW   r   r   r   rL      s   c                 C  s   | t| j|S rU   )not_rX   r;   rW   r   r   r   rL      s   c                 C  R   t t| j}t|}|tt||t||}|D ]}|t|||}q|S rU   )r0   reversedrE   r3   or_rX   rP   rR   rE   lastcurar   r   r   rL        c                 C  r`   rU   )r0   ra   rE   r3   and_rX   rc   r   r   r   rL     rg   )r   r   r   r   )r0   r1   r   r2   )rH   rI   r   r7   )rH   r   r   r7   )rP   rQ   rR   rS   r   rT   )rP   r:   rR   rS   r   rT   )rP   r?   rR   rS   r   rT   )rP   r@   rR   rS   r   rT   )rP   rG   rR   rS   r   rT   )rP   rC   rR   rS   r   rT   )rP   rF   rR   rS   r   rT   )*r8   
__future__r   dataclassesr   	functoolstypingTr#   r   r   Zmesonlibr   TYPE_CHECKINGTypeVarr   TupleOptionalr   Z
_LEX_TOKENIterabler   IteratorrI   Enumr	   r/   r6   	dataclassr7   r:   r?   r@   rC   rF   rG   rJ   rO   singledispatchrX   registerrL   r   r   r   r   <module>   s^   



/

,
	