
V}Oc           @   sl  d  Z  d d l Z d d l m Z d d l m Z m Z d d l m Z m Z d e	 f d     YZ
 d e
 f d	     YZ d
 e
 f d     YZ d e f d     YZ d e f d     YZ d e f d     YZ e Z e Z e Z d e f d     YZ d e	 f d     YZ d e f d     YZ d e f d     YZ d e f d     YZ d e f d     YZ d S(   s   ANTLR3 runtime packageiN(   t   StringIO(   t   DEFAULT_CHANNELt   EOF(   t   Tokent	   EOF_TOKENt	   IntStreamc           B   se   e  Z d  Z d   Z d   Z d   Z d   Z d
 d  Z d
 d  Z	 d   Z
 d   Z d	   Z RS(   s   
    @brief Base interface for streams of integer values.

    A simple stream of integers used when all I care about is the char
    or token type sequence (such as interpretation).
    c         C   s
   t   d  S(   N(   t   NotImplementedError(   t   self(    (    sW   /afs/athena.mit.edu/user/x/a/xavid/.local/lib/python2.7/site-packages/antlr3/streams.pyt   consume;   s    c         C   s
   t   d S(   s   Get int at current input pointer + i ahead where i=1 is next int.

        Negative indexes are allowed.  LA(-1) is previous token (token
	just matched).  LA(-i) where i is before first token should
	yield -1, invalid char / EOF.
	N(   R   (   R   t   i(    (    sW   /afs/athena.mit.edu/user/x/a/xavid/.local/lib/python2.7/site-packages/antlr3/streams.pyt   LA?   s    c         C   s
   t   d S(   s  
        Tell the stream to start buffering if it hasn't already.  Return
        current input position, index(), or some other marker so that
        when passed to rewind() you get back to the same spot.
        rewind(mark()) should not affect the input cursor.  The Lexer
        track line/col info as well as input index so its markers are
        not pure input indexes.  Same for tree node streams.
        N(   R   (   R   (    (    sW   /afs/athena.mit.edu/user/x/a/xavid/.local/lib/python2.7/site-packages/antlr3/streams.pyt   markJ   s    
c         C   s
   t   d S(   s   
        Return the current input symbol index 0..n where n indicates the
        last symbol has been read.  The index is the symbol about to be
        read not the most recently read symbol.
        N(   R   (   R   (    (    sW   /afs/athena.mit.edu/user/x/a/xavid/.local/lib/python2.7/site-packages/antlr3/streams.pyt   indexW   s    c         C   s
   t   d S(   s  
        Reset the stream so that next call to index would return marker.
        The marker will usually be index() but it doesn't have to be.  It's
        just a marker to indicate what state the stream was in.  This is
        essentially calling release() and seek().  If there are markers
        created after this marker argument, this routine must unroll them
        like a stack.  Assume the state the stream was in when this marker
        was created.

        If marker is None:
        Rewind to the input position of the last marker.
        Used currently only after a cyclic DFA and just
        before starting a sem/syn predicate to get the
        input position back to the start of the decision.
        Do not "pop" the marker off the state.  mark(i)
        and rewind(i) should balance still. It is
        like invoking rewind(last marker) but it should not "pop"
        the marker off.  It's like seek(last marker's input position).       
	N(   R   (   R   t   marker(    (    sW   /afs/athena.mit.edu/user/x/a/xavid/.local/lib/python2.7/site-packages/antlr3/streams.pyt   rewinda   s    c         C   s
   t   d S(   s  
        You may want to commit to a backtrack but don't want to force the
        stream to keep bookkeeping objects around for a marker that is
        no longer necessary.  This will have the same behavior as
        rewind() except it releases resources without the backward seek.
        This must throw away resources for all markers back to the marker
        argument.  So if you're nested 5 levels of mark(), and then release(2)
        you have to release resources for depths 2..5.
	N(   R   (   R   R   (    (    sW   /afs/athena.mit.edu/user/x/a/xavid/.local/lib/python2.7/site-packages/antlr3/streams.pyt   releasey   s    c         C   s
   t   d S(   s:  
        Set the input cursor to the position indicated by index.  This is
        normally used to seek ahead in the input stream.  No buffering is
        required to do this unless you know your stream will use seek to
        move backwards such as when backtracking.

        This is different from rewind in its multi-directional
        requirement and in that its argument is strictly an input cursor
        (index).

        For char streams, seeking forward must update the stream state such
        as line number.  For seeking backwards, you will be presumably
        backtracking using the mark/rewind mechanism that restores state and
        so this method does not need to update state when seeking backwards.

        Currently, this method is only used for efficient backtracking using
        memoization, but in the future it may be used for incremental parsing.

        The index is 0..n-1.  A seek to position i means that LA(1) will
        return the ith symbol.  So, seeking to 0 means LA(1) will return the
        first element in the stream. 
        N(   R   (   R   R   (    (    sW   /afs/athena.mit.edu/user/x/a/xavid/.local/lib/python2.7/site-packages/antlr3/streams.pyt   seek   s    c         C   s
   t   d S(   s   
        Only makes sense for streams that buffer everything up probably, but
        might be useful to display the entire stream or for testing.  This
        value includes a single EOF.
	N(   R   (   R   (    (    sW   /afs/athena.mit.edu/user/x/a/xavid/.local/lib/python2.7/site-packages/antlr3/streams.pyt   size   s    c         C   s
   t   d S(   s   
        Where are you getting symbols from?  Normally, implementations will
        pass the buck all the way to the lexer who can ask its input stream
        for the file name or whatever.
        N(   R   (   R   (    (    sW   /afs/athena.mit.edu/user/x/a/xavid/.local/lib/python2.7/site-packages/antlr3/streams.pyt   getSourceName   s    N(   t   __name__t
   __module__t   __doc__R   R
   R   R   t   NoneR   R   R   R   R   (    (    (    sW   /afs/athena.mit.edu/user/x/a/xavid/.local/lib/python2.7/site-packages/antlr3/streams.pyR   3   s   				
		
t
   CharStreamc           B   sJ   e  Z d  Z d Z d   Z d   Z d   Z d   Z d   Z d   Z	 RS(   s   
    @brief A source of characters for an ANTLR lexer.

    This is an abstract class that must be implemented by a subclass.
    
    ic         C   s
   t   d S(   s   
        For infinite streams, you don't need this; primarily I'm providing
        a useful interface for action code.  Just make sure actions don't
        use this on streams that don't support it.
        N(   R   (   R   t   startt   stop(    (    sW   /afs/athena.mit.edu/user/x/a/xavid/.local/lib/python2.7/site-packages/antlr3/streams.pyt	   substring   s    c         C   s
   t   d S(   s   
        Get the ith character of lookahead.  This is the same usually as
        LA(i).  This will be used for labels in the generated
        lexer code.  I'd prefer to return a char here type-wise, but it's
        probably better to be 32-bit clean and be consistent with LA.
        N(   R   (   R   R	   (    (    sW   /afs/athena.mit.edu/user/x/a/xavid/.local/lib/python2.7/site-packages/antlr3/streams.pyt   LT   s    c         C   s
   t   d S(   s/   ANTLR tracks the line information automaticallyN(   R   (   R   (    (    sW   /afs/athena.mit.edu/user/x/a/xavid/.local/lib/python2.7/site-packages/antlr3/streams.pyt   getLine   s    c         C   s
   t   d S(   sV   
        Because this stream can rewind, we need to be able to reset the line
        N(   R   (   R   t   line(    (    sW   /afs/athena.mit.edu/user/x/a/xavid/.local/lib/python2.7/site-packages/antlr3/streams.pyt   setLine   s    c         C   s
   t   d S(   sY   
        The index of the character relative to the beginning of the line 0..n-1
        N(   R   (   R   (    (    sW   /afs/athena.mit.edu/user/x/a/xavid/.local/lib/python2.7/site-packages/antlr3/streams.pyt   getCharPositionInLine   s    c         C   s
   t   d  S(   N(   R   (   R   t   pos(    (    sW   /afs/athena.mit.edu/user/x/a/xavid/.local/lib/python2.7/site-packages/antlr3/streams.pyt   setCharPositionInLine   s    (
   R   R   R   R   R   R   R   R   R   R!   (    (    (    sW   /afs/athena.mit.edu/user/x/a/xavid/.local/lib/python2.7/site-packages/antlr3/streams.pyR      s   	
				t   TokenStreamc           B   s8   e  Z d  Z d   Z d   Z d   Z d d d  Z RS(   s   

    @brief A stream of tokens accessing tokens from a TokenSource

    This is an abstract class that must be implemented by a subclass.
    
    c         C   s
   t   d S(   sU  
        Get Token at current input pointer + i ahead where i=1 is next Token.
        i<0 indicates tokens in the past.  So -1 is previous token and -2 is
        two tokens ago. LT(0) is undefined.  For i>=n, return Token.EOFToken.
        Return null for LT(0) and any index that results in an absolute address
        that is negative.
	N(   R   (   R   t   k(    (    sW   /afs/athena.mit.edu/user/x/a/xavid/.local/lib/python2.7/site-packages/antlr3/streams.pyR      s    	c         C   s
   t   d S(   s  
        Get a token at an absolute index i; 0..n-1.  This is really only
        needed for profiling and debugging and token stream rewriting.
        If you don't want to buffer up tokens, then this method makes no
        sense for you.  Naturally you can't use the rewrite stream feature.
        I believe DebugTokenStream can easily be altered to not use
        this method, removing the dependency.
        N(   R   (   R   R	   (    (    sW   /afs/athena.mit.edu/user/x/a/xavid/.local/lib/python2.7/site-packages/antlr3/streams.pyt   get  s    
c         C   s
   t   d S(   s   
        Where is this stream pulling tokens from?  This is not the name, but
        the object that provides Token objects.
	N(   R   (   R   (    (    sW   /afs/athena.mit.edu/user/x/a/xavid/.local/lib/python2.7/site-packages/antlr3/streams.pyt   getTokenSource  s    c         C   s
   t   d S(   s\  
        Return the text of all tokens from start to stop, inclusive.
        If the stream does not buffer all the tokens then it can just
        return "" or null;  Users should not access $ruleLabel.text in
        an action of course in that case.

        Because the user is not required to use a token with an index stored
        in it, we must provide a means for two token objects themselves to
        indicate the start/end location.  Most often this will just delegate
        to the other toString(int,int).  This is also parallel with
        the TreeNodeStream.toString(Object,Object).
	N(   R   (   R   R   R   (    (    sW   /afs/athena.mit.edu/user/x/a/xavid/.local/lib/python2.7/site-packages/antlr3/streams.pyt   toString!  s    N(   R   R   R   R   R$   R%   R   R&   (    (    (    sW   /afs/athena.mit.edu/user/x/a/xavid/.local/lib/python2.7/site-packages/antlr3/streams.pyR"      s
   				t   ANTLRStringStreamc           B   s   e  Z d  Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z	 d   Z
 d d	  Z d d
  Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z RS(   s   
    @brief CharStream that pull data from a unicode string.
    
    A pretty quick CharStream that pulls all data from an array
    directly.  Every method call counts in the lexer.

    c         C   s   t  j |   t |  |  _ g  |  j D] } t |  ^ q& |  _ t |  |  _ d |  _ d |  _	 d |  _
 g  |  _ d |  _ d |  _ d |  _ d S(   s   
        @param data This should be a unicode string holding the data you want
           to parse. If you pass in a byte string, the Lexer will choke on
           non-ascii data.
           
        i    i   N(   R   t   __init__t   unicodet   strdatat   ordt   datat   lent   nt   pR   t   charPositionInLinet   _markersR   t
   lastMarkert	   markDeptht   name(   R   R,   t   c(    (    sW   /afs/athena.mit.edu/user/x/a/xavid/.local/lib/python2.7/site-packages/antlr3/streams.pyR(   E  s    %						c         C   s(   d |  _  d |  _ d |  _ g  |  _ d S(   s   
        Reset the stream so that it's in the same state it was
        when the object was created *except* the data array is not
        touched.
        i    i   N(   R/   R   R0   R1   (   R   (    (    sW   /afs/athena.mit.edu/user/x/a/xavid/.local/lib/python2.7/site-packages/antlr3/streams.pyt   resetk  s    			c         C   sk   yS |  j  |  j d k r4 |  j d 7_ d |  _ n |  j d 7_ |  j d 7_ Wn t k
 rf n Xd  S(   Ni
   i   i    (   R,   R/   R   R0   t
   IndexError(   R   (    (    sW   /afs/athena.mit.edu/user/x/a/xavid/.local/lib/python2.7/site-packages/antlr3/streams.pyR   x  s    c         C   s\   | d k r d S| d k  r) | d 7} n  y |  j  |  j | d SWn t k
 rW t SXd  S(   Ni    i   (   R,   R/   R7   R   (   R   R	   (    (    sW   /afs/athena.mit.edu/user/x/a/xavid/.local/lib/python2.7/site-packages/antlr3/streams.pyR
     s    c         C   s\   | d k r d S| d k  r) | d 7} n  y |  j  |  j | d SWn t k
 rW t SXd  S(   Ni    i   (   R*   R/   R7   R   (   R   R	   (    (    sW   /afs/athena.mit.edu/user/x/a/xavid/.local/lib/python2.7/site-packages/antlr3/streams.pyR     s    c         C   s   |  j  S(   s   
        Return the current input symbol index 0..n where n indicates the
        last symbol has been read.  The index is the index of char to
        be returned from LA(1).
        (   R/   (   R   (    (    sW   /afs/athena.mit.edu/user/x/a/xavid/.local/lib/python2.7/site-packages/antlr3/streams.pyR     s    c         C   s   |  j  S(   N(   R.   (   R   (    (    sW   /afs/athena.mit.edu/user/x/a/xavid/.local/lib/python2.7/site-packages/antlr3/streams.pyR     s    c         C   sr   |  j  |  j |  j f } y | |  j |  j <Wn! t k
 rO |  j j |  n X|  j d 7_ |  j |  _ |  j S(   Ni   (   R/   R   R0   R1   R3   R7   t   appendR2   (   R   t   state(    (    sW   /afs/athena.mit.edu/user/x/a/xavid/.local/lib/python2.7/site-packages/antlr3/streams.pyR     s    c         C   sb   | d  k r |  j } n  |  j | d \ } } } |  j |  | |  _ | |  _ |  j |  d  S(   Ni   (   R   R2   R1   R   R   R0   R   (   R   R   R/   R   R0   (    (    sW   /afs/athena.mit.edu/user/x/a/xavid/.local/lib/python2.7/site-packages/antlr3/streams.pyR     s    		c         C   s)   | d  k r |  j } n  | d |  _ d  S(   Ni   (   R   R2   R3   (   R   R   (    (    sW   /afs/athena.mit.edu/user/x/a/xavid/.local/lib/python2.7/site-packages/antlr3/streams.pyR     s    c         C   s@   | |  j  k r | |  _  d Sx |  j  | k  r; |  j   q Wd S(   s   
        consume() ahead until p==index; can't just set p=index as we must
        update line and charPositionInLine.
        N(   R/   R   (   R   R   (    (    sW   /afs/athena.mit.edu/user/x/a/xavid/.local/lib/python2.7/site-packages/antlr3/streams.pyR     s
    	c         C   s   |  j  | | d !S(   Ni   (   R*   (   R   R   R   (    (    sW   /afs/athena.mit.edu/user/x/a/xavid/.local/lib/python2.7/site-packages/antlr3/streams.pyR     s    c         C   s   |  j  S(   s>   Using setter/getter methods is deprecated. Use o.line instead.(   R   (   R   (    (    sW   /afs/athena.mit.edu/user/x/a/xavid/.local/lib/python2.7/site-packages/antlr3/streams.pyR     s    c         C   s   |  j  S(   sf   
        Using setter/getter methods is deprecated. Use o.charPositionInLine
        instead.
        (   R0   (   R   (    (    sW   /afs/athena.mit.edu/user/x/a/xavid/.local/lib/python2.7/site-packages/antlr3/streams.pyR     s    c         C   s   | |  _  d S(   s>   Using setter/getter methods is deprecated. Use o.line instead.N(   R   (   R   R   (    (    sW   /afs/athena.mit.edu/user/x/a/xavid/.local/lib/python2.7/site-packages/antlr3/streams.pyR     s    c         C   s   | |  _  d S(   sf   
        Using setter/getter methods is deprecated. Use o.charPositionInLine
        instead.
        N(   R0   (   R   R    (    (    sW   /afs/athena.mit.edu/user/x/a/xavid/.local/lib/python2.7/site-packages/antlr3/streams.pyR!     s    c         C   s   |  j  S(   N(   R4   (   R   (    (    sW   /afs/athena.mit.edu/user/x/a/xavid/.local/lib/python2.7/site-packages/antlr3/streams.pyR     s    N(   R   R   R   R(   R6   R   R
   R   R   R   R   R   R   R   R   R   R   R   R   R!   R   (    (    (    sW   /afs/athena.mit.edu/user/x/a/xavid/.local/lib/python2.7/site-packages/antlr3/streams.pyR'   ;  s$   	&					
								t   ANTLRFileStreamc           B   s#   e  Z d  Z d d  Z d   Z RS(   s   
    @brief CharStream that opens a file to read the data.
    
    This is a char buffer stream that is loaded from a file
    all at once when you construct the object.
    c         C   sP   | |  _  t j | d |  } z | j   } Wd | j   Xt j |  |  d S(   s   
        @param fileName The path to the file to be opened. The file will be
           opened with mode 'rb'.

        @param encoding If you set the optional encoding argument, then the
           data will be decoded on the fly.
           
        t   rbN(   t   fileNamet   codecst   opent   readt   closeR'   R(   (   R   R<   t   encodingt   fpR,   (    (    sW   /afs/athena.mit.edu/user/x/a/xavid/.local/lib/python2.7/site-packages/antlr3/streams.pyR(     s    
	c         C   s   |  j  S(   s'   Deprecated, access o.fileName directly.(   R<   (   R   (    (    sW   /afs/athena.mit.edu/user/x/a/xavid/.local/lib/python2.7/site-packages/antlr3/streams.pyR      s    N(   R   R   R   R   R(   R   (    (    (    sW   /afs/athena.mit.edu/user/x/a/xavid/.local/lib/python2.7/site-packages/antlr3/streams.pyR:     s   t   ANTLRInputStreamc           B   s   e  Z d  Z d d  Z RS(   s   
    @brief CharStream that reads data from a file-like object.

    This is a char buffer stream that is loaded from a file like object
    all at once when you construct the object.
    
    All input is consumed from the file, but it is not closed.
    c         C   sN   | d k	 r. t j |  d } | |  } n  | j   } t j |  |  d S(   s   
        @param file A file-like object holding your input. Only the read()
           method must be implemented.

        @param encoding If you set the optional encoding argument, then the
           data will be decoded on the fly.
           
        i   N(   R   R=   t   lookupR?   R'   R(   (   R   t   fileRA   t   readerR,   (    (    sW   /afs/athena.mit.edu/user/x/a/xavid/.local/lib/python2.7/site-packages/antlr3/streams.pyR(   0  s
    
N(   R   R   R   R   R(   (    (    (    sW   /afs/athena.mit.edu/user/x/a/xavid/.local/lib/python2.7/site-packages/antlr3/streams.pyRC   &  s   t   CommonTokenStreamc           B   s   e  Z d  Z d e d  Z d   Z d   Z d   Z d   Z	 d   Z
 d   Z d   Z d	   Z d d d d
  Z d   Z d   Z d   Z d   Z d   Z d d  Z d   Z d   Z d d  Z d   Z d   Z d   Z d d d  Z RS(   s&  
    @brief The most common stream of tokens
    
    The most common stream of tokens is one where every token is buffered up
    and tokens are prefiltered for a certain channel (the parser will only
    see these tokens and cannot change the filter channel number during the
    parse).
    c         C   s\   t  j |   | |  _ g  |  _ i  |  _ t   |  _ | |  _ t |  _	 d |  _
 d |  _ d S(   s   
        @param tokenSource A TokenSource instance (usually a Lexer) to pull
            the tokens from.

        @param channel Skip tokens on any channel but this one; this is how we
            skip whitespace...
            
        iN(   R"   R(   t   tokenSourcet   tokenst   channelOverrideMapt   sett
   discardSett   channelt   Falset   discardOffChannelTokensR/   R   R2   (   R   RH   RM   (    (    sW   /afs/athena.mit.edu/user/x/a/xavid/.local/lib/python2.7/site-packages/antlr3/streams.pyR(   `  s    
						c         C   s(   | |  _  g  |  _ d |  _ t |  _ d S(   s4   Reset this token stream by setting its token source.iN(   RH   RI   R/   R   RM   (   R   RH   (    (    sW   /afs/athena.mit.edu/user/x/a/xavid/.local/lib/python2.7/site-packages/antlr3/streams.pyt   setTokenSource  s    			c         C   s   d |  _  d  |  _ d  S(   Ni    (   R/   R   R2   (   R   (    (    sW   /afs/athena.mit.edu/user/x/a/xavid/.local/lib/python2.7/site-packages/antlr3/streams.pyR6     s    	c         C   s1  d } |  j  j   } x | d k	 r| j t k rt } |  j d k	 rc | j |  j k rc t } n$ |  j r | j	 |  j	 k r t } n  y |  j
 | j } Wn t k
 r n" X| |  j	 k r | | _	 n t } | s | | _ |  j j |  | d 7} n  |  j  j   } q Wd |  _ |  j |  j  |  _ d S(   s   
        Load all tokens from the token source and put in tokens.
	This is done upon first LT request because you might want to
        set some token type / channel overrides before filling buffer.
        i    i   N(   RH   t	   nextTokenR   t   typeR   RN   RL   t   TrueRO   RM   RJ   t   KeyErrorR   RI   R8   R/   t   skipOffTokenChannels(   R   R   t   tt   discardt   overrideChannel(    (    sW   /afs/athena.mit.edu/user/x/a/xavid/.local/lib/python2.7/site-packages/antlr3/streams.pyt
   fillBuffer  s,    !				c         C   sC   |  j  t |  j  k  r? |  j  d 7_  |  j |  j   |  _  n  d S(   sR  
        Move the input pointer to the next incoming token.  The stream
        must become active with LT(1) available.  consume() simply
        moves the input pointer so that LT(1) points at the next
        input symbol. Consume at least one token.

        Walk past any token not on the channel the parser is listening to.
        i   N(   R/   R-   RI   RU   (   R   (    (    sW   /afs/athena.mit.edu/user/x/a/xavid/.local/lib/python2.7/site-packages/antlr3/streams.pyR     s    
c         C   sF   y. x' |  j  | j |  j k r, | d 7} q WWn t k
 rA n X| S(   sa   
        Given a starting index, return the index of the first on-channel
        token.
        i   (   RI   RM   R7   (   R   R	   (    (    sW   /afs/athena.mit.edu/user/x/a/xavid/.local/lib/python2.7/site-packages/antlr3/streams.pyRU     s    c         C   s:   x3 | d k r5 |  j  | j |  j k r5 | d 8} q W| S(   Ni    i   (   RI   RM   (   R   R	   (    (    sW   /afs/athena.mit.edu/user/x/a/xavid/.local/lib/python2.7/site-packages/antlr3/streams.pyt   skipOffTokenChannelsReverse  s    (c         C   s   | |  j  | <d S(   s5  
        A simple filter mechanism whereby you can tell this token stream
        to force all tokens of type ttype to be on channel.  For example,
        when interpreting, we cannot exec actions so we need to tell
        the stream to force all WS and NEWLINE to be a different, ignored
        channel.
	N(   RJ   (   R   t   ttypeRM   (    (    sW   /afs/athena.mit.edu/user/x/a/xavid/.local/lib/python2.7/site-packages/antlr3/streams.pyt   setTokenTypeChannel  s    	c         C   s   |  j  j |  d  S(   N(   RL   t   add(   R   R[   (    (    sW   /afs/athena.mit.edu/user/x/a/xavid/.local/lib/python2.7/site-packages/antlr3/streams.pyt   discardTokenType  s    c         C   s  |  j  d k r |  j   n  | d k s= | t |  j  k rS t |  j  d } n  | d k sk | d k  rt d } n  | | k r d St | t t f  r t | g  } n  g  |  j | | !D]' } | d k s | j	 | k r | ^ q } t |  d k r d S| S(   s   
        Given a start and stop index, return a list of all tokens in
        the token type set.  Return None if no tokens were found.  This
        method looks at both on and off channel tokens.
        ii   i    N(
   R/   RY   R   R-   RI   t
   isinstancet   intt   longRK   RR   (   R   R   R   t   typest   tokent   filteredTokens(    (    sW   /afs/athena.mit.edu/user/x/a/xavid/.local/lib/python2.7/site-packages/antlr3/streams.pyt	   getTokens  s    !	'c         C   s   |  j  d k r |  j   n  | d k r, d S| d k  rF |  j |  S|  j  } d } x- | | k  r |  j | d  } | d 7} qX Wy |  j | SWn t k
 r t SXd S(   sv   
        Get the ith token from the current position 1..n where k=1 is the
        first symbol of lookahead.
        ii    i   N(   R/   RY   R   t   LBRU   RI   R7   R   (   R   R#   R	   R.   (    (    sW   /afs/athena.mit.edu/user/x/a/xavid/.local/lib/python2.7/site-packages/antlr3/streams.pyR     s    	c         C   s   |  j  d k r |  j   n  | d k r, d S|  j  | d k  rC d S|  j  } d } x- | | k r |  j | d  } | d 7} qU W| d k  r d S|  j | S(   s)   Look backwards k tokens on-channel tokensii    i   N(   R/   RY   R   RZ   RI   (   R   R#   R	   R.   (    (    sW   /afs/athena.mit.edu/user/x/a/xavid/.local/lib/python2.7/site-packages/antlr3/streams.pyRf   7  s    	c         C   s   |  j  | S(   s   
        Return absolute token i; ignore which channel the tokens are on;
        that is, count all tokens not just on-channel tokens.
        (   RI   (   R   R	   (    (    sW   /afs/athena.mit.edu/user/x/a/xavid/.local/lib/python2.7/site-packages/antlr3/streams.pyR$   Q  s    c         C   s   |  j  |  j S(   N(   R   RR   (   R   R	   (    (    sW   /afs/athena.mit.edu/user/x/a/xavid/.local/lib/python2.7/site-packages/antlr3/streams.pyR
   Z  s    c         C   s   |  j    |  _ |  j S(   N(   R   R2   (   R   (    (    sW   /afs/athena.mit.edu/user/x/a/xavid/.local/lib/python2.7/site-packages/antlr3/streams.pyR   ^  s    c         C   s   d  S(   N(    (   R   R   (    (    sW   /afs/athena.mit.edu/user/x/a/xavid/.local/lib/python2.7/site-packages/antlr3/streams.pyR   c  s    c         C   s   t  |  j  S(   N(   R-   RI   (   R   (    (    sW   /afs/athena.mit.edu/user/x/a/xavid/.local/lib/python2.7/site-packages/antlr3/streams.pyR   h  s    c         C   s   |  j  S(   N(   R/   (   R   (    (    sW   /afs/athena.mit.edu/user/x/a/xavid/.local/lib/python2.7/site-packages/antlr3/streams.pyR   l  s    c         C   s)   | d  k r |  j } n  |  j |  d  S(   N(   R   R2   R   (   R   R   (    (    sW   /afs/athena.mit.edu/user/x/a/xavid/.local/lib/python2.7/site-packages/antlr3/streams.pyR   p  s    c         C   s   | |  _  d  S(   N(   R/   (   R   R   (    (    sW   /afs/athena.mit.edu/user/x/a/xavid/.local/lib/python2.7/site-packages/antlr3/streams.pyR   w  s    c         C   s   |  j  S(   N(   RH   (   R   (    (    sW   /afs/athena.mit.edu/user/x/a/xavid/.local/lib/python2.7/site-packages/antlr3/streams.pyR%   {  s    c         C   s   |  j  j   S(   N(   RH   R   (   R   (    (    sW   /afs/athena.mit.edu/user/x/a/xavid/.local/lib/python2.7/site-packages/antlr3/streams.pyR     s    c         C   s   |  j  d k r |  j   n  | d  k r1 d } n t | t  sL | j } n  | d  k rn t |  j  d } n t | t  s | j } n  | t |  j  k r t |  j  d } n  d j g  |  j | | d !D] } | j	 ^ q  S(   Nii    i   t    (
   R/   RY   R   R_   R`   R   R-   RI   t   joint   text(   R   R   R   RV   (    (    sW   /afs/athena.mit.edu/user/x/a/xavid/.local/lib/python2.7/site-packages/antlr3/streams.pyR&     s    	N(   R   R   R   R   R   R(   RP   R6   RY   R   RU   RZ   R\   R^   Re   R   Rf   R$   R
   R   R   R   R   R   R   R%   R   R&   (    (    (    sW   /afs/athena.mit.edu/user/x/a/xavid/.local/lib/python2.7/site-packages/antlr3/streams.pyRG   V  s0   &				-					"											t   RewriteOperationc           B   s5   e  Z d  Z d   Z d   Z d   Z e Z e Z RS(   s   @brief Internal helper class.c         C   s   | |  _  | |  _ | |  _ d  S(   N(   t   streamR   Ri   (   R   Rk   R   Ri   (    (    sW   /afs/athena.mit.edu/user/x/a/xavid/.local/lib/python2.7/site-packages/antlr3/streams.pyR(     s    		c         C   s   |  j  S(   s   Execute the rewrite operation by possibly adding to the buffer.
        Return the index of the next token to operate on.
        (   R   (   R   t   buf(    (    sW   /afs/athena.mit.edu/user/x/a/xavid/.local/lib/python2.7/site-packages/antlr3/streams.pyt   execute  s    c         C   s#   |  j  j } d | |  j |  j f S(   Ns   <%s@%d:"%s">(   t	   __class__R   R   Ri   (   R   t   opName(    (    sW   /afs/athena.mit.edu/user/x/a/xavid/.local/lib/python2.7/site-packages/antlr3/streams.pyR&     s    (   R   R   R   R(   Rm   R&   t   __str__t   __repr__(    (    (    sW   /afs/athena.mit.edu/user/x/a/xavid/.local/lib/python2.7/site-packages/antlr3/streams.pyRj     s   			t   InsertBeforeOpc           B   s   e  Z d  Z d   Z RS(   s   @brief Internal helper class.c         C   s8   | j  |  j  | j  |  j j |  j j  |  j d S(   Ni   (   t   writeRi   Rk   RI   R   (   R   Rl   (    (    sW   /afs/athena.mit.edu/user/x/a/xavid/.local/lib/python2.7/site-packages/antlr3/streams.pyRm     s    (   R   R   R   Rm   (    (    (    sW   /afs/athena.mit.edu/user/x/a/xavid/.local/lib/python2.7/site-packages/antlr3/streams.pyRr     s   t	   ReplaceOpc           B   s5   e  Z d  Z d   Z d   Z d   Z e Z e Z RS(   s   
    @brief Internal helper class.
    
    I'm going to try replacing range from x..y with (y-x)+1 ReplaceOp
    instructions.
    c         C   s#   t  j |  | | |  | |  _ d  S(   N(   Rj   R(   t	   lastIndex(   R   Rk   t   firstt   lastRi   (    (    sW   /afs/athena.mit.edu/user/x/a/xavid/.local/lib/python2.7/site-packages/antlr3/streams.pyR(     s    c         C   s-   |  j  d  k	 r" | j |  j   n  |  j d S(   Ni   (   Ri   R   Rs   Ru   (   R   Rl   (    (    sW   /afs/athena.mit.edu/user/x/a/xavid/.local/lib/python2.7/site-packages/antlr3/streams.pyRm     s    c         C   s   d |  j  |  j |  j f S(   Ns   <ReplaceOp@%d..%d:"%s">(   R   Ru   Ri   (   R   (    (    sW   /afs/athena.mit.edu/user/x/a/xavid/.local/lib/python2.7/site-packages/antlr3/streams.pyR&     s    (   R   R   R   R(   Rm   R&   Rp   Rq   (    (    (    sW   /afs/athena.mit.edu/user/x/a/xavid/.local/lib/python2.7/site-packages/antlr3/streams.pyRt     s   			t   DeleteOpc           B   s,   e  Z d  Z d   Z d   Z e Z e Z RS(   s'   
    @brief Internal helper class.
    c         C   s   t  j |  | | | d   d  S(   N(   Rt   R(   R   (   R   Rk   Rv   Rw   (    (    sW   /afs/athena.mit.edu/user/x/a/xavid/.local/lib/python2.7/site-packages/antlr3/streams.pyR(     s    c         C   s   d |  j  |  j f S(   Ns   <DeleteOp@%d..%d>(   R   Ru   (   R   (    (    sW   /afs/athena.mit.edu/user/x/a/xavid/.local/lib/python2.7/site-packages/antlr3/streams.pyR&     s    (   R   R   R   R(   R&   Rp   Rq   (    (    (    sW   /afs/athena.mit.edu/user/x/a/xavid/.local/lib/python2.7/site-packages/antlr3/streams.pyRx     s
   		t   TokenRewriteStreamc           B   s   e  Z d  Z d Z d Z d e d  Z d   Z e d  Z	 d   Z
 d   Z d   Z d	   Z e d
  Z d   Z d   Z d   Z d d d  Z d   Z e Z d   Z d   Z d d  Z d d d  Z RS(   sw	  @brief CommonTokenStream that can be modified.

    Useful for dumping out the input stream after doing some
    augmentation or other manipulations.

    You can insert stuff, replace, and delete chunks.  Note that the
    operations are done lazily--only if you convert the buffer to a
    String.  This is very efficient because you are not moving data around
    all the time.  As the buffer of tokens is converted to strings, the
    toString() method(s) check to see if there is an operation at the
    current index.  If so, the operation is done and then normal String
    rendering continues on the buffer.  This is like having multiple Turing
    machine instruction streams (programs) operating on a single input tape. :)

    Since the operations are done lazily at toString-time, operations do not
    screw up the token index values.  That is, an insert operation at token
    index i does not change the index values for tokens i+1..n-1.

    Because operations never actually alter the buffer, you may always get
    the original token stream back without undoing anything.  Since
    the instructions are queued up, you can easily simulate transactions and
    roll back any changes if there is an error just by removing instructions.
    For example,

     CharStream input = new ANTLRFileStream("input");
     TLexer lex = new TLexer(input);
     TokenRewriteStream tokens = new TokenRewriteStream(lex);
     T parser = new T(tokens);
     parser.startRule();

     Then in the rules, you can execute
        Token t,u;
        ...
        input.insertAfter(t, "text to put after t");}
        input.insertAfter(u, "text after u");}
        System.out.println(tokens.toString());

    Actually, you have to cast the 'input' to a TokenRewriteStream. :(

    You can also have multiple "instruction streams" and get multiple
    rewrites from a single pass over the input.  Just name the instruction
    streams and use that name again when printing the buffer.  This could be
    useful for generating a C file and also its header file--all from the
    same buffer:

        tokens.insertAfter("pass1", t, "text to put after t");}
        tokens.insertAfter("pass2", u, "text after u");}
        System.out.println(tokens.toString("pass1"));
        System.out.println(tokens.toString("pass2"));

    If you don't use named rewrite streams, a "default" stream is used as
    the first example shows.
    t   defaulti    c         C   s9   t  j |  | |  i  |  _ g  |  j |  j <i  |  _ d  S(   N(   RG   R(   t   programst   DEFAULT_PROGRAM_NAMEt   lastRewriteTokenIndexes(   R   RH   RM   (    (    sW   /afs/athena.mit.edu/user/x/a/xavid/.local/lib/python2.7/site-packages/antlr3/streams.pyR(     s    	c         G   s   t  |  d k r) | d } | d } n4 t  |  d k rQ |  j } | d } n t d   |  j j | d  } | d k	 r | |  j | !|  j | <n  d S(   s   
        Rollback the instruction stream for a program so that
        the indicated instruction (via instructionIndex) is no
        longer in the stream.  UNTESTED!
        i   i    i   s   Invalid argumentsN(   R-   R|   t	   TypeErrorR{   R$   R   t   MIN_TOKEN_INDEX(   R   t   argst   programNamet   instructionIndexR/   (    (    sW   /afs/athena.mit.edu/user/x/a/xavid/.local/lib/python2.7/site-packages/antlr3/streams.pyt   rollback*  s    
	c         C   s   |  j  | |  j  d S(   s/   Reset the program so that no instructions existN(   R   R   (   R   R   (    (    sW   /afs/athena.mit.edu/user/x/a/xavid/.local/lib/python2.7/site-packages/antlr3/streams.pyt   deleteProgram@  s    c         G   s   t  |  d k r2 |  j } | d } | d } n? t  |  d k re | d } | d } | d } n t d   t | t  r | j } n  |  j | | d |  d  S(   Ni   i    i   i   s   Invalid arguments(   R-   R|   R~   R_   R   R   t   insertBefore(   R   R   R   R   Ri   (    (    sW   /afs/athena.mit.edu/user/x/a/xavid/.local/lib/python2.7/site-packages/antlr3/streams.pyt   insertAfterF  s    	


c         G   s   t  |  d k r2 |  j } | d } | d } n? t  |  d k re | d } | d } | d } n t d   t | t  r | j } n  t |  | |  } |  j |  } | j |  d  S(   Ni   i    i   i   s   Invalid arguments(	   R-   R|   R~   R_   R   R   Rr   t
   getProgramR8   (   R   R   R   R   Ri   t   opt   rewrites(    (    sW   /afs/athena.mit.edu/user/x/a/xavid/.local/lib/python2.7/site-packages/antlr3/streams.pyR   \  s    	


c         G   s  t  |  d k r< |  j } | d } | d } | d } n t  |  d k rx |  j } | d } | d } | d } nI t  |  d k r | d } | d } | d } | d } n t d   t | t  r | j } n  t | t  r | j } n  | | k s0| d k  s0| d k  s0| t  |  j  k r`t d | d | d	 t  |  j  d
   n  t |  | | |  } |  j	 |  } | j
 |  d  S(   Ni   i    i   i   i   s   Invalid argumentss   replace: range invalid: s   ..s   (size=t   )(   R-   R|   R~   R_   R   R   RI   t
   ValueErrorRt   R   R8   (   R   R   R   Rv   Rw   Ri   R   R   (    (    sW   /afs/athena.mit.edu/user/x/a/xavid/.local/lib/python2.7/site-packages/antlr3/streams.pyt   replaces  s4    	

	




9-c         G   s   |  j  t |  d  g   d  S(   N(   R   t   listR   (   R   R   (    (    sW   /afs/athena.mit.edu/user/x/a/xavid/.local/lib/python2.7/site-packages/antlr3/streams.pyt   delete  s    c         C   s   |  j  j | d  S(   Ni(   R}   R$   (   R   R   (    (    sW   /afs/athena.mit.edu/user/x/a/xavid/.local/lib/python2.7/site-packages/antlr3/streams.pyt   getLastRewriteTokenIndex  s    c         C   s   | |  j  | <d  S(   N(   R}   (   R   R   R	   (    (    sW   /afs/athena.mit.edu/user/x/a/xavid/.local/lib/python2.7/site-packages/antlr3/streams.pyt   setLastRewriteTokenIndex  s    c         C   s7   |  j  j | d   } | d  k r3 |  j |  } n  | S(   N(   R{   R$   R   t   initializeProgram(   R   R4   R/   (    (    sW   /afs/athena.mit.edu/user/x/a/xavid/.local/lib/python2.7/site-packages/antlr3/streams.pyR     s    c         C   s   g  } | |  j  | <| S(   N(   R{   (   R   R4   R/   (    (    sW   /afs/athena.mit.edu/user/x/a/xavid/.local/lib/python2.7/site-packages/antlr3/streams.pyR     s    c         C   s   | d  k r |  j } n  | d  k r7 |  j   d } n  t   } | } xW | |  j k r | | k r | t |  j  k  r | j |  j |  j  | d 7} qI W| j	   S(   Ni   (
   R   R   R   R    R-   RI   Rs   R$   Ri   t   getvalue(   R   R   t   endRl   R	   (    (    sW   /afs/athena.mit.edu/user/x/a/xavid/.local/lib/python2.7/site-packages/antlr3/streams.pyt   toOriginalString  s    	3c         G   s  t  |  d k r7 |  j } |  j } |  j   d } nj t  |  d k ro | d } |  j } |  j   d } n2 t  |  d k r |  j } | d } | d } n  | d  k r |  j } n t | t  s | j } n  | d  k r t  |  j  d } n t | t  s| j } n  | t  |  j  k r<t  |  j  d } n  | d k  rQd } n  |  j	 j
 |  } | d  k st  |  d k r|  j | |  St   } |  j |  } | } x | | k rI| t  |  j  k  rI| j
 |  }	 y | | =Wn t k
 r n X|  j | }
 |	 d  k r7| j |
 j  | d 7} q|	 j |  } qW| t  |  j  d k rxV t | j    D]? } | | }	 |	 j t  |  j  d k rv| j |	 j  qvqvWn  | j   S(   Ni    i   i   (   R-   R|   R   R   R   R_   R`   R   RI   R{   R$   R   R    t   reduceToSingleOperationPerIndexRT   Rs   Ri   Rm   t   sortedt   keysR   (   R   R   R   R   R   R   Rl   t	   indexToOpR	   R   RV   (    (    sW   /afs/athena.mit.edu/user/x/a/xavid/.local/lib/python2.7/site-packages/antlr3/streams.pyR&     s^    		
		
		$
c      
   C   s	  xat  |  D]S\ } } | d k r+ q n  t | t  s@ q n  xT |  j | t |  D]= \ } } | j | j k rV | j | j k rV d | | <qV qV Wx |  j | t |  D] \ } } | j | j k r | j | j k r d | | <q n  | j | j k  p| j | j k } | j | j k o2| j | j k } | r | r t d | | f   q q Wq WxAt  |  D]3\ } } | d k rqqn  t | t  sqqn  x] |  j | t |  D]F \ } }	 |	 j | j k r|  j	 | j
 |	 j
  | _
 d | | <qqWx |  j | t |  D] \ } } | j | j k rc|  j	 | j
 | j
  | _
 d | | <qn  | j | j k r| j | j k rt d | | f   qqWqqWi  }
 xT t  |  D]F \ } } | d k rqn  | j |
 k st d   | |
 | j <qW|
 S(   sT  
        We need to combine operations and report invalid operations (like
        overlapping replaces that are not completed nested).  Inserts to
        same index need to be combined etc...   Here are the cases:

        I.i.u I.j.v                           leave alone, nonoverlapping
        I.i.u I.i.v                           combine: Iivu

        R.i-j.u R.x-y.v | i-j in x-y          delete first R
        R.i-j.u R.i-j.v                       delete first R
        R.i-j.u R.x-y.v | x-y in i-j          ERROR
        R.i-j.u R.x-y.v | boundaries overlap  ERROR

        I.i.u R.x-y.v   | i in x-y            delete I
        I.i.u R.x-y.v   | i not in x-y        leave alone, nonoverlapping
        R.x-y.v I.i.u   | i in x-y            ERROR
        R.x-y.v I.x.u                         R.x-y.uv (combine, delete I)
        R.x-y.v I.i.u   | i not in x-y        leave alone, nonoverlapping

        I.i.u = insert u before op @ index i
        R.x-y.u = replace x-y indexed tokens with u

        First we need to examine replaces.  For any replace op:

          1. wipe out any insertions before op within that range.
          2. Drop any replace op before that is contained completely within
             that range.
          3. Throw exception upon boundary overlap with any previous replace.

        Then we can deal with inserts:

          1. for any inserts to same index, combine even if not adjacent.
          2. for any prior replace with same left boundary, combine this
             insert with replace and delete this replace.
          3. throw exception if index in same range as previous replace

        Don't actually delete; make op null in list. Easier to walk list.
        Later we can throw as we add to index -> op map.

        Note that I.2 R.2-2 will wipe out I.2 even though, technically, the
        inserted stuff would be before the replace range.  But, if you
        add tokens in front of a method body '{' and then delete the method
        body, I think the stuff before the '{' you added should disappear too.

        Return a map from token index to operation.
        s4   replace op boundaries of %s overlap with previous %ss-   insert op %s within boundaries of previous %ss   should only be one op per indexN(   t	   enumerateR   R_   Rt   t   getKindOfOpsRr   R   Ru   R   t	   catOpTextRi   t   AssertionError(   R   R   R	   t   ropt   jt   iopt   prevRopt   disjointt   samet   prevIopt   mR   (    (    sW   /afs/athena.mit.edu/user/x/a/xavid/.local/lib/python2.7/site-packages/antlr3/streams.pyR     s\    1"$"
""
$c         C   s>   d } d } | d  k	 r! | } n  | d  k	 r6 | } n  | | S(   NRg   (   R   (   R   t   at   bt   xt   y(    (    sW   /afs/athena.mit.edu/user/x/a/xavid/.local/lib/python2.7/site-packages/antlr3/streams.pyR     s    		c         c   s   | d  k r t |  } n! | t |  k r< t |  } n  xM t | |   D]; \ } } | d  k rk qM n  | j | k rM | | f VqM qM Wd  S(   N(   R   R-   R   Rn   (   R   R   t   kindt   beforeR	   R   (    (    sW   /afs/athena.mit.edu/user/x/a/xavid/.local/lib/python2.7/site-packages/antlr3/streams.pyR     s    c         C   s   | d  k r |  j } n  | d  k r7 |  j   d } n  t   } | } xT | |  j k r | | k r | t |  j  k  r | j |  j |   | d 7} qI W| j   S(   Ni   (	   R   R   R   R    R-   RI   Rs   R$   R   (   R   R   R   Rl   R	   (    (    sW   /afs/athena.mit.edu/user/x/a/xavid/.local/lib/python2.7/site-packages/antlr3/streams.pyt   toDebugString  s    	3N(   R   R   R   R|   R   R   R   R(   R   R   R   R   R   R   R   R   R   R   R   R&   Rp   R   R   R   R   (    (    (    sW   /afs/athena.mit.edu/user/x/a/xavid/.local/lib/python2.7/site-packages/antlr3/streams.pyRy     s*   5				(					K	v	
(   R   R=   R    t   antlr3.constantsR   R   t   antlr3.tokensR   R   t   objectR   R   R"   R'   R:   RC   t   StringStreamt
   FileStreamt   InputStreamRG   Rj   Rr   Rt   Rx   Ry   (    (    (    sW   /afs/athena.mit.edu/user/x/a/xavid/.local/lib/python2.7/site-packages/antlr3/streams.pyt   <module>   s(    =H#! B	