comparison mercurial/parser.py @ 28892:0c135f37c6f8

parser: construct alias object by rule-set class It was odd that the revsetalias did the whole parsing stuff in __init__(). Instead, this patch adds a factory function to the aliasrules class, and makes the alias (= revsetalias) class a plain-old value object.
author Yuya Nishihara <yuya@tcha.org>
date Mon, 29 Feb 2016 18:33:30 +0900
parents 2e9f5453ab5a
children ee11167fe1da
comparison
equal deleted inserted replaced
28891:ac30adb260ea 28892:0c135f37c6f8
227 if len(inst.args) > 1: 227 if len(inst.args) > 1:
228 return _('at %s: %s') % (inst.args[1], inst.args[0]) 228 return _('at %s: %s') % (inst.args[1], inst.args[0])
229 else: 229 else:
230 return inst.args[0] 230 return inst.args[0]
231 231
232 class alias(object):
233 """Parsed result of alias"""
234
235 def __init__(self, name, tree, args, err, replacement):
236 self.name = name
237 self.tree = tree
238 self.args = args
239 self.error = err
240 self.replacement = replacement
241 # whether own `error` information is already shown or not.
242 # this avoids showing same warning multiple times at each `findaliases`.
243 self.warned = False
244
232 class basealiasrules(object): 245 class basealiasrules(object):
233 """Parsing and expansion rule set of aliases 246 """Parsing and expansion rule set of aliases
234 247
235 This is a helper for fileset/revset/template aliases. A concrete rule set 248 This is a helper for fileset/revset/template aliases. A concrete rule set
236 should be made by sub-classing this and implementing class/static methods. 249 should be made by sub-classing this and implementing class/static methods.
428 if args: 441 if args:
429 args = set(args) 442 args = set(args)
430 else: 443 else:
431 args = set() 444 args = set()
432 return cls._relabelargs(tree, args) 445 return cls._relabelargs(tree, args)
446
447 @classmethod
448 def build(cls, decl, defn):
449 """Parse an alias declaration and definition into an alias object"""
450 repl = efmt = None
451 name, tree, args, err = cls._builddecl(decl)
452 if err:
453 efmt = _('failed to parse the declaration of %(section)s '
454 '"%(name)s": %(error)s')
455 else:
456 try:
457 repl = cls._builddefn(defn, args)
458 except error.ParseError as inst:
459 err = parseerrordetail(inst)
460 efmt = _('failed to parse the definition of %(section)s '
461 '"%(name)s": %(error)s')
462 if err:
463 err = efmt % {'section': cls._section, 'name': name, 'error': err}
464 return alias(name, tree, args, err, repl)