;๒
น0sDc           @   sั   d  Z  d k Z d k Z d k l Z d k l Z d f  d     YZ d e i d e i e i d Z	 d	 Z
 d e i d
 Z e i e	  Z e i e
  Z e i e  Z d   Z e d j o e   n d S(   s@  
Template parsing module inspired by REXX (with thanks to Donn Cave for discussion).

Template initialization has the form:
   T = Template(template_string, wild_card_marker, single_char_marker,
             x = regex_x, y = regex_y, ...)
Parsing has the form
   ([match1, match2, ..., matchn], lastindex) = T.PARSE(string)

Only the first argument is mandatory.

The resultant object efficiently parses strings that match the template_string,
giving a list of substrings that correspond to each "directive" of the template.

Template directives:

  Wildcard:
    The template may be initialized with a wildcard that matches any string
    up to the string matching the next directive (which may not be a wild
    card or single character marker) or the next literal sequence of characters
    of the template.  The character that represents a wildcard is specified
    by the wild_card_marker parameter, which has no default.

    For example, using X as the wildcard:


    >>> T = Template("prefixXinteriorX", "X")
    >>> T.PARSE("prefix this is before interior and this is after")
    ([' this is before ', ' and this is after'], 47)
    >>> T = Template("<X>X<X>", "X")
    >>> T.PARSE('<A HREF="index.html">go to index</A>')
    (['A HREF="index.html"', 'go to index', '/A'], 36)

    Obviously the character used to represent the wildcard must be distinct
    from the characters used to represent literals or other directives.

  Fixed length character sequences:
    The template may have a marker character which indicates a fixed
    length field.  All adjacent instances of this marker will be matched
    by a substring of the same length in the parsed string.  For example:

      >>> T = Template("NNN-NN-NNNN", single_char_marker="N")
      >>> T.PARSE("1-2-34-5-12")
      (['1-2', '34', '5-12'], 11)
      >>> T.PARSE("111-22-3333")
      (['111', '22', '3333'], 11)
      >>> T.PARSE("1111-22-3333")
      ValueError: literal not found at (3, '-')

    A template may have multiple fixed length markers, which allows fixed
    length fields to be adjacent, but recognized separately.  For example:

      >>> T = Template("MMDDYYX", "X", "MDY")
      >>> T.PARSE("112489 Somebody's birthday!")
      (['11', '24', '89', " Somebody's birthday!"], 27)

  Regular expression markers:
    The template may have markers associated with regular expressions.
    the regular expressions may be either string represenations of compiled.
    For example:
      >>> T = Template("v: s i", v=id, s=str, i=int)
      >>> T.PARSE("this_is_an_identifier: 'a string' 12344")
      (['this_is_an_identifier', "'a string'", '12344'], 39)
      >>>
    Here id, str, and int are regular expression conveniences provided by
    this module.

  Directive markers may be mixed and matched, except that wildcards cannot precede
  wildcards or single character markers.
  Example:
>>> T = Template("ssnum: NNN-NN-NNNN, fn=X, ln=X, age=I, quote=Q", "X", "N", I=int, Q=str)
>>> T.PARSE("ssnum: 123-45-6789, fn=Aaron, ln=Watters, age=13, quote='do be do be do'")
(['123', '45', '6789', 'Aaron', 'Watters', '13', "'do be do be do'"], 72)
>>>

N(   s
   StringType(   s   finds   Templatec           B   s#   t  Z e e d  Z d d  Z RS(   Nc         K   sโ  | |  _  | |  _ | |  _ | i   }	 | o |	 i	 |  n | o3 x | D] } |	 i	 |  qM W| d |  _ } n |	 |  _ x3 |	 D]+ }
 t |
  d j o t d |
  q q Wh  |  _ } xJ | i   D]< \ }
 } t |  t j o t i |  } n | | |
 <qา Wg  } t } d } t |  } d } x| | j  o| } | | } | | j oE | | j o t d  n | i	 | t f  | d } | d } n| o
 | | j oo | | j o t d  n x- | | j  o | | | j o | d } qูW| i	 | | | f  | d } n | |	 j oO x. | | j  o | | |	 j o | d } q<W| i	 t | | | !f  n2 | | } | i	 | | f  | d } | d } | | d } q9W| |  _ | |  _ d  S(   Ni    i   s!   Marks must be single characters: s)   two wild cards in sequence is not alloweds+   wild card cannot precede single char marker(   s   templates   selfs   wild_card_markers	   wild_cards   single_char_markers   chars   marker_to_regex_dicts   keyss   markerss   appends   chs   single_char_primarys   marks   lens
   ValueErrors   marker_dicts   itemss   rgexs   types
   StringTypes   res   compiles	   parse_seqs   Nones   lastchars   indexs   lasts   ndirectivess   starts   thischar(   s   selfs   templates   wild_card_markers   single_char_markers   marker_to_regex_dicts   chs   single_char_primarys   starts	   parse_seqs   markerss   marks   indexs   thischars   rgexs   lasts   marker_dicts   ndirectivess   lastchar(    (    sG   /home/packages/reportlab/reportlab_2_0/reportlab/tools/docco/t_parse.pys   __init__]   sr    			 	   

  


	i    c         C   s  |  i } |  i } |  i } |  i } t |  d }
 t g | } d }	 | } x t |
 d  D]} | | \ } } | t j oE t | | |  | j o t d | | f  n | t |  } q^ | | j oจ | |
 j o t |  } qห| | d \ } } | t j o5 t | | |  } | | j  o t d |  qwqห| i | |  } | | j  o t d |  qหnQ | | j o | | } n6 | i | |  | } | | j  o t d |  n | | | !| |	 <|	 d }	 | } q^ W|	 | j o t d  n | | f Sd  S(   Ni   i    s   literal not found at s!   couldn't terminate wild with lit s    couldn't terminate wild with re s   couldn't match re at s   not enough directives found?(   s   selfs   ndirectivess	   wild_cards   chars   single_chars	   parse_seqs   lens
   lparse_seqs   Nones   results   current_directive_indexs   starts   currentindexs   xranges   parse_indexs	   indicators   datas   finds   strs
   ValueErrors   lasts   nextindicators   nextdatas   searchs   matchs   SystemError(   s   selfs   strs   starts   results	   indicators   nextindicators	   parse_seqs   single_chars   nextdatas   current_directive_indexs
   lparse_seqs   parse_indexs   currentindexs	   wild_cards   datas   lasts   ndirectives(    (    sG   /home/packages/reportlab/reportlab_2_0/reportlab/tools/docco/t_parse.pys   PARSE    sJ    				 

(   s   __name__s
   __module__s   Nones   __init__s   PARSE(    (    (    sG   /home/packages/reportlab/reportlab_2_0/reportlab/tools/docco/t_parse.pys   Template[   s   Cs   [s   ][s   _]*s   '[^
']*'s   ]+c          C   sย   t  d d d  a t i d  GHt  d d t a d } t i |  GHt  d d d d t d	 t a t i d
  GHt  d d d d t d	 t a t i d  GHt  d d d  }  |  i d  GHd  S(   Ns   (NNN)NNN-NNNN X Xs   Xs   Ns   (908)949-2726 Aaron Watterss   s --> s blahs   ss=   ' <-- a string --> ' --> 'blah blah another string blah' blahs   s --> NNNiXs   is'   'A STRING' --> 15964653alpha beta gammas   XsXis)   prefix'string'interior1234junk not parseds   MMDDYYXs   MDYs   122961 Somebody's birthday!(
   s   Templates   Ts   PARSEs   strs   T1s   ss   ints   T2s   T3s   T4(   s   T4s   s(    (    sG   /home/packages/reportlab/reportlab_2_0/reportlab/tools/docco/t_parse.pys   testใ   s     s   __main__(   s   __doc__s   res   strings   typess
   StringTypes   finds   Templates   letterss   digitss   USERNAMEREGEXs   STRINGLITREGEXs   SIMPLEINTREGEXs   compiles   ids   strs   ints   tests   __name__(   s   USERNAMEREGEXs
   StringTypes   strings   STRINGLITREGEXs   SIMPLEINTREGEXs   ids   res   ints   strs   Templates   tests   find(    (    sG   /home/packages/reportlab/reportlab_2_0/reportlab/tools/docco/t_parse.pys   ?O   s   #	 