o
    3                     @   s   d Z ddlZddlZddlmZ zddlmZ W n ey'   ddlmZ Y nw ze W n e	y7   e
ZY nw G dd dZdS )8A lexical analyzer class for simple shell-like syntaxes.    N)deque)StringIOc                   @   sn   e Zd ZdZ		dddZdd Zd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eZdS )shlexr   NFc                 C   s  t |tr	t|}|d ur|| _|| _ntj| _d | _|| _|r$d | _nd| _d| _	d| _
d| _d| _d| _d| _d| _d	| _t | _d
| _d| _d| _t | _d | _|sXd}n|du r^d}|| _|r~t | _|  j
d7  _
| j
t|}| j
|| _
d | _d| _d S )N #?abcdfeghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_z 	
Fz'"\"    r   Tz();<>|&z~-./*?=) 
isinstance
basestringr   instreaminfilesysstdinposixeof
commenters	wordchars
whitespacewhitespace_splitquotesescapeescapedquotesstater   pushbacklinenodebugtoken	filestacksourcepunctuation_chars_pushback_chars	maketransdictfromkeys	translatelast_wordbreak_pos
wordbreaks)selfr   r   r   r#   t r-   =/tmp/google-cloud-sdk/lib/third_party/argcomplete/my_shlex.py__init__"   sJ   

zshlex.__init__c                 C   s*   | j dkrtdt|  | j| dS )z:Push a token onto the stack popped by the get_token methodr   zshlex: pushing token N)r   printreprr   
appendleft)r+   tokr-   r-   r.   
push_tokenW   s   
zshlex.push_tokenc                 C   sv   t |tr	t|}| j| j| j| jf || _|| _d| _| jr9|dur/t	d| jf  dS t	d| jf  dS dS )z9Push an input source onto the lexer's input source stack.r   Nzshlex: pushing to file %szshlex: pushing to stream %s)
r   r   r   r!   r2   r   r   r   r   r0   )r+   	newstreamnewfiler-   r-   r.   push_source]   s   
zshlex.push_sourcec                 C   sD   | j   | j \| _| _ | _| jrtd| j | jf  d| _dS )zPop the input source stack.zshlex: popping to %s, line %dr   N)	r   closer!   popleftr   r   r   r0   r   r+   r-   r-   r.   
pop_sourcel   s   


zshlex.pop_sourcec                 C   s   | j r| j  }| jdkrtdt|  |S |  }| jdurA|| jkrA| |  }|r8|\}}| || | 	 }|| jks%|| j
krY| jsL| j
S |   | 	 }|| j
ksF| jdkrq|| j
krmtdt|  |S td |S )zBGet a token from the input stream (or from stack if it's nonempty)r   zshlex: popping token Nzshlex: token=zshlex: token=EOF)r   r9   r   r0   r1   
read_tokenr"   
sourcehookr7   	get_tokenr   r!   r;   )r+   r3   rawspecr6   r5   r-   r-   r.   r>   u   s4   








zshlex.get_tokenc                 C   s  d}d}	 | j r| jr| j }n| jd}|dkr"|  jd7  _| jdkr0td| j|f  | jd u r:d| _	n| jdkr|sFd | _n|| j
v r_| jd	krTtd
 | j	s\| jr^|r^nq|| jv rr| j  |  jd7  _n| jr|| jv rd}|| _n|| jv r|| _	d| _n|| j v r|| _	d| _n|| jv r| js|| _	|| _n| jr|| _	d| _n|| _	| j	s| jr|rÐnxq| j| jv rd}|s| jd	krtd td|| jkr| js|  j	|7  _	d| _nKd| _nE| jr|| jv r| j| jv r| j}|| _n,|  j	|7  _	n#| j| jv rS|s/| jd	kr+td td|| jv rH|| jkrH||krH|  j	| j7  _	|  j	|7  _	|| _n| jdv r:|s`d | _n|| j
v r| jd	krptd d| _| j	s~| jr|rnq|| jv r| j  |  jd7  _| jrd| _| j	s| jr|rnqn| jr|| jv r|| _n| jr|| jv rd}|| _nu| jdkr|| j v r|  j	|7  _	na|| j
vr| j| d| _nR|| jv s|| jv s| jr|  j	|7  _	|| jv rt| j	d | _n+| j r| j| n| j| | jd	kr*td d| _| j	s8| jr9|r9nqq| j	}d| _	| jrO|sO|dkrOd }| jdkre|ratdt|  ntd | jdkrnd | _|S )NFr   Tr   
   z&shlex: in state %r I see character: %rr      z+shlex: I see whitespace in whitespace stateacz shlex: I see EOF in quotes statezNo closing quotationz shlex: I see EOF in escape statezNo escaped character)rD   rE   z%shlex: I see whitespace in word statez&shlex: I see punctuation in word statezshlex: raw token=zshlex: raw token=EOF)r#   r$   popr   readr   r   r0   r   r    r   r   r   readliner   r   r   r   
ValueErrorr   appendr*   lenr)   r   r2   r1   )r+   quotedescapedstatenextcharresultr-   r-   r.   r<      s  



















  zshlex.read_tokenc                 C   sV   |d dkr|dd }t | jtr$tj|s$tjtj| j|}|t|dfS )z(Hook called on a filename to be sourced.r   r
   r   r)	r   r   r   ospathisabsjoindirnameopen)r+   r6   r-   r-   r.   r=   ,  s
   zshlex.sourcehookc                 C   s(   |du r| j }|du r| j}d||f S )z<Emit a C-compiler-like, Emacs-friendly error-message leader.Nz"%s", line %d: )r   r   )r+   r   r   r-   r-   r.   error_leader6  s
   zshlex.error_leaderc                 C   s   | S Nr-   r:   r-   r-   r.   __iter__>  s   zshlex.__iter__c                 C   s   |   }|| jkrt|S rY   )r>   r   StopIteration)r+   r    r-   r-   r.   __next__A  s   
zshlex.__next__)NNFFrY   )NN)__name__
__module____qualname____doc__r/   r4   r7   r;   r>   r<   r=   rX   rZ   r\   nextr-   r-   r-   r.   r       s     
5
	  

r   )r`   rR   r   collectionsr   r   ImportErrorior   	NameErrorstrr   r-   r-   r-   r.   <module>   s   	