revsetlang.py
941 lines
| 28.4 KiB
| text/x-python
|
PythonLexer
/ mercurial / revsetlang.py
Yuya Nishihara
|
r31024 | # revsetlang.py - parser, tokenizer and utility for revision set language | ||
# | ||||
Raphaël Gomès
|
r47575 | # Copyright 2010 Olivia Mackall <olivia@selenic.com> | ||
Yuya Nishihara
|
r31024 | # | ||
# This software may be used and distributed according to the terms of the | ||||
# GNU General Public License version 2 or any later version. | ||||
import string | ||||
from .i18n import _ | ||||
Gregory Szorc
|
r43359 | from .pycompat import getattr | ||
Joerg Sonnenberger
|
r46729 | from .node import hex | ||
Yuya Nishihara
|
r31024 | from . import ( | ||
error, | ||||
parser, | ||||
pycompat, | ||||
Boris Feld
|
r41257 | smartset, | ||
Augie Fackler
|
r31606 | util, | ||
Yuya Nishihara
|
r31024 | ) | ||
Augie Fackler
|
r43346 | from .utils import stringutil | ||
Yuya Nishihara
|
r31024 | |||
elements = { | ||||
# token-type: binding-strength, primary, prefix, infix, suffix | ||||
Augie Fackler
|
r43347 | b"(": (21, None, (b"group", 1, b")"), (b"func", 1, b")"), None), | ||
b"[": (21, None, None, (b"subscript", 1, b"]"), None), | ||||
b"#": (21, None, None, (b"relation", 21), None), | ||||
b"##": (20, None, None, (b"_concat", 20), None), | ||||
b"~": (18, None, None, (b"ancestor", 18), None), | ||||
b"^": (18, None, None, (b"parent", 18), b"parentpost"), | ||||
b"-": (5, None, (b"negate", 19), (b"minus", 5), None), | ||||
b"::": ( | ||||
Augie Fackler
|
r43346 | 17, | ||
Augie Fackler
|
r43347 | b"dagrangeall", | ||
(b"dagrangepre", 17), | ||||
(b"dagrange", 17), | ||||
b"dagrangepost", | ||||
Augie Fackler
|
r43346 | ), | ||
Augie Fackler
|
r43347 | b"..": ( | ||
Augie Fackler
|
r43346 | 17, | ||
Augie Fackler
|
r43347 | b"dagrangeall", | ||
(b"dagrangepre", 17), | ||||
(b"dagrange", 17), | ||||
b"dagrangepost", | ||||
Augie Fackler
|
r43346 | ), | ||
Augie Fackler
|
r43347 | b":": (15, b"rangeall", (b"rangepre", 15), (b"range", 15), b"rangepost"), | ||
b"not": (10, None, (b"not", 10), None, None), | ||||
b"!": (10, None, (b"not", 10), None, None), | ||||
b"and": (5, None, None, (b"and", 5), None), | ||||
b"&": (5, None, None, (b"and", 5), None), | ||||
b"%": (5, None, None, (b"only", 5), b"onlypost"), | ||||
b"or": (4, None, None, (b"or", 4), None), | ||||
b"|": (4, None, None, (b"or", 4), None), | ||||
b"+": (4, None, None, (b"or", 4), None), | ||||
b"=": (3, None, None, (b"keyvalue", 3), None), | ||||
b",": (2, None, None, (b"list", 2), None), | ||||
b")": (0, None, None, None, None), | ||||
b"]": (0, None, None, None, None), | ||||
b"symbol": (0, b"symbol", None, None, None), | ||||
b"string": (0, b"string", None, None, None), | ||||
b"end": (0, None, None, None, None), | ||||
Yuya Nishihara
|
r31024 | } | ||
Augie Fackler
|
r43347 | keywords = {b'and', b'or', b'not'} | ||
Yuya Nishihara
|
r31024 | |||
Jun Wu
|
r34274 | symbols = {} | ||
Augie Fackler
|
r43347 | _quoteletters = {b'"', b"'"} | ||
_simpleopletters = set(pycompat.iterbytestr(b"()[]#:=,-|&+!~^%")) | ||||
Yuya Nishihara
|
r31384 | |||
Yuya Nishihara
|
r31024 | # default set of valid characters for the initial letter of symbols | ||
Augie Fackler
|
r43346 | _syminitletters = set( | ||
pycompat.iterbytestr( | ||||
pycompat.sysbytes(string.ascii_letters) | ||||
+ pycompat.sysbytes(string.digits) | ||||
Augie Fackler
|
r43347 | + b'._@' | ||
Augie Fackler
|
r43346 | ) | ||
) | set(map(pycompat.bytechr, pycompat.xrange(128, 256))) | ||||
Yuya Nishihara
|
r31024 | |||
# default set of valid characters for non-initial letters of symbols | ||||
Augie Fackler
|
r43347 | _symletters = _syminitletters | set(pycompat.iterbytestr(b'-/')) | ||
Yuya Nishihara
|
r31024 | |||
Augie Fackler
|
r43346 | |||
Yuya Nishihara
|
r31024 | def tokenize(program, lookup=None, syminitletters=None, symletters=None): | ||
Augie Fackler
|
r46554 | """ | ||
Yuya Nishihara
|
r31024 | Parse a revset statement into a stream of tokens | ||
``syminitletters`` is the set of valid characters for the initial | ||||
letter of symbols. | ||||
By default, character ``c`` is recognized as valid for initial | ||||
letter of symbols, if ``c.isalnum() or c in '._@' or ord(c) > 127``. | ||||
``symletters`` is the set of valid characters for non-initial | ||||
letters of symbols. | ||||
By default, character ``c`` is recognized as valid for non-initial | ||||
letters of symbols, if ``c.isalnum() or c in '-._/@' or ord(c) > 127``. | ||||
Check that @ is a valid unquoted token character (issue3686): | ||||
Yuya Nishihara
|
r34133 | >>> list(tokenize(b"@::")) | ||
Yuya Nishihara
|
r31024 | [('symbol', '@', 0), ('::', None, 1), ('end', None, 3)] | ||
Augie Fackler
|
r46554 | """ | ||
Yuya Nishihara
|
r37793 | if not isinstance(program, bytes): | ||
Augie Fackler
|
r43346 | raise error.ProgrammingError( | ||
Augie Fackler
|
r43347 | b'revset statement must be bytes, got %r' % program | ||
Augie Fackler
|
r43346 | ) | ||
Yuya Nishihara
|
r31441 | program = pycompat.bytestr(program) | ||
Yuya Nishihara
|
r31024 | if syminitletters is None: | ||
syminitletters = _syminitletters | ||||
if symletters is None: | ||||
symletters = _symletters | ||||
if program and lookup: | ||||
# attempt to parse old-style ranges first to deal with | ||||
# things like old-tag which contain query metacharacters | ||||
Augie Fackler
|
r43347 | parts = program.split(b':', 1) | ||
Yuya Nishihara
|
r31024 | if all(lookup(sym) for sym in parts if sym): | ||
if parts[0]: | ||||
Augie Fackler
|
r43347 | yield (b'symbol', parts[0], 0) | ||
Yuya Nishihara
|
r31024 | if len(parts) > 1: | ||
s = len(parts[0]) | ||||
Augie Fackler
|
r43347 | yield (b':', None, s) | ||
Yuya Nishihara
|
r31024 | if parts[1]: | ||
Augie Fackler
|
r43347 | yield (b'symbol', parts[1], s + 1) | ||
yield (b'end', None, len(program)) | ||||
Yuya Nishihara
|
r31024 | return | ||
pos, l = 0, len(program) | ||||
while pos < l: | ||||
Yuya Nishihara
|
r31441 | c = program[pos] | ||
Augie Fackler
|
r43346 | if c.isspace(): # skip inter-token whitespace | ||
Yuya Nishihara
|
r31024 | pass | ||
Augie Fackler
|
r43346 | elif ( | ||
Augie Fackler
|
r43347 | c == b':' and program[pos : pos + 2] == b'::' | ||
Augie Fackler
|
r43346 | ): # look ahead carefully | ||
Augie Fackler
|
r43347 | yield (b'::', None, pos) | ||
Augie Fackler
|
r43346 | pos += 1 # skip ahead | ||
elif ( | ||||
Augie Fackler
|
r43347 | c == b'.' and program[pos : pos + 2] == b'..' | ||
Augie Fackler
|
r43346 | ): # look ahead carefully | ||
Augie Fackler
|
r43347 | yield (b'..', None, pos) | ||
Augie Fackler
|
r43346 | pos += 1 # skip ahead | ||
elif ( | ||||
Augie Fackler
|
r43347 | c == b'#' and program[pos : pos + 2] == b'##' | ||
Augie Fackler
|
r43346 | ): # look ahead carefully | ||
Augie Fackler
|
r43347 | yield (b'##', None, pos) | ||
Augie Fackler
|
r43346 | pos += 1 # skip ahead | ||
elif c in _simpleopletters: # handle simple operators | ||||
Yuya Nishihara
|
r31024 | yield (c, None, pos) | ||
Augie Fackler
|
r43346 | elif ( | ||
c in _quoteletters | ||||
Augie Fackler
|
r43347 | or c == b'r' | ||
and program[pos : pos + 2] in (b"r'", b'r"') | ||||
Augie Fackler
|
r43346 | ): # handle quoted strings | ||
Augie Fackler
|
r43347 | if c == b'r': | ||
Yuya Nishihara
|
r31024 | pos += 1 | ||
Yuya Nishihara
|
r31441 | c = program[pos] | ||
Yuya Nishihara
|
r31024 | decode = lambda x: x | ||
else: | ||||
decode = parser.unescapestr | ||||
pos += 1 | ||||
s = pos | ||||
Augie Fackler
|
r43346 | while pos < l: # find closing quote | ||
Yuya Nishihara
|
r31441 | d = program[pos] | ||
Augie Fackler
|
r43347 | if d == b'\\': # skip over escaped characters | ||
Yuya Nishihara
|
r31024 | pos += 2 | ||
continue | ||||
if d == c: | ||||
Augie Fackler
|
r43347 | yield (b'string', decode(program[s:pos]), s) | ||
Yuya Nishihara
|
r31024 | break | ||
pos += 1 | ||||
else: | ||||
Augie Fackler
|
r43347 | raise error.ParseError(_(b"unterminated string"), s) | ||
Yuya Nishihara
|
r31024 | # gather up a symbol/keyword | ||
elif c in syminitletters: | ||||
s = pos | ||||
pos += 1 | ||||
Augie Fackler
|
r43346 | while pos < l: # find end of symbol | ||
Yuya Nishihara
|
r31441 | d = program[pos] | ||
Yuya Nishihara
|
r31024 | if d not in symletters: | ||
break | ||||
Augie Fackler
|
r43347 | if ( | ||
d == b'.' and program[pos - 1] == b'.' | ||||
): # special case for .. | ||||
Yuya Nishihara
|
r31024 | pos -= 1 | ||
break | ||||
pos += 1 | ||||
sym = program[s:pos] | ||||
Augie Fackler
|
r43346 | if sym in keywords: # operator keywords | ||
Yuya Nishihara
|
r31024 | yield (sym, None, s) | ||
Augie Fackler
|
r43347 | elif b'-' in sym: | ||
Yuya Nishihara
|
r31024 | # some jerk gave us foo-bar-baz, try to check if it's a symbol | ||
if lookup and lookup(sym): | ||||
# looks like a real symbol | ||||
Augie Fackler
|
r43347 | yield (b'symbol', sym, s) | ||
Yuya Nishihara
|
r31024 | else: | ||
# looks like an expression | ||||
Augie Fackler
|
r43347 | parts = sym.split(b'-') | ||
Yuya Nishihara
|
r31024 | for p in parts[:-1]: | ||
Augie Fackler
|
r43346 | if p: # possible consecutive - | ||
Augie Fackler
|
r43347 | yield (b'symbol', p, s) | ||
Yuya Nishihara
|
r31024 | s += len(p) | ||
Augie Fackler
|
r43347 | yield (b'-', None, s) | ||
Yuya Nishihara
|
r31024 | s += 1 | ||
Augie Fackler
|
r43346 | if parts[-1]: # possible trailing - | ||
Augie Fackler
|
r43347 | yield (b'symbol', parts[-1], s) | ||
Yuya Nishihara
|
r31024 | else: | ||
Augie Fackler
|
r43347 | yield (b'symbol', sym, s) | ||
Yuya Nishihara
|
r31024 | pos -= 1 | ||
else: | ||||
Augie Fackler
|
r43346 | raise error.ParseError( | ||
Augie Fackler
|
r43347 | _(b"syntax error in revset '%s'") % program, pos | ||
Augie Fackler
|
r43346 | ) | ||
Yuya Nishihara
|
r31024 | pos += 1 | ||
Augie Fackler
|
r43347 | yield (b'end', None, pos) | ||
Yuya Nishihara
|
r31024 | |||
Augie Fackler
|
r43346 | |||
Yuya Nishihara
|
r31024 | # helpers | ||
_notset = object() | ||||
Augie Fackler
|
r43346 | |||
Yuya Nishihara
|
r31024 | def getsymbol(x): | ||
Augie Fackler
|
r43347 | if x and x[0] == b'symbol': | ||
Yuya Nishihara
|
r31024 | return x[1] | ||
Augie Fackler
|
r43347 | raise error.ParseError(_(b'not a symbol')) | ||
Yuya Nishihara
|
r31024 | |||
Augie Fackler
|
r43346 | |||
Yuya Nishihara
|
r31024 | def getstring(x, err): | ||
Augie Fackler
|
r43347 | if x and (x[0] == b'string' or x[0] == b'symbol'): | ||
Yuya Nishihara
|
r31024 | return x[1] | ||
raise error.ParseError(err) | ||||
Augie Fackler
|
r43346 | |||
Yuya Nishihara
|
r31024 | def getinteger(x, err, default=_notset): | ||
if not x and default is not _notset: | ||||
return default | ||||
try: | ||||
return int(getstring(x, err)) | ||||
except ValueError: | ||||
raise error.ParseError(err) | ||||
Augie Fackler
|
r43346 | |||
Denis Laxalde
|
r31997 | def getboolean(x, err): | ||
Yuya Nishihara
|
r37102 | value = stringutil.parsebool(getsymbol(x)) | ||
Denis Laxalde
|
r31997 | if value is not None: | ||
return value | ||||
raise error.ParseError(err) | ||||
Augie Fackler
|
r43346 | |||
Yuya Nishihara
|
r31024 | def getlist(x): | ||
if not x: | ||||
return [] | ||||
Augie Fackler
|
r43347 | if x[0] == b'list': | ||
Yuya Nishihara
|
r31024 | return list(x[1:]) | ||
return [x] | ||||
Augie Fackler
|
r43346 | |||
Yuya Nishihara
|
r31024 | def getrange(x, err): | ||
if not x: | ||||
raise error.ParseError(err) | ||||
op = x[0] | ||||
Augie Fackler
|
r43347 | if op == b'range': | ||
Yuya Nishihara
|
r31024 | return x[1], x[2] | ||
Augie Fackler
|
r43347 | elif op == b'rangepre': | ||
Yuya Nishihara
|
r31024 | return None, x[1] | ||
Augie Fackler
|
r43347 | elif op == b'rangepost': | ||
Yuya Nishihara
|
r31024 | return x[1], None | ||
Augie Fackler
|
r43347 | elif op == b'rangeall': | ||
Yuya Nishihara
|
r31024 | return None, None | ||
raise error.ParseError(err) | ||||
Augie Fackler
|
r43346 | |||
Yuya Nishihara
|
r41702 | def getintrange(x, err1, err2, deffirst=_notset, deflast=_notset): | ||
"""Get [first, last] integer range (both inclusive) from a parsed tree | ||||
If any of the sides omitted, and if no default provided, ParseError will | ||||
be raised. | ||||
""" | ||||
Augie Fackler
|
r43347 | if x and (x[0] == b'string' or x[0] == b'symbol'): | ||
Yuya Nishihara
|
r41703 | n = getinteger(x, err1) | ||
return n, n | ||||
Yuya Nishihara
|
r41702 | a, b = getrange(x, err1) | ||
return getinteger(a, err2, deffirst), getinteger(b, err2, deflast) | ||||
Augie Fackler
|
r43346 | |||
Yuya Nishihara
|
r31024 | def getargs(x, min, max, err): | ||
l = getlist(x) | ||||
if len(l) < min or (max >= 0 and len(l) > max): | ||||
raise error.ParseError(err) | ||||
return l | ||||
Augie Fackler
|
r43346 | |||
Yuya Nishihara
|
r31024 | def getargsdict(x, funcname, keys): | ||
Augie Fackler
|
r43346 | return parser.buildargsdict( | ||
getlist(x), | ||||
funcname, | ||||
parser.splitargspec(keys), | ||||
Augie Fackler
|
r43347 | keyvaluenode=b'keyvalue', | ||
keynode=b'symbol', | ||||
Augie Fackler
|
r43346 | ) | ||
Yuya Nishihara
|
r31024 | |||
Yuya Nishihara
|
r34046 | # cache of {spec: raw parsed tree} built internally | ||
_treecache = {} | ||||
Augie Fackler
|
r43346 | |||
Yuya Nishihara
|
r34046 | def _cachedtree(spec): | ||
# thread safe because parse() is reentrant and dict.__setitem__() is atomic | ||||
tree = _treecache.get(spec) | ||||
if tree is None: | ||||
_treecache[spec] = tree = parse(spec) | ||||
return tree | ||||
Augie Fackler
|
r43346 | |||
Yuya Nishihara
|
r34046 | def _build(tmplspec, *repls): | ||
"""Create raw parsed tree from a template revset statement | ||||
Yuya Nishihara
|
r34133 | >>> _build(b'f(_) and _', (b'string', b'1'), (b'symbol', b'2')) | ||
Yuya Nishihara
|
r34046 | ('and', ('func', ('symbol', 'f'), ('string', '1')), ('symbol', '2')) | ||
""" | ||||
template = _cachedtree(tmplspec) | ||||
Augie Fackler
|
r43347 | return parser.buildtree(template, (b'symbol', b'_'), *repls) | ||
Yuya Nishihara
|
r34046 | |||
Augie Fackler
|
r43346 | |||
Yuya Nishihara
|
r34048 | def _match(patspec, tree): | ||
"""Test if a tree matches the given pattern statement; return the matches | ||||
Yuya Nishihara
|
r34133 | >>> _match(b'f(_)', parse(b'f()')) | ||
>>> _match(b'f(_)', parse(b'f(1)')) | ||||
Yuya Nishihara
|
r34048 | [('func', ('symbol', 'f'), ('symbol', '1')), ('symbol', '1')] | ||
Yuya Nishihara
|
r34133 | >>> _match(b'f(_)', parse(b'f(1, 2)')) | ||
Yuya Nishihara
|
r34048 | """ | ||
pattern = _cachedtree(patspec) | ||||
Augie Fackler
|
r43346 | return parser.matchtree( | ||
Augie Fackler
|
r43347 | pattern, tree, (b'symbol', b'_'), {b'keyvalue', b'list'} | ||
Augie Fackler
|
r43346 | ) | ||
Yuya Nishihara
|
r34048 | |||
Yuya Nishihara
|
r31024 | def _matchonly(revs, bases): | ||
Augie Fackler
|
r43347 | return _match(b'ancestors(_) and not ancestors(_)', (b'and', revs, bases)) | ||
Yuya Nishihara
|
r31024 | |||
Augie Fackler
|
r43346 | |||
Yuya Nishihara
|
r31024 | def _fixops(x): | ||
"""Rewrite raw parsed tree to resolve ambiguous syntax which cannot be | ||||
handled well by our simple top-down parser""" | ||||
if not isinstance(x, tuple): | ||||
return x | ||||
op = x[0] | ||||
Augie Fackler
|
r43347 | if op == b'parent': | ||
Yuya Nishihara
|
r31024 | # x^:y means (x^) : y, not x ^ (:y) | ||
# x^: means (x^) :, not x ^ (:) | ||||
Augie Fackler
|
r43347 | post = (b'parentpost', x[1]) | ||
if x[2][0] == b'dagrangepre': | ||||
return _fixops((b'dagrange', post, x[2][1])) | ||||
elif x[2][0] == b'dagrangeall': | ||||
return _fixops((b'dagrangepost', post)) | ||||
elif x[2][0] == b'rangepre': | ||||
return _fixops((b'range', post, x[2][1])) | ||||
elif x[2][0] == b'rangeall': | ||||
return _fixops((b'rangepost', post)) | ||||
elif op == b'or': | ||||
Yuya Nishihara
|
r31024 | # make number of arguments deterministic: | ||
# x + y + z -> (or x y z) -> (or (list x y z)) | ||||
Augie Fackler
|
r43347 | return (op, _fixops((b'list',) + x[1:])) | ||
elif op == b'subscript' and x[1][0] == b'relation': | ||||
Yuya Nishihara
|
r33416 | # x#y[z] ternary | ||
Augie Fackler
|
r43347 | return _fixops((b'relsubscript', x[1][1], x[1][2], x[2])) | ||
Yuya Nishihara
|
r31024 | |||
return (op,) + tuple(_fixops(y) for y in x[1:]) | ||||
Augie Fackler
|
r43346 | |||
Jun Wu
|
r34013 | def _analyze(x): | ||
Yuya Nishihara
|
r31024 | if x is None: | ||
return x | ||||
op = x[0] | ||||
Augie Fackler
|
r43347 | if op == b'minus': | ||
return _analyze(_build(b'_ and not _', *x[1:])) | ||||
elif op == b'only': | ||||
return _analyze(_build(b'only(_, _)', *x[1:])) | ||||
elif op == b'onlypost': | ||||
return _analyze(_build(b'only(_)', x[1])) | ||||
elif op == b'dagrangeall': | ||||
raise error.ParseError(_(b"can't use '::' in this context")) | ||||
elif op == b'dagrangepre': | ||||
return _analyze(_build(b'ancestors(_)', x[1])) | ||||
elif op == b'dagrangepost': | ||||
return _analyze(_build(b'descendants(_)', x[1])) | ||||
elif op == b'negate': | ||||
s = getstring(x[1], _(b"can't negate that")) | ||||
return _analyze((b'string', b'-' + s)) | ||||
elif op in (b'string', b'symbol', b'smartset'): | ||||
Yuya Nishihara
|
r31024 | return x | ||
Augie Fackler
|
r43347 | elif op == b'rangeall': | ||
Jun Wu
|
r34013 | return (op, None) | ||
Augie Fackler
|
r43347 | elif op in {b'or', b'not', b'rangepre', b'rangepost', b'parentpost'}: | ||
Jun Wu
|
r34013 | return (op, _analyze(x[1])) | ||
Augie Fackler
|
r43347 | elif op == b'group': | ||
Jun Wu
|
r34013 | return _analyze(x[1]) | ||
Augie Fackler
|
r43346 | elif op in { | ||
Augie Fackler
|
r43347 | b'and', | ||
b'dagrange', | ||||
b'range', | ||||
b'parent', | ||||
b'ancestor', | ||||
b'relation', | ||||
b'subscript', | ||||
Augie Fackler
|
r43346 | }: | ||
Jun Wu
|
r34013 | ta = _analyze(x[1]) | ||
tb = _analyze(x[2]) | ||||
return (op, ta, tb) | ||||
Augie Fackler
|
r43347 | elif op == b'relsubscript': | ||
Jun Wu
|
r34013 | ta = _analyze(x[1]) | ||
tb = _analyze(x[2]) | ||||
tc = _analyze(x[3]) | ||||
return (op, ta, tb, tc) | ||||
Augie Fackler
|
r43347 | elif op == b'list': | ||
Jun Wu
|
r34013 | return (op,) + tuple(_analyze(y) for y in x[1:]) | ||
Augie Fackler
|
r43347 | elif op == b'keyvalue': | ||
Jun Wu
|
r34013 | return (op, x[1], _analyze(x[2])) | ||
Augie Fackler
|
r43347 | elif op == b'func': | ||
Jun Wu
|
r34013 | return (op, x[1], _analyze(x[2])) | ||
Augie Fackler
|
r43347 | raise ValueError(b'invalid operator %r' % op) | ||
Yuya Nishihara
|
r31024 | |||
Augie Fackler
|
r43346 | |||
Jun Wu
|
r34013 | def analyze(x): | ||
Yuya Nishihara
|
r31024 | """Transform raw parsed tree to evaluatable tree which can be fed to | ||
optimize() or getset() | ||||
All pseudo operations should be mapped to real operations or functions | ||||
defined in methods or symbols table respectively. | ||||
""" | ||||
Jun Wu
|
r34013 | return _analyze(x) | ||
Yuya Nishihara
|
r31024 | |||
Augie Fackler
|
r43346 | |||
Jun Wu
|
r34273 | def _optimize(x): | ||
Yuya Nishihara
|
r31024 | if x is None: | ||
return 0, x | ||||
op = x[0] | ||||
Augie Fackler
|
r43347 | if op in (b'string', b'symbol', b'smartset'): | ||
Augie Fackler
|
r43346 | return 0.5, x # single revisions are small | ||
Augie Fackler
|
r43347 | elif op == b'and': | ||
Jun Wu
|
r34273 | wa, ta = _optimize(x[1]) | ||
wb, tb = _optimize(x[2]) | ||||
Yuya Nishihara
|
r31024 | w = min(wa, wb) | ||
Jun Wu
|
r34067 | # (draft/secret/_notpublic() & ::x) have a fast path | ||
Augie Fackler
|
r43347 | m = _match(b'_() & ancestors(_)', (b'and', ta, tb)) | ||
if m and getsymbol(m[1]) in {b'draft', b'secret', b'_notpublic'}: | ||||
return w, _build(b'_phaseandancestors(_, _)', m[1], m[2]) | ||||
Jun Wu
|
r34067 | |||
Yuya Nishihara
|
r31024 | # (::x and not ::y)/(not ::y and ::x) have a fast path | ||
Yuya Nishihara
|
r34046 | m = _matchonly(ta, tb) or _matchonly(tb, ta) | ||
if m: | ||||
Augie Fackler
|
r43347 | return w, _build(b'only(_, _)', *m[1:]) | ||
Yuya Nishihara
|
r31024 | |||
Augie Fackler
|
r43347 | m = _match(b'not _', tb) | ||
Yuya Nishihara
|
r34048 | if m: | ||
Augie Fackler
|
r43347 | return wa, (b'difference', ta, m[1]) | ||
Yuya Nishihara
|
r31024 | if wa > wb: | ||
Augie Fackler
|
r43347 | op = b'andsmally' | ||
Jun Wu
|
r34013 | return w, (op, ta, tb) | ||
Augie Fackler
|
r43347 | elif op == b'or': | ||
Yuya Nishihara
|
r31024 | # fast path for machine-generated expression, that is likely to have | ||
# lots of trivial revisions: 'a + b + c()' to '_list(a b) + c()' | ||||
ws, ts, ss = [], [], [] | ||||
Augie Fackler
|
r43346 | |||
Yuya Nishihara
|
r31024 | def flushss(): | ||
if not ss: | ||||
return | ||||
if len(ss) == 1: | ||||
w, t = ss[0] | ||||
else: | ||||
Augie Fackler
|
r43347 | s = b'\0'.join(t[1] for w, t in ss) | ||
y = _build(b'_list(_)', (b'string', s)) | ||||
Jun Wu
|
r34273 | w, t = _optimize(y) | ||
Yuya Nishihara
|
r31024 | ws.append(w) | ||
ts.append(t) | ||||
del ss[:] | ||||
Augie Fackler
|
r43346 | |||
Yuya Nishihara
|
r31024 | for y in getlist(x[1]): | ||
Jun Wu
|
r34273 | w, t = _optimize(y) | ||
Augie Fackler
|
r43347 | if t is not None and (t[0] == b'string' or t[0] == b'symbol'): | ||
Yuya Nishihara
|
r31024 | ss.append((w, t)) | ||
continue | ||||
flushss() | ||||
ws.append(w) | ||||
ts.append(t) | ||||
flushss() | ||||
if len(ts) == 1: | ||||
Augie Fackler
|
r43346 | return ws[0], ts[0] # 'or' operation is fully optimized out | ||
Augie Fackler
|
r43347 | return max(ws), (op, (b'list',) + tuple(ts)) | ||
elif op == b'not': | ||||
Yuya Nishihara
|
r31024 | # Optimize not public() to _notpublic() because we have a fast version | ||
Augie Fackler
|
r43347 | if _match(b'public()', x[1]): | ||
o = _optimize(_build(b'_notpublic()')) | ||||
Yuya Nishihara
|
r31024 | return o[0], o[1] | ||
else: | ||||
Jun Wu
|
r34273 | o = _optimize(x[1]) | ||
Jun Wu
|
r34013 | return o[0], (op, o[1]) | ||
Augie Fackler
|
r43347 | elif op == b'rangeall': | ||
Jun Wu
|
r34273 | return 1, x | ||
Augie Fackler
|
r43347 | elif op in (b'rangepre', b'rangepost', b'parentpost'): | ||
Jun Wu
|
r34273 | o = _optimize(x[1]) | ||
Jun Wu
|
r34013 | return o[0], (op, o[1]) | ||
Augie Fackler
|
r43347 | elif op in (b'dagrange', b'range'): | ||
Jun Wu
|
r34273 | wa, ta = _optimize(x[1]) | ||
wb, tb = _optimize(x[2]) | ||||
Jun Wu
|
r34013 | return wa + wb, (op, ta, tb) | ||
Augie Fackler
|
r43347 | elif op in (b'parent', b'ancestor', b'relation', b'subscript'): | ||
Jun Wu
|
r34273 | w, t = _optimize(x[1]) | ||
Jun Wu
|
r34013 | return w, (op, t, x[2]) | ||
Augie Fackler
|
r43347 | elif op == b'relsubscript': | ||
Jun Wu
|
r34273 | w, t = _optimize(x[1]) | ||
Jun Wu
|
r34013 | return w, (op, t, x[2], x[3]) | ||
Augie Fackler
|
r43347 | elif op == b'list': | ||
Jun Wu
|
r34273 | ws, ts = zip(*(_optimize(y) for y in x[1:])) | ||
Yuya Nishihara
|
r31024 | return sum(ws), (op,) + ts | ||
Augie Fackler
|
r43347 | elif op == b'keyvalue': | ||
Jun Wu
|
r34273 | w, t = _optimize(x[2]) | ||
Yuya Nishihara
|
r31024 | return w, (op, x[1], t) | ||
Augie Fackler
|
r43347 | elif op == b'func': | ||
Yuya Nishihara
|
r31024 | f = getsymbol(x[1]) | ||
Jun Wu
|
r34273 | wa, ta = _optimize(x[2]) | ||
Jun Wu
|
r34274 | w = getattr(symbols.get(f), '_weight', 1) | ||
Augie Fackler
|
r43347 | m = _match(b'commonancestors(_)', ta) | ||
Sean Farley
|
r38644 | |||
# Optimize heads(commonancestors(_)) because we have a fast version | ||||
Augie Fackler
|
r43347 | if f == b'heads' and m: | ||
return w + wa, _build(b'_commonancestorheads(_)', m[1]) | ||||
Sean Farley
|
r38644 | |||
Jun Wu
|
r34013 | return w + wa, (op, x[1], ta) | ||
Augie Fackler
|
r43347 | raise ValueError(b'invalid operator %r' % op) | ||
Yuya Nishihara
|
r31024 | |||
Augie Fackler
|
r43346 | |||
Yuya Nishihara
|
r31024 | def optimize(tree): | ||
"""Optimize evaluatable tree | ||||
All pseudo operations should be transformed beforehand. | ||||
""" | ||||
Jun Wu
|
r34273 | _weight, newtree = _optimize(tree) | ||
Yuya Nishihara
|
r31024 | return newtree | ||
Augie Fackler
|
r43346 | |||
Yuya Nishihara
|
r31024 | # the set of valid characters for the initial letter of symbols in | ||
# alias declarations and definitions | ||||
Augie Fackler
|
r43347 | _aliassyminitletters = _syminitletters | {b'$'} | ||
Yuya Nishihara
|
r31024 | |||
Augie Fackler
|
r43346 | |||
Yuya Nishihara
|
r31024 | def _parsewith(spec, lookup=None, syminitletters=None): | ||
"""Generate a parse tree of given spec with given tokenizing options | ||||
Yuya Nishihara
|
r34133 | >>> _parsewith(b'foo($1)', syminitletters=_aliassyminitletters) | ||
Yuya Nishihara
|
r31024 | ('func', ('symbol', 'foo'), ('symbol', '$1')) | ||
Martin von Zweigbergk
|
r46498 | >>> from . import error | ||
>>> from . import pycompat | ||||
>>> try: | ||||
... _parsewith(b'$1') | ||||
... except error.ParseError as e: | ||||
... pycompat.sysstr(e.message) | ||||
... e.location | ||||
"syntax error in revset '$1'" | ||||
0 | ||||
>>> try: | ||||
... _parsewith(b'foo bar') | ||||
... except error.ParseError as e: | ||||
... pycompat.sysstr(e.message) | ||||
... e.location | ||||
'invalid token' | ||||
4 | ||||
Yuya Nishihara
|
r31024 | """ | ||
Augie Fackler
|
r43347 | if lookup and spec.startswith(b'revset(') and spec.endswith(b')'): | ||
Boris Feld
|
r37778 | lookup = None | ||
Yuya Nishihara
|
r31024 | p = parser.parser(elements) | ||
Augie Fackler
|
r43346 | tree, pos = p.parse( | ||
tokenize(spec, lookup=lookup, syminitletters=syminitletters) | ||||
) | ||||
Yuya Nishihara
|
r31024 | if pos != len(spec): | ||
Augie Fackler
|
r43347 | raise error.ParseError(_(b'invalid token'), pos) | ||
return _fixops(parser.simplifyinfixops(tree, (b'list', b'or'))) | ||||
Yuya Nishihara
|
r31024 | |||
Augie Fackler
|
r43346 | |||
Yuya Nishihara
|
r31024 | class _aliasrules(parser.basealiasrules): | ||
"""Parsing and expansion rule set of revset aliases""" | ||||
Augie Fackler
|
r43346 | |||
Augie Fackler
|
r43347 | _section = _(b'revset alias') | ||
Yuya Nishihara
|
r31024 | |||
@staticmethod | ||||
def _parse(spec): | ||||
"""Parse alias declaration/definition ``spec`` | ||||
This allows symbol names to use also ``$`` as an initial letter | ||||
(for backward compatibility), and callers of this function should | ||||
examine whether ``$`` is used also for unexpected symbols or not. | ||||
""" | ||||
return _parsewith(spec, syminitletters=_aliassyminitletters) | ||||
@staticmethod | ||||
def _trygetfunc(tree): | ||||
Augie Fackler
|
r43347 | if tree[0] == b'func' and tree[1][0] == b'symbol': | ||
Yuya Nishihara
|
r31024 | return tree[1][1], getlist(tree[2]) | ||
Augie Fackler
|
r43346 | |||
Jun Wu
|
r33336 | def expandaliases(tree, aliases, warn=None): | ||
"""Expand aliases in a tree, aliases is a list of (name, value) tuples""" | ||||
aliases = _aliasrules.buildmap(aliases) | ||||
Yuya Nishihara
|
r31024 | tree = _aliasrules.expand(aliases, tree) | ||
# warn about problematic (but not referred) aliases | ||||
Jun Wu
|
r33336 | if warn is not None: | ||
Gregory Szorc
|
r49768 | for name, alias in sorted(aliases.items()): | ||
Jun Wu
|
r33336 | if alias.error and not alias.warned: | ||
Augie Fackler
|
r43347 | warn(_(b'warning: %s\n') % (alias.error)) | ||
Jun Wu
|
r33336 | alias.warned = True | ||
Yuya Nishihara
|
r31024 | return tree | ||
Augie Fackler
|
r43346 | |||
Yuya Nishihara
|
r31024 | def foldconcat(tree): | ||
Augie Fackler
|
r46554 | """Fold elements to be concatenated by `##`""" | ||
Augie Fackler
|
r43346 | if not isinstance(tree, tuple) or tree[0] in ( | ||
Augie Fackler
|
r43347 | b'string', | ||
b'symbol', | ||||
b'smartset', | ||||
Augie Fackler
|
r43346 | ): | ||
Yuya Nishihara
|
r31024 | return tree | ||
Augie Fackler
|
r43347 | if tree[0] == b'_concat': | ||
Yuya Nishihara
|
r31024 | pending = [tree] | ||
l = [] | ||||
while pending: | ||||
e = pending.pop() | ||||
Augie Fackler
|
r43347 | if e[0] == b'_concat': | ||
Yuya Nishihara
|
r31024 | pending.extend(reversed(e[1:])) | ||
Augie Fackler
|
r43347 | elif e[0] in (b'string', b'symbol'): | ||
Yuya Nishihara
|
r31024 | l.append(e[1]) | ||
else: | ||||
Augie Fackler
|
r43347 | msg = _(b"\"##\" can't concatenate \"%s\" element") % (e[0]) | ||
Yuya Nishihara
|
r31024 | raise error.ParseError(msg) | ||
Augie Fackler
|
r43347 | return (b'string', b''.join(l)) | ||
Yuya Nishihara
|
r31024 | else: | ||
return tuple(foldconcat(t) for t in tree) | ||||
Augie Fackler
|
r43346 | |||
Yuya Nishihara
|
r31024 | def parse(spec, lookup=None): | ||
Ryan McElroy
|
r36703 | try: | ||
return _parsewith(spec, lookup=lookup) | ||||
except error.ParseError as inst: | ||||
Martin von Zweigbergk
|
r46361 | if inst.location is not None: | ||
loc = inst.location | ||||
Ryan McElroy
|
r36703 | # Remove newlines -- spaces are equivalent whitespace. | ||
Augie Fackler
|
r43347 | spec = spec.replace(b'\n', b' ') | ||
Ryan McElroy
|
r36703 | # We want the caret to point to the place in the template that | ||
# failed to parse, but in a hint we get a open paren at the | ||||
# start. Therefore, we print "loc + 1" spaces (instead of "loc") | ||||
# to line up the caret with the location of the error. | ||||
Augie Fackler
|
r43347 | inst.hint = spec + b'\n' + b' ' * (loc + 1) + b'^ ' + _(b'here') | ||
Ryan McElroy
|
r36703 | raise | ||
Yuya Nishihara
|
r31024 | |||
Augie Fackler
|
r43346 | |||
Augie Fackler
|
r31604 | def _quote(s): | ||
Augie Fackler
|
r31605 | r"""Quote a value in order to make it safe for the revset engine. | ||
Yuya Nishihara
|
r34133 | >>> _quote(b'asdf') | ||
Augie Fackler
|
r31605 | "'asdf'" | ||
Yuya Nishihara
|
r34133 | >>> _quote(b"asdf'\"") | ||
Augie Fackler
|
r31605 | '\'asdf\\\'"\'' | ||
Yuya Nishihara
|
r34133 | >>> _quote(b'asdf\'') | ||
Augie Fackler
|
r31606 | "'asdf\\''" | ||
Augie Fackler
|
r31605 | >>> _quote(1) | ||
"'1'" | ||||
""" | ||||
Augie Fackler
|
r43347 | return b"'%s'" % stringutil.escapestr(pycompat.bytestr(s)) | ||
Augie Fackler
|
r31604 | |||
Augie Fackler
|
r43346 | |||
Yuya Nishihara
|
r35614 | def _formatargtype(c, arg): | ||
Augie Fackler
|
r43347 | if c == b'd': | ||
return b'_rev(%d)' % int(arg) | ||||
elif c == b's': | ||||
Yuya Nishihara
|
r35614 | return _quote(arg) | ||
Augie Fackler
|
r43347 | elif c == b'r': | ||
Yuya Nishihara
|
r37793 | if not isinstance(arg, bytes): | ||
raise TypeError | ||||
Augie Fackler
|
r43346 | parse(arg) # make sure syntax errors are confined | ||
Augie Fackler
|
r43347 | return b'(%s)' % arg | ||
elif c == b'n': | ||||
Joerg Sonnenberger
|
r46729 | return _quote(hex(arg)) | ||
Augie Fackler
|
r43347 | elif c == b'b': | ||
Yuya Nishihara
|
r35614 | try: | ||
return _quote(arg.branch()) | ||||
except AttributeError: | ||||
raise TypeError | ||||
Augie Fackler
|
r43347 | raise error.ParseError(_(b'unexpected revspec format character %s') % c) | ||
Yuya Nishihara
|
r35614 | |||
Augie Fackler
|
r43346 | |||
Yuya Nishihara
|
r35614 | def _formatlistexp(s, t): | ||
l = len(s) | ||||
if l == 0: | ||||
Augie Fackler
|
r43347 | return b"_list('')" | ||
Yuya Nishihara
|
r35614 | elif l == 1: | ||
return _formatargtype(t, s[0]) | ||||
Augie Fackler
|
r43347 | elif t == b'd': | ||
Boris Feld
|
r41256 | return _formatintlist(s) | ||
Augie Fackler
|
r43347 | elif t == b's': | ||
return b"_list(%s)" % _quote(b"\0".join(s)) | ||||
elif t == b'n': | ||||
Joerg Sonnenberger
|
r46729 | return b"_hexlist('%s')" % b"\0".join(hex(a) for a in s) | ||
Augie Fackler
|
r43347 | elif t == b'b': | ||
Yuya Nishihara
|
r35614 | try: | ||
Augie Fackler
|
r43347 | return b"_list('%s')" % b"\0".join(a.branch() for a in s) | ||
Yuya Nishihara
|
r35614 | except AttributeError: | ||
raise TypeError | ||||
m = l // 2 | ||||
Augie Fackler
|
r43347 | return b'(%s or %s)' % (_formatlistexp(s[:m], t), _formatlistexp(s[m:], t)) | ||
Yuya Nishihara
|
r35614 | |||
Augie Fackler
|
r43346 | |||
Boris Feld
|
r41256 | def _formatintlist(data): | ||
try: | ||||
l = len(data) | ||||
if l == 0: | ||||
Augie Fackler
|
r43347 | return b"_list('')" | ||
Boris Feld
|
r41256 | elif l == 1: | ||
Augie Fackler
|
r43347 | return _formatargtype(b'd', data[0]) | ||
return b"_intlist('%s')" % b"\0".join(b'%d' % int(a) for a in data) | ||||
Boris Feld
|
r41256 | except (TypeError, ValueError): | ||
Augie Fackler
|
r43347 | raise error.ParseError(_(b'invalid argument for revspec')) | ||
Boris Feld
|
r41256 | |||
Augie Fackler
|
r43346 | |||
Yuya Nishihara
|
r35615 | def _formatparamexp(args, t): | ||
Augie Fackler
|
r43347 | return b', '.join(_formatargtype(t, a) for a in args) | ||
Yuya Nishihara
|
r35615 | |||
Augie Fackler
|
r43346 | |||
Yuya Nishihara
|
r35615 | _formatlistfuncs = { | ||
Augie Fackler
|
r43347 | b'l': _formatlistexp, | ||
b'p': _formatparamexp, | ||||
Yuya Nishihara
|
r35615 | } | ||
Augie Fackler
|
r43346 | |||
Yuya Nishihara
|
r31024 | def formatspec(expr, *args): | ||
Augie Fackler
|
r46554 | """ | ||
Yuya Nishihara
|
r31024 | This is a convenience function for using revsets internally, and | ||
escapes arguments appropriately. Aliases are intentionally ignored | ||||
so that intended expression behavior isn't accidentally subverted. | ||||
Supported arguments: | ||||
%r = revset expression, parenthesized | ||||
Boris Feld
|
r41254 | %d = rev(int(arg)), no quoting | ||
Yuya Nishihara
|
r31024 | %s = string(arg), escaped and single-quoted | ||
%b = arg.branch(), escaped and single-quoted | ||||
%n = hex(arg), single-quoted | ||||
%% = a literal '%' | ||||
Yuya Nishihara
|
r35615 | Prefixing the type with 'l' specifies a parenthesized list of that type, | ||
and 'p' specifies a list of function parameters of that type. | ||||
Yuya Nishihara
|
r31024 | |||
Yuya Nishihara
|
r34133 | >>> formatspec(b'%r:: and %lr', b'10 or 11', (b"this()", b"that()")) | ||
Yuya Nishihara
|
r31024 | '(10 or 11):: and ((this()) or (that()))' | ||
Yuya Nishihara
|
r34133 | >>> formatspec(b'%d:: and not %d::', 10, 20) | ||
Boris Feld
|
r41333 | '_rev(10):: and not _rev(20)::' | ||
Yuya Nishihara
|
r34133 | >>> formatspec(b'%ld or %ld', [], [1]) | ||
Boris Feld
|
r41333 | "_list('') or _rev(1)" | ||
Yuya Nishihara
|
r34133 | >>> formatspec(b'keyword(%s)', b'foo\\xe9') | ||
Yuya Nishihara
|
r31024 | "keyword('foo\\\\xe9')" | ||
Yuya Nishihara
|
r34133 | >>> b = lambda: b'default' | ||
Yuya Nishihara
|
r31024 | >>> b.branch = b | ||
Yuya Nishihara
|
r34133 | >>> formatspec(b'branch(%b)', b) | ||
Yuya Nishihara
|
r31024 | "branch('default')" | ||
Yuya Nishihara
|
r34133 | >>> formatspec(b'root(%ls)', [b'a', b'b', b'c', b'd']) | ||
Yuya Nishihara
|
r35613 | "root(_list('a\\\\x00b\\\\x00c\\\\x00d'))" | ||
Yuya Nishihara
|
r35615 | >>> formatspec(b'sort(%r, %ps)', b':', [b'desc', b'user']) | ||
"sort((:), 'desc', 'user')" | ||||
Augie Fackler
|
r35840 | >>> formatspec(b'%ls', [b'a', b"'"]) | ||
Yuya Nishihara
|
r35613 | "_list('a\\\\x00\\\\'')" | ||
Augie Fackler
|
r46554 | """ | ||
Boris Feld
|
r41255 | parsed = _parseargs(expr, args) | ||
ret = [] | ||||
for t, arg in parsed: | ||||
if t is None: | ||||
ret.append(arg) | ||||
Augie Fackler
|
r43347 | elif t == b'baseset': | ||
Boris Feld
|
r41257 | if isinstance(arg, set): | ||
arg = sorted(arg) | ||||
ret.append(_formatintlist(list(arg))) | ||||
Boris Feld
|
r41255 | else: | ||
Augie Fackler
|
r43347 | raise error.ProgrammingError(b"unknown revspec item type: %r" % t) | ||
Boris Feld
|
r41255 | return b''.join(ret) | ||
Augie Fackler
|
r43346 | |||
Boris Feld
|
r41258 | def spectree(expr, *args): | ||
"""similar to formatspec but return a parsed and optimized tree""" | ||||
parsed = _parseargs(expr, args) | ||||
ret = [] | ||||
inputs = [] | ||||
for t, arg in parsed: | ||||
if t is None: | ||||
ret.append(arg) | ||||
Augie Fackler
|
r43347 | elif t == b'baseset': | ||
newtree = (b'smartset', smartset.baseset(arg)) | ||||
Boris Feld
|
r41258 | inputs.append(newtree) | ||
Augie Fackler
|
r43347 | ret.append(b"$") | ||
Boris Feld
|
r41258 | else: | ||
Augie Fackler
|
r43347 | raise error.ProgrammingError(b"unknown revspec item type: %r" % t) | ||
Boris Feld
|
r41258 | expr = b''.join(ret) | ||
tree = _parsewith(expr, syminitletters=_aliassyminitletters) | ||||
Augie Fackler
|
r43347 | tree = parser.buildtree(tree, (b'symbol', b'$'), *inputs) | ||
Boris Feld
|
r41258 | tree = foldconcat(tree) | ||
tree = analyze(tree) | ||||
tree = optimize(tree) | ||||
return tree | ||||
Augie Fackler
|
r43346 | |||
Boris Feld
|
r41255 | def _parseargs(expr, args): | ||
"""parse the expression and replace all inexpensive args | ||||
return a list of tuple [(arg-type, arg-value)] | ||||
Arg-type can be: | ||||
Boris Feld
|
r41257 | * None: a string ready to be concatenated into a final spec | ||
* 'baseset': an iterable of revisions | ||||
Boris Feld
|
r41255 | """ | ||
Yuya Nishihara
|
r31440 | expr = pycompat.bytestr(expr) | ||
Yuya Nishihara
|
r35574 | argiter = iter(args) | ||
Yuya Nishihara
|
r35571 | ret = [] | ||
Yuya Nishihara
|
r31024 | pos = 0 | ||
while pos < len(expr): | ||||
Augie Fackler
|
r43347 | q = expr.find(b'%', pos) | ||
Yuya Nishihara
|
r35572 | if q < 0: | ||
Boris Feld
|
r41255 | ret.append((None, expr[pos:])) | ||
Yuya Nishihara
|
r35572 | break | ||
Boris Feld
|
r41255 | ret.append((None, expr[pos:q])) | ||
Yuya Nishihara
|
r35572 | pos = q + 1 | ||
Yuya Nishihara
|
r35611 | try: | ||
d = expr[pos] | ||||
except IndexError: | ||||
Augie Fackler
|
r43347 | raise error.ParseError(_(b'incomplete revspec format character')) | ||
if d == b'%': | ||||
Boris Feld
|
r41255 | ret.append((None, d)) | ||
Yuya Nishihara
|
r35610 | pos += 1 | ||
continue | ||||
try: | ||||
arg = next(argiter) | ||||
except StopIteration: | ||||
Augie Fackler
|
r43347 | raise error.ParseError(_(b'missing argument for revspec')) | ||
Yuya Nishihara
|
r35615 | f = _formatlistfuncs.get(d) | ||
if f: | ||||
Boris Feld
|
r41255 | # a list of some type, might be expensive, do not replace | ||
Yuya Nishihara
|
r35573 | pos += 1 | ||
Augie Fackler
|
r43347 | islist = d == b'l' | ||
Yuya Nishihara
|
r35611 | try: | ||
d = expr[pos] | ||||
except IndexError: | ||||
Augie Fackler
|
r43347 | raise error.ParseError( | ||
_(b'incomplete revspec format character') | ||||
) | ||||
if islist and d == b'd' and arg: | ||||
Boris Feld
|
r41275 | # we don't create a baseset yet, because it come with an | ||
# extra cost. If we are going to serialize it we better | ||||
# skip it. | ||||
Augie Fackler
|
r43347 | ret.append((b'baseset', arg)) | ||
Boris Feld
|
r41275 | pos += 1 | ||
continue | ||||
Yuya Nishihara
|
r35612 | try: | ||
Boris Feld
|
r41255 | ret.append((None, f(list(arg), d))) | ||
Yuya Nishihara
|
r35612 | except (TypeError, ValueError): | ||
Augie Fackler
|
r43347 | raise error.ParseError(_(b'invalid argument for revspec')) | ||
Yuya Nishihara
|
r35573 | else: | ||
Boris Feld
|
r41255 | # a single entry, not expensive, replace | ||
Yuya Nishihara
|
r35612 | try: | ||
Boris Feld
|
r41255 | ret.append((None, _formatargtype(d, arg))) | ||
Yuya Nishihara
|
r35612 | except (TypeError, ValueError): | ||
Augie Fackler
|
r43347 | raise error.ParseError(_(b'invalid argument for revspec')) | ||
Yuya Nishihara
|
r31024 | pos += 1 | ||
Yuya Nishihara
|
r35610 | try: | ||
next(argiter) | ||||
Augie Fackler
|
r43347 | raise error.ParseError(_(b'too many revspec arguments specified')) | ||
Yuya Nishihara
|
r35610 | except StopIteration: | ||
pass | ||||
Boris Feld
|
r41255 | return ret | ||
Yuya Nishihara
|
r31024 | |||
Augie Fackler
|
r43346 | |||
Yuya Nishihara
|
r31024 | def prettyformat(tree): | ||
Augie Fackler
|
r43347 | return parser.prettyformat(tree, (b'string', b'symbol')) | ||
Yuya Nishihara
|
r31024 | |||
Augie Fackler
|
r43346 | |||
Yuya Nishihara
|
r31024 | def depth(tree): | ||
if isinstance(tree, tuple): | ||||
return max(map(depth, tree)) + 1 | ||||
else: | ||||
return 0 | ||||
Augie Fackler
|
r43346 | |||
Yuya Nishihara
|
r31024 | def funcsused(tree): | ||
Augie Fackler
|
r43347 | if not isinstance(tree, tuple) or tree[0] in (b'string', b'symbol'): | ||
Yuya Nishihara
|
r31024 | return set() | ||
else: | ||||
funcs = set() | ||||
for s in tree[1:]: | ||||
funcs |= funcsused(s) | ||||
Augie Fackler
|
r43347 | if tree[0] == b'func': | ||
Yuya Nishihara
|
r31024 | funcs.add(tree[1][1]) | ||
return funcs | ||||
Pulkit Goyal
|
r35510 | |||
Augie Fackler
|
r43346 | |||
Augie Fackler
|
r43347 | _hashre = util.re.compile(b'[0-9a-fA-F]{1,40}$') | ||
Pulkit Goyal
|
r35510 | |||
Augie Fackler
|
r43346 | |||
Pulkit Goyal
|
r35510 | def _ishashlikesymbol(symbol): | ||
"""returns true if the symbol looks like a hash""" | ||||
return _hashre.match(symbol) | ||||
Augie Fackler
|
r43346 | |||
Pulkit Goyal
|
r35510 | def gethashlikesymbols(tree): | ||
"""returns the list of symbols of the tree that look like hashes | ||||
Yuya Nishihara
|
r35900 | >>> gethashlikesymbols(parse(b'3::abe3ff')) | ||
Pulkit Goyal
|
r35510 | ['3', 'abe3ff'] | ||
Yuya Nishihara
|
r35900 | >>> gethashlikesymbols(parse(b'precursors(.)')) | ||
Pulkit Goyal
|
r35510 | [] | ||
Yuya Nishihara
|
r35900 | >>> gethashlikesymbols(parse(b'precursors(34)')) | ||
Pulkit Goyal
|
r35510 | ['34'] | ||
Yuya Nishihara
|
r35900 | >>> gethashlikesymbols(parse(b'abe3ffZ')) | ||
Pulkit Goyal
|
r35510 | [] | ||
""" | ||||
if not tree: | ||||
return [] | ||||
Augie Fackler
|
r43347 | if tree[0] == b"symbol": | ||
Pulkit Goyal
|
r35510 | if _ishashlikesymbol(tree[1]): | ||
return [tree[1]] | ||||
elif len(tree) >= 3: | ||||
results = [] | ||||
for subtree in tree[1:]: | ||||
results += gethashlikesymbols(subtree) | ||||
return results | ||||
return [] | ||||