^clfddlmZddlmZmZddlmZGddZdZdZ dZ d Z d Z d Z d Zd ZdZddZdZGddZGddZdS))_)errorutil) stringutilcJeZdZd dZdZdZdZd dZd dZd Z d Z d Z dS)parserNc0||_||_d|_dSN) _elements_methodscurrent)selfelementsmethodss 2/usr/lib/python3/dist-packages/mercurial/parser.py__init__zparser.__init__s!  cH|j}t|jd|_|S)zadvance the tokenizerN)r next_iter)rts r_advancezparser._advance"s! LDJ-- rc\t|j|jdddS)z%True if next token may start new termr)anyr r )rs r _hasnewtermzparser._hasnewterm(s&4>$,q/21Q37888rc|jd|kr;tjtd|jdz|jd|dS)z0make sure the tokenizer matches an end conditionrsunexpected token: %sN)r r ParseErrorrr)rms r_matchz parser._match,sZ <?a  ")**T\!_?DD"1%8#9#9E#A3GG GT^DLO4Q777 $  E5# N51!""5ME6 Lu L)9)9);); L~ La$(:(:E!""I(FG&q)<'='='EsKKKT^DLO4Q777 rc~||_||}|j\}}}||fS)z!generate a parse tree from tokens)rrr$r )r tokeniterresr)r*r+s rparsez parser.parseVs:  kkmm LucCxrct|ts|Sj|dfd|ddDS)z4recursively evaluate a parse tree using node methodsrc:g|]}|S)eval).0rrs r zparser.eval..bs#'G'G'G ! 'G'G'GrrN) isinstancetupler )rtrees` rr7z parser.eval^sO$&& K%t}T!W%'G'G'G'Gd122h'G'G'GHHrch||}|jr||S|S)zr6rrrrs  999    .III rrcd}|d\}}}|rY|}|stjdt |dkrtjd|d}|d\}}}|}|}|r*|stjd||d|dd|fSgd||fS) aParse spec of function arguments into (poskeys, varkey, keys, optkey) >>> splitargspec(b'') ([], None, [], None) >>> splitargspec(b'foo bar') ([], None, ['foo', 'bar'], None) >>> splitargspec(b'foo *bar baz **qux') (['foo'], 'bar', ['baz'], 'qux') >>> splitargspec(b'*foo') ([], 'foo', [], None) >>> splitargspec(b'**foo') ([], None, [], 'foo') Ns**sno **optkey name providedrs!excessive **optkey names providedr*sno *varkey name provided) partitionsplitrProgrammingErrorlen)specoptkeypreseppostpostspress r splitargspecrOlsF^^E**NCd   G()EFF F u::>>()MNN Nq]]4((NCd 99;;D JJLLE 1 F()DEE EU1XuQRRy&00 tT6 !!rc|\}}}}tfdt|Dt|} | t|kr4tjt d|t|dz|sg| t|t|zkrDtjt d|t|t|zdzt j} t||d| D] \} } | | | < |r|t| | | |<n0t||t| | D] \} } | | | < |rt j| |<|| dD]} | r| dks| dd|kr&tjt dd |iz| dd} | |vr| } n1|s'tjt d || d z| |} | | vr'tjt d || d z| d | | <| S)aBuild dict from list containing positional and keyword arguments Arguments are specified by a tuple of ``(poskeys, varkey, keys, optkey)`` where - ``poskeys``: list of names of positional arguments - ``varkey``: optional argument name that takes up remainder - ``keys``: list of names that can be either positional or keyword arguments - ``optkey``: optional argument name that takes up excess keyword arguments If ``varkey`` specified, all ``keys`` must be given as keyword arguments. Invalid keywords, too few positional arguments, or too many positional arguments are rejected, but missing keyword arguments are just omitted. c3>K|]\}}||dk|VdSrNr6)r8ix keyvaluenodes r z buildargsdict..s:FFtq!1F11E1E1E1E1E1EFFrs6%(func)s takes at least %(nargs)d positional arguments)funcsnargss5%(func)s takes at most %(nargs)d positional argumentsNrrs %(func)s got an invalid argumentrWs5%(func)s got an unexpected keyword argument '%(key)s')rWskeys;%(func)s got multiple values for keyword argument '%(key)s'r) r enumeraterGrr rrsortdictzip)treesfuncnameargspecrUkeynodeposkeysvarkeykeysrIkwstartargskrTds ` r buildargsdictrfs %,!GVT6FFFFy''FFF E GW G H H CLL99 :     gG s4yy 888 F G G CLL3t99,DEE F    =??DGU8G8_--1Q SYY01V eCII$7899  DAqDGG '}V 788_ AaDL((AaDGw,>,>"566'89LL  aDG 99AA "JKK$a001  V A 66"*%a00 1 t! Krc tj|S#t$r=}tjtj|d}~wwxYwr )r unescapestr ValueErrorrr forcebytestrlower)ses rrhrhsfC%a((( CCC z6q99??AABBB Cs A8AAct|ts+||tj|fdS|d|vrYt tj|dd}||d|dd|fzfdS||d|dzf|ddD]}t|||dz||dd|dddzfg|dd<dS)Nrrs(%s %s) s(%s))r:r;appendrpprintmapjoin _prettyformat)r< leafnodeslevellinesrsrls rrvrvs dE " " ; eZ.t44566666 aI   "DH - - eZ47DIIbMM*BBCDDDDD eVd1g-.///abb : :A !Y 5 9 9 9 9Ry|U2Yq\D%89:bcc rclg}t||d|dd|D}|S)Nr c3,K|]\}}d|z|zVdS)s Nr6)r8lrls rrVzprettyformat..s.::DAqQ::::::r)rvru)r<rwryoutputs r prettyformatrs@ E$ 1e,,, ZZ::E::: : :F Mrct|ts|S|d}|vr'|ftfd|ddDzSg}|}|d|kr>|dd\}}|t||}|d|k>|t|||tt |S)a_Flatten chained infix operations to reduce usage of Python stack >>> from . import pycompat >>> def f(tree): ... s = prettyformat(simplifyinfixops(tree, (b'or',)), (b'symbol',)) ... print(pycompat.sysstr(s)) >>> f((b'or', ... (b'or', ... (b'symbol', b'1'), ... (b'symbol', b'2')), ... (b'symbol', b'3'))) (or (symbol '1') (symbol '2') (symbol '3')) >>> f((b'func', ... (b'symbol', b'p1'), ... (b'or', ... (b'or', ... (b'func', ... (b'symbol', b'sort'), ... (b'list', ... (b'or', ... (b'or', ... (b'symbol', b'1'), ... (b'symbol', b'2')), ... (b'symbol', b'3')), ... (b'negate', ... (b'symbol', b'rev')))), ... (b'and', ... (b'symbol', b'4'), ... (b'group', ... (b'or', ... (b'or', ... (b'symbol', b'5'), ... (b'symbol', b'6')), ... (b'symbol', b'7'))))), ... (b'symbol', b'8')))) (func (symbol 'p1') (or (func (symbol 'sort') (list (or (symbol '1') (symbol '2') (symbol '3')) (negate (symbol 'rev')))) (and (symbol '4') (group (or (symbol '5') (symbol '6') (symbol '7')))) (symbol '8'))) rc38K|]}t|VdSr )simplifyinfixops)r8rT targetnodess rrVz#simplifyinfixops..4s.PP!-a==PPPPPPrrN)r:r;rrrreversed)r<rop simplifiedrTr~rs ` rrrs x dE " " aB uuPPPPtABBxPPPPPPP J A A$"**u1*1k::;;;  A$"**&q+66777b *%% & &&rc|krSt|ts|Stfd|DS)Nc3:K|]}t|VdSr ) _buildtree)r8rT placeholder replstacks rrVz_buildtree..Is/II1A{I66IIIIIIr)popr:r;)templaterrs ``rrrDsZ;}} h & & IIIIIIII I IIrct|tstjdt t |}t |||}|rtjd|S)aCreate new tree by substituting placeholders by replacements >>> _ = (b'symbol', b'_') >>> def f(template, *repls): ... return buildtree(template, _, *repls) >>> f((b'func', (b'symbol', b'only'), (b'list', _, _)), ... ('symbol', '1'), ('symbol', '2')) ('func', ('symbol', 'only'), ('list', ('symbol', '1'), ('symbol', '2'))) >>> f((b'and', _, (b'not', _)), (b'symbol', b'1'), (b'symbol', b'2')) ('and', ('symbol', '1'), ('not', ('symbol', '2'))) placeholder must be a node tuplestoo many replacements)r:r;rrFlistrr)rrreplsrrs r buildtreerLsl k5 ) )J$%HIIIXe__%%I8[)44A?$%=>>> HrcX||krdSt|trt|tsdS|kr!|dvr|dSt|t|krdSt fdt ||DS)NTFrc3DK|]\}}t||VdSr ) _matchtree)r8prTincompletenodesmatchesrs rrVz_matchtree..ksK Aq 1aow??r)r:r;rrrGallrZpatternr<rrrs ```rrras$t gu % %Ze-D-Du+$q'"@"@tt 7||s4yy  u &&  rNr6c|)t|tstjd|g}t |||||r|SdS)a If a tree matches the pattern, return a list of the tree and nodes matched with the placeholder; Otherwise None >>> def f(pattern, tree): ... m = matchtree(pattern, tree, _, {b'keyvalue', b'list'}) ... if m: ... return m[1:] >>> _ = (b'symbol', b'_') >>> f((b'func', (b'symbol', b'ancestors'), _), ... (b'func', (b'symbol', b'ancestors'), (b'symbol', b'1'))) [('symbol', '1')] >>> f((b'func', (b'symbol', b'ancestors'), _), ... (b'func', (b'symbol', b'ancestors'), None)) >>> f((b'range', (b'dagrange', _, _), _), ... (b'range', ... (b'dagrange', (b'symbol', b'1'), (b'symbol', b'2')), ... (b'symbol', b'3'))) [('symbol', '1'), ('symbol', '2'), ('symbol', '3')] The placeholder does not match the specified incomplete nodes because an incomplete node (e.g. argument list) cannot construct an expression. >>> f((b'func', (b'symbol', b'ancestors'), _), ... (b'func', (b'symbol', b'ancestors'), ... (b'list', (b'symbol', b'1'), (b'symbol', b'2')))) The placeholder may be omitted, but which shouldn't match a None node. >>> _ = None >>> f((b'func', (b'symbol', b'ancestors'), None), ... (b'func', (b'symbol', b'ancestors'), (b'symbol', b'0'))) Nr)r:r;rrFrrs r matchtreerqsZDz+u'E'E$%HIIIfG'4owGGrcZ|jtd|j|jfzS|jS)z6Compose error message from specified ParseError objectNs at %d: %s)locationrmessage)insts rparseerrordetailrs, } $-!>>>|rceZdZdZdZdS)aliaszParsed result of aliascL||_||_||_||_d|_dS)NF)namercr replacementwarned)rrrcerrrs rrzalias.__init__s,   & rN)r?r@rA__doc__rr6rrrrs)  rrceZdZdZdZdZdZedZedZ e dZ e dZ e d Z e d Ze d Ze d Ze d Ze dZe dZdS)basealiasrulesawParsing and expansion rule set of aliases This is a helper for fileset/revset/template aliases. A concrete rule set should be made by sub-classing this and implementing class/static methods. It supports alias expansion of symbol and function-call styles:: # decl = defn h = heads(default) b($1) = ancestors($1) - ancestors(default) Nssymbolc0td|jz)Ns'%s' is not instantiatable) TypeErrorr?)clss r__new__zbasealiasrules.__new__s5 DEEErct)z-Parse an alias name, arguments and definitionNotImplementedError)rHs rr$zbasealiasrules._parse "!rct)z9Return (name, args) if tree is a function; otherwise Noner)r<s r _trygetfunczbasealiasrules._trygetfuncrrc |}n.#tj$r}|dt|fcYd}~Sd}~wwxYw|djkr7|d}|dr|dt d|zfS|ddfS|}|r|\}}|dr|dt d|zfStfd|Dr|dt dfSt|tt|kr|dt d fS|d |DdfS|dt d fS) a Parse an alias declaration into ``(name, args, errorstr)`` This function analyzes the parsed tree. The parsing rule is provided by ``_parse()``. - ``name``: of declared alias (may be ``decl`` itself at error) - ``args``: list of argument names (or None for symbol declaration) - ``errorstr``: detail about detected error (or None) >>> sym = lambda x: (b'symbol', x) >>> symlist = lambda *xs: (b'list',) + tuple(sym(x) for x in xs) >>> func = lambda n, a: (b'func', sym(n), a) >>> parsemap = { ... b'foo': sym(b'foo'), ... b'$foo': sym(b'$foo'), ... b'foo::bar': (b'dagrange', sym(b'foo'), sym(b'bar')), ... b'foo()': func(b'foo', None), ... b'$foo()': func(b'$foo', None), ... b'foo($1, $2)': func(b'foo', symlist(b'$1', b'$2')), ... b'foo(bar_bar, baz.baz)': ... func(b'foo', symlist(b'bar_bar', b'baz.baz')), ... b'foo(bar($1, $2))': ... func(b'foo', func(b'bar', symlist(b'$1', b'$2'))), ... b'foo($1, $2, nested($1, $2))': ... func(b'foo', (symlist(b'$1', b'$2') + ... (func(b'nested', symlist(b'$1', b'$2')),))), ... b'foo("bar")': func(b'foo', (b'string', b'bar')), ... b'foo($1, $2': error.ParseError(b'unexpected token: end', 10), ... b'foo("bar': error.ParseError(b'unterminated string', 5), ... b'foo($1, $2, $1)': func(b'foo', symlist(b'$1', b'$2', b'$1')), ... } >>> def parse(expr): ... x = parsemap[expr] ... if isinstance(x, Exception): ... raise x ... return x >>> def trygetfunc(tree): ... if not tree or tree[0] != b'func' or tree[1][0] != b'symbol': ... return None ... if not tree[2]: ... return tree[1][1], [] ... if tree[2][0] == b'list': ... return tree[1][1], list(tree[2][1:]) ... return tree[1][1], [tree[2]] >>> class aliasrules(basealiasrules): ... _parse = staticmethod(parse) ... _trygetfunc = staticmethod(trygetfunc) >>> builddecl = aliasrules._builddecl >>> builddecl(b'foo') ('foo', None, None) >>> builddecl(b'$foo') ('$foo', None, "invalid symbol '$foo'") >>> builddecl(b'foo::bar') ('foo::bar', None, 'invalid format') >>> builddecl(b'foo()') ('foo', [], None) >>> builddecl(b'$foo()') ('$foo()', None, "invalid function '$foo'") >>> builddecl(b'foo($1, $2)') ('foo', ['$1', '$2'], None) >>> builddecl(b'foo(bar_bar, baz.baz)') ('foo', ['bar_bar', 'baz.baz'], None) >>> builddecl(b'foo($1, $2, nested($1, $2))') ('foo($1, $2, nested($1, $2))', None, 'invalid argument list') >>> builddecl(b'foo(bar($1, $2))') ('foo(bar($1, $2))', None, 'invalid argument list') >>> builddecl(b'foo("bar")') ('foo("bar")', None, 'invalid argument list') >>> builddecl(b'foo($1, $2') ('foo($1, $2', None, 'at 10: unexpected token: end') >>> builddecl(b'foo("bar') ('foo("bar', None, 'at 5: unterminated string') >>> builddecl(b'foo($1, $2, $1)') ('foo', None, 'argument names collide with each other') Nrr$invalid symbol '%s'sinvalid function '%s'c3:K|]}|djkVdSrR) _symbolnode)r8rrs rrVz,basealiasrules._builddecl...s.99q1Q43?*999999rsinvalid argument lists&argument names collide with each othercg|] }|d S)rr6)r8rs rr9z-basealiasrules._builddecl..6s...A1Q4...rsinvalid format) r$rr rr startswithrrrrGset)rdeclr<rrfuncrcs` r _builddeclzbasealiasrules._builddeclsZ 8::d##DD 8 8 8$ 0 6 67 7 7 7 7 7 7 8 7co % %7Dt$$ FdA&<$=$=$DEE$% %t$$  6JD$t$$ HdA&>$?$?$$FGG9999D99999 AdA&>$?$?@@4yyCD NN**?@@ .....5 5dA/0011sA?AAclt|ts|S|d}|jkr(|ftfd|ddDzSt|dksJ|d}|vrd}n9|dr$t jtd|z||fS) z%Mark alias arguments as ``_aliasarg``rc3DK|]}|VdSr ) _relabelargs)r8rTrcrs rrVz.basealiasrules._relabelargs..As3 M Mq!1!1!T!:!: M M M M M MrrNr _aliasargrr)r:r;rrGrrr r)rr<rcrsyms` ` rrzbasealiasrules._relabelargs:s$&& K !W  55 M M M M MDH M M MMMM M4yyA~~~~1g $;;BB ^^D ! ! D"1%;#<#>> from . import pycompat >>> parsemap = { ... b'$1 or foo': (b'or', (b'symbol', b'$1'), (b'symbol', b'foo')), ... b'$1 or $bar': ... (b'or', (b'symbol', b'$1'), (b'symbol', b'$bar')), ... b'$10 or baz': ... (b'or', (b'symbol', b'$10'), (b'symbol', b'baz')), ... b'"$1" or "foo"': ... (b'or', (b'string', b'$1'), (b'string', b'foo')), ... } >>> class aliasrules(basealiasrules): ... _parse = staticmethod(parsemap.__getitem__) ... _trygetfunc = staticmethod(lambda x: None) >>> builddefn = aliasrules._builddefn >>> def pprint(tree): ... s = prettyformat(tree, (b'_aliasarg', b'string', b'symbol')) ... print(pycompat.sysstr(s)) >>> args = [b'$1', b'$2', b'foo'] >>> pprint(builddefn(b'$1 or foo', args)) (or (_aliasarg '$1') (_aliasarg 'foo')) >>> try: ... builddefn(b'$1 or $bar', args) ... except error.ParseError as inst: ... print(pycompat.sysstr(parseerrordetail(inst))) invalid symbol '$bar' >>> args = [b'$1', b'$10', b'foo'] >>> pprint(builddefn(b'$10 or baz', args)) (or (_aliasarg '$10') (symbol 'baz')) >>> pprint(builddefn(b'"$1" or "foo"', args)) (or (string '$1') (string 'foo')) )r$rr)rdefnrcr<s r _builddefnzbasealiasrules._builddefnKsJ^zz$  t99DD55Dd+++rcHdx}}||\}}}|rtd}nR |||}n:#tj$r(}t |}td}Yd}~nd}~wwxYw|r||j||dz}t||||S)z>Parse an alias declaration and definition into an alias objectNs4bad declaration of %(section)s "%(name)s": %(error)ss3bad definition of %(section)s "%(name)s": %(error)s)sectionnameserror)rrrrr r_sectionr) rrrreplefmtrrcrrs rbuildzbasealiasrules.buildst....dC  QLMMDD Q~~dD11# Q Q Q&t,,OPP Q  LC T4d+++sAA?A::A?cZi}|D]%\}}|||}|||j<&|S)zTParse a list of alias (name, replacement) pairs into a dict of alias objects)rr)ritemsaliasesrras rbuildmapzbasealiasrules.buildmapsA  JD$ $%%AGAFOOrc&t|tsdS|d|jkr*|d}||}|r |j|dfS||}|r'|\}}||}|r |j||fSdS)zrIf tree looks like an unexpanded alias, return (alias, pattern-args) pair. Return None otherwise. Nrr)r:r;rgetrcr)rrr<rrrrcs r _getaliaszbasealiasrules._getaliass $&& 4 7co % %7D D!!A QV^$wt$$  JD$ D!!A QV'$wtrct|ts|S|ddkr|d}|Stfd|DS)zoReplace _aliasarg instances with the substitution value of the same name in args, recursively. rrrc3DK|]}|VdSr ) _expandargs)r8rrcrs rrVz-basealiasrules._expandargs..s1<.sF>? GQ 599rs5infinite expansion of %(section)s "%(name)s" detected)rrsinvalid number of arguments: %dc@g|]}|gSr6r)r8rrrrs rr9z*basealiasrules._expand..s+ ; ; ;AS[[!R / / ; ; ;r)r:r;rrAbortr rrrrrrrrrcrGrdictrZ) rrr<rrrrr~results `` `` rrzbasealiasrules._expands$&& K MM'4 ( ( 9CG 1 7 '+ag&& & >>"JKK"|af==>   6  KK 5E!&Mqv  6>M q66S[[ "455A>  < ; ; ; ; ; ; ; ;vtCNN';';<<rs(MMMMMMMM`"""DCCCLCCC ; ; ;M'M'M'`JJJ   *    &&&&R        y2y2y2y2y2y2y2y2y2y2r