From patchwork Sat Apr 2 08:30:22 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: [7, of, 7, V2] parser: unify parser function of alias declaration and definition From: Yuya Nishihara X-Patchwork-Id: 14254 Message-Id: <84e713d2ce321e54c89f.1459585822@mimosa> To: mercurial-devel@mercurial-scm.org Date: Sat, 02 Apr 2016 17:30:22 +0900 # HG changeset patch # User Yuya Nishihara # Date 1459177705 -32400 # Tue Mar 29 00:08:25 2016 +0900 # Node ID 84e713d2ce321e54c89f2623ffbbcc461651a565 # Parent 57f4e13ef151c728a9c7245b0918fdc096cfd4c8 parser: unify parser function of alias declaration and definition We no longer have to keep them separately. diff --git a/mercurial/parser.py b/mercurial/parser.py --- a/mercurial/parser.py +++ b/mercurial/parser.py @@ -241,16 +241,14 @@ class aliasrules(object): - ``section`` is typically a config section name, which will be included in error messages - - ``parsedecl(spec)`` parses an alias name and arguments - - ``parsedefn(spec)`` parses an alias definition + - ``parse(spec)`` parses an alias name, arguments and definition - ``getlist(tree)`` extracts a list of arguments from parsed tree """ - def __init__(self, section, parsedecl, parsedefn, getlist, + def __init__(self, section, parse, getlist, symbolnode='symbol', funcnode='func'): self._section = section - self._parsedecl = parsedecl - self._parsedefn = parsedefn + self._parse = parse self._getlist = getlist self._symbolnode = symbolnode self._funcnode = funcnode @@ -259,7 +257,7 @@ class aliasrules(object): """Parse an alias declaration into ``(name, tree, args, errorstr)`` This function analyzes the parsed tree. The parsing rule is provided - by ``_parsedecl()``. + by ``_parse()``. - ``name``: of declared alias (may be ``decl`` itself at error) - ``tree``: parse result (or ``None`` at error) @@ -299,7 +297,7 @@ class aliasrules(object): ... if tree[0] == 'list': ... return list(tree[1:]) ... return [tree] - >>> builddecl = aliasrules('', parse, parse, getlist)._builddecl + >>> builddecl = aliasrules('', parse, getlist)._builddecl >>> builddecl('foo') ('foo', ('symbol', 'foo'), None, None) >>> builddecl('$foo') @@ -328,7 +326,7 @@ class aliasrules(object): ('foo', None, None, 'argument names collide with each other') """ try: - tree = self._parsedecl(decl) + tree = self._parse(decl) except error.ParseError as inst: return (decl, None, None, parseerrordetail(inst)) @@ -376,7 +374,7 @@ class aliasrules(object): """Parse an alias definition into a tree and marks substitutions This function marks alias argument references as ``_aliasarg``. The - parsing rule is provided by ``_parsedefn()``. + parsing rule is provided by ``_parse()``. ``args`` is a list of alias argument names, or None if the alias is declared as a symbol. @@ -388,7 +386,7 @@ class aliasrules(object): ... '"$1" or "foo"': ('or', ('string', '$1'), ('string', 'foo')), ... } >>> parse = parsemap.__getitem__ - >>> builddefn = aliasrules('', parse, parse, lambda x: [])._builddefn + >>> builddefn = aliasrules('', parse, lambda x: [])._builddefn >>> def pprint(tree): ... print prettyformat(tree, ('_aliasarg', 'string', 'symbol')) >>> args = ['$1', '$2', 'foo'] @@ -411,7 +409,7 @@ class aliasrules(object): ('string', '$1') ('string', 'foo')) """ - tree = self._parsedefn(defn) + tree = self._parse(defn) if args: args = set(args) else: diff --git a/mercurial/revset.py b/mercurial/revset.py --- a/mercurial/revset.py +++ b/mercurial/revset.py @@ -2265,8 +2265,7 @@ class revsetalias(object): h = heads(default) b($1) = ancestors($1) - ancestors(default) ''' - rules = parser.aliasrules(_('revset alias'), - _parsealias, _parsealias, getlist) + rules = parser.aliasrules(_('revset alias'), _parsealias, getlist) self.name, self.tree, self.args, self.error = rules._builddecl(name) if self.error: self.error = _('failed to parse the declaration of revset alias'