Mercurial > public > mercurial-scm > hg-stable
comparison mercurial/revset.py @ 28706:b33ca687c1e3
revset: inline isvalidsymbol() and getsymbol() into _parsealiasdecl()
Since I'm going to extract a common alias parser, I want to eliminate
dependencies to the revset parsing rules. These functions are trivial,
so we can go without them.
author | Yuya Nishihara <yuya@tcha.org> |
---|---|
date | Mon, 29 Feb 2016 16:32:18 +0900 |
parents | 0e4148950e19 |
children | af5f90f23515 |
comparison
equal
deleted
inserted
replaced
28705:0e4148950e19 | 28706:b33ca687c1e3 |
---|---|
330 | 330 |
331 def getargsdict(x, funcname, keys): | 331 def getargsdict(x, funcname, keys): |
332 return parser.buildargsdict(getlist(x), funcname, keys.split(), | 332 return parser.buildargsdict(getlist(x), funcname, keys.split(), |
333 keyvaluenode='keyvalue', keynode='symbol') | 333 keyvaluenode='keyvalue', keynode='symbol') |
334 | 334 |
335 def isvalidsymbol(tree): | |
336 """Examine whether specified ``tree`` is valid ``symbol`` or not | |
337 """ | |
338 return tree[0] == 'symbol' | |
339 | |
340 def getsymbol(tree): | |
341 """Get symbol name from valid ``symbol`` in ``tree`` | |
342 | |
343 This assumes that ``tree`` is already examined by ``isvalidsymbol``. | |
344 """ | |
345 return tree[1] | |
346 | |
347 def isvalidfunc(tree): | 335 def isvalidfunc(tree): |
348 """Examine whether specified ``tree`` is valid ``func`` or not | 336 """Examine whether specified ``tree`` is valid ``func`` or not |
349 """ | 337 """ |
350 return tree[0] == 'func' and isvalidsymbol(tree[1]) | 338 return tree[0] == 'func' and tree[1][0] == 'symbol' |
351 | 339 |
352 def getfuncname(tree): | 340 def getfuncname(tree): |
353 """Get function name from valid ``func`` in ``tree`` | 341 """Get function name from valid ``func`` in ``tree`` |
354 | 342 |
355 This assumes that ``tree`` is already examined by ``isvalidfunc``. | 343 This assumes that ``tree`` is already examined by ``isvalidfunc``. |
356 """ | 344 """ |
357 return getsymbol(tree[1]) | 345 return tree[1][1] |
358 | 346 |
359 def getfuncargs(tree): | 347 def getfuncargs(tree): |
360 """Get list of function arguments from valid ``func`` in ``tree`` | 348 """Get list of function arguments from valid ``func`` in ``tree`` |
361 | 349 |
362 This assumes that ``tree`` is already examined by ``isvalidfunc``. | 350 This assumes that ``tree`` is already examined by ``isvalidfunc``. |
2317 tree, pos = p.parse(_tokenizealias(decl)) | 2305 tree, pos = p.parse(_tokenizealias(decl)) |
2318 if (pos != len(decl)): | 2306 if (pos != len(decl)): |
2319 raise error.ParseError(_('invalid token'), pos) | 2307 raise error.ParseError(_('invalid token'), pos) |
2320 tree = parser.simplifyinfixops(tree, ('list',)) | 2308 tree = parser.simplifyinfixops(tree, ('list',)) |
2321 | 2309 |
2322 if isvalidsymbol(tree): | 2310 if tree[0] == 'symbol': |
2323 # "name = ...." style | 2311 # "name = ...." style |
2324 name = getsymbol(tree) | 2312 name = tree[1] |
2325 if name.startswith('$'): | 2313 if name.startswith('$'): |
2326 return (decl, None, None, _("'$' not for alias arguments")) | 2314 return (decl, None, None, _("'$' not for alias arguments")) |
2327 return (name, ('symbol', name), None, None) | 2315 return (name, ('symbol', name), None, None) |
2328 | 2316 |
2329 if isvalidfunc(tree): | 2317 if isvalidfunc(tree): |
2331 name = getfuncname(tree) | 2319 name = getfuncname(tree) |
2332 if name.startswith('$'): | 2320 if name.startswith('$'): |
2333 return (decl, None, None, _("'$' not for alias arguments")) | 2321 return (decl, None, None, _("'$' not for alias arguments")) |
2334 args = [] | 2322 args = [] |
2335 for arg in getfuncargs(tree): | 2323 for arg in getfuncargs(tree): |
2336 if not isvalidsymbol(arg): | 2324 if arg[0] != 'symbol': |
2337 return (decl, None, None, _("invalid argument list")) | 2325 return (decl, None, None, _("invalid argument list")) |
2338 args.append(getsymbol(arg)) | 2326 args.append(arg[1]) |
2339 if len(args) != len(set(args)): | 2327 if len(args) != len(set(args)): |
2340 return (name, None, None, | 2328 return (name, None, None, |
2341 _("argument names collide with each other")) | 2329 _("argument names collide with each other")) |
2342 return (name, ('func', ('symbol', name)), args, None) | 2330 return (name, ('func', ('symbol', name)), args, None) |
2343 | 2331 |