
    AHj2.                       d dl mZ d dlZd dlmZmZ d dlZd dl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 d d
lmZ d dlmZ erd dlmZ  G d de      Z G d de      Zej<                  j>                   G d d             Z  G d de      Z!ej<                  j?                  d       G d de             Z" G d d      Z#y)    )annotationsN)TYPE_CHECKING
NamedTuple)GroupRenderableType)ReprHighlighter)Padding)Panel)Text)friendly_list)VALID_PSEUDO_CLASSES)get_suggestion)CSSLocationc                  N     e Zd ZdZ	 d	 	 	 	 	 	 	 	 	 	 	 d fdZddZddZ xZS )	
TokenErrorz=Error raised when the CSS cannot be tokenized (syntax error).c                d    || _         || _        || _        |xs || _        t        |   |       y)aI  
        Args:
            read_from: The location where the CSS was read from.
            code: The code being parsed.
            start: Line and column number of the error (1-indexed).
            message: A message associated with the error.
            end: End location of token (1-indexed), or None if not known.
        N)	read_fromcodestartendsuper__init__)selfr   r   r   messager   	__class__s         M/root/tools/cai/cai_env/lib/python3.12/site-packages/textual/css/tokenizer.pyr   zTokenError.__init__   s2    " #	
<%!    c                @   ddl m} | j                  d   } || j                  ddddt	        d|dz
        |dz   f|h      }|j                  d| j                  d   | j                  d	   d	z
  f| j                  d   | j                  d	   d	z
  f       t        |d
      S )zlGet a short snippet of code around a given line number.

        Returns:
            A renderable.
        r   )Syntaxscss
ansi_lightT   )lexerthemeline_numbersindent_guides
line_rangehighlight_lineszreverse bold   red)border_style)rich.syntaxr   r   r   maxstylize_ranger   r
   )r   r   line_nosyntaxs       r   _get_snippetzTokenError._get_snippet/   s     	'**Q-IIAw{+Wq[9$I
 	ZZ]DJJqMA-.XXa[$((1+/*	

 V%00r   c           
        t               }g }t        |       }|j                  t        dd             | j                  \  }}| j
                  \  }}|rd| d| d| d| }nd| d| d| }|j                   ||             |j                  | j                                dj                  d |j                  d	      D              }	|j                  t         |t        |	d
            d             t        | S )Nz Error in stylesheet:zbold red)style z, :
c              3  B   K   | ]  }d |j                            yw)u   • N)strip).0message_parts     r   	<genexpr>z&TokenError.__rich__.<locals>.<genexpr>Z   s&      "
.:d<%%'()"
s   ;r*   )r   r)   )pad)r   strappendr   r   r   r1   joinsplitr	   r   )
r   highlightererrorsr   r/   col_nopathwidget_variablecss_locationfinal_messages
             r   __rich__zTokenError.__rich__I   s   %'')d)d2*EF** $otfB&7q	6(KLtfAgYax8Lk,/0d'')*		 "
>EmmC>P"
 
 	. 		
 f~r   N)r   r   r   r>   r   tuple[int, int]r   r>   r   ztuple[int, int] | NonereturnNone)rL   r
   )rL   r   )__name__
__module____qualname____doc__r   r1   rI   __classcell__)r   s   @r   r   r      sT    G '+"" " 	"
 " $" 
".14r   r   c                      e Zd ZdZy)UnexpectedEndz:Indicates that the text being tokenized ended prematurely.N)rN   rO   rP   rQ    r   r   rT   rT   i   s    Dr   rT   c                  >    e Zd ZdZddZd	d
dZd	ddZd	ddZddZy)Expectz+Object that describes the format of tokens.c                   d| | _         t        |j                               | _        t        |j	                               | _        t        j                  ddj                  d |j                         D              z   dz         | _
        | j                  j                  | _        | j                  j                  | _        d| _        d| _        d| _        y)	z{Create Expect object.

        Args:
            description: Description of this class of tokens, used in errors.
        z	Expected (|c              3  4   K   | ]  \  }}d | d| d  yw)z(?P<>)NrU   )r9   nameregexs      r   r;   z"Expect.__init__.<locals>.<genexpr>|   s#     P+$dV1UG1-Ps   r]   FTN)descriptionlistkeysnamesvaluesregexesrecompiler@   items_regexmatchsearch_expect_eof_expect_semicolon_extract_text)r   r`   tokenss      r   r   zExpect.__init__q   s     '{m4&++-(
FMMO,jjhhPPPQ

 [[&&
kk(( !%"r   c                    || _         | S )zExpect an end of file.)rl   )r   eofs     r   
expect_eofzExpect.expect_eof   s    r   c                    || _         | S )z:Tokenizer expects text to be terminated with a semi-colon.)rm   )r   	semicolons     r   expect_semicolonzExpect.expect_semicolon   s    !*r   c                    || _         | S rJ   )rn   )r   extracts     r   extract_textzExpect.extract_text   s    $r   c              #  `   K   t        | j                  | j                        E d {    y 7 wrJ   )ziprc   re   r   s    r   __rich_repr__zExpect.__rich_repr__   s     tzz4<<000s   $.,.N)r`   r>   ro   r>   rL   rM   )T)rq   boolrL   rW   )rt   r}   rL   rW   )rw   r}   rL   rW   rL   zrich.repr.Result)	rN   rO   rP   rQ   r   rr   ru   rx   r|   rU   r   r   rW   rW   m   s    5#(

1r   rW   c                  6    e Zd ZU ded<   ded<   ded<   ded<   y)	ReferencedByr>   r^   rK   locationintlengthr   N)rN   rO   rP   __annotations__rU   r   r   r   r      s    
IK
Ir   r   T)angularc                      e Zd ZU ded<   ded<   ded<   ded<   ded<   	 d	Zd
ed<   edd       Zedd       ZddZddZ	ddZ
y	)Tokenr>   r^   valuer   r   r   rK   r   NReferencedBy | Nonereferenced_byc                4    | j                   \  }}|dz   |dz   fS )z"Start line and column (1-indexed).r)   )r   r   lineoffsets      r   r   zToken.start   s$     }}fq&1*%%r   c                `    | j                   \  }}|dz   |t        | j                        z   dz   fS )z End line and column (1-indexed).r)   )r   lenr   r   s      r   r   z	Token.end   s2     }}fq&3tzz?2Q677r   c                    t        | j                  | j                  | j                  | j                  | j
                  |      S )aH  Return a copy of the Token, with reference information attached.
        This is used for variable substitution, where a variable reference
        can refer to tokens which were defined elsewhere. With the additional
        ReferencedBy data attached, we can track where the token we are referring
        to is used.
        )r^   r   r   r   r   r   )r   r^   r   r   r   r   )r   bys     r   with_referencezToken.with_reference   s7     **nn]]
 	
r   c                    | j                   S rJ   )r   r{   s    r   __str__zToken.__str__   s    zzr   c              #  ^  K   d| j                   f d| j                  f d| j                  d   s| j                  d   n| j                  f dt        | j                        dk  r| j                  n| j                  d d dz   f d	| j
                  f d
| j                  d f y w)Nr^   r   r   r)   r   r   (   z...r   r   )r^   r   r   r   r   r   r   r{   s    r   r|   zToken.__rich_repr__   s     diitzz!!%)^^A%6DNN1DNN
 	
 3tyy>B#6diiDIIcrNU<RRR$--''t11477s   B+B-)rL   rK   )r   r   rL   z'Token')rL   r>   r~   )rN   rO   rP   r   r   propertyr   r   r   r   r|   rU   r   r   r   r      s`    
IJ
I-)-M&-& &
 8 8

 	8r   r   c                  *    e Zd ZdZdddZddZddZy)		TokenizerzTokenizes Textual CSS.c                j    || _         || _        |j                  d      | _        d| _        d| _        y)zInitialize the tokenizer.

        Args:
            text: String containing CSS.
            read_from: Information regarding where the CSS was read from.
        T)keependsr   N)r   r   
splitlineslinesr/   rD   )r   textr   s      r   r   zTokenizer.__init__   s2     #	__d_3
r   c                   | j                   }| j                  }|t        | j                        k\  rn|j                  r&t        dd| j                  | j                  ||fd      S t        | j                  | j                  |dz   |dz   f|j                  rd      d      | j                  |   }d}|j                  r|j                  ||      }|,|| j                  d }| xj                   dz  c_         d| _        n&|j                         }|| j                  | }|| _        |r;t        d|| j                  | j                  ||fd	      }|S |j                  ||      }|u||d }|j                   d
|j                  d      d   d}	|j                  r|j!                  d      s|	dz  }	t#        | j                  | j                  |dz   |dz   f|	      t%        |j&                  |j)                         dd       D ]	  \  }
}|	 n t+        d      t        |
|| j                  | j                  ||fd	      }|j,                  dk(  r|j.                  j1                  d      t2        vr|j.                  j1                  d      }t5        |t7        t2                    }dt9        t2               }|r3t#        | j                  | j                  |dz   |dz   fd|d|d|       t#        | j                  | j                  |dz   |dz   fd|d|       |t        |      z  }|t        |      k\  r|dz  }d}|| _         || _        |S )a2  Get the next token.

        Args:
            expect: Expect object which describes which tokens may be read.

        Raises:
            UnexpectedEnd: If there is an unexpected end of file.
            TokenError: If there is an error with the token.

        Returns:
            A new Token.
        rq    Nr)   .Unexpected end of file; did you forget a '}' ?zUnexpected end of textr   r   )r   z (found r<   z).z2; Did you forget a semicolon at the end of a line?zcan't reach herepseudo_classr5   zmust be one of zunknown pseudo-class z; did you mean z?; z; )r/   rD   r   r   rl   r   r   r   rT   rm   rn   rk   r   rj   r`   rA   endswithr   rz   rc   groupsAssertionErrorr^   r   r8   r   r   ra   r   )r   expectr/   rD   r   preceding_textrj   token
error_lineerror_messager^   r   r   
suggestion	all_valids                  r   	get_tokenzTokenizer.get_token   s    ,,c$**o%!!NNIIf%  $NNIIq[&1*- "33 I	 	 6	 	 zz'" MM$/E}!%dkkm!4!!%dkkF!;$"NNIIf%"&  LLv.E=fgJ%%&hz/?/?/DQ/G.J"M  ''
0C0CC0H!UU		GaK!+Dm  v||U\\^AB-?@ 	5KD% 	5
 !!344NNIIf
 JJ.(!!#&.BB ;;,,S1L'd;O6PQJ)-8L*M)NOI NNIIq[&1*-+L+;?:.X[\e[fg	  !NNIIq[&1*-+L+;2i[I	  	#e*SYqLGFr   c                   | j                   }| j                  }	 |t        | j                        k\  r6t	        | j
                  | j                  ||f|j                  rd      d      | j                  |   }|j                  ||      }||dz  }d}n1|| _         |j                  d      d   | _        | j                  |      S )zSkip tokens.

        Args:
            expect: Expect object describing the expected token.

        Raises:
            UnexpectedEndOfText: If end of file is reached.

        Returns:
            A new token.
        r   zUnexpected end of markupr)   r   )r/   rD   r   r   rT   r   r   rm   rk   spanr   )r   r   r/   rD   r   rj   s         r   skip_tozTokenizer.skip_to\  s     ,,#djj/)#NNIIf% "33 I	 	 8	 	 ::g&DMM$/E}1&#jjmA.~~f--+ r   N))r   r   )r   r>   r   r   rL   rM   )r   rW   rL   r   )rN   rO   rP   rQ   r   r   r   rU   r   r   r   r      s     wr$.r   r   )$
__future__r   rf   typingr   r   	rich.reprrichrich.consoler   r   rich.highlighterr   rich.paddingr	   
rich.panelr
   	rich.textr   textual.css._error_toolsr   textual.css.constantsr   textual.suggestionsr   textual.css.typesr   	Exceptionr   rT   reprautorW   r   r   r   rU   r   r   <module>r      s    " 	 ,  . ,     2 6 .-Q QhEJ E &1 &1 &1R:  18J 18 18hm. m.r   