comparison mercurial/parser.py @ 25705:48919d246a47

revset: add function to build dict of positional and keyword arguments Keyword arguments will be convenient for functions that will take more than one optional or boolean flags. For example, file(pattern[, subrepos=false]) subrepo([[pattern], status]) Because I don't think all functions should accept key=value syntax, getkwargs() does not support variadic functions such as 'ancestor(*changeset)'. The core logic is placed in the parser module because keyword arguments will be more useful in the templater, where functions take more options. Test cases will be added by the next patch.
author Yuya Nishihara <yuya@tcha.org>
date Sat, 27 Jun 2015 17:25:01 +0900
parents b8b73652c1c9
children 272ff3680bf3
comparison
equal deleted inserted replaced
25704:70a2082f855a 25705:48919d246a47
89 t = self.parse(tokeniter) 89 t = self.parse(tokeniter)
90 if self._methods: 90 if self._methods:
91 return self.eval(t) 91 return self.eval(t)
92 return t 92 return t
93 93
94 def buildargsdict(trees, funcname, keys, keyvaluenode, keynode):
95 """Build dict from list containing positional and keyword arguments
96
97 Invalid keywords or too many positional arguments are rejected, but
98 missing arguments are just omitted.
99 """
100 if len(trees) > len(keys):
101 raise error.ParseError(_("%(func)s takes at most %(nargs)d arguments")
102 % {'func': funcname, 'nargs': len(keys)})
103 args = {}
104 # consume positional arguments
105 for k, x in zip(keys, trees):
106 if x[0] == keyvaluenode:
107 break
108 args[k] = x
109 # remainder should be keyword arguments
110 for x in trees[len(args):]:
111 if x[0] != keyvaluenode or x[1][0] != keynode:
112 raise error.ParseError(_("%(func)s got an invalid argument")
113 % {'func': funcname})
114 k = x[1][1]
115 if k not in keys:
116 raise error.ParseError(_("%(func)s got an unexpected keyword "
117 "argument '%(key)s'")
118 % {'func': funcname, 'key': k})
119 if k in args:
120 raise error.ParseError(_("%(func)s got multiple values for keyword "
121 "argument '%(key)s'")
122 % {'func': funcname, 'key': k})
123 args[k] = x[2]
124 return args
125
94 def _prettyformat(tree, leafnodes, level, lines): 126 def _prettyformat(tree, leafnodes, level, lines):
95 if not isinstance(tree, tuple) or tree[0] in leafnodes: 127 if not isinstance(tree, tuple) or tree[0] in leafnodes:
96 lines.append((level, str(tree))) 128 lines.append((level, str(tree)))
97 else: 129 else:
98 lines.append((level, '(%s' % tree[0])) 130 lines.append((level, '(%s' % tree[0]))