Show More
@@ -232,9 +232,8 b' def parseerrordetail(inst):' | |||
|
232 | 232 | class alias(object): |
|
233 | 233 | """Parsed result of alias""" |
|
234 | 234 | |
|
235 |
def __init__(self, name, |
|
|
235 | def __init__(self, name, args, err, replacement): | |
|
236 | 236 | self.name = name |
|
237 | self.tree = tree | |
|
238 | 237 | self.args = args |
|
239 | 238 | self.error = err |
|
240 | 239 | self.replacement = replacement |
@@ -276,13 +275,12 b' class basealiasrules(object):' | |||
|
276 | 275 | |
|
277 | 276 | @classmethod |
|
278 | 277 | def _builddecl(cls, decl): |
|
279 |
"""Parse an alias declaration into ``(name, |
|
|
278 | """Parse an alias declaration into ``(name, args, errorstr)`` | |
|
280 | 279 | |
|
281 | 280 | This function analyzes the parsed tree. The parsing rule is provided |
|
282 | 281 | by ``_parse()``. |
|
283 | 282 | |
|
284 | 283 | - ``name``: of declared alias (may be ``decl`` itself at error) |
|
285 | - ``tree``: parse result (or ``None`` at error) | |
|
286 | 284 | - ``args``: list of argument names (or None for symbol declaration) |
|
287 | 285 | - ``errorstr``: detail about detected error (or None) |
|
288 | 286 | |
@@ -324,60 +322,59 b' class basealiasrules(object):' | |||
|
324 | 322 | ... _getlist = staticmethod(getlist) |
|
325 | 323 | >>> builddecl = aliasrules._builddecl |
|
326 | 324 | >>> builddecl('foo') |
|
327 |
('foo', |
|
|
325 | ('foo', None, None) | |
|
328 | 326 | >>> builddecl('$foo') |
|
329 |
('$foo', None, |
|
|
327 | ('$foo', None, "'$' not for alias arguments") | |
|
330 | 328 | >>> builddecl('foo::bar') |
|
331 |
('foo::bar', None, |
|
|
329 | ('foo::bar', None, 'invalid format') | |
|
332 | 330 | >>> builddecl('foo()') |
|
333 |
('foo', |
|
|
331 | ('foo', [], None) | |
|
334 | 332 | >>> builddecl('$foo()') |
|
335 |
('$foo()', None, |
|
|
333 | ('$foo()', None, "'$' not for alias arguments") | |
|
336 | 334 | >>> builddecl('foo($1, $2)') |
|
337 |
('foo', |
|
|
335 | ('foo', ['$1', '$2'], None) | |
|
338 | 336 | >>> builddecl('foo(bar_bar, baz.baz)') |
|
339 |
('foo', |
|
|
337 | ('foo', ['bar_bar', 'baz.baz'], None) | |
|
340 | 338 | >>> builddecl('foo($1, $2, nested($1, $2))') |
|
341 |
('foo($1, $2, nested($1, $2))', None, |
|
|
339 | ('foo($1, $2, nested($1, $2))', None, 'invalid argument list') | |
|
342 | 340 | >>> builddecl('foo(bar($1, $2))') |
|
343 |
('foo(bar($1, $2))', None, |
|
|
341 | ('foo(bar($1, $2))', None, 'invalid argument list') | |
|
344 | 342 | >>> builddecl('foo("bar")') |
|
345 |
('foo("bar")', None, |
|
|
343 | ('foo("bar")', None, 'invalid argument list') | |
|
346 | 344 | >>> builddecl('foo($1, $2') |
|
347 |
('foo($1, $2', None, |
|
|
345 | ('foo($1, $2', None, 'at 10: unexpected token: end') | |
|
348 | 346 | >>> builddecl('foo("bar') |
|
349 |
('foo("bar', None, |
|
|
347 | ('foo("bar', None, 'at 5: unterminated string') | |
|
350 | 348 | >>> builddecl('foo($1, $2, $1)') |
|
351 |
('foo', None, |
|
|
349 | ('foo', None, 'argument names collide with each other') | |
|
352 | 350 | """ |
|
353 | 351 | try: |
|
354 | 352 | tree = cls._parse(decl) |
|
355 | 353 | except error.ParseError as inst: |
|
356 |
return (decl, None, |
|
|
354 | return (decl, None, parseerrordetail(inst)) | |
|
357 | 355 | |
|
358 | 356 | if tree[0] == cls._symbolnode: |
|
359 | 357 | # "name = ...." style |
|
360 | 358 | name = tree[1] |
|
361 | 359 | if name.startswith('$'): |
|
362 |
return (decl |
|
|
363 |
return (name, |
|
|
360 | return (decl, None, _("'$' not for alias arguments")) | |
|
361 | return (name, None, None) | |
|
364 | 362 | |
|
365 | 363 | if tree[0] == cls._funcnode and tree[1][0] == cls._symbolnode: |
|
366 | 364 | # "name(arg, ....) = ...." style |
|
367 | 365 | name = tree[1][1] |
|
368 | 366 | if name.startswith('$'): |
|
369 |
return (decl |
|
|
367 | return (decl, None, _("'$' not for alias arguments")) | |
|
370 | 368 | args = [] |
|
371 | 369 | for arg in cls._getlist(tree[2]): |
|
372 | 370 | if arg[0] != cls._symbolnode: |
|
373 |
return (decl |
|
|
371 | return (decl, None, _("invalid argument list")) | |
|
374 | 372 | args.append(arg[1]) |
|
375 | 373 | if len(args) != len(set(args)): |
|
376 |
return (name, None, |
|
|
377 | _("argument names collide with each other")) | |
|
378 | return (name, tree[:2], args, None) | |
|
374 | return (name, None, _("argument names collide with each other")) | |
|
375 | return (name, args, None) | |
|
379 | 376 | |
|
380 |
return (decl |
|
|
377 | return (decl, None, _("invalid format")) | |
|
381 | 378 | |
|
382 | 379 | @classmethod |
|
383 | 380 | def _relabelargs(cls, tree, args): |
@@ -449,7 +446,7 b' class basealiasrules(object):' | |||
|
449 | 446 | def build(cls, decl, defn): |
|
450 | 447 | """Parse an alias declaration and definition into an alias object""" |
|
451 | 448 | repl = efmt = None |
|
452 |
name |
|
|
449 | name, args, err = cls._builddecl(decl) | |
|
453 | 450 | if err: |
|
454 | 451 | efmt = _('failed to parse the declaration of %(section)s ' |
|
455 | 452 | '"%(name)s": %(error)s') |
@@ -462,7 +459,7 b' class basealiasrules(object):' | |||
|
462 | 459 | '"%(name)s": %(error)s') |
|
463 | 460 | if err: |
|
464 | 461 | err = efmt % {'section': cls._section, 'name': name, 'error': err} |
|
465 |
return alias(name |
|
|
462 | return alias(name, args, err, repl) | |
|
466 | 463 | |
|
467 | 464 | @classmethod |
|
468 | 465 | def buildmap(cls, items): |
@@ -484,12 +481,12 b' class basealiasrules(object):' | |||
|
484 | 481 | if tree[0] == cls._symbolnode: |
|
485 | 482 | name = tree[1] |
|
486 | 483 | a = aliases.get(name) |
|
487 |
if a and a.args is None |
|
|
484 | if a and a.args is None: | |
|
488 | 485 | return a |
|
489 | 486 | if tree[0] == cls._funcnode and tree[1][0] == cls._symbolnode: |
|
490 | 487 | name = tree[1][1] |
|
491 | 488 | a = aliases.get(name) |
|
492 |
if a and a.args is not None |
|
|
489 | if a and a.args is not None: | |
|
493 | 490 | return a |
|
494 | 491 | return None |
|
495 | 492 |
General Comments 0
You need to be logged in to leave comments.
Login now