ó
€™xTc           @   s\   d  Z  d d l m Z d d l Z d d l m Z d d l m Z d e	 f d „  ƒ  YZ
 d S(   s    Parsing of ins/res files iÿÿÿÿ(   t   divisionN(   t   error(   t   tokenst   command_streamc        2   B   s  e  Z d  Z e g  d d d d d d d d d	 d
 d d d d d d d d d d d d d d g D] Z e d f ^ q^ ƒ Z e g  d d d d d d d  d! d" d# d$ d% d& d' d( d) d* d+ d, d- d. d/ d0 d1 d2 d3 d4 d5 d6 d7 d8 d9 d: d; d< d= d> d? d@ dA dB dC dD dE dF dG dH dI g0 D] Z e d f ^ qƒ Z e j e ƒ dU dU dJ „ Z	 e
 j dK dL j e j ƒ  ƒ e
 j e
 j Bƒ Z e
 j dM e
 j e
 j Bƒ Z e
 j dN e
 j e
 j Bƒ Z e
 j dO e
 j e
 j Bƒ Z e
 j dP e
 j e
 j Bƒ Z e
 j dQ ƒ Z dR „  Z dS „  Z dT „  Z RS(V   s0    An ins/res file parsed as a stream of commands t   ANISt   BINDt   BLOCt   BONDt   CHIVt   CONFt   CONNt   DANGt   DELUt   DFIXt   EADPt   EXYZt   FLATt   FREEt   HFIXt   HTABt   ISORt   MPLAt   NCSYt   OMITt   RTABt   SADIt   SAMEt   SIMUi   t   ACTAt   AFIXt   BASFt   BUMPt   CELLt   CGLSt   DAMPt   DEFSt   DISPt   ENDt   EQIVt   EXTIt   FENDt   FMAPt   FRAGt   FVARt   GRIDt   HKLFt   HOPEs   L.S.t   LATTt   LAUEt   LISTt   MERGt   MOLEt   MOREt   MOVEt   MUSTt   PARTt   PLANt   REMt   RESIt   SFACt   SHELt   SIZEt   SPECt   STIRt   SUMPt   SWATt   SYMMt   TEMPt   TIMEt   TITLt   TWINt   UNITt   WGHTt   WPDBt   ZERRc         C   sI   | | g j  d  ƒ d k s! t ‚ | d  k r< t | ƒ } n  | |  _ d  S(   Ni   (   t   countt   Nonet   AssertionErrort   opent   file(   t   selfRO   t   filename(    (    s?   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/shelx/lexer.pyt   __init__   s    ! sB  
    ^
    (?:
      (TITL | REM)
      \s?
      (.*)
      |
      (?:
        (?:
          ( %s )
          _
          (?:
            ([a-z] \S{0,3})
            |
            (\d{1,4})
            |
            \*
          )?
        )
        |
        ([a-z] \S{0,3})
      )
      ([^!=]*)
      (=?)
    )
    s    | s)   
    ^
    \s+
    ([^!=]*)
    (=?)
    sÄ   
    (?P<name>[a-z] [^_]{0,3})
    (?: _ \$ (?P<symmetry>\d+) )?
    (?: _ (?P<resnum>\d+))?
    (?: _ (?P<plusminus>[\+\-]))?
    |
    \$ (?P<element>[a-z]{1,2})
    |
    (?P<range> > | <)
    s   
    (?<! , ) \s+
    s   
    \$ (\d+) \s+ (.*) $
    s   (\+)(.*)c         c   sz  t  } xmt |  j ƒ D]\\ } } | s. q n  | rŒ |  j j | ƒ } | d k rd t d | ƒ ‚ n  | j ƒ  \ } } | j | j	 ƒ  ƒ n}|  j
 j | ƒ } | d k r| d j ƒ  rÀ q nM |  j j | ƒ } | d k	 r| j d d ƒ \ } } | | f | f Vq n  t d | ƒ ‚ n  | j d ƒ p:| j d ƒ } | rX| j ƒ  } d }	 nr | j d d d	 ƒ \ } }
 } | j ƒ  } |
 r t j |
 j ƒ  f }	 n* | r¾t j t | ƒ f }	 n t j f }	 | j d ƒ pë| j d
 ƒ pëd } | j d ƒ } | j	 ƒ  } | s |  j | | | | | ƒ } | d k rT|  j | |	 | | | ƒ } n  | | f V| d k rrPqrq q Wd S(   sˆ  
    Yields the commands in self.file as tuples:
      - either (cmd, args) where args is a tuple,
        e.g. ('CELL', (0.71073, 1, 2, 3, 90, 90, 90))
      - or (cmd, (residue_class, residue_number), args) for those commands
        which can be suffixed by a residue, e.g.
        ('HFIX', (parser.residue_number_tok, 1), (23, ))
    Notes:
      - For atoms, cmd is '__ATOM__' and the name is the first argument
      - For so-called Q-peaks, cmd is '__Q_PEAK__' and the number is the
        first argument
      - In args, floating point items are reported as is whereas any
        string comes as (type, value) where type is one of the class
        constants defined just above this method (actually, for
        the residue_number_tok, the value is an integer stored as a float).
      - The atomic refinable variables are not decoded: this is the job of
        a parser, not of a lexer.
    s   illegal continuation line errori    i   i   s"   illegal command or atom name errori   i   i   i   i   t    i   R-   N(   t   Falset	   enumerateRO   t   _continuation_patt   searchRL   t   shelx_errort   groupst   extendt   splitt   _cmd_patt   isspacet   _include_filename_patt   groupt   upperR   t   residue_class_tokt   residue_number_tokt   intt   all_residues_tokt   _parse_special_casest   _parse_general_case(   RP   t	   continuedt   it   lit   mt	   cont_argst	   argumentst   cmdRQ   t   cmd_residuet   cmd_residue_classt   cmd_residue_numbert   argst   result(    (    s?   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/shelx/lexer.pyt   __iter__Z   sR      	$ c         C   s­  | d
 k r: | j  ƒ  } | r* | f } n d } | | f S| d k r‰ | d  k rg t d | | ƒ ‚ n  | |  j j d | ƒ j ƒ  f f S| d k r>|  j j | ƒ } | d  k rÈ t d | | ƒ ‚ n  y t | j	 d ƒ ƒ } Wn, t
 k
 rt d | | j	 d ƒ ƒ ‚ n X| | |  j j d | j	 d ƒ ƒ j ƒ  f f S| d	 k r©g  }	 xF | D]> }
 y |	 j t |
 ƒ ƒ WqWt
 k
 r”|	 j |
 ƒ qWXqWW| t |	 ƒ f Sd  S(   NRE   R9   RB   s   illegal argument '%s'RS   R&   i   i   R;   (   RE   R9   (    (   t   stripRL   RX   t
   symm_spacet   subR`   t	   _eqiv_patRW   Rc   R_   t
   ValueErrort   appendt   floatt   tuple(   RP   Rm   Rq   Rl   Rh   Ri   t	   arg_tupleRj   t   idxt	   sfac_argst   e(    (    s?   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/shelx/lexer.pyRe   ˜   s8     
".c         C   sg  g  } xÊt  | ƒ D]¼\ } } y | j t | ƒ ƒ Wq t k
 rÎ| d k rb | j | ƒ qÏ|  j j | ƒ }	 |	 d  k r• t d | | ƒ ‚ n  |	 j d d d d ƒ \ }
 } } } |
 d  k	 r,| d  k	 rà t	 | ƒ } n  | d  k	 rû t	 | ƒ } n  | j t
 j d |
 d | d | d | ƒ ƒ q n  |	 j d	 d ƒ \ } } | d  k	 ru| j t
 j d	 | d | ƒ ƒ q n  |	 j d
 ƒ d k r | j t
 j f ƒ qÏ|	 j d
 ƒ d k rÏ| j t
 j f ƒ qÏq Xq W| rì| | t | ƒ f S| |  j k rM| d d k r1t	 | d ƒ f t | ƒ } d } qY| f t | ƒ } d } n t | ƒ } | | f Sd  S(   NR:   s   illegal argument '%s't   namet   symmetryt   resnumt	   plusminust   residue_numbert
   plus_minust   elementt   ranget   >t   <i    t   Qi   t
   __Q_PEAK__t   __ATOM__(   RU   Ry   Rz   Rx   t   _atom_name_patt   matchRL   RX   R_   Rc   R   t   atomname_tokent   element_tokent   forward_range_tokt   backward_range_tokR{   t   shelx_commands(   RP   Rm   Rn   Rl   Rh   Ri   t   tokst   jt   argRj   R€   R�   R‚   R…   R†   (    (    s?   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/shelx/lexer.pyRf   µ   sR      

		N(   t   __name__t
   __module__t   __doc__t   dictRm   t   commands_allowing_atom_namesR“   t   updateRL   RR   t   ret   compilet   joint   keyst   Xt   IR\   RV   R�   Ru   Rw   R^   Rs   Re   Rf   (    (    (    s?   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/shelx/lexer.pyR   	   s:   1(		>	(   R™   t
   __future__R    R�   t   iotbx.shelx.errorsR   RX   t   iotbx.shelxR   t   objectR   (    (    (    s?   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/shelx/lexer.pyt   <module>   s
   