a
    :c                     @   sl  d dl mZmZmZmZmZ d dlmZmZm	Z	m
Z
mZmZmZmZmZmZmZmZ d dlZd dlZd dlZd dlZd dlZd dlZd dlmZ dag add ZG dd	 d	eZG d
d de Z!dd Z"e#dej$Z%dd Z&dd Z'dd Z(da)d)ddZ*dd Z+e,g dZ-g dZ.g dZ/d0dd  e.D e/ Z1d!Z2d"Z3G d#d$ d$e Z4G d%d& d&e Z5d'd( Z6dS )*    )divisionabsolute_importwith_statementprint_functionunicode_literals)PY2
basestringbchrbordchropenpystrrangeroundstrtobytesunicodeN)match_logical_word c                 C   s   ddl }tj| }|tkr|azdt|d }| dd}W d   n1 sR0    Y  |drpt	|d}|d7 }|
daW n ty   g aY n0 |ttkrt|d	  d S dS dS )
z
    Gets the text of a line, in a best-effort way, for debugging purposes. May
    return just a newline, if the line doesn't exist.
    r   Nrbutf-8python_strict_ren.py


   )	linecacherenpyexportsunelide_filenameline_text_filenamer   readdecodeendswithren_py_to_rpysplitline_text_cache	Exceptionlen)filenamelinenor   Zfull_filenamefdata r-   !/home/tom/ab/renpy/renpy/lexer.pyget_line_text,   s     	.


r/   c                   @   s   e Zd ZdddZdd ZdS )
ParseErrorNFc                 C   s6  dt |||f }|r t|tr,d|}|d}t|dkrd }	d}
|
t|d k r|d |
 }|dkrx|
d7 }
n0||	krd }	n"|	rn|dks|dks|d	kr|}	|
d7 }
qJ|	r|d
|	 7 }|D ]X}|d| 7 }|d ur|t|kr|dd|  d 7 }d }n|t|8 }|r q q|| _t| | d S )NzFile "%s", line %d: %sr   r   r   r   \`'"z:
(Perhaps you left out a %s at the end of the first line.)z
     ^)	unicode_filename
isinstancelistjoinr%   r(   messager'   __init__)selfr)   numbermsglineposfirstr;   linesZopen_stringiclr-   r-   r.   r<   T   s@    





zParseError.__init__c                 C   s   | j S N)r;   r=   r-   r-   r.   __unicode__   s    zParseError.__unicode__)NNF)__name__
__module____qualname__r<   rI   r-   r-   r-   r.   r0   R   s   
/r0   c                   @   s   e Zd ZdZdd ZdS )LineNumberHolderz)
    Holds the expected line number.
    c                 C   s
   d| _ d S Nr   )r@   rH   r-   r-   r.   r<      s    zLineNumberHolder.__init__N)rJ   rK   rL   __doc__r<   r-   r-   r-   r.   rM      s   rM   c                 C   sX   t | tr| S z| dW S  ty,   Y n0 z| dW S  tyL   Y n0 | dS )z4
    Converts the supplied filename to unicode.
    mbcsr   zlatin-1)r8   r   r"   r'   )fnr-   r-   r.   r7      s    
r7   z__(\w+)|\w+| +|.c                 C   sJ   t j| }t j|d }|dd}dd }td||}d| d S )	Nr   r5   _c                 S   s   t t| dS rN   )hexordgroup)mr-   r-   r.   
munge_char   s    z"munge_filename.<locals>.munge_charz[^a-zA-Z0-9_]Z_m1___)ospathbasenamesplitextreplaceresub)rQ   rvrW   r-   r-   r.   munge_filename   s    ra   c                 C   s   |  dd} tjtjj ddd }tjtjj ddd }||rX||g}n||g}|D ]"}| |rd| t	|d } qqd| }|S )zy
    Returns a version of fn that is either relative to the base directory,
    or relative to the Ren'Py directory.
    r1   /N)
r]   rY   rZ   abspathr   configbasedir
renpy_base
startswithr(   )rQ   re   rf   dirsdr`   r-   r-   r.   elide_filename   s    


rj   c                 C   s   t j| } tjjd ur:t jtjj| }t j|r:|S t jtjj| }t j|r\|S t jtjj	| }t j|r~|S | S rG   )
rY   rZ   normpathr   rd   alternate_unelide_pathr:   existsre   rf   )rQ   Zfn0Zfn1Zfn2r-   r-   r.   r      s    r   r   Fc                    s   fdd}| a |r|}n:t| d }| dd}W d   n1 sJ0    Y  | drht|| }t| } t|  |d7 }g }|}d	}	t|r|d	 d
kr|	d7 }	|st	j
 jrt	jj}
ni }
t|}t	jj|  d	}d	}|	|k r|}g }d	}| |f}t	jt ||	|
|< d}|	|k r|	}||	 }|dkrDt| |d|dkr|sd|}td|s||| ||f |du r|	}|d |
| _||d  dv r|d8 }q||
| _||
| j|
| j |
| _||
| j|
| j |
| _|	d7 }	|d7 }d}g }q|dkr,|d7 }d}|dkrB|	d7 }	q|dkr|||	d  dkr||	d7 }	|d7 }|d q|dv r|d7 }|dv r|r|d8 }|dkr|	}||	 dkr|	d7 }	qq|dv r|}|| |	d7 }	d}d}|	|d k rH||	 |krH||	d  |krH|| || |	d7 }	d}g }|	|k rP||	 }|dkrp|d7 }|dkr|	d7 }	qL|rd}|	d7 }	|| qL||kr(|s|	d7 }	|| qP|	|d k r(||	d  |kr(||	d  |kr(|	d7 }	|| || || qP|dkr6d}|| |	d7 }	qLqLd|}d|v rrtd||}|| qt||	\}}}|r|dd }d|vr | }|| |}	|	| d krt| |d!|dd"qq|r t| |d#|dd"|S )$a  
    Reads `filename`, and divides it into logical lines.

    Returns a list of (filename, line number, line text) triples.

    If `filedata` is given, it should be a unicode string giving the file
    contents. In that case, `filename` need not exist.
    c                    sN   |  d}t|d@ dkr$|  dS d|  dv r<|  dS |  |  d S )Nr   r   rX      )rU   r(   )rV   Zbracketsprefixr-   r.   munge_string   s    


z(list_logical_lines.<locals>.munge_stringr   r   r   Nr   r   r   u   ﻿r   	z1Tab characters are not allowed in Ren'Py scripts.r   r   z^\s*$z r1   rn   z\
z([{z}])#z"'`FT   z[__z(\.|\[+)__(\w+)rX   i   z:Overly long logical line. (Check strings and parenthesis.))r@   rB   zBis not terminated with a newline. (Check strings and parenthesis.))original_filenamer   r!   r"   r#   r$   rj   ra   r(   r   gamecontext
init_phaseZ
scripteditrC   filesaddZLiner0   r:   r^   matchappendZ	end_delimendstarttextZ	full_textr_   r   )r)   Zfiledata
linenumberZ	add_linesrq   r,   r+   r`   r>   rA   rC   Zlen_datar@   Zstart_numberZ
parendepthlocendposstartposrE   delimescapeZtriplequoteswordmagicr~   restr-   ro   r.   list_logical_lines   s   
.














.







2









r   c                    sT   dd   fddrFd \}}} |d dkrFt ||dddd S )aE  
    This takes as input the list of logical line triples output from
    list_logical_lines, and breaks the lines into blocks. Each block
    is represented as a list of (filename, line number, line text,
    block) triples, where block is a block list (which may be empty if
    no block is associated with this line.)
    c                 S   s:   d}d}| | dkr*|d7 }|d7 }qq*q|| |d  fS )Nr   r5   r   r-   )rF   depthindexr-   r-   r.   depth_split  s    z(group_logical_lines.<locals>.depth_splitc           
         s   g }d }| t k r|  \}}} |\}}||k r8q|d u rD|}||krXt||d| d7 } | |d \}	} |||||	f q|| fS )NzIndentation mismatch.r   )r(   r0   r}   )
rD   Z	min_depthr`   r   r)   r>   r   Z
line_depthr   blockr   gll_corerC   r-   r.   r      s    z%group_logical_lines.<locals>.gll_corer   z(Unexpected indentation at start of file.)r0   )rC   r)   r>   r   r-   r   r.   group_logical_lines  s    
r   )$asatbehindcall
expressionhideifinimageinitjumpmenuZonlayerpythonreturnsceneshowwithwhilezorder	transform)z<>z<<z<=<z>>z>=>z!=z==|r6   &+-z***z//rb   %~)z\bor\bz\band\bz\bnot\bz\bin\bz\bis\br   c                 C   s   g | ]}t |qS r-   )r^   r   ).0rD   r-   r-   r.   
<listcomp>g      r   z0[a-zA-Z_\u00a0-\ufffd][0-9a-zA-Z_\u00a0-\ufffd]*z5[-0-9a-zA-Z_\u00a0-\ufffd][-0-9a-zA-Z_\u00a0-\ufffd]*c                   @   s    e Zd ZdZdd Zdd ZdS )SubParsezs
    This represents the information about a subparse that can be provided to
    a creator-defined statement.
    c                 C   s
   || _ d S rG   )r   )r=   r   r-   r-   r.   r<   s  s    zSubParse.__init__c                 C   s*   | j s
dS d| j d j| j d jS d S )Nz<SubParse empty>z<SubParse {}:{}>r   )r   formatr)   r   rH   r-   r-   r.   __repr__v  s    zSubParse.__repr__N)rJ   rK   rL   rO   r<   r   r-   r-   r-   r.   r   m  s   r   c                   @   s  e Zd ZdZdaddZdd	 Zd
d Zdd Zdd Zdd Z	dd Z
ejdd Zdd Zdd Zdd Zdd Zdd Zd d! Zdbd"d#Zd$d% Zd&d' Zd(d) Zd*d+ Zd,d- Zd.d/ Zd0d1 Zd2d3 Zdcd4d5Zd6d7 Zd8d9 Zd:d; Z d<d= Z!d>d? Z"dddAdBZ#dedCdDZ$dEdF Z%dfdGdHZ&dIdJ Z'dKdL Z(dMdN Z)dOdP Z*dQdR Z+dgdSdTZ,dUdV Z-dWdX Z.dYdZ Z/d[d\ Z0d]d^ Z1dhd_d`Z2dS )iLexerz
    The lexer that is used to lex script files. This works on the idea
    that we want to lex each line in a block individually, and use
    sub-lexers to lex sub-blocks.
    Fr   Nr   c                 C   sd   || _ || _|| _d| _d| _d| _d| _d| _g | _|| _	d| _
d| _d| _d| _|| _|| _d S )NFr   r   )r   init_offsetr   eobr@   r)   r   r>   subblockglobal_labelrA   word_cache_posword_cache_newpos
word_cachemonologue_delimiter	subparses)r=   r   r   r   r   r   r   r-   r-   r.   r<     s     zLexer.__init__c                 C   sT   |  j d7  _ | j t| jkr(d| _dS | j| j  \| _| _| _| _d| _d| _	dS )a'  
        Advances this lexer to the next line in the block. The lexer
        starts off before the first line, so advance must be called
        before any matching can be done. Returns True if we've
        successfully advanced to a line in the block, or False if we
        have advanced beyond all lines in the block. In general, once
        this method has returned False, the lexer is in an undefined
        state, and it doesn't make sense to call any method other than
        advance (which will always return False) on the lexer.
        r   TFr   r   )
r@   r(   r   r   r)   r>   r   r   rA   r   rH   r-   r-   r.   advance  s    zLexer.advancec                 C   sF   |  j d8  _ d| _| j| j  \| _| _| _| _t| j| _d| _	dS )z
        Puts the parsing point at the end of the previous line. This is used
        after renpy_statement to prevent the advance that Ren'Py statements
        do.
        r   Fr   N)
r@   r   r   r)   r>   r   r   r(   rA   r   rH   r-   r-   r.   	unadvance  s
    zLexer.unadvancec                 C   sT   | j r
dS | jt| jkrdS t|tj| j| j}|s@dS | | _|	dS )a  
        Tries to match the given regexp at the current location on the
        current line. If it succeds, it returns the matched text (if
        any), and updates the current position to be after the
        match. Otherwise, returns None and the position is unchanged.
        Nr   )
r   rA   r(   r   r^   compileDOTALLr|   r~   rU   )r=   regexprV   r-   r-   r.   match_regexp  s    
zLexer.match_regexpc                 C   s   |  d dS )zQ
        Advances the current position beyond any contiguous whitespace.
        z(\s+|\\\n)+N)r   rH   r-   r-   r.   skip_whitespace  s    zLexer.skip_whitespacec                 C   s   |    | |S )z
        Matches something at the current position, skipping past
        whitespace. Even if we can't match, the current position is
        still skipped past the leading whitespace.
        )r   r   )r=   r   r-   r-   r.   r|     s    zLexer.matchc                 C   s    | j }|  |kr|S || _ dS )z
        Matches a keyword at the current position. A keyword is a word
        that is surrounded by things that aren't words, like
        whitespace. (This prevents a keyword from matching a prefix.)
        r   )rA   r   )r=   r   oldposr-   r-   r.   keyword  s
    zLexer.keywordc              
   c   sD   z
dV  W n4 t y> } ztjj|j W Y d}~n
d}~0 0 dS )zm
        Catches errors, then causes the line to advance if it hasn't been
        advanced already.
        N)r0   r   parserparse_errorsr}   r;   )r=   er-   r-   r.   catch_error  s    
zLexer.catch_errorc                 C   sF   | j dkr*| jr*| jd \| _| _| _| _t| j| j|| j| jdS )zc
        Convenience function for reporting a parse error at the current
        location.
        r   r   N)r@   r   r)   r>   r   r   r0   rA   )r=   r?   r-   r-   r.   error
  s    zLexer.errorc                 C   s   |    | jt| jkS )z
        Returns True if, after skipping whitespace, the current
        position is at the end of the end of the current line, or
        False otherwise.
        )r   rA   r(   r   rH   r-   r-   r.   eol  s    z	Lexer.eolc                 C   s   |   s| d dS )zG
        If we are not at the end of the line, raise an error.
        zend of line expected.N)r   r   rH   r-   r-   r.   
expect_eol  s    zLexer.expect_eolc                 C   s(   | j r$|  }|  |d|  dS )zz
        Called to indicate this statement does not expect a block.
        If a block is found, raises an error.
        zoLine is indented, but the preceding %s statement does not expect a block. Please check this line's indentation.N)r   subblock_lexerr   r   )r=   stmtllr-   r-   r.   expect_noblock'  s    zLexer.expect_noblockc                 C   s   | j s| d|  dS )zk
        Called to indicate that the statement requires that a non-empty
        block is present.
        z%s expects a non-empty block.N)r   r   )r=   r   r-   r-   r.   expect_block2  s    zLexer.expect_blockc                 C   s
   t | jS )zL
        Called to check if the current line has a non-empty block.
        )boolr   rH   r-   r-   r.   	has_block;  s    zLexer.has_blockc                 C   s(   | j p|}t| j|| j| j| j| jdS )zk
        Returns a new lexer object, equiped to parse the block
        associated with this line.
        )r   r   r   r   r   )r   r   r   r   r   r   r   )r=   r   r-   r-   r.   r   A  s    
zLexer.subblock_lexerc                 C   s   |  d}|du r|  d}|du r.|  d}|du r:dS |d dkrXd}|dd }nd	}|dd
 }dd }|stdd|}td||}|S )a'  
        Lexes a string, and returns the string to the user, or None if
        no string could be found. This also takes care of expanding
        escapes and collapsing whitespace.

        Be a little careful, as this can return an empty string, which is
        different than None.
        zr?"([^\\"]|\\.)*"Nzr?'([^\\']|\\.)*'zr?`([^\\`]|\\.)*`r   rTr   Fr   c                 S   sr   |  d}|dkrdS |dkr"dS |dkr.dS |dkr:d	S |d
 dkrj|  d}|rntt|  ddS n|S d S Nr   {z{{[z[[r   z%%nr   r   urn      rU   r   intrV   rE   Zgroup2r-   r-   r.   dequotei  s    

zLexer.string.<locals>.dequote\s+r5   \\(u([0-9a-fA-F]{1,4})|.))r|   r^   r_   )r=   r   rawr   r-   r-   r.   stringK  s"    



zLexer.stringc                 C   s   |  d}|du r|  d}|du r.|  d}|du r:dS |d dkrXd}|dd }nd	}|d
d }dd }|stdd|}g }|| jD ]8}| }|sqtdd|}td||}|| q|S |S )z
        Lexes a triple quoted string, intended for use with monologue mode.
        This is about the same as the double-quoted strings, except that
        runs of whitespace with multiple newlines are turned into a single
        newline.
        zr?"""([^\\"]|\\.|"(?!""))*"""Nzr?'''([^\\']|\\.|'(?!''))*'''zr?```([^\\`]|\\.|`(?!``))*```r   r   Tr   Fru   c                 S   sr   |  d}|dkrdS |dkr"dS |dkr.dS |dkr:d	S |d
 dkrj|  d}|rntt|  ddS n|S d S r   r   r   r-   r-   r.   r     s    

z$Lexer.triple_string.<locals>.dequotez *\n *r   r   r5   r   )r|   r^   r_   r%   r   stripr}   )r=   r   r   r   r`   r-   r-   r.   triple_string  s2    


zLexer.triple_stringc                 C   s
   |  dS )zf
        Tries to parse an integer. Returns a string containing the
        integer, or None.
        z(\+|\-)?\d+r|   rH   r-   r-   r.   integer  s    zLexer.integerc                 C   s
   |  dS )zk
        Tries to parse a number (float). Returns a string containing the
        number, or None.
        z((\+|\-)?(\d+\.?\d*|\.\d+)([eE][-+]?\d+)?r   rH   r-   r-   r.   float  s    zLexer.floatc                 C   s
   |  dS )zG
        Matches the characters in an md5 hash, and then some.
        z\w+r   rH   r-   r-   r.   hash  s    z
Lexer.hashc                 C   s>   | j | jkr| j| _ | jS | j | _| t}|| _| j | _|S )z?
        Parses a name, which may be a keyword or not.
        )rA   r   r   r   r|   word_regexp)r=   r`   r-   r-   r.   r     s    
z
Lexer.wordc                 C   s`   | j }|  }|dks&|dks&|dkrJ| j| j | j d  dv rJ|| _ dS |tv r\|| _ dS |S )zG
        This tries to parse a name. Returns the name or None.
        r   r   urr   r4   r3   r2   N)rA   r   r   KEYWORDSr=   r   r`   r-   r-   r.   name  s    z
Lexer.namec                 C   s$   |r |d dkr | dd | _dS )z
        Set current global_label, which is used for label_name calculations.
        label can be any valid label or None, but this has only effect if label
        has global part.
        r   .N)r%   r   )r=   labelr-   r-   r.   set_global_label  s    zLexer.set_global_labelc                 C   s   | j }d}|  }|sN| dr&| js0|| _ dS | j}|  }|s|| _ dS n8| dr|rp|| jkrp|| _ dS |  }|s|| _ dS |s|S |d | S )a  
        Try to parse label name. Returns name in form of "global.local" if local
        is present, "global" otherwise; or None if it doesn't parse.

        If declare is True, allow only such names that are valid for declaration
        (e.g. forbid global name mismatch)
        N\.r   )rA   r   r|   r   )r=   declareold_posZ
local_nameZglobal_namer-   r-   r.   
label_name  s.    	
zLexer.label_namec                 C   s   | j ddS )z>
        Same as label_name, but set declare to True.
        T)r   )r   rH   r-   r-   r.   label_name_declare4  s    zLexer.label_name_declarec                 C   sZ   | j }| t}|dks |dkrD| j| j | j d  dv rD|| _ dS |tv rV|| _ dS |S )z
        Matches a word that is a component of an image name. (These are
        strings of numbers, letters, and underscores.)
        r   r   r   r   N)rA   r|   image_word_regexpr   r   r   r-   r-   r.   image_name_component:  s    
zLexer.image_name_componentc                 C   s   |   rdS | j}| j| j }dD ]D}||kr0q"|  jd7  _| jt| jkrZ|| _ dS | j| j }q"|dvrz|| _dS |}|  jd7  _|   r| d | j| j }||krq|dkr~|  jd7  _q~|  jd7  _dS )z
        This tries to match a python string at the current
        location. If it matches, it returns True, and the current
        position is updated to the end of the string. Otherwise,
        returns False.
        F)r   r   r+   r   )r4   r3   z)end of line reached while parsing string.r1   T)r   rA   r   r(   r   )r=   r   rE   modr   r-   r-   r.   python_stringN  s4    
zLexer.python_stringc                 C   sB   |   }|sdS | dr>|   }|s0| d |d| 7 }q|S )aQ  
        This tries to match a dotted name, which is one or more names,
        separated by dots. Returns the dotted name if it can, or None
        if it cannot.

        Once this sees the first name, it commits to parsing a
        dotted_name. It will report an error if it then sees a dot
        without a name behind it.
        Nr   zexpecting name.r   )r   r|   r   )r=   r`   r   r-   r-   r.   dotted_name  s    

zLexer.dotted_namec                 C   s   |s|S t j|| j| jS rG   )r   astPyExprr)   r>   )r=   r   exprr-   r-   r.   r    s    z
Lexer.exprTc                 C   sx   | j }|  sf| j| j  }||v r:| | j|| j  |S |dv rL|   q|  rVq|  j d7  _ q| d|  dS )a#  
        This matches python code up to, but not including, the non-whitespace
        delimiter characters. Returns a string containing the matched code,
        which may be empty if the first thing is the delimiter. Raises an
        error if EOL is reached before the delimiter.
        z'"r   z(reached end of line when expecting '%s'.N)rA   r   r   r  r   parenthesised_pythonr   )r=   r   r  r   rE   r-   r-   r.   delimited_python  s    zLexer.delimited_pythonc                 C   s.   |  dd}|s| d | | |}|S )zk
        Returns a python expression, which is arbitrary python code
        extending to a colon.
        :Fzexpected python_expression)r  r   r  r   )r=   r  Zper`   r-   r-   r.   python_expression  s
    
zLexer.python_expressionc                 C   s   | j | j }|dkr@|  jd7  _| dd |  jd7  _dS |dkrt|  jd7  _| dd |  jd7  _dS |dkr|  jd7  _| d	d |  jd7  _dS dS )
z
        Tries to match a parenthesised python expression. If it can,
        returns true and updates the current position to be after the
        closing parenthesis. Returns False otherwise.
        (r   )FTr   ]r   })r   rA   r  )r=   rE   r-   r-   r.   r    s"    zLexer.parenthesised_pythonc                 C   s   | j }| trq|  rq|  s>|  s>|  s>|  s>q|   |  rPq| drr| 	 }|s>| 
d q>|  rq>qq>|r| trq|r| drqqq| j|| j   }|sdS tj|| j| jS )zo
        Tries to parse a simple_expression. Returns the text if it can, or
        None if it cannot.
        r   zexpecting name after dot.,N)rA   r|   operator_regexpr   r   r   r   r  r   r   r   r   r   r   r  r  r)   r>   )r=   commaoperatorr   r   r   r-   r-   r.   simple_expression  sB    


zLexer.simple_expressionc                 C   s   | j ddS )zt
        One or more simple expressions, separated by commas, including an
        optional trailing comma.
        T)r  r  rH   r-   r-   r.   comma_expression$  s    zLexer.comma_expressionc                 C   s   | j ddS )z=
        Parses the name portion of a say statement.
        F)r  r  rH   r-   r-   r.   say_expression,  s    zLexer.say_expressionc                 C   s&   | j | j| j| j| j| jtjj	 fS )z
        Returns an opaque representation of the lexer state. This can be
        passed to revert to back the lexer up.
        )
r@   r)   r>   r   r   rA   r   r  r  
checkpointrH   r-   r-   r.   r  2  s    zLexer.checkpointc                 C   sT   |\| _ | _| _| _| _| _}tjj	| d| _
| j t| jk rJd| _nd| _dS )z
        Reverts the lexer to the given state. State must have been returned
        by a previous checkpoint operation on this lexer.
        r   FTN)r@   r)   r>   r   r   rA   r   r  r  revertr   r(   r   r   )r=   stateZpyexpr_checkpointr-   r-   r.   r  :  s    zLexer.revertc                 C   s   | j | jfS )z
        Returns a (filename, line number) tuple representing the current
        physical location of the start of the current logical line.
        )r)   r>   rH   r-   r-   r.   get_locationJ  s    zLexer.get_locationc                 C   sJ   t |tr|p|}| |}n|p(|jj}| }|du rF| d|  |S )z
        Tries to parse thing, and reports an error if it cannot be done.

        If thing is a string, tries to parse it using
        self.match(thing). Otherwise, thing must be a method on this lexer
        object, which is called directly.
        Nzexpected '%s' not found.)r8   r   r|   __func__rJ   r   )r=   thingr   r`   r-   r-   r.   requireR  s    	
zLexer.requirec                 C   s<   |    | j}t| j| _tj| j|d  | j| j	S )z
        Skips whitespace, then returns the rest of the current
        line, and advances the current position to the end of
        the current line.
        N)
r   rA   r(   r   r   r  r  r   r)   r>   r=   rA   r-   r-   r.   r   g  s    z
Lexer.restc                 C   s$   | j }t| j| _ | j|d  S )zG
        Like rest, but returns a string rather than a PyExpr.
        N)rA   r(   r   r   r  r-   r-   r.   rest_statementt  s    zLexer.rest_statementc                    s8   g t   | j _ fdd| jd dS )z
        Returns the subblock of this code, and subblocks of that
        subblock, as indented python code. This tries to insert
        whitespace to ensure line numbers match up.
        c                    sv   | D ]l\}}}} j |k r8|d    j d7  _ q|| d }|   j |d7  _ ||d  qd S )Nr   r       )r@   r}   count)r   indentZ_fnZlnr   r   Zlinetextoprocessr`   r-   r.   r#    s    

z#Lexer.python_block.<locals>.processr   )rM   r>   r@   r   r:   rH   r-   r!  r.   python_block}  s    zLexer.python_blockc                 C   s   t j| S )zo
        Returns an Argument object if there is a list of arguments, or None
        there is not one.
        )r   r   Zparse_argumentsrH   r-   r-   r.   	arguments  s    zLexer.argumentsc                 C   sN   | j du rtdtj| }|   t|ts6|g}t|}| j 	| |S )z
        Parses the remainder of the current line as a statement in the
        Ren'Py script language. Returns a SubParse corresponding to the
        AST node generated by that statement.
        NzHA renpy_statement can only be parsed inside a creator-defined statement.)
r   r'   r   r   parse_statementr   r8   r9   r   r}   )r=   r   spr-   r-   r.   renpy_statement  s    

zLexer.renpy_statementc              
   C   s   | j d u rtd| jdk r$|   g }| jsz0tj| }t|t	rR|
| n
|| W q( ty } z$tjj|j |   W Y d }~q(d }~0 0 q(|s|r|tj|   n
| d t|}| j | |S )NzDA renpy_block can only be parsed inside a creator-defined statement.r   z*At least one Ren'Py statement is expected.)r   r'   r@   r   r   r   r   r&  r8   r9   extendr}   r0   r   r;   r  Passr  r   r   )r=   emptyr   r   r   r'  r-   r-   r.   renpy_block  s*    


 
zLexer.renpy_block)Fr   Nr   N)F)F)T)T)FT)N)F)3rJ   rK   rL   rO   r<   r   r   r   r   r|   r   
contextlibcontextmanagerr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r  r  r  r  r  r  r  r  r  r  r  r  r   r  r$  r%  r(  r,  r-   r-   r-   r.   r   ~  s^   
	


	

9E	
(2


=
	r   c                 C   sf  |   }g }d}d}d}d}|}t|ddD ]\}	}
||krZ|
drZ|}|d |	}q,||kr|
dkrz|}|d q,|
 }|s||
 q,|d dkr||
 q,d}|
D ]}|d	kr q|d	7 }q|d
 dkr|d7 }||
 q,||kr|||
  q,||kr,|d q,q,|durX||kr>td|||krXtd||d|}|S )a  
    Transforms an _ren.py file into the equivalent .rpy file. This should retain line numbers.

    `filename`
        If not None, and an error occurs, the error is reported with the given filename.
        Otherwise, errors are ignored and a a best effort is used.
    r   r   r   rn   )r   z"""renpyz"""rt   r5   r   r  r  Nz@In {!r}, there are no """renpy blocks, so every line is ignored.zLIn {!r}, there is a """renpy block at line {} that is not terminated by """.r   )
splitlines	enumeraterg   r}   r   r'   r   r:   )r   r)   rC   resultrp   ZIGNOREZRENPYZPYTHONr  r   rF   Zopen_linenumberslrD   r`   r-   r-   r.   r$     sd    	












r$   )Nr   F)7
__future__r   r   r   r   r   renpy.compatr   r   r	   r
   r   r   r   r   r   r   r   r   codecsr^   rY   timer-  r   Zrenpy.lexersupportr   r    r&   r/   r'   r0   objectrM   r7   r   SZlllwordra   rj   r   rv   r   r   setr   Z	OPERATORSZESCAPED_OPERATORSr:   r  r   r   r   r   r$   r-   r-   r-   r.   <module>   sJ   8&7	
 qN      d