##// END OF EJS Templates
revset: extract parsing logic out of formatspec...
Boris Feld -
r41255:e5b227f4 default
parent child Browse files
Show More
@@ -666,6 +666,23 b' def formatspec(expr, *args):'
666 666 >>> formatspec(b'%ls', [b'a', b"'"])
667 667 "_list('a\\\\x00\\\\'')"
668 668 '''
669 parsed = _parseargs(expr, args)
670 ret = []
671 for t, arg in parsed:
672 if t is None:
673 ret.append(arg)
674 else:
675 raise error.ProgrammingError("unknown revspec item type: %r" % t)
676 return b''.join(ret)
677
678 def _parseargs(expr, args):
679 """parse the expression and replace all inexpensive args
680
681 return a list of tuple [(arg-type, arg-value)]
682
683 Arg-type can be:
684 * None: a string ready to be concatenated into a final spec
685 """
669 686 expr = pycompat.bytestr(expr)
670 687 argiter = iter(args)
671 688 ret = []
@@ -673,16 +690,16 b' def formatspec(expr, *args):'
673 690 while pos < len(expr):
674 691 q = expr.find('%', pos)
675 692 if q < 0:
676 ret.append(expr[pos:])
693 ret.append((None, expr[pos:]))
677 694 break
678 ret.append(expr[pos:q])
695 ret.append((None, expr[pos:q]))
679 696 pos = q + 1
680 697 try:
681 698 d = expr[pos]
682 699 except IndexError:
683 700 raise error.ParseError(_('incomplete revspec format character'))
684 701 if d == '%':
685 ret.append(d)
702 ret.append((None, d))
686 703 pos += 1
687 704 continue
688 705
@@ -692,19 +709,20 b' def formatspec(expr, *args):'
692 709 raise error.ParseError(_('missing argument for revspec'))
693 710 f = _formatlistfuncs.get(d)
694 711 if f:
695 # a list of some type
712 # a list of some type, might be expensive, do not replace
696 713 pos += 1
697 714 try:
698 715 d = expr[pos]
699 716 except IndexError:
700 717 raise error.ParseError(_('incomplete revspec format character'))
701 718 try:
702 ret.append(f(list(arg), d))
719 ret.append((None, f(list(arg), d)))
703 720 except (TypeError, ValueError):
704 721 raise error.ParseError(_('invalid argument for revspec'))
705 722 else:
723 # a single entry, not expensive, replace
706 724 try:
707 ret.append(_formatargtype(d, arg))
725 ret.append((None, _formatargtype(d, arg)))
708 726 except (TypeError, ValueError):
709 727 raise error.ParseError(_('invalid argument for revspec'))
710 728 pos += 1
@@ -714,7 +732,7 b' def formatspec(expr, *args):'
714 732 raise error.ParseError(_('too many revspec arguments specified'))
715 733 except StopIteration:
716 734 pass
717 return ''.join(ret)
735 return ret
718 736
719 737 def prettyformat(tree):
720 738 return parser.prettyformat(tree, ('string', 'symbol'))
General Comments 0
You need to be logged in to leave comments. Login now