ó
ži‚Nc           @   sj   d  Z  d d l Z d d l Z d d l m Z m Z m Z d d l m Z i  Z	 d e
 f d „  ƒ  YZ d S(   sG   provides the Lexer class for parsing template strings into parse trees.iÿÿÿÿN(   t	   parsetreet
   exceptionst   util(   t   adjust_whitespacet   Lexerc           B   s¼   e  Z d e d 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 RS(   c         C   sÓ   | |  _  | |  _ t j |  j ƒ |  _ d |  _ d |  _ d |  _ d |  _ g  |  _	 g  |  _
 | |  _ | |  _ t j r | r t j d ƒ ‚ n  | d  k r¨ g  |  _ n' t | d ƒ sÆ | g |  _ n	 | |  _ d  S(   Ni   i    s4   Mako for Python 3 does not support disabling Unicodet   __iter__(   t   textt   filenameR    t   TemplateNodet   templatet   matched_linenot   matched_charpost   linenot   match_positiont   tagt   control_linet   disable_unicodet   encodingR   t   py3kR   t   UnsupportedErrort   Nonet   preprocessort   hasattr(   t   selfR   R   R   t   input_encodingR   (    (    s.   /usr/lib/python2.7/dist-packages/mako/lexer.pyt   __init__   s&    										c         C   s,   i |  j  d 6|  j d 6|  j d 6|  j d 6S(   Nt   sourceR   t   posR   (   R   R
   R   R   (   R   (    (    s.   /usr/lib/python2.7/dist-packages/mako/lexer.pyt   exception_kwargs+   s    

c         C   so   y t  | | f } WnK t k
 ra | r? t j | | ƒ } n t j | ƒ } | t  | | f <n X|  j | ƒ S(   s>   compile the given regexp, cache the reg, and call match_reg().(   t   _regexp_cachet   KeyErrort   ret   compilet	   match_reg(   R   t   regexpt   flagst   reg(    (    s.   /usr/lib/python2.7/dist-packages/mako/lexer.pyt   match2   s    c         C   sû   |  j  } | j |  j |  j  ƒ } | r÷ | j ƒ  \ } } | | k rU | d |  _  n	 | |  _  |  j |  _ t j d |  j | |  j  !ƒ } | d } x< | d k rÑ | |  j k  rÑ |  j | d k rÑ | d 8} q– W| | |  _	 |  j t
 | ƒ 7_ n  | S(   sš   match the given regular expression object to the current text position.
 
        if a match occurs, update the current text and line position.
 
        i   s   \ni    s   
(   R   R%   R   t   spanR   R
   R   t   findallt
   textlengthR   t   len(   R   R$   t   mpR%   t   startt   endt   linest   cp(    (    s.   /usr/lib/python2.7/dist-packages/mako/lexer.pyR!   @   s    		
1c         G   s<  |  j  } x,t r7|  j d ƒ } | r- q n  |  j d ƒ } | r’ |  j d | j d ƒ t j ƒ } | s4t j d | j d ƒ |  j  ‚ q4q |  j d d j	 | ƒ ƒ } | rç |  j
 | |  j  t | j d ƒ ƒ !| j d ƒ f S|  j d d j	 | ƒ t j ƒ } | s t j d	 d
 j	 | ƒ |  j  ‚ q q Wd  S(   Ns   #.*\ns   (\"\"\"|\'\'\'|\"|\')s   .*?%si   s   Unmatched '%s's   (%s)t   |s   .*?(?=\"|\'|#|%s)s   Expected: %st   ,(   R   t   TrueR%   t   groupR   t   SR   t   SyntaxExceptionR   t   joinR   R)   (   R   R   t   startposR%   t   m(    (    s.   /usr/lib/python2.7/dist-packages/mako/lexer.pyt   parse_until_text\   s.    		"#"c         O   s~  | j  d |  j ƒ | j  d |  j ƒ | j  d |  j ƒ |  j | d <| | | Ž  } t |  j ƒ r~ |  j d j j | ƒ n |  j	 j j | ƒ t
 | t j ƒ rØ t |  j ƒ rÅ |  j d | _ n  |  j j | ƒ n¢ t
 | t j ƒ rz| j r|  j j ƒ  qz| j r|  j j | ƒ qzt |  j ƒ rz|  j d j | j ƒ rzt j d | j |  j d j f |  j  ‚ qzn  d  S(   NR   R   R   R   iÿÿÿÿs1   Keyword '%s' not a legal ternary for keyword '%s'(   t
   setdefaultR   R
   R   R   R)   R   t   nodest   appendR	   t
   isinstanceR    t   Tagt   parentt   ControlLinet   isendR   t   popt
   is_primaryt
   is_ternaryt   keywordR   R4   R   (   R   t   nodeclst   argst   kwargst   node(    (    s.   /usr/lib/python2.7/dist-packages/mako/lexer.pyt   append_nodex   s.    		s    #.*coding[:=]\s*([-\w.]+).*\r?\nc   	   
   C   s™  t  | t ƒ rL |  j j | ƒ } | r6 | j d ƒ p? | p? d } | | f S| j t j ƒ rð | t t j ƒ } d } |  j j | j	 d d ƒ ƒ } | d k	 r2| j d ƒ d k r2t j d | j d ƒ | j	 d d ƒ d d | ƒ ‚ q2nB |  j j | j	 d d ƒ ƒ } | r&| j d ƒ } n | p/d } | ry | j	 | ƒ } Wqt k
 r‹} t j d | | j	 d d ƒ d d | ƒ ‚ qXn  | | f S(	   s§   given string/unicode or bytes/string, determine encoding
           from magic encoding comment, return body as unicode
           or raw if decode_raw=False

        i   t   asciis   utf-8t   ignoresH   Found utf-8 BOM in file, with conflicting magic encoding comment of '%s'i    s0   Unicode decode operation of encoding '%s' failedN(   R<   t   unicodet
   _coding_reR%   R2   t
   startswitht   codecst   BOM_UTF8R)   t   decodeR   R   t   CompileExceptiont   UnicodeDecodeError(	   R   R   t
   decode_rawt   known_encodingR   R7   R   t   parsed_encodingt   e(    (    s.   /usr/lib/python2.7/dist-packages/mako/lexer.pyt   decode_raw_stream”   s8    !
!c         C   sà  |  j  |  j |  j |  j |  j ƒ \ |  _ |  _ x# |  j D] } | |  j ƒ |  _ q; W|  j |  j ƒ t |  j ƒ |  _	 xÓ t
 rN|  j |  j	 k r˜ Pn  |  j ƒ  r¨ Pn  |  j ƒ  rº q| n  |  j ƒ  rÌ q| n  |  j ƒ  rÞ q| n  |  j ƒ  rð q| n  |  j ƒ  rq| n  |  j ƒ  rq| n  |  j ƒ  r&q| n  |  j |  j	 k r<Pn  t j d ƒ ‚ q| Wt |  j ƒ r„t j d |  j d j |  j  ‚ n  t |  j ƒ rÙt j d |  j d j |  j |  j d j |  j d j |  j ƒ ‚ n  |  j S(   Ns   assertion faileds   Unclosed tag: <%%%s>iÿÿÿÿs"   Unterminated control keyword: '%s'(   RX   R   R   R   R   R   R!   RM   R)   R(   R1   R   t	   match_endt   match_expressiont   match_control_linet   match_commentt   match_tag_startt   match_tag_endt   match_python_blockt
   match_textR   RR   R   R4   RD   R   R   R   R   R	   (   R   t   preproc(    (    s.   /usr/lib/python2.7/dist-packages/mako/lexer.pyt   parse¼   sR    			c         C   so  |  j  d t j t j Bt j Bƒ } | rg| j d ƒ | j d ƒ | j d ƒ } } } | |  _ i  } | rÄ xT t j d | ƒ D]= } | \ } } }	 | pž |	 }
 |
 j d d ƒ }
 |
 | | <q€ Wn  |  j	 t
 j | | ƒ | rð |  j j ƒ  ns | d k rc|  j  d	 t j ƒ } | s=t j d
 |  j d j |  j  ‚ n  |  j	 t
 j | j d ƒ ƒ |  j ƒ  St St Sd  S(   Nsö   
            \<%     # opening tag
 
            ([\w\.\:]+)   # keyword
 
            ((?:\s+\w+|\s*=\s*|".*?"|'.*?')*)  # attrname, = sign, string expression
 
            \s*     # more whitespace
 
            (/)?>   # closing
 
            i   i   i   s)   \s*(\w+)\s*=\s*(?:'([^']*)'|\"([^\"]*)\")s   
s   
R   s   (.*?)(?=\</%text>)s   Unclosed tag: <%%%s>iÿÿÿÿ(   R%   R   t   IR3   t   XR2   RD   R'   t   replaceRI   R    R=   R   RA   R   R4   R   t   TextR^   R1   t   False(   R   R%   RD   t   attrR@   t
   attributest   attt   keyt   val1t   val2R   (    (    s.   /usr/lib/python2.7/dist-packages/mako/lexer.pyR]   ð   s6    /	
c         C   s¶   |  j  d ƒ } | r® t |  j ƒ sI t j d | j d ƒ |  j  ‚ nT |  j d j | j d ƒ k r t j d | j d ƒ |  j d j f |  j  ‚ n  |  j j ƒ  t	 St
 Sd  S(   Ns   \</%[\t ]*(.+?)[\t ]*>s(   Closing tag without opening tag: </%%%s>i   iÿÿÿÿs.   Closing tag </%%%s> does not match tag: <%%%s>(   R%   R)   R   R   R4   R2   R   RD   RA   R1   Rg   (   R   R%   (    (    s.   /usr/lib/python2.7/dist-packages/mako/lexer.pyR^     s    c         C   s@   |  j  d t j ƒ } | r8 | j ƒ  } | r1 | St Sn t Sd  S(   Ns   \Z(   R%   R   R3   R2   R1   Rg   (   R   R%   t   string(    (    s.   /usr/lib/python2.7/dist-packages/mako/lexer.pyRY   -  s    c         C   sY   |  j  d t j t j Bƒ } | rQ | j d ƒ } | rM |  j t j | ƒ n  t St	 Sd  S(   Nsá  
                (.*?)         # anything, followed by:
                (
                 (?<=\n)(?=[ \t]*(?=%|\#\#)) # an eval or line-based 
                                             # comment preceded by a 
                                             # consumed newline and whitespace
                 |
                 (?=\${)      # an expression
                 |
                 (?=\#\*)     # multiline comment
                 |
                 (?=</?[%&])  # a substitution or block or call start or end
                              # - don't consume
                 |
                 (\\\r?\n)    # an escaped newline  - throw away
                 |
                 \Z           # end of string
                )i   (
   R%   R   Rd   R3   R2   RI   R    Rf   R1   Rg   (   R   R%   R   (    (    s.   /usr/lib/python2.7/dist-packages/mako/lexer.pyR`   8  s    c         C   sŠ   |  j  d ƒ } | r‚ |  j |  j } } |  j d ƒ \ } } t | ƒ d } |  j t j | | j d ƒ d k d | d | ƒt	 St
 Sd  S(   Ns   <%(!)?s   %>s   
i   t   !R   R   (   R%   R
   R   R8   R   RI   R    t   CodeR2   R1   Rg   (   R   R%   t   lineR   R   R,   (    (    s.   /usr/lib/python2.7/dist-packages/mako/lexer.pyR_   T  s    "c         C   s°   |  j  d ƒ } | r¨ |  j |  j } } |  j d d ƒ \ } } | d k rd |  j d ƒ \ } } n d } | j d d ƒ } |  j t j | | j ƒ  d | d	 | ƒt	 St
 Sd  S(
   Ns   \${s   \|t   }R/   t    s   
s   
R   R   (   R%   R
   R   R8   Re   RI   R    t
   Expressiont   stripR1   Rg   (   R   R%   Rq   R   R   R,   t   escapes(    (    s.   /usr/lib/python2.7/dist-packages/mako/lexer.pyRZ   d  s    c         C   sT  |  j  d t j ƒ } | rL| j d ƒ } | j d ƒ } | d k r5t j  d | ƒ } | sy t j d | |  j  ‚ n  | j d d ƒ \ } } | d  k	 } | rt |  j	 ƒ sÔ t j d | | f |  j  ‚ q|  j	 d j
 | k rt j d	 | |  j	 d j
 f |  j  ‚ qn  |  j t j | | | ƒ n |  j t j | ƒ t St Sd  S(
   NsB   (?<=^)[\t ]*(%(?!%)|##)[\t ]*((?:(?:\\r?\n)|[^\r\n])*)(?:\r?\n|\Z)i   i   t   %s   (end)?(\w+)\s*(.*)s   Invalid control line: '%s's!   No starting keyword '%s' for '%s'iÿÿÿÿs'   Keyword '%s' doesn't match keyword '%s'(   R%   R   t   MR2   R   R4   R   R   R)   R   RD   RI   R    R?   t   CommentR1   Rg   (   R   R%   t   operatorR   t   m2R@   RD   (    (    s.   /usr/lib/python2.7/dist-packages/mako/lexer.pyR[   v  s8    
c         C   sC   |  j  d t j ƒ } | r; |  j t j | j d ƒ ƒ t St Sd S(   s*   matches the multiline version of a comments   <%doc>(.*?)</%doc>i   N(	   R%   R   R3   RI   R    Ry   R2   R1   Rg   (   R   R%   (    (    s.   /usr/lib/python2.7/dist-packages/mako/lexer.pyR\   —  s
    N(   t   __name__t
   __module__R   Rg   R   t   propertyR   R%   R!   R8   RI   R   R    RM   RX   Rb   R]   R^   RY   R`   R_   RZ   R[   R\   (    (    (    s.   /usr/lib/python2.7/dist-packages/mako/lexer.pyR      s&   				(	4	+						!(   t   __doc__R   RO   t   makoR    R   R   t
   mako.pygenR   R   t   objectR   (    (    (    s.   /usr/lib/python2.7/dist-packages/mako/lexer.pyt   <module>   s
   