
    q&fU<                     n    d dl Z d dlZddlmZ ddlmZmZ ddlmZ d Z		 	 	 	 	 	 	 d	dZ
	 	 	 	 	 	 	 d	dZy)
    N   )_)errorpycompat)
stringutilc              #     K   | syt        j                  t        j                  t        j                  z         i ddfd}t        j
                  |       fdfdfdfd}        }|d	k7  rh|t        j                  t        j                        v r-        }|t        j                  t        j                        v r-|d
k(  rdgff dz          }n|dk(  r`         t        j                  t        j                              \  }}t        |      }t        d|      D ]  }dgff dz   n|dv rl|dk(  r        } ||      \  }}|g}|dk(  r' |              \  }}|j                  |       |dk(  r'|D 	cg c]
  }	 ||	       }
}	d|
ff dz  n|dk(  r |              \  }}	 ||	      n|dk(  r |              \  }}|<   d|ff n|dk(  r |              \  }}d|f n|dk(  rC        }|dk(  r%d}        }|dvr||z  }        }|dvrd|f n ||      \  }}d|f nv|dk(  r|dvrm        }|dvrn`|dk(  r
d        }nQ|d	k(  ryd}d}|d	k7  r!|dk  r||z  }|dz  }        }|d	k7  r|dk  rt        j                  t        d      |z        |d	k7  rgyyc c}	w w)a#  parses a DAG from a concise textual description; generates events

    "+n" is a linear run of n nodes based on the current default parent
    "." is a single node based on the current default parent
    "$" resets the default parent to -1 (implied at the start);
        otherwise the default parent is always the last node created
    "<p" sets the default parent to the backref p
    "*p" is a fork at parent p, where p is a backref
    "*p1/p2/.../pn" is a merge of parents p1..pn, where the pi are backrefs
    "/p2/.../pn" is a merge of the preceding node and p2..pn
    ":name" defines a label for the preceding node; labels can be redefined
    "@text" emits an annotation event for text
    "!command" emits an action event for the current node
    "!!my command
" is like "!", but to the end of the line
    "#...
" is a comment up to the end of the line

    Whitespace between the above elements is ignored.

    A backref is either
     * a number n, which references the node curr-n, where curr is the current
       node, or
     * the name of a label you placed earlier using ":name", or
     * empty to denote the default parent.

    All string valued-elements are either strictly alphanumeric, or must
    be enclosed in double quotes ("..."), with "" as escape character.

    Generates sequence of

      ('n', (id, [parentids])) for node creation
      ('l', (id, labelname)) for labels on nodes
      ('a', text) for annotations
      ('c', command) for actions (!)
      ('C', command) for line actions (!!)

    Examples
    --------

    Example of a complex graph (output not shown for brevity):

        >>> len(list(parsedag(b"""
        ...
        ... +3         # 3 nodes in linear run
        ... :forkhere  # a label for the last of the 3 nodes from above
        ... +5         # 5 more nodes on one branch
        ... :mergethis # label again
        ... <forkhere  # set default parent to labeled fork node
        ... +10        # 10 more nodes on a parallel branch
        ... @stable    # following nodes will be annotated as "stable"
        ... +5         # 5 nodes in stable
        ... !addfile   # custom command; could trigger new file in next node
        ... +2         # two more nodes
        ... /mergethis # merge last node with labeled node
        ... +4         # 4 more nodes descending from merge node
        ...
        ... """)))
        34

    Empty list:

        >>> list(parsedag(b""))
        []

    A simple linear run:

        >>> list(parsedag(b"+3"))
        [('n', (0, [-1])), ('n', (1, [0])), ('n', (2, [1]))]

    Some non-standard ways to define such runs:

        >>> list(parsedag(b"+1+2"))
        [('n', (0, [-1])), ('n', (1, [0])), ('n', (2, [1]))]

        >>> list(parsedag(b"+1*1*"))
        [('n', (0, [-1])), ('n', (1, [0])), ('n', (2, [1]))]

        >>> list(parsedag(b"*"))
        [('n', (0, [-1]))]

        >>> list(parsedag(b"..."))
        [('n', (0, [-1])), ('n', (1, [0])), ('n', (2, [1]))]

    A fork and a join, using numeric back references:

        >>> list(parsedag(b"+2*2*/2"))
        [('n', (0, [-1])), ('n', (1, [0])), ('n', (2, [0])), ('n', (3, [2, 1]))]

        >>> list(parsedag(b"+2<2+1/2"))
        [('n', (0, [-1])), ('n', (1, [0])), ('n', (2, [0])), ('n', (3, [2, 1]))]

    Placing a label:

        >>> list(parsedag(b"+1 :mylabel +1"))
        [('n', (0, [-1])), ('l', (0, 'mylabel')), ('n', (1, [0]))]

    An empty label (silly, really):

        >>> list(parsedag(b"+1:+1"))
        [('n', (0, [-1])), ('l', (0, '')), ('n', (1, [0]))]

    Fork and join, but with labels instead of numeric back references:

        >>> list(parsedag(b"+1:f +1:p2 *f */p2"))
        [('n', (0, [-1])), ('l', (0, 'f')), ('n', (1, [0])), ('l', (1, 'p2')),
         ('n', (2, [0])), ('n', (3, [2, 1]))]

        >>> list(parsedag(b"+1:f +1:p2 <f +1 /p2"))
        [('n', (0, [-1])), ('l', (0, 'f')), ('n', (1, [0])), ('l', (1, 'p2')),
         ('n', (2, [0])), ('n', (3, [2, 1]))]

    Restarting from the root:

        >>> list(parsedag(b"+1 $ +1"))
        [('n', (0, [-1])), ('n', (1, [-1]))]

    Annotations, which are meant to introduce sticky state for subsequent nodes:

        >>> list(parsedag(b"+1 @ann +1"))
        [('n', (0, [-1])), ('a', 'ann'), ('n', (1, [0]))]

        >>> list(parsedag(b'+1 @"my annotation" +1'))
        [('n', (0, [-1])), ('a', 'my annotation'), ('n', (1, [0]))]

    Commands, which are meant to operate on the most recently created node:

        >>> list(parsedag(b"+1 !cmd +1"))
        [('n', (0, [-1])), ('c', 'cmd'), ('n', (1, [0]))]

        >>> list(parsedag(b'+1 !"my command" +1'))
        [('n', (0, [-1])), ('c', 'my command'), ('n', (1, [0]))]

        >>> list(parsedag(b'+1 !!my command line\n +1'))
        [('n', (0, [-1])), ('C', 'my command line'), ('n', (1, [0]))]

    Comments, which extend to the end of the line:

        >>> list(parsedag(b'+1 # comment\n+1'))
        [('n', (0, [-1])), ('n', (1, [0]))]

    Error:

        >>> try: list(parsedag(b'+1 bad'))
        ... except Exception as e: print(pycompat.sysstr(bytes(e)))
        invalid character in dag description: bad...

    Nr   c                     | sS | d   t        j                  t        j                        v rt	        |       z
  S |    S )Nr   )r   bytestrstringdigitsint)reflabelsp1rs    5/usr/lib/python3/dist-packages/mercurial/dagparser.pyresolvezparsedag.<locals>.resolve   s>    IVx''66s3x<#;    c                      t         d      S )N    )next)chiters   r   nextchzparsedag.<locals>.nextch   s    FE""r   c                 :    d}| |v r|| z  }        } | |v r| |fS Nr    )callowsr   s      r   nextrunzparsedag.<locals>.nextrun   s3    5jFAA 5j !tr   c                 `    d}| |k7  r| |k(  r        } || z  }        } | |k7  r        |fS r   r   )r   limitescaper    r   s       r   nextdelimitedzparsedag.<locals>.nextdelimited   sD    5jF{HFAA	 5j
 x{r   c                 >    | dk(  r         dd      S  |       S )N   "   \r   )r   r   r%   r!   	wordcharss    r   
nextstringzparsedag.<locals>.nextstring   s(    9 4771i((r   r      .   nr      +s   */   *   /   <   :   l   @   a   !r   s   
    C   c   #   $
   s+   invalid character in dag description: %s...)r   r   r   ascii_lettersr   iterbytestr
whitespacer   rangeappendr   Abortr   )descr   r*   r   digsniprefprefsr   psnametextcmdr    r   r   r   r%   r!   r   r   r)   s                  @@@@@@@@r   parsedagrK      sV    f    !5!5!EFI F	B	A !!$'F#) 	A
u*8##F$5$566A 8##F$5$566 9RD	/!BFAA$Yfh(8(8(GHGAtD	A1a[ QIo%Q %ZDyH mGAtFEt)$VX.4T" t) +003'#,0B0B-BFA$Y)FAsB$Y *GAtF4LT
""$Y *GAt*$YADyHy(1HCA y( Ci#A3Ci$Y9$H 9$$YBA%ZAAu*RQQH u*R ++@AAE I u*6 1s8   CK6CK6'K6+K1:BK6-K6?K6&K6/K6c              #       K   d  fd}d}	 |       D ]E  }
|
dk(  r
|	s|	 d}	t        |	      t        |
      z   |k\  r|	 d}	n|r|	r
|
dk7  r|	dz  }	|	|
z  }	G |	r|	 yyw)z$generates single lines for dagtext()c                     t        j                  d|       r| S d| j                  dd      j                  dd      z   dz   S )Ns   ^[0-9a-z]*$r'   r(   s   \\)rematchreplace)rI   s    r   
wrapstringz dagtextlines.<locals>.wrapstring-  s=    88ND)Kdll5'2::4GG$NNr   c               3   b  K   i } d}d}d}D ]  \  }}|dk(  r:|\  }}||k7  r#t        j                  t        d      ||fz        |sdg}n/|D ]*  }||k\  s	t        j                  t        d      ||fz         |dz  }|dz
  }	t        |      dk(  r$|d   dk(  r|r|r	d|z   d}rd	 d
 d}	nd}t        |      dk(  r|d   |	k(  rrd |dz  }|r	d|z   d}rd	 g }
|D ]I  }||	k(  r|
j	                  d       || v r|
j	                  | |          3|
j	                  d||z
  z         K ddj                  |
      z    G|r	d|z   d}|dk(  r|\  }}|| |<   d|z    sld	 r|dk(  rd |      z    sd	 |dk(  rd|z    d	 |dk(  rrd	 d |      z    |dk(  rd|z    d	 t        j                  t        d      t        j                  |      t        j                  |      fz         |rd|z   y y w)Nr   Fr,   s   expected id %i, got %ir	   s)   parent id %i is larger than current id %ir   s   +%d   
r9   Tr+   r   s   %dr.   r/   r2   r1   r7   r5   r6   s   !!r4   r3   r8   s'   invalid event type in dag: ('%s', '%s'))r   r@   r   lenr?   joinr   	escapestr)r   runwantrneedrootkinddatar   rG   pr   rF   ridrH   eventsusedotswrapannotationswrapcommands
wraplabelswrapnonlinearrQ   s                r   genzdagtextlines.<locals>.gen2  s      U	JD$t|2 :++a(A&BeQZ&OPPB 6"'++ !%5!" $%a&	!)#  
 Ur7a<BqERK"(3,."#C("'K"
#'r7a<BqERK"
q$sl*$#E :7!LL-&[!LL3!LL!a%9: 5!111 3,&C4< $IC"&F3K+%!#T\D!111##T\$,&KT\&#D!111T\+%K++DE&006&006 _U	l 3, s   AH/DH/:H/BH/r   rS   r+       N)rT   )r^   	addspacesrb   r`   ra   rc   r_   maxlinewidthrd   linepartrQ   s   ` `````    @r   dagtextlinesrj   !  s     O
\ \| D 5=
4y3t9$4
tDLD 
 s   "A1AA1c                 D    dj                  t        | |||||||            S )a  generates lines of a textual representation for a dag event stream

    events should generate what parsedag() does, so:

      ('n', (id, [parentids])) for node creation
      ('l', (id, labelname)) for labels on nodes
      ('a', text) for annotations
      ('c', text) for commands
      ('C', text) for line commands ('!!')
      ('#', text) for comment lines

    Parent nodes must come before child nodes.

    Examples
    --------

    Linear run:

        >>> dagtext([(b'n', (0, [-1])), (b'n', (1, [0]))])
        '+2'

    Two roots:

        >>> dagtext([(b'n', (0, [-1])), (b'n', (1, [-1]))])
        '+1 $ +1'

    Fork and join:

        >>> dagtext([(b'n', (0, [-1])), (b'n', (1, [0])), (b'n', (2, [0])),
        ...          (b'n', (3, [2, 1]))])
        '+2 *2 */2'

    Fork and join with labels:

        >>> dagtext([(b'n', (0, [-1])), (b'l', (0, b'f')), (b'n', (1, [0])),
        ...          (b'l', (1, b'p2')), (b'n', (2, [0])), (b'n', (3, [2, 1]))])
        '+1 :f +1 :p2 *f */p2'

    Annotations:

        >>> dagtext([(b'n', (0, [-1])), (b'a', b'ann'), (b'n', (1, [0]))])
        '+1 @ann +1'

        >>> dagtext([(b'n', (0, [-1])),
        ...          (b'a', b'my annotation'),
        ...          (b'n', (1, [0]))])
        '+1 @"my annotation" +1'

    Commands:

        >>> dagtext([(b'n', (0, [-1])), (b'c', b'cmd'), (b'n', (1, [0]))])
        '+1 !cmd +1'

        >>> dagtext([(b'n', (0, [-1])),
        ...          (b'c', b'my command'),
        ...          (b'n', (1, [0]))])
        '+1 !"my command" +1'

        >>> dagtext([(b'n', (0, [-1])),
        ...          (b'C', b'my command line'),
        ...          (b'n', (1, [0]))])
        '+1 !!my command line\n+1'

    Comments:

        >>> dagtext([(b'n', (0, [-1])), (b'#', b' comment'), (b'n', (1, [0]))])
        '+1 # comment\n+1'

        >>> dagtext([])
        ''

    Combining parsedag and dagtext:

        >>> dagtext(parsedag(b'+1 :f +1 :p2 *f */p2'))
        '+1 :f +1 :p2 *f */p2'

    rS   )rU   rj   )dagrf   rb   r`   ra   rc   r_   rg   s           r   dagtextrm     s6    n ::		
 r   )TFFFFFF   )rN   r   i18nr    r   r   utilsr   rK   rj   rm   r   r   r   <module>rr      s\    
   J^ }D br   