
    2h7                     X   d Z ddlmZ ddlmZ ddlZddlmZ ddlmZ ddl	m
Z
  G d	 d
e          Z G d de          Z G d de          Z G d de          Zd Zedk    rddlZdZ eej                  dk    rej        d         Z ee          Ze                    dd            eed          5 Ze                                Z ee           e                    e           	  ej                    Zesn ee           	 ddd           dS # 1 swxY w Y   dS dS )z?
The lexer for AnkiBuffers
Author: Mark Pauley
Date: 12/8/2014
    )absolute_import)print_functionN   )	constants)lex)TOKENc                        e Zd Zd Zd Zd ZdS )WrappedIntConstantc                 L    || _         || _        t          ||          | _        d S N)texttypeintvalue)selfr   r   bases       J/home/metal/girl-os-victor/victor-clad/tools/message-buffers/clad/lexer.py__init__zWrappedIntConstant.__init__    s#    		t__


    c                     | j         S r   r   r   s    r   __str__zWrappedIntConstant.__str__%   
    yr   c                 t    d                     t          |           j        | j        | j        | j                  S Nz{0}({1!r}, {2!r}, {3!r})formatr   __name__r   r   r   s    r   __repr__zWrappedIntConstant.__repr__(   /    )00d1DdiQUQZ\`\fgggr   Nr   
__module____qualname__r   r   r     r   r   r
   r
      sF        % % %
  h h h h hr   r
   c                        e Zd Zd Zd Zd ZdS )WrappedFloatConstantc                 J    || _         || _        t          |          | _        d S r   )r   r   floatr   )r   r   r   s      r   r   zWrappedFloatConstant.__init__-   s!    		4[[


r   c                     | j         S r   r   r   s    r   r   zWrappedFloatConstant.__str__2   r   r   c                 t    d                     t          |           j        | j        | j        | j                  S r   r   r   s    r   r    zWrappedFloatConstant.__repr__5   r!   r   Nr"   r%   r   r   r'   r'   +   sF        ! ! !
  h h h h hr   r'   c                        e Zd Zd Zd Zd ZdS )WrappedStringConstantc                 X    || _         d | _        |                    dd          | _        d S )Nz	verbatim  )r   r   replacer   r   r   s     r   r   zWrappedStringConstant.__init__:   s+    		 \\+r22


r   c                     | j         S r   r   r   s    r   __strzWrappedStringConstant.__strA   r   r   c                 t    d                     t          |           j        | j        | j        | j                  S r   r   r   s    r   r    zWrappedStringConstant.__repr__D   r!   r   N)r   r#   r$   r   _WrappedStringConstant__strr    r%   r   r   r-   r-   8   sF        3 3 3  h h h h hr   r-   c                   |   e Zd ZdZdd d fdZd Zd Zd Zd	 Zd
 Z	dZ
dZi Ze
D ]<Ze                                Zeee                                <   e eeg          z   Z=edz   ZdZdZdZd ZdZdZdZdZdZdZdZdZd Z e e          d             Z! e e          d             Z"d Z#d Z$d Z%d Z&d  Z'd!S )"AnkiBufferLexera   A lexer for the Anki Buffer definition language.  After building it, set the
    input text with input(), and call token() to get new tokens.

    The initial filename attribute can be set to an initial filename, but the lexer will update it upon include directives
    Fc                     dS NFr%   )names    r   <lambda>zAnkiBufferLexer.<lambda>O   s    u r   c                     dS r9   r%   )messagelinecolumns      r   r;   zAnkiBufferLexer.<lambda>P   s    5 r   c                 Z    || _         d| _        d| _        d| _        || _        || _        dS )z Create a new lexer
        error_func:
          An error function. Will be called with an error message,
          line and locumn as arguments, in case of an error during lexing.
        r/   N)
error_funcinput_directoryfilename
last_tokentype_lookup_func	lex_debug)r   rF   rE   rA   s       r   r   zAnkiBufferLexer.__init__M   s4     %! 0"r   c                 6    t          j         dd| i|| _        dS )z Builds the lexer from the spec given.  Must be called
        after the lexer object is created.
        The PLY manual warns against calling lex.lex inside __init__
        objectNr%   )r   lexer)r   kwargss     r   buildzAnkiBufferLexer.build]   s$    
 W33D3F33


r   c                 :    | j                             |           d S r   )rI   inputr1   s     r   rM   zAnkiBufferLexer.inpute   s    
r   c                     | j                                         | _        | j        rt	          d| j        z             | j        S )Nz
(Lexer) %s)rI   tokenrD   rF   printr   s    r   rO   zAnkiBufferLexer.tokenh   s>    ***,,> 	2,0111r   c                 n    | j         j                            dd|          }|t          d|          z
  }|S )z2 Find the column of the token in its line
        
r   )rI   lexdatarfindmax)r   lexposfoundr?   s       r   find_tok_columnzAnkiBufferLexer.find_tok_columnn   s7     
"((q&99#a--'r   c                     | j         r|                      ||           d S t          j        d                    t          t
          |                     d S )Nz{lineno}:{column}: {msg})linenor?   msg)rA   sysexitr   rZ   r?   )r   r[   ts      r   _errorzAnkiBufferLexer._errorv   sT    ? 	_OOC#####H/66fVY\6]]^^^^^r   )boolfloat_32float_64stringenumint_8int_16int_32int_64uint_8uint_16uint_32uint_64r=   	structureunion	autounion	namespaceincludeno_cpp_classno_default_constructorenum_conceptdupes_allowedr%   )STRING_LITERALIDQUOTED_PATHINT_CONST_DECINT_CONST_HEXFLOAT_CONST_DECLBRACERBRACELSQRSQCOMMAEQCOLONz3(?:[a-zA-Z_][0-9a-zA-Z_]*::)*[a-zA-Z_][0-9a-zA-Z_]*z 	c                 `    |j         xj        |j                            d          z  c_        dS )z\n+rR   NrI   rZ   r   countr   r^   s     r   	t_NEWLINEzAnkiBufferLexer.t_NEWLINE   &    	!'-----r   z\{z\}z\[z\],=z\:z(verbatim [^,\{\}\[\]=;\n]*)c                 `    |j         xj        |j                            d          z  c_        dS )z(/\*(.|\n)*?\*/)|(//.*)rR   Nr   r   s     r   	t_COMMENTzAnkiBufferLexer.t_COMMENT   r   r   c                     t          |j                  |_        |j        j        dk    r|                     d|           |S )Nr/   z No text after "verbatim" keyword)r-   r   r_   r   s     r   t_STRING_LITERALz AnkiBufferLexer.t_STRING_LITERAL   s<    '007=B
++8!
<
<
<r   c                    | j                             |j        d          |_        |j        dk    rN|j        t          j        v r;d|j        dt	          j        |j                  d}|                     ||           |S )Nrw   zAttempt to use the name "z", which is a keyword in .)keyword_mapgetr   r   r   keywordswhat_language_uses_keywordr_   r   r^   r[   s      r   t_IDzAnkiBufferLexer.t_ID   sz    !%%agt446T>>ag);;;;=agFFFFHCKKQr   c                     |S )z4\"([0-9a-zA-Z_]+/)*[0-9a-zA-Z_]+(\.[0-9a-zA-Z_]+)?\"r%   r   s     r   t_QUOTED_PATHzAnkiBufferLexer.t_QUOTED_PATH   s	     r   c                 :    t          |j        d          |_        |S )z-?[0-9]+\.[0-9]+dec)r'   r   r   s     r   t_FLOAT_CONST_DECz!AnkiBufferLexer.t_FLOAT_CONST_DEC   s    &qw66r   c                 <    t          |j        dd          |_        |S )z0[xX][0-9a-fA-F]+hex   r
   r   r   s     r   t_INT_CONST_HEXzAnkiBufferLexer.t_INT_CONST_HEX       $QWeR88r   c                 <    t          |j        dd          |_        |S )z-?[0-9]+r   
   r   r   s     r   t_INT_CONST_DECzAnkiBufferLexer.t_INT_CONST_DEC   r   r   c                 l    dt          |j        d                   z  }|                     ||           d S )NzIllegal character %sr   )reprr   r_   r   s      r   t_errorzAnkiBufferLexer.t_error   s4    $tAGAJ'7'77Cr   N)(r   r#   r$   __doc__r   rK   rM   rO   rX   r_   r   keywords_upperr   keywordupperkeyword_upperlowertupletokens
identifierstatest_ignorer   t_LBRACEt_RBRACEt_LSQt_RSQt_COMMAt_EQt_COLONstring_literalr   r   r   r   r   r   r   r   r   r%   r   r   r7   r7   G   s         ""4"4@@# # # # 4 4 4      _ _ _H6 NK A A'4GMMOO$'%%*@*@@
   F$ HJF H. . . HHEEGDG 5N. . . U>   U:      
  
  
    r   r7   c                 0    t          d| ||fz             d S )NzLexical Error: %s(%d,%d))rP   )r[   r>   r?   s      r   	errorFuncr     s#    	
$T6':
:;;;;;r   __main__z../emitters/tests/src/Foo.cladF)optimizedebugr)r   
__future__r   r   r\   r/   r   plyr   ply.lexr   rH   r
   r'   r-   r7   r   r   in_filelenargv
test_lexerrK   openfreadr   rP   rM   rO   tokr%   r   r   <module>r      s    ' & & & & & % % % % % % 



                  h h h h h h h hh h h h h6 h h hh h h h hF h h hz z z z zf z z z~< < < zJJJ.G
s38}}q(1+ ++Je1---	gs		 qvvxxd	#)++CE#JJJ	                  s   ;ADD!$D!