Mercurial > public > mercurial-scm > hg-stable
comparison mercurial/parser.py @ 28873:2ca3b7c563f3
parser: move alias definition parser to common rule-set class
The original _parsealiasdefn() function is split into common _builddefn()
and revset-specific _parsealiasdefn(). revset._relabelaliasargs() is removed
as it is no longer used.
The doctests are ported by using the dummy parse().
author | Yuya Nishihara <yuya@tcha.org> |
---|---|
date | Mon, 29 Feb 2016 18:10:07 +0900 |
parents | 5f31d2248745 |
children | 2e9f5453ab5a |
comparison
equal
deleted
inserted
replaced
28872:5f31d2248745 | 28873:2ca3b7c563f3 |
---|---|
384 if sym in args: | 384 if sym in args: |
385 op = '_aliasarg' | 385 op = '_aliasarg' |
386 elif sym.startswith('$'): | 386 elif sym.startswith('$'): |
387 raise error.ParseError(_("'$' not for alias arguments")) | 387 raise error.ParseError(_("'$' not for alias arguments")) |
388 return (op, sym) | 388 return (op, sym) |
389 | |
390 @classmethod | |
391 def _builddefn(cls, defn, args): | |
392 """Parse an alias definition into a tree and marks substitutions | |
393 | |
394 This function marks alias argument references as ``_aliasarg``. The | |
395 parsing rule is provided by ``_parsedefn()``. | |
396 | |
397 ``args`` is a list of alias argument names, or None if the alias | |
398 is declared as a symbol. | |
399 | |
400 >>> parsemap = { | |
401 ... '$1 or foo': ('or', ('symbol', '$1'), ('symbol', 'foo')), | |
402 ... '$1 or $bar': ('or', ('symbol', '$1'), ('symbol', '$bar')), | |
403 ... '$10 or baz': ('or', ('symbol', '$10'), ('symbol', 'baz')), | |
404 ... '"$1" or "foo"': ('or', ('string', '$1'), ('string', 'foo')), | |
405 ... } | |
406 >>> class aliasrules(basealiasrules): | |
407 ... _parsedefn = staticmethod(parsemap.__getitem__) | |
408 ... _getlist = staticmethod(lambda x: []) | |
409 >>> builddefn = aliasrules._builddefn | |
410 >>> def pprint(tree): | |
411 ... print prettyformat(tree, ('_aliasarg', 'string', 'symbol')) | |
412 >>> args = ['$1', '$2', 'foo'] | |
413 >>> pprint(builddefn('$1 or foo', args)) | |
414 (or | |
415 ('_aliasarg', '$1') | |
416 ('_aliasarg', 'foo')) | |
417 >>> try: | |
418 ... builddefn('$1 or $bar', args) | |
419 ... except error.ParseError as inst: | |
420 ... print parseerrordetail(inst) | |
421 '$' not for alias arguments | |
422 >>> args = ['$1', '$10', 'foo'] | |
423 >>> pprint(builddefn('$10 or baz', args)) | |
424 (or | |
425 ('_aliasarg', '$10') | |
426 ('symbol', 'baz')) | |
427 >>> pprint(builddefn('"$1" or "foo"', args)) | |
428 (or | |
429 ('string', '$1') | |
430 ('string', 'foo')) | |
431 """ | |
432 tree = cls._parsedefn(defn) | |
433 if args: | |
434 args = set(args) | |
435 else: | |
436 args = set() | |
437 return cls._relabelargs(tree, args) |