##// END OF EJS Templates
parser: move alias definition parser to common rule-set class...
Yuya Nishihara -
r28873:2ca3b7c5 default
parent child Browse files
Show More
@@ -386,3 +386,52 b' class basealiasrules(object):'
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)
@@ -2250,57 +2250,19 b' def _parsealiasdecl(decl):'
2250 raise error.ParseError(_('invalid token'), pos)
2250 raise error.ParseError(_('invalid token'), pos)
2251 return parser.simplifyinfixops(tree, ('list',))
2251 return parser.simplifyinfixops(tree, ('list',))
2252
2252
2253 def _relabelaliasargs(tree, args):
2253 def _parsealiasdefn(defn):
2254 return _aliasrules._relabelargs(tree, args)
2254 """Parse alias definition ``defn``"""
2255
2256 def _parsealiasdefn(defn, args):
2257 """Parse alias definition ``defn``
2258
2259 This function marks alias argument references as ``_aliasarg``.
2260
2261 ``args`` is a list of alias argument names, or None if the alias
2262 is declared as a symbol.
2263
2264 This returns "tree" as parsing result.
2265
2266 >>> def prettyformat(tree):
2267 ... return parser.prettyformat(tree, ('_aliasarg', 'string', 'symbol'))
2268 >>> args = ['$1', '$2', 'foo']
2269 >>> print prettyformat(_parsealiasdefn('$1 or foo', args))
2270 (or
2271 ('_aliasarg', '$1')
2272 ('_aliasarg', 'foo'))
2273 >>> try:
2274 ... _parsealiasdefn('$1 or $bar', args)
2275 ... except error.ParseError, inst:
2276 ... print parser.parseerrordetail(inst)
2277 '$' not for alias arguments
2278 >>> args = ['$1', '$10', 'foo']
2279 >>> print prettyformat(_parsealiasdefn('$10 or foobar', args))
2280 (or
2281 ('_aliasarg', '$10')
2282 ('symbol', 'foobar'))
2283 >>> print prettyformat(_parsealiasdefn('"$1" or "foo"', args))
2284 (or
2285 ('string', '$1')
2286 ('string', 'foo'))
2287 """
2288 if args:
2289 args = set(args)
2290 else:
2291 args = set()
2292
2293 p = parser.parser(elements)
2255 p = parser.parser(elements)
2294 tree, pos = p.parse(_tokenizealias(defn))
2256 tree, pos = p.parse(_tokenizealias(defn))
2295 if pos != len(defn):
2257 if pos != len(defn):
2296 raise error.ParseError(_('invalid token'), pos)
2258 raise error.ParseError(_('invalid token'), pos)
2297 tree = parser.simplifyinfixops(tree, ('list', 'or'))
2259 return parser.simplifyinfixops(tree, ('list', 'or'))
2298 return _relabelaliasargs(tree, args)
2299
2260
2300 class _aliasrules(parser.basealiasrules):
2261 class _aliasrules(parser.basealiasrules):
2301 """Parsing and expansion rule set of revset aliases"""
2262 """Parsing and expansion rule set of revset aliases"""
2302 _section = _('revset alias')
2263 _section = _('revset alias')
2303 _parsedecl = staticmethod(_parsealiasdecl)
2264 _parsedecl = staticmethod(_parsealiasdecl)
2265 _parsedefn = staticmethod(_parsealiasdefn)
2304 _getlist = staticmethod(getlist)
2266 _getlist = staticmethod(getlist)
2305
2267
2306 class revsetalias(object):
2268 class revsetalias(object):
@@ -2322,7 +2284,7 b' class revsetalias(object):'
2322 return
2284 return
2323
2285
2324 try:
2286 try:
2325 self.replacement = _parsealiasdefn(value, self.args)
2287 self.replacement = _aliasrules._builddefn(value, self.args)
2326 except error.ParseError as inst:
2288 except error.ParseError as inst:
2327 self.error = _('failed to parse the definition of revset alias'
2289 self.error = _('failed to parse the definition of revset alias'
2328 ' "%s": %s') % (self.name,
2290 ' "%s": %s') % (self.name,
General Comments 0
You need to be logged in to leave comments. Login now