
    +h?                     T    d Z ddlZddlZddlmZ ddlmZ ddlmZ i Z G d d      Z	y)zGprovides the Lexer class for parsing template strings into parse trees.    N)
exceptions)	parsetree)adjust_whitespacec                       e Zd Z	 ddZed        ZddZd Zd Zd Z	 e
j                  d      Zd	 Zd
 Zd Zd Zd Zd Zd Zd Zd Zd Zd Zy)LexerNc                 $   || _         || _        t        j                  | j                        | _        d| _        d| _        d| _        d| _        g | _	        g | _
        g | _        || _        |g | _        y t        |d      s	|g| _        y || _        y )N   r   __iter__)textfilenamer   TemplateNodetemplatematched_linenomatched_charposlinenomatch_positiontagcontrol_lineternary_stackencodingpreprocessorhasattr)selfr   r   input_encodingr   s        =/var/www/html/venv/lib/python3.12/site-packages/mako/lexer.py__init__zLexer.__init__   s     	 !..t}}= & "Dz2!-D ,D    c                 `    | j                   | j                  | j                  | j                  dS )N)sourcer   posr   )r   r   r   r   )r   s    r   exception_kwargszLexer.exception_kwargs*   s.     ii))''	
 	
r   c                     	 t         ||f   }| j	                  |      S # t        $ r; |rt        j                  ||      nt        j                  |      }|t         ||f<   Y Tw xY w)z>compile the given regexp, cache the reg, and call match_reg().)_regexp_cacheKeyErrorrecompile	match_reg)r   regexpflagsregs       r   matchzLexer.match3   sb    	10C
 ~~c""	  	1/4"**VU+"**V:LC-0M65/*	1s    AA"!A"c                    | j                   }|j                  | j                  | j                         }|r|j                         \  }}||k(  r|dz   n|| _         | j                  | _        |dz
  }|dk\  r0|| j                  k  r!| j                  d|dz    j                  d      }||z
  | _        | xj                  | j                  || j                    j                  d      z  c_        |S )zmatch the given regular expression object to the current text
        position.

        if a match occurs, update the current text and line position.

        r	   r   N
)
r   r+   r   spanr   r   
textlengthrfindr   count)r   r*   mpr+   startendcps          r   r'   zLexer.match_reg>   s       		$))T%8%89 ::<LUC-0E\#'sD"&++DaBQw2/YYxa(..t4#%7D KK499R$*=*=>DDTJJKr   c                    | j                   }dj                  |      }d}d}d}	 | j                  d      }|r| j                  dt        j                        }|r8| j                  d|z        }|rV|r|dkD  sO|dkD  sJ|dkD  sE| j
                  || j                   t        |j                  d            z
   |j                  d      fS |s#| j                  d|z  t        j                        }|r||j                  d      j                  d      z  }||j                  d      j                  d	      z  }||j                  d      j                  d
      z  }||j                  d      j                  d      z  }||j                  d      j                  d      z  }||j                  d      j                  d      z  }t        j                  ddj                  |      z  fi | j                  )N|r   z#.*\nz+(\"\"\"|\'\'\'|\"|\')[^\\]*?(\\.[^\\]*?)*\1z(%s)r	   z(.*?)(?=\"|\'|#|%s){}()[]zExpected: %s,)r   joinr+   r%   Sr   lengroupr1   r   SyntaxExceptionr!   )	r   watch_nestingr   startpostext_rebrace_levelparen_levelbracket_levelr+   s	            r   parse_until_textzLexer.parse_until_textT   s   &&))D/JJx(EJJ>E JJw01E 1_a=1;L II 4#6#6U[[^9L#L KKN	  

#9G#CRTTJu{{1~33C88u{{1~33C88u{{1~33C88u{{1~33C88Q!5!5c!::Q!5!5c!::,,$/373H3H r   c                    |j                  d| j                         |j                  d| j                         |j                  d| j                         | j                  |d<    ||i |}t        | j                        r)| j                  d   j                  j                  |       n%| j                  j                  j                  |       | j                  r| j                  d   }|j                  j                  |       t        |t        j                        r|j                  |j                        sF| j                   r:| j                   d   r+| j                   d   d   j                  j                  |       t        |t        j"                        rEt        | j                        r| j                  d   |_        | j                  j                  |       y t        |t        j                        rU|j&                  r5| j                  j)                          | j                   j)                          y |j*                  r7| j                  j                  |       | j                   j                  g        y | j                  rG| j                  d   j                  |j                        r| j                   d   j                  |       y | j                  rq| j                  d   j                  |j                        sHt-        j.                  d|j                  d| j                  d   j                  dfi | j0                  y y y )	Nr   r   r    r   	Keyword 'z#' not a legal ternary for keyword '')
setdefaultr   r   r   r   rA   r   nodesappendr   r   
isinstancer   ControlLine
is_ternarykeywordr   Tagparentisendpop
is_primaryr   rC   r!   )r   nodeclsargskwargsnodecontrol_frames         r   append_nodezLexer.append_node|   s   (DII.(D$7$78%!5!56!]]z''txx=HHRL%%d+MM&&t,
  --b1M&&t, tY%:%:;%00>&&&&r*""2&r*0077=dIMM*488}"hhrlHHOOD!i334zz!!%%'""&&(!!((.""))"-""t'8'8'<'G'G( ""2&--d3""4+<+<R+@+K+K, !00||T%6%6r%:%B%BD ++ ," 5r   z #.*coding[:=]\s*([-\w.]+).*\r?\nc                 >   t        |t              r<| j                  j                  |      }|xr |j	                  d      xs |xs d}||fS |j                  t        j                        r|t        t        j                        d }d}| j                  j                  |j                  dd            }||j	                  d      dk7  rt        j                  d|j	                  d      z  |j                  dd      dd|      | j                  j                  |j                  dd            }|r|j	                  d      n|xs d}|r	 |j                  |      }||fS ||fS # t        $ r- t        j                  d|z  |j                  dd      dd|      w xY w)zgiven string/unicode or bytes/string, determine encoding
        from magic encoding comment, return body as unicode
        or raw if decode_raw=False

        r	   zutf-8NignorezHFound utf-8 BOM in file, with conflicting magic encoding comment of '%s'r   z0Unicode decode operation of encoding '%s' failed)rR   str
_coding_rer+   rB   
startswithcodecsBOM_UTF8rA   decoder   CompileExceptionUnicodeDecodeError)r   r   
decode_rawknown_encodingr   mr   parsed_encodings           r   decode_raw_streamzLexer.decode_raw_stream   s    dC %%d+A'QWWQZD>DWHT>!??6??+FOO,./D%O%%dkk'8&DEA}w!6 11578wwqzBKK2  %%dkk'8&DEA,-aggaj>3LWO
{{?3 $$$$ &  11F%&KK2 s   E& &6Fc                    | j                  | j                  d| j                  | j                        \  | _        | _        | j                  D ]  } || j                        | _         | j                  | j                         t        | j                        | _        	 | j                  | j                  kD  rn| j                         rn| j                         r=| j                         rN| j                         r_| j                         rp| j                         r| j!                         r| j#                         r| j%                         r| j                  | j                  kD  rnt'        j(                  d      t        | j*                        r:t'        j,                  d| j*                  d   j.                  z  fi | j0                  t        | j2                        rut'        j,                  d| j2                  d   j.                  z  | j                  | j2                  d   j4                  | j2                  d   j6                  | j                        | j8                  S )NTzassertion failedUnclosed tag: <%%%s>rL   z"Unterminated control keyword: '%s')ro   r   r   r   r   r'   rd   rA   r/   r   	match_endmatch_expressionmatch_control_linematch_commentmatch_tag_startmatch_tag_endmatch_python_blockmatch_percent
match_textr   MakoExceptionr   rC   rU   r!   r   r   r    r   )r   preprocs     r   parsezLexer.parse   s   #'#9#9IItT]]DMM$
 ty (( 	+G		*DI	+
 	t'dii.""T__4~~$$&&&(!!###%!!#&&(!!# ""T__4**+=>>txx=,,&")=)==''  t  !,,4##B'//0		!!"%,,!!"%))  }}r   c                    d}| j                  |t        j                  t        j                  z  t        j                  z        }|sy|j                         \  }}}|| _        i }|r>t        j                  d|      D ]%  }|\  }}	}
|	xs |
}|j                  dd      }|||<   ' | j                  t        j                  ||       |r| j                  j                          y|dk(  r| j                  dt        j                        }|s:t        j                  d| j                  d	   j                  z  fi | j                   | j                  t        j"                  |j%                  d
             | j'                         S y)Na  
            \<%     # opening tag

            ([\w\.\:]+)   # keyword

            ((?:\s+\w+|\s*=\s*|"[^"]*?"|'[^']*?'|\s*,\s*)*)  # attrname, = \
                                               #        sign, string expression
                                               # comma is for backwards compat
                                               # identified in #366

            \s*     # more whitespace

            (/)?>   # closing

        Fz)\s*(\w+)\s*=\s*(?:'([^']*)'|\"([^\"]*)\")
r-   r   z(.*?)(?=\</%text>)rq   rL   r	   T)r+   r%   Ir@   XgroupsrU   findallreplacer`   r   rV   r   rY   r   rC   r!   TextrB   rw   )r   r*   r+   rU   attrrX   
attributesattkeyval1val2r   s               r   rv   zLexer.match_tag_start  s_     

DD244K"$$

 $||~u
zz<d ' #&T4|t||FD1"&
3' 	<HHLLN  JJ4bdd;E 00*TXXb\-A-AA++  Y^^U[[^<%%''r   c                    | j                  d      }|rt        | j                        s2t        j                  d|j                  d      z  fi | j                  | j                  d   j                  |j                  d      k7  rMt        j                  d|j                  d      d| j                  d   j                  dfi | j                  | j                  j                          yy	)
Nz\</%[\t ]*([^\t ]+?)[\t ]*>z(Closing tag without opening tag: </%%%s>r	   rL   zClosing tag </%z> does not match tag: <%>TF)	r+   rA   r   r   rC   rB   r!   rU   rY   r   r+   s     r   rw   zLexer.match_tag_endF  s    

9:txx= 00>kk!n% ++ 
 "%%Q7 00{{1~txx|';';= ++ 
 HHLLNr   c                 r    | j                  dt        j                        }|sy|j                         }|r|S y)Nz\ZFT)r+   r%   r@   rB   )r   r+   strings      r   rr   zLexer.match_endZ  s2    

5"$$'Mr   c                     | j                  dt        j                        }|rE| j                  t        j
                  |j                  d      dz   |j                  d      z          yy)Nz(?<=^)(\s*)%%(%*)r	   %   TF)r+   r%   Mr`   r   r   rB   r   s     r   ry   zLexer.match_percente  sS    

/6A 4u{{1~ E r   c                     | j                  dt        j                  t        j                  z        }|r4|j	                  d      }|r | j                  t        j                  |       yy)Na  
                (.*?)         # anything, followed by:
                (
                 (?<=\n)(?=[ \t]*(?=%|\#\#))  # an eval or line-based
                                            # comment, preceded by a
                                            # consumed newline and whitespace
                 |
                 (?=\${)      # an expression
                 |
                 (?=</?%)  # a substitution or block or call start or end
                              # - don't consume
                 |
                 (\\\r?\n)    # an escaped newline  - throw away
                 |
                 \Z           # end of string
                )r	   TF)r+   r%   r   r@   rB   r`   r   r   )r   r+   r   s      r   rz   zLexer.match_texto  sU    

  DD244K#
( ;;q>D  6r   c                    | j                  d      }|rr| j                  | j                  }}| j                  dd      \  }}t	        |      dz   }| j                  t        j                  ||j                  d      dk(  ||       yy)	Nz<%(!)?Fz%>r-   r	   !r   r    T)	r+   r   r   rJ   r   r`   r   CoderB   )r   r+   liner    r   r4   s         r   rx   zLexer.match_python_block  s    

9%++T-A-A#D--eU;ID# %T*T1DA#%   r   c                 J   | j                  d      }|sy| j                  | j                  }}| j                  ddd      \  }}|dk(  r| j                  dd      \  }}nd}|j	                  dd	      }| j                  t        j                  ||j                         ||
       y)Nz\${FTz\|r9   r7    r   r-   r   )	r+   r   r   rJ   r   r`   r   
Expressionstrip)r   r+   r   r    r   r4   escapess          r   rs   zLexer.match_expression  s    

6"'')=)=c))$t<	c#:00t<LGSG||FD)  MMO 	 	
 r   c                    | j                  dt        j                        }|sy|j                  d      }|j                  d      }|dk(  rt        j                   d|      }|s#t	        j
                  d|z  fi | j                  |j                  dd      \  }}|d u}|rt        | j                        s't	        j
                  d|d	|d
fi | j                  | j                  d   j                  |k7  r>t	        j
                  d|d| j                  d   j                  d
fi | j                  | j                  t        j                  |||       y| j                  t        j                  |       y)NzC(?<=^)[\t ]*(%(?!%)|##)[\t ]*((?:(?:\\\r?\n)|[^\r\n])*)(?:\r?\n|\Z)Fr	   r   r   z(end)?(\w+)\s*(.*)zInvalid control line: '%s'zNo starting keyword 'z' for 'rN   rL   rM   z' doesn't match keyword 'T)r+   r%   r   rB   r   rC   r!   rA   r   rU   r`   r   rS   Comment)r   r+   operatorr   m2rX   rU   s          r   rt   zLexer.match_control_line  sq   

DD

 ;;q>{{1~s?/6B 00047++   XXa^NE7%E4,,-$44?FM//  &&r*22g=$44!2!22!6!>!>@ // 
 Y22GUDI  Y..5r   c                     | j                  dt        j                        }|r0| j                  t        j
                  |j                  d             yy)z*matches the multiline version of a commentz<%doc>(.*?)</%doc>r	   TF)r+   r%   r@   r`   r   r   rB   r   s     r   ru   zLexer.match_comment  s>    

0"$$7Y..A?r   )NNN)N)__name__
__module____qualname__r   propertyr!   r+   r'   rJ   r`   r%   r&   rd   ro   r}   rv   rw   rr   ry   rz   rx   rs   rt   ru    r   r   r   r      s    EI-, 
 
	#,&P0d ?@J(%T8t0d(	:&*$Lr   r   )
__doc__rf   r%   makor   r   
mako.pygenr   r#   r   r   r   r   <module>r      s+    N  	   (N Nr   