o
    !dtD                     @   s   d Z ddlZddlZddlmZ 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ZG dd deZdS )a  Module for parsing shorthand syntax.

This module parses any CLI options that use a "shorthand"
syntax::

    --foo A=b,C=d
         |------|
            |
            Shorthand syntax


This module provides two main classes to do this.
First, there's a ``ShorthandParser`` class.  This class works
on a purely syntactic level.  It looks only at the string value
provided to it in order to figure out how the string should be parsed.

However, because there was a pre-existing shorthand parser, we need
to remain backwards compatible with the previous parser.  One of the
things the previous parser did was use the associated JSON model to
control how the expression was parsed.

In order to accommodate this a post processing class is provided that
takes the parsed values from the ``ShorthandParser`` as well as the
corresponding JSON model for the CLI argument and makes any adjustments
necessary to maintain backwards compatibility.  This is done in the
``BackCompatVisitor`` class.

    N)is_document_typec                   @   s   e Zd Zdd Zdd ZdS )_NamedRegexc                 C   s   || _ t|tj| _d S N)namerecompileUNICODEregex)selfr   Z	regex_str r   4usr/lib/python3.10/site-packages/awscli/shorthand.py__init__3   s   z_NamedRegex.__init__c                 C   s   | j |S r   )r	   matchr
   valuer   r   r   r   7   s   z_NamedRegex.matchN)__name__
__module____qualname__r   r   r   r   r   r   r   2   s    r   c                   @   s   e Zd Zdd ZdS )ShorthandParseErrorc                 C   s   | j d| j}}}d| j d | j v r&| j d | j d}| j| d }d| j | jd  v rL| j| j | jd  d }| j d | }| j |d  }d|d| d |f S )N 
   z%s
%s%s ^)r   indexrindex)r
   consumed	remainingZ
num_spacesZlast_newlineZnext_newliner   r   r   _error_location=   s   z#ShorthandParseError._error_locationN)r   r   r   r   r   r   r   r   r   ;   s    r   c                       $   e Zd Z fddZdd Z  ZS )ShorthandParseSyntaxErrorc                    s4   || _ || _|| _|| _|  }tt| | d S r   )r   expectedactualr   _construct_msgsuperr    r   )r
   r   r!   r"   r   msg	__class__r   r   r   U   s   z"ShorthandParseSyntaxError.__init__c                 C   s   d| j | j|  f }|S )Nz,Expected: '%s', received: '%s' for input:
%s)r!   r"   r   r
   r%   r   r   r   r#   ]   s   z(ShorthandParseSyntaxError._construct_msgr   r   r   r   r#   __classcell__r   r   r&   r   r    T   s    r    c                       r   )DuplicateKeyInObjectErrorc                    s.   || _ || _|| _|  }tt| | d S r   )keyr   r   r#   r$   r+   r   )r
   r,   r   r   r%   r&   r   r   r   f   s
   z"DuplicateKeyInObjectError.__init__c                 C   s   d| j |  f }|S )NzxSecond instance of key "%s" encountered for input:
%s
This is often because there is a preceding "," instead of a space.)r,   r   r(   r   r   r   r#   m   s   z(DuplicateKeyInObjectError._construct_msgr)   r   r   r&   r   r+   e   s    r+   c                   @   s   e Zd ZdS )DocumentTypesNotSupportedErrorN)r   r   r   r   r   r   r   r-   v   s    r-   c                   @   s  e Zd ZdZeddZeddZdZdZdZ	d	Z
ed
dje
eedZeddje
ee	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d d! Zd"d# Zd$d% Zd&d' Zd>d)d*Zd+d, Zd-d. Zd?d0d1Zd2d3 Zd4d5 Z d6d7 Z!d8d9 Z"d:d; Z#d<d= Z$d(S )@ShorthandParserzParses shorthand syntax in the CLI.

    Note that this parser does not rely on any JSON models to control
    how to parse the shorthand syntax.

    zsingled quotedz\'(?:\\\\|\\\'|[^\'])*\'zdouble quotedz"(?:\\\\|\\"|[^"])*"u   \!\#-&\(-\+\--\<\>-Z\\-z|-￿u   \s\!\#-&\(-\+\--\\\^-\|~-￿u   \s\!\#-&\(-\+\--\<\>-￿z(\\,)firstzC({escaped_comma}|[{start_word}])({escaped_comma}|[{follow_chars}])*)Zescaped_commaZ
start_wordZfollow_charssecondc                 C   s
   g | _ d S r   )Z_tokensr
   r   r   r   r      s   
zShorthandParser.__init__c                 C   s   || _ d| _|  S )aS  Parse shorthand syntax.

        For example::

            parser = ShorthandParser()
            parser.parse('a=b')  # {'a': 'b'}
            parser.parse('a=b,c')  # {'a': ['b', 'c']}

        :type value: str
        :param value: Any value that needs to be parsed.

        :return: Parsed value, which will be a dictionary.
        r   )_input_value_index
_parameterr   r   r   r   parse   s   zShorthandParser.parsec                 C   s   i }|   \}}|||< | j}| jt| jk r@| jddd |   \}}||v r1t|| j|d |||< | j}| jt| jk s|S N,Tconsume_whitespacer   )_keyvalr3   lenr2   _expectr+   )r
   paramsr,   val
last_indexr   r   r   r4      s   zShorthandParser._parameterc                 C   s&   |   }| jddd |  }||fS )N=Tr8   )_keyr<   _values)r
   r,   valuesr   r   r   r:      s   zShorthandParser._keyvalc                 C   sR   t jt j d }| j}|  s!|  |vrn|  jd7  _|  r| j|| j S )Nz-_.#/:r   )stringascii_lettersdigitsr3   _at_eof_currentr2   )r
   Zvalid_charsstartr   r   r   rA      s   zShorthandParser._keyc                 C   s<   |   rdS |  dkr|  S |  dkr|  S |  S )Nr   [{)rG   rH   _explicit_list_hash_literal
_csv_valuer1   r   r   r   rB      s   zShorthandParser._valuesc                 C   s   |   }|   |  s| j| j dkr|S | jddd |g}	 z!|  }|   |  r5|| W n"| jddd || W n tyU   |  rN | 	d Y nw q!t
|dkr_|S |S r6   )_first_value_consume_whitespacerG   r2   r3   r<   _second_valueappendr    _backtrack_tor;   )r
   first_valueZcsv_listcurrentr   r   r   rN      s2   
	
zShorthandParser._csv_valuec                 C   s>   | j | j| jd  }|d ur| |}|dd S dS )N\,r7   r   )_FIRST_VALUEr   r2   r3   _consume_matched_regexreplacerstrip)r
   resultr   r   r   r   _value  s
   
zShorthandParser._valuec                 C   sp   | j ddd g }|  dkr1|  }|| |   |  dkr+|  d |   |  dks|  d |S )NrJ   Tr8   ]r7   )r<   rH   _explicit_valuesrR   rP   )r
   rC   r>   r   r   r   rL     s   


zShorthandParser._explicit_listc                 C   0   |   dkr
|  S |   dkr|  S |  S )NrJ   rK   )rH   rL   rM   rO   r1   r   r   r   r^   "  s
   z ShorthandParser._explicit_valuesc                 C   s   | j ddd i }|  dkr;|  }| j ddd |  }|   |  dkr1|  d |   |||< |  dks|  d |S )NrK   Tr8   }r@   r7   )r<   rH   rA   r^   rP   )r
   Zkeyvalsr,   vr   r   r   rM   +  s   

	zShorthandParser._hash_literalc                 C   r_   )N'")rH   _single_quoted_value_double_quoted_valuer\   r1   r   r   r   rO   :  s
   zShorthandParser._first_valuec                 C      | j | jddS )Nrb   escaped_char)_consume_quoted_SINGLE_QUOTEDr1   r   r   r   rd   B  s   z$ShorthandParser._single_quoted_valueNc                 C   s:   |  |dd }|d ur|d| |}|dd}|S )Nr   z\%sz\\\)_must_consume_regexrY   )r
   r	   rh   r   r   r   r   ri   H  s
   zShorthandParser._consume_quotedc                 C   rf   )Nrc   rg   )ri   _DOUBLE_QUOTEDr1   r   r   r   re   O     z$ShorthandParser._double_quoted_valuec                 C   sD   |   dkr
|  S |   dkr|  S | | j}|dd S )Nrb   rc   rV   r7   )rH   rd   re   rm   _SECOND_VALUErY   rZ   )r
   r   r   r   r   rQ   R  s   zShorthandParser._second_valueFc                 C   sv   |r|    | jt| jkrt| j|d| j| j| j }||kr*t| j||| j|  jd7  _|r9|    d S d S )NZEOFr   )rP   r3   r;   r2   r    )r
   charr9   r"   r   r   r   r<   [  s   zShorthandParser._expectc                 C   s@   | | j| jd  }|d ur| |S t| jd|j d| j)Nz<%s>z<none>)r   r2   r3   rX   r    r   )r
   r	   r[   r   r   r   rm   i  s   
z#ShorthandParser._must_consume_regexc                 C   s<   |  \}}| j| j| | j|  }|  j|| 7  _|S r   )spanr2   r3   )r
   r[   rI   endra   r   r   r   rX   p  s   z&ShorthandParser._consume_matched_regexc                 C   s    | j t| jk r| j| j  S tS r   )r3   r;   r2   _EOFr1   r   r   r   rH   v  s   zShorthandParser._currentc                 C   s   | j t| jkS r   )r3   r;   r2   r1   r   r   r   rG   }  ro   zShorthandParser._at_eofc                 C   sR   | j dkr#| j| j  |kr'|  j d8  _ | j dkr%| j| j  |ksd S d S d S d S )Nr   r   )r3   r2   )r
   rq   r   r   r   rS        *zShorthandParser._backtrack_toc                 C   sR   |   tkr#|   tjv r'|  jd7  _|   tkr%|   tjv sd S d S d S d S )Nr   )rH   rt   rD   
whitespacer3   r1   r   r   r   rP     ru   z#ShorthandParser._consume_whitespacer   )F)%r   r   r   __doc__r   rj   rn   Z_START_WORDZ_FIRST_FOLLOW_CHARSZ_SECOND_FOLLOW_CHARSZ_ESCAPED_COMMAformatrW   rp   r   r5   r4   r:   rA   rB   rN   r\   rL   r^   rM   rO   rd   ri   re   rQ   r<   rm   rX   rH   rG   rS   rP   r   r   r   r   r.   z   sb    

	
0	

	r.   c                   @   s<   e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd ZdS )ModelVisitorc                 C   s   |  i |d| d S )Nr   )_visit)r
   r=   modelr   r   r   visit  s   zModelVisitor.visitc                 C   s&   t | d|j | j}||||| d S )Nz	_visit_%s)getattr	type_name_visit_scalar)r
   parentshaper   r   methodr   r   r   rz     s   zModelVisitor._visitc              	   C   s<   t |tsd S |j D ]\}}| ||||| qd S r   )
isinstancedictmembersitemsrz   getr
   r   r   r   r   member_namemember_shaper   r   r   _visit_structure  s   

zModelVisitor._visit_structurec                 C   s6   t |tsd S t|D ]\}}| ||j|| qd S r   )r   list	enumeraterz   member)r
   r   r   r   r   ielementr   r   r   _visit_list  s
   
zModelVisitor._visit_listc                 C   s:   t |tsd S |j}| D ]\}}| |||| qd S r   )r   r   r   r   rz   )r
   r   r   r   r   Zvalue_shapekra   r   r   r   
_visit_map  s   
zModelVisitor._visit_mapc                 C   s   d S r   r   r
   r   r   r   r   r   r   r   r     s   zModelVisitor._visit_scalarN)	r   r   r   r|   rz   r   r   r   r   r   r   r   r   ry     s    ry   c                       s4   e Zd Zdd Z fddZdd Zdd Z  ZS )	BackCompatVisitorc              	   C   sf   |  || t|tsd S |j D ]\}}z| ||||| W q ty0   td| w d S )NzwShorthand syntax does not support document types. Use JSON input for top-level argument to specify nested parameter: %s)	_raise_if_document_type_foundr   r   r   r   rz   r   r-   r   r   r   r   r   r     s    

z"BackCompatVisitor._visit_structurec                    s:   t |ts|d ur|g||< d S d S tt| ||||S r   )r   r   r$   r   r   r   r&   r   r   r     s   

zBackCompatVisitor._visit_listc                 C   s   |d u rd S |j }|dv rt|||< d S |dv r!t|||< d S |dkr=| dkr1d||< d S | dkr?d||< d S d S d S )N)integerlong)ZdoublefloatZbooleantrueTfalseF)r~   intr   lower)r
   r   r   r   r   r~   r   r   r   r     s   zBackCompatVisitor._visit_scalarc                 C   s   |d urt |rt d S d S r   )r   r-   )r
   r   r   r   r   r   r     s   z/BackCompatVisitor._raise_if_document_type_found)r   r   r   r   r   r   r   r*   r   r   r&   r   r     s
    
r   )rw   r   rD   Zawscli.utilsr   objectrt   r   	Exceptionr   r    r+   r-   r.   ry   r   r   r   r   r   <module>   s   	  !