##// END OF EJS Templates
revset: fix aliases with 0 or more than 2 parameters...
Mads Kiilerich -
r14723:b9faf94e stable
parent child Browse files
Show More
@@ -954,70 +954,46 b' def optimize(x, small):'
954 954
955 955 class revsetalias(object):
956 956 funcre = re.compile('^([^(]+)\(([^)]+)\)$')
957 args = ()
957 args = None
958 958
959 def __init__(self, token, value):
959 def __init__(self, name, value):
960 960 '''Aliases like:
961 961
962 962 h = heads(default)
963 963 b($1) = ancestors($1) - ancestors(default)
964 964 '''
965 if isinstance(token, tuple):
966 self.type, self.name = token
967 else:
968 m = self.funcre.search(token)
965 if isinstance(name, tuple): # parameter substitution
966 self.tree = name
967 self.replacement = value
968 else: # alias definition
969 m = self.funcre.search(name)
969 970 if m:
970 self.type = 'func'
971 self.name = m.group(1)
971 self.tree = ('func', ('symbol', m.group(1)))
972 972 self.args = [x.strip() for x in m.group(2).split(',')]
973 for arg in self.args:
974 value = value.replace(arg, repr(arg))
973 975 else:
974 self.type = 'symbol'
975 self.name = token
976 self.tree = ('symbol', name)
976 977
977 if isinstance(value, str):
978 for arg in self.args:
979 value = value.replace(arg, repr(arg))
980 978 self.replacement, pos = parse(value)
981 979 if pos != len(value):
982 980 raise error.ParseError(_('invalid token'), pos)
983 else:
984 self.replacement = value
985
986 def match(self, tree):
987 if not tree:
988 return False
989 if tree == (self.type, self.name):
990 return True
991 if tree[0] != self.type:
992 return False
993 if len(tree) > 1 and tree[1] != ('symbol', self.name):
994 return False
995 # 'func' + funcname + args
996 if ((self.args and len(tree) != 3) or
997 (len(self.args) == 1 and tree[2][0] == 'list') or
998 (len(self.args) > 1 and (tree[2][0] != 'list' or
999 len(tree[2]) - 1 != len(self.args)))):
1000 raise error.ParseError(_('invalid amount of arguments'),
1001 len(tree) - 2)
1002 return True
1003
1004 def replace(self, tree):
1005 if tree == (self.type, self.name):
1006 return self.replacement
1007 result = self.replacement
1008 def getsubtree(i):
1009 if tree[2][0] == 'list':
1010 return tree[2][i + 1]
1011 return tree[i + 2]
1012 for i, v in enumerate(self.args):
1013 valalias = revsetalias(('string', v), getsubtree(i))
1014 result = valalias.process(result)
1015 return result
1016 981
1017 982 def process(self, tree):
1018 if self.match(tree):
1019 return self.replace(tree)
1020 983 if isinstance(tree, tuple):
984 if self.args is None:
985 if tree == self.tree:
986 return self.replacement
987 elif tree[:2] == self.tree:
988 l = getlist(tree[2])
989 if len(l) != len(self.args):
990 raise error.ParseError(
991 _('invalid number of arguments: %s') % len(l))
992 result = self.replacement
993 for a, v in zip(self.args, l):
994 valalias = revsetalias(('string', a), v)
995 result = valalias.process(result)
996 return result
1021 997 return tuple(map(self.process, tree))
1022 998 return tree
1023 999
@@ -420,6 +420,7 b' aliases:'
420 420 $ echo 'm = merge()' >> .hg/hgrc
421 421 $ echo 'd($1) = reverse(sort($1, date))' >> .hg/hgrc
422 422 $ echo 'rs(ARG1, ARG2) = reverse(sort(ARG1, ARG2))' >> .hg/hgrc
423 $ echo 'rs4(ARG1, ARGA, ARGB, ARG2) = reverse(sort(ARG1, ARG2))' >> .hg/hgrc
423 424
424 425 $ try m
425 426 ('symbol', 'm')
@@ -437,6 +438,23 b' aliases:'
437 438 ('func', ('symbol', 'reverse'), ('func', ('symbol', 'sort'), ('list', ('or', ('symbol', '2'), ('symbol', '3')), ('symbol', 'date'))))
438 439 3
439 440 2
441 $ try 'rs()'
442 ('func', ('symbol', 'rs'), None)
443 hg: parse error: invalid number of arguments: 0
444 [255]
445 $ try 'rs(2)'
446 ('func', ('symbol', 'rs'), ('symbol', '2'))
447 hg: parse error: invalid number of arguments: 1
448 [255]
449 $ try 'rs(2, data, 7)'
450 ('func', ('symbol', 'rs'), ('list', ('list', ('symbol', '2'), ('symbol', 'data')), ('symbol', '7')))
451 hg: parse error: invalid number of arguments: 3
452 [255]
453 $ try 'rs4(2 or 3, x, x, date)'
454 ('func', ('symbol', 'rs4'), ('list', ('list', ('list', ('or', ('symbol', '2'), ('symbol', '3')), ('symbol', 'x')), ('symbol', 'x')), ('symbol', 'date')))
455 ('func', ('symbol', 'reverse'), ('func', ('symbol', 'sort'), ('list', ('or', ('symbol', '2'), ('symbol', '3')), ('symbol', 'date'))))
456 3
457 2
440 458
441 459 issue2549 - correct optimizations
442 460
General Comments 0
You need to be logged in to leave comments. Login now