comparison mercurial/parser.py @ 28909:edbffdc7f6a0

parser: make _getalias() return (alias, pattern-args) pair This allows us to factor out a function that extracts a function (name, args) pair. See the next patch for why.
author Yuya Nishihara <yuya@tcha.org>
date Tue, 29 Mar 2016 17:21:11 +0900
parents 7a772deffa12
children 1203159c8928
comparison
equal deleted inserted replaced
28908:7a772deffa12 28909:edbffdc7f6a0
471 aliases[a.name] = a 471 aliases[a.name] = a
472 return aliases 472 return aliases
473 473
474 @classmethod 474 @classmethod
475 def _getalias(cls, aliases, tree): 475 def _getalias(cls, aliases, tree):
476 """If tree looks like an unexpanded alias, return it. Return None 476 """If tree looks like an unexpanded alias, return (alias, pattern-args)
477 otherwise. 477 pair. Return None otherwise.
478 """ 478 """
479 if not isinstance(tree, tuple): 479 if not isinstance(tree, tuple):
480 return None 480 return None
481 if tree[0] == cls._symbolnode: 481 if tree[0] == cls._symbolnode:
482 name = tree[1] 482 name = tree[1]
483 a = aliases.get(name) 483 a = aliases.get(name)
484 if a and a.args is None: 484 if a and a.args is None:
485 return a 485 return a, None
486 if tree[0] == cls._funcnode and tree[1][0] == cls._symbolnode: 486 if tree[0] == cls._funcnode and tree[1][0] == cls._symbolnode:
487 name = tree[1][1] 487 name = tree[1][1]
488 a = aliases.get(name) 488 a = aliases.get(name)
489 if a and a.args is not None: 489 if a and a.args is not None:
490 return a 490 return a, cls._getlist(tree[2])
491 return None 491 return None
492 492
493 @classmethod 493 @classmethod
494 def _expandargs(cls, tree, args): 494 def _expandargs(cls, tree, args):
495 """Replace _aliasarg instances with the substitution value of the 495 """Replace _aliasarg instances with the substitution value of the
504 504
505 @classmethod 505 @classmethod
506 def _expand(cls, aliases, tree, expanding, cache): 506 def _expand(cls, aliases, tree, expanding, cache):
507 if not isinstance(tree, tuple): 507 if not isinstance(tree, tuple):
508 return tree 508 return tree
509 a = cls._getalias(aliases, tree) 509 r = cls._getalias(aliases, tree)
510 if a is None: 510 if r is None:
511 return tuple(cls._expand(aliases, t, expanding, cache) 511 return tuple(cls._expand(aliases, t, expanding, cache)
512 for t in tree) 512 for t in tree)
513 a, l = r
513 if a.error: 514 if a.error:
514 raise error.Abort(a.error) 515 raise error.Abort(a.error)
515 if a in expanding: 516 if a in expanding:
516 raise error.ParseError(_('infinite expansion of %(section)s ' 517 raise error.ParseError(_('infinite expansion of %(section)s '
517 '"%(name)s" detected') 518 '"%(name)s" detected')
524 result = cache[a.name] 525 result = cache[a.name]
525 expanding.pop() 526 expanding.pop()
526 if a.args is None: 527 if a.args is None:
527 return result 528 return result
528 # substitute function arguments in replacement tree 529 # substitute function arguments in replacement tree
529 l = cls._getlist(tree[2])
530 if len(l) != len(a.args): 530 if len(l) != len(a.args):
531 raise error.ParseError(_('invalid number of arguments: %d') 531 raise error.ParseError(_('invalid number of arguments: %d')
532 % len(l)) 532 % len(l))
533 l = [cls._expand(aliases, t, [], cache) for t in l] 533 l = [cls._expand(aliases, t, [], cache) for t in l]
534 return cls._expandargs(result, dict(zip(a.args, l))) 534 return cls._expandargs(result, dict(zip(a.args, l)))