##// END OF EJS Templates
revsetlang: fix quoting of %ls string...
Yuya Nishihara -
r35613:91201737 default
parent child Browse files
Show More
@@ -1,727 +1,729 b''
1 1 # revsetlang.py - parser, tokenizer and utility for revision set language
2 2 #
3 3 # Copyright 2010 Matt Mackall <mpm@selenic.com>
4 4 #
5 5 # This software may be used and distributed according to the terms of the
6 6 # GNU General Public License version 2 or any later version.
7 7
8 8 from __future__ import absolute_import
9 9
10 10 import string
11 11
12 12 from .i18n import _
13 13 from . import (
14 14 error,
15 15 node,
16 16 parser,
17 17 pycompat,
18 18 util,
19 19 )
20 20
21 21 elements = {
22 22 # token-type: binding-strength, primary, prefix, infix, suffix
23 23 "(": (21, None, ("group", 1, ")"), ("func", 1, ")"), None),
24 24 "[": (21, None, None, ("subscript", 1, "]"), None),
25 25 "#": (21, None, None, ("relation", 21), None),
26 26 "##": (20, None, None, ("_concat", 20), None),
27 27 "~": (18, None, None, ("ancestor", 18), None),
28 28 "^": (18, None, None, ("parent", 18), "parentpost"),
29 29 "-": (5, None, ("negate", 19), ("minus", 5), None),
30 30 "::": (17, "dagrangeall", ("dagrangepre", 17), ("dagrange", 17),
31 31 "dagrangepost"),
32 32 "..": (17, "dagrangeall", ("dagrangepre", 17), ("dagrange", 17),
33 33 "dagrangepost"),
34 34 ":": (15, "rangeall", ("rangepre", 15), ("range", 15), "rangepost"),
35 35 "not": (10, None, ("not", 10), None, None),
36 36 "!": (10, None, ("not", 10), None, None),
37 37 "and": (5, None, None, ("and", 5), None),
38 38 "&": (5, None, None, ("and", 5), None),
39 39 "%": (5, None, None, ("only", 5), "onlypost"),
40 40 "or": (4, None, None, ("or", 4), None),
41 41 "|": (4, None, None, ("or", 4), None),
42 42 "+": (4, None, None, ("or", 4), None),
43 43 "=": (3, None, None, ("keyvalue", 3), None),
44 44 ",": (2, None, None, ("list", 2), None),
45 45 ")": (0, None, None, None, None),
46 46 "]": (0, None, None, None, None),
47 47 "symbol": (0, "symbol", None, None, None),
48 48 "string": (0, "string", None, None, None),
49 49 "end": (0, None, None, None, None),
50 50 }
51 51
52 52 keywords = {'and', 'or', 'not'}
53 53
54 54 symbols = {}
55 55
56 56 _quoteletters = {'"', "'"}
57 57 _simpleopletters = set(pycompat.iterbytestr("()[]#:=,-|&+!~^%"))
58 58
59 59 # default set of valid characters for the initial letter of symbols
60 60 _syminitletters = set(pycompat.iterbytestr(
61 61 string.ascii_letters.encode('ascii') +
62 62 string.digits.encode('ascii') +
63 63 '._@')) | set(map(pycompat.bytechr, xrange(128, 256)))
64 64
65 65 # default set of valid characters for non-initial letters of symbols
66 66 _symletters = _syminitletters | set(pycompat.iterbytestr('-/'))
67 67
68 68 def tokenize(program, lookup=None, syminitletters=None, symletters=None):
69 69 '''
70 70 Parse a revset statement into a stream of tokens
71 71
72 72 ``syminitletters`` is the set of valid characters for the initial
73 73 letter of symbols.
74 74
75 75 By default, character ``c`` is recognized as valid for initial
76 76 letter of symbols, if ``c.isalnum() or c in '._@' or ord(c) > 127``.
77 77
78 78 ``symletters`` is the set of valid characters for non-initial
79 79 letters of symbols.
80 80
81 81 By default, character ``c`` is recognized as valid for non-initial
82 82 letters of symbols, if ``c.isalnum() or c in '-._/@' or ord(c) > 127``.
83 83
84 84 Check that @ is a valid unquoted token character (issue3686):
85 85 >>> list(tokenize(b"@::"))
86 86 [('symbol', '@', 0), ('::', None, 1), ('end', None, 3)]
87 87
88 88 '''
89 89 program = pycompat.bytestr(program)
90 90 if syminitletters is None:
91 91 syminitletters = _syminitletters
92 92 if symletters is None:
93 93 symletters = _symletters
94 94
95 95 if program and lookup:
96 96 # attempt to parse old-style ranges first to deal with
97 97 # things like old-tag which contain query metacharacters
98 98 parts = program.split(':', 1)
99 99 if all(lookup(sym) for sym in parts if sym):
100 100 if parts[0]:
101 101 yield ('symbol', parts[0], 0)
102 102 if len(parts) > 1:
103 103 s = len(parts[0])
104 104 yield (':', None, s)
105 105 if parts[1]:
106 106 yield ('symbol', parts[1], s + 1)
107 107 yield ('end', None, len(program))
108 108 return
109 109
110 110 pos, l = 0, len(program)
111 111 while pos < l:
112 112 c = program[pos]
113 113 if c.isspace(): # skip inter-token whitespace
114 114 pass
115 115 elif c == ':' and program[pos:pos + 2] == '::': # look ahead carefully
116 116 yield ('::', None, pos)
117 117 pos += 1 # skip ahead
118 118 elif c == '.' and program[pos:pos + 2] == '..': # look ahead carefully
119 119 yield ('..', None, pos)
120 120 pos += 1 # skip ahead
121 121 elif c == '#' and program[pos:pos + 2] == '##': # look ahead carefully
122 122 yield ('##', None, pos)
123 123 pos += 1 # skip ahead
124 124 elif c in _simpleopletters: # handle simple operators
125 125 yield (c, None, pos)
126 126 elif (c in _quoteletters or c == 'r' and
127 127 program[pos:pos + 2] in ("r'", 'r"')): # handle quoted strings
128 128 if c == 'r':
129 129 pos += 1
130 130 c = program[pos]
131 131 decode = lambda x: x
132 132 else:
133 133 decode = parser.unescapestr
134 134 pos += 1
135 135 s = pos
136 136 while pos < l: # find closing quote
137 137 d = program[pos]
138 138 if d == '\\': # skip over escaped characters
139 139 pos += 2
140 140 continue
141 141 if d == c:
142 142 yield ('string', decode(program[s:pos]), s)
143 143 break
144 144 pos += 1
145 145 else:
146 146 raise error.ParseError(_("unterminated string"), s)
147 147 # gather up a symbol/keyword
148 148 elif c in syminitletters:
149 149 s = pos
150 150 pos += 1
151 151 while pos < l: # find end of symbol
152 152 d = program[pos]
153 153 if d not in symletters:
154 154 break
155 155 if d == '.' and program[pos - 1] == '.': # special case for ..
156 156 pos -= 1
157 157 break
158 158 pos += 1
159 159 sym = program[s:pos]
160 160 if sym in keywords: # operator keywords
161 161 yield (sym, None, s)
162 162 elif '-' in sym:
163 163 # some jerk gave us foo-bar-baz, try to check if it's a symbol
164 164 if lookup and lookup(sym):
165 165 # looks like a real symbol
166 166 yield ('symbol', sym, s)
167 167 else:
168 168 # looks like an expression
169 169 parts = sym.split('-')
170 170 for p in parts[:-1]:
171 171 if p: # possible consecutive -
172 172 yield ('symbol', p, s)
173 173 s += len(p)
174 174 yield ('-', None, pos)
175 175 s += 1
176 176 if parts[-1]: # possible trailing -
177 177 yield ('symbol', parts[-1], s)
178 178 else:
179 179 yield ('symbol', sym, s)
180 180 pos -= 1
181 181 else:
182 182 raise error.ParseError(_("syntax error in revset '%s'") %
183 183 program, pos)
184 184 pos += 1
185 185 yield ('end', None, pos)
186 186
187 187 # helpers
188 188
189 189 _notset = object()
190 190
191 191 def getsymbol(x):
192 192 if x and x[0] == 'symbol':
193 193 return x[1]
194 194 raise error.ParseError(_('not a symbol'))
195 195
196 196 def getstring(x, err):
197 197 if x and (x[0] == 'string' or x[0] == 'symbol'):
198 198 return x[1]
199 199 raise error.ParseError(err)
200 200
201 201 def getinteger(x, err, default=_notset):
202 202 if not x and default is not _notset:
203 203 return default
204 204 try:
205 205 return int(getstring(x, err))
206 206 except ValueError:
207 207 raise error.ParseError(err)
208 208
209 209 def getboolean(x, err):
210 210 value = util.parsebool(getsymbol(x))
211 211 if value is not None:
212 212 return value
213 213 raise error.ParseError(err)
214 214
215 215 def getlist(x):
216 216 if not x:
217 217 return []
218 218 if x[0] == 'list':
219 219 return list(x[1:])
220 220 return [x]
221 221
222 222 def getrange(x, err):
223 223 if not x:
224 224 raise error.ParseError(err)
225 225 op = x[0]
226 226 if op == 'range':
227 227 return x[1], x[2]
228 228 elif op == 'rangepre':
229 229 return None, x[1]
230 230 elif op == 'rangepost':
231 231 return x[1], None
232 232 elif op == 'rangeall':
233 233 return None, None
234 234 raise error.ParseError(err)
235 235
236 236 def getargs(x, min, max, err):
237 237 l = getlist(x)
238 238 if len(l) < min or (max >= 0 and len(l) > max):
239 239 raise error.ParseError(err)
240 240 return l
241 241
242 242 def getargsdict(x, funcname, keys):
243 243 return parser.buildargsdict(getlist(x), funcname, parser.splitargspec(keys),
244 244 keyvaluenode='keyvalue', keynode='symbol')
245 245
246 246 # cache of {spec: raw parsed tree} built internally
247 247 _treecache = {}
248 248
249 249 def _cachedtree(spec):
250 250 # thread safe because parse() is reentrant and dict.__setitem__() is atomic
251 251 tree = _treecache.get(spec)
252 252 if tree is None:
253 253 _treecache[spec] = tree = parse(spec)
254 254 return tree
255 255
256 256 def _build(tmplspec, *repls):
257 257 """Create raw parsed tree from a template revset statement
258 258
259 259 >>> _build(b'f(_) and _', (b'string', b'1'), (b'symbol', b'2'))
260 260 ('and', ('func', ('symbol', 'f'), ('string', '1')), ('symbol', '2'))
261 261 """
262 262 template = _cachedtree(tmplspec)
263 263 return parser.buildtree(template, ('symbol', '_'), *repls)
264 264
265 265 def _match(patspec, tree):
266 266 """Test if a tree matches the given pattern statement; return the matches
267 267
268 268 >>> _match(b'f(_)', parse(b'f()'))
269 269 >>> _match(b'f(_)', parse(b'f(1)'))
270 270 [('func', ('symbol', 'f'), ('symbol', '1')), ('symbol', '1')]
271 271 >>> _match(b'f(_)', parse(b'f(1, 2)'))
272 272 """
273 273 pattern = _cachedtree(patspec)
274 274 return parser.matchtree(pattern, tree, ('symbol', '_'),
275 275 {'keyvalue', 'list'})
276 276
277 277 def _matchonly(revs, bases):
278 278 return _match('ancestors(_) and not ancestors(_)', ('and', revs, bases))
279 279
280 280 def _fixops(x):
281 281 """Rewrite raw parsed tree to resolve ambiguous syntax which cannot be
282 282 handled well by our simple top-down parser"""
283 283 if not isinstance(x, tuple):
284 284 return x
285 285
286 286 op = x[0]
287 287 if op == 'parent':
288 288 # x^:y means (x^) : y, not x ^ (:y)
289 289 # x^: means (x^) :, not x ^ (:)
290 290 post = ('parentpost', x[1])
291 291 if x[2][0] == 'dagrangepre':
292 292 return _fixops(('dagrange', post, x[2][1]))
293 293 elif x[2][0] == 'dagrangeall':
294 294 return _fixops(('dagrangepost', post))
295 295 elif x[2][0] == 'rangepre':
296 296 return _fixops(('range', post, x[2][1]))
297 297 elif x[2][0] == 'rangeall':
298 298 return _fixops(('rangepost', post))
299 299 elif op == 'or':
300 300 # make number of arguments deterministic:
301 301 # x + y + z -> (or x y z) -> (or (list x y z))
302 302 return (op, _fixops(('list',) + x[1:]))
303 303 elif op == 'subscript' and x[1][0] == 'relation':
304 304 # x#y[z] ternary
305 305 return _fixops(('relsubscript', x[1][1], x[1][2], x[2]))
306 306
307 307 return (op,) + tuple(_fixops(y) for y in x[1:])
308 308
309 309 def _analyze(x):
310 310 if x is None:
311 311 return x
312 312
313 313 op = x[0]
314 314 if op == 'minus':
315 315 return _analyze(_build('_ and not _', *x[1:]))
316 316 elif op == 'only':
317 317 return _analyze(_build('only(_, _)', *x[1:]))
318 318 elif op == 'onlypost':
319 319 return _analyze(_build('only(_)', x[1]))
320 320 elif op == 'dagrangeall':
321 321 raise error.ParseError(_("can't use '::' in this context"))
322 322 elif op == 'dagrangepre':
323 323 return _analyze(_build('ancestors(_)', x[1]))
324 324 elif op == 'dagrangepost':
325 325 return _analyze(_build('descendants(_)', x[1]))
326 326 elif op == 'negate':
327 327 s = getstring(x[1], _("can't negate that"))
328 328 return _analyze(('string', '-' + s))
329 329 elif op in ('string', 'symbol'):
330 330 return x
331 331 elif op == 'rangeall':
332 332 return (op, None)
333 333 elif op in {'or', 'not', 'rangepre', 'rangepost', 'parentpost'}:
334 334 return (op, _analyze(x[1]))
335 335 elif op == 'group':
336 336 return _analyze(x[1])
337 337 elif op in {'and', 'dagrange', 'range', 'parent', 'ancestor', 'relation',
338 338 'subscript'}:
339 339 ta = _analyze(x[1])
340 340 tb = _analyze(x[2])
341 341 return (op, ta, tb)
342 342 elif op == 'relsubscript':
343 343 ta = _analyze(x[1])
344 344 tb = _analyze(x[2])
345 345 tc = _analyze(x[3])
346 346 return (op, ta, tb, tc)
347 347 elif op == 'list':
348 348 return (op,) + tuple(_analyze(y) for y in x[1:])
349 349 elif op == 'keyvalue':
350 350 return (op, x[1], _analyze(x[2]))
351 351 elif op == 'func':
352 352 return (op, x[1], _analyze(x[2]))
353 353 raise ValueError('invalid operator %r' % op)
354 354
355 355 def analyze(x):
356 356 """Transform raw parsed tree to evaluatable tree which can be fed to
357 357 optimize() or getset()
358 358
359 359 All pseudo operations should be mapped to real operations or functions
360 360 defined in methods or symbols table respectively.
361 361 """
362 362 return _analyze(x)
363 363
364 364 def _optimize(x):
365 365 if x is None:
366 366 return 0, x
367 367
368 368 op = x[0]
369 369 if op in ('string', 'symbol'):
370 370 return 0.5, x # single revisions are small
371 371 elif op == 'and':
372 372 wa, ta = _optimize(x[1])
373 373 wb, tb = _optimize(x[2])
374 374 w = min(wa, wb)
375 375
376 376 # (draft/secret/_notpublic() & ::x) have a fast path
377 377 m = _match('_() & ancestors(_)', ('and', ta, tb))
378 378 if m and getsymbol(m[1]) in {'draft', 'secret', '_notpublic'}:
379 379 return w, _build('_phaseandancestors(_, _)', m[1], m[2])
380 380
381 381 # (::x and not ::y)/(not ::y and ::x) have a fast path
382 382 m = _matchonly(ta, tb) or _matchonly(tb, ta)
383 383 if m:
384 384 return w, _build('only(_, _)', *m[1:])
385 385
386 386 m = _match('not _', tb)
387 387 if m:
388 388 return wa, ('difference', ta, m[1])
389 389 if wa > wb:
390 390 op = 'andsmally'
391 391 return w, (op, ta, tb)
392 392 elif op == 'or':
393 393 # fast path for machine-generated expression, that is likely to have
394 394 # lots of trivial revisions: 'a + b + c()' to '_list(a b) + c()'
395 395 ws, ts, ss = [], [], []
396 396 def flushss():
397 397 if not ss:
398 398 return
399 399 if len(ss) == 1:
400 400 w, t = ss[0]
401 401 else:
402 402 s = '\0'.join(t[1] for w, t in ss)
403 403 y = _build('_list(_)', ('string', s))
404 404 w, t = _optimize(y)
405 405 ws.append(w)
406 406 ts.append(t)
407 407 del ss[:]
408 408 for y in getlist(x[1]):
409 409 w, t = _optimize(y)
410 410 if t is not None and (t[0] == 'string' or t[0] == 'symbol'):
411 411 ss.append((w, t))
412 412 continue
413 413 flushss()
414 414 ws.append(w)
415 415 ts.append(t)
416 416 flushss()
417 417 if len(ts) == 1:
418 418 return ws[0], ts[0] # 'or' operation is fully optimized out
419 419 return max(ws), (op, ('list',) + tuple(ts))
420 420 elif op == 'not':
421 421 # Optimize not public() to _notpublic() because we have a fast version
422 422 if _match('public()', x[1]):
423 423 o = _optimize(_build('_notpublic()'))
424 424 return o[0], o[1]
425 425 else:
426 426 o = _optimize(x[1])
427 427 return o[0], (op, o[1])
428 428 elif op == 'rangeall':
429 429 return 1, x
430 430 elif op in ('rangepre', 'rangepost', 'parentpost'):
431 431 o = _optimize(x[1])
432 432 return o[0], (op, o[1])
433 433 elif op in ('dagrange', 'range'):
434 434 wa, ta = _optimize(x[1])
435 435 wb, tb = _optimize(x[2])
436 436 return wa + wb, (op, ta, tb)
437 437 elif op in ('parent', 'ancestor', 'relation', 'subscript'):
438 438 w, t = _optimize(x[1])
439 439 return w, (op, t, x[2])
440 440 elif op == 'relsubscript':
441 441 w, t = _optimize(x[1])
442 442 return w, (op, t, x[2], x[3])
443 443 elif op == 'list':
444 444 ws, ts = zip(*(_optimize(y) for y in x[1:]))
445 445 return sum(ws), (op,) + ts
446 446 elif op == 'keyvalue':
447 447 w, t = _optimize(x[2])
448 448 return w, (op, x[1], t)
449 449 elif op == 'func':
450 450 f = getsymbol(x[1])
451 451 wa, ta = _optimize(x[2])
452 452 w = getattr(symbols.get(f), '_weight', 1)
453 453 return w + wa, (op, x[1], ta)
454 454 raise ValueError('invalid operator %r' % op)
455 455
456 456 def optimize(tree):
457 457 """Optimize evaluatable tree
458 458
459 459 All pseudo operations should be transformed beforehand.
460 460 """
461 461 _weight, newtree = _optimize(tree)
462 462 return newtree
463 463
464 464 # the set of valid characters for the initial letter of symbols in
465 465 # alias declarations and definitions
466 466 _aliassyminitletters = _syminitletters | {'$'}
467 467
468 468 def _parsewith(spec, lookup=None, syminitletters=None):
469 469 """Generate a parse tree of given spec with given tokenizing options
470 470
471 471 >>> _parsewith(b'foo($1)', syminitletters=_aliassyminitletters)
472 472 ('func', ('symbol', 'foo'), ('symbol', '$1'))
473 473 >>> _parsewith(b'$1')
474 474 Traceback (most recent call last):
475 475 ...
476 476 ParseError: ("syntax error in revset '$1'", 0)
477 477 >>> _parsewith(b'foo bar')
478 478 Traceback (most recent call last):
479 479 ...
480 480 ParseError: ('invalid token', 4)
481 481 """
482 482 p = parser.parser(elements)
483 483 tree, pos = p.parse(tokenize(spec, lookup=lookup,
484 484 syminitletters=syminitletters))
485 485 if pos != len(spec):
486 486 raise error.ParseError(_('invalid token'), pos)
487 487 return _fixops(parser.simplifyinfixops(tree, ('list', 'or')))
488 488
489 489 class _aliasrules(parser.basealiasrules):
490 490 """Parsing and expansion rule set of revset aliases"""
491 491 _section = _('revset alias')
492 492
493 493 @staticmethod
494 494 def _parse(spec):
495 495 """Parse alias declaration/definition ``spec``
496 496
497 497 This allows symbol names to use also ``$`` as an initial letter
498 498 (for backward compatibility), and callers of this function should
499 499 examine whether ``$`` is used also for unexpected symbols or not.
500 500 """
501 501 return _parsewith(spec, syminitletters=_aliassyminitletters)
502 502
503 503 @staticmethod
504 504 def _trygetfunc(tree):
505 505 if tree[0] == 'func' and tree[1][0] == 'symbol':
506 506 return tree[1][1], getlist(tree[2])
507 507
508 508 def expandaliases(tree, aliases, warn=None):
509 509 """Expand aliases in a tree, aliases is a list of (name, value) tuples"""
510 510 aliases = _aliasrules.buildmap(aliases)
511 511 tree = _aliasrules.expand(aliases, tree)
512 512 # warn about problematic (but not referred) aliases
513 513 if warn is not None:
514 514 for name, alias in sorted(aliases.iteritems()):
515 515 if alias.error and not alias.warned:
516 516 warn(_('warning: %s\n') % (alias.error))
517 517 alias.warned = True
518 518 return tree
519 519
520 520 def foldconcat(tree):
521 521 """Fold elements to be concatenated by `##`
522 522 """
523 523 if not isinstance(tree, tuple) or tree[0] in ('string', 'symbol'):
524 524 return tree
525 525 if tree[0] == '_concat':
526 526 pending = [tree]
527 527 l = []
528 528 while pending:
529 529 e = pending.pop()
530 530 if e[0] == '_concat':
531 531 pending.extend(reversed(e[1:]))
532 532 elif e[0] in ('string', 'symbol'):
533 533 l.append(e[1])
534 534 else:
535 535 msg = _("\"##\" can't concatenate \"%s\" element") % (e[0])
536 536 raise error.ParseError(msg)
537 537 return ('string', ''.join(l))
538 538 else:
539 539 return tuple(foldconcat(t) for t in tree)
540 540
541 541 def parse(spec, lookup=None):
542 542 return _parsewith(spec, lookup=lookup)
543 543
544 544 def _quote(s):
545 545 r"""Quote a value in order to make it safe for the revset engine.
546 546
547 547 >>> _quote(b'asdf')
548 548 "'asdf'"
549 549 >>> _quote(b"asdf'\"")
550 550 '\'asdf\\\'"\''
551 551 >>> _quote(b'asdf\'')
552 552 "'asdf\\''"
553 553 >>> _quote(1)
554 554 "'1'"
555 555 """
556 556 return "'%s'" % util.escapestr(pycompat.bytestr(s))
557 557
558 558 def formatspec(expr, *args):
559 559 '''
560 560 This is a convenience function for using revsets internally, and
561 561 escapes arguments appropriately. Aliases are intentionally ignored
562 562 so that intended expression behavior isn't accidentally subverted.
563 563
564 564 Supported arguments:
565 565
566 566 %r = revset expression, parenthesized
567 567 %d = int(arg), no quoting
568 568 %s = string(arg), escaped and single-quoted
569 569 %b = arg.branch(), escaped and single-quoted
570 570 %n = hex(arg), single-quoted
571 571 %% = a literal '%'
572 572
573 573 Prefixing the type with 'l' specifies a parenthesized list of that type.
574 574
575 575 >>> formatspec(b'%r:: and %lr', b'10 or 11', (b"this()", b"that()"))
576 576 '(10 or 11):: and ((this()) or (that()))'
577 577 >>> formatspec(b'%d:: and not %d::', 10, 20)
578 578 '10:: and not 20::'
579 579 >>> formatspec(b'%ld or %ld', [], [1])
580 580 "_list('') or 1"
581 581 >>> formatspec(b'keyword(%s)', b'foo\\xe9')
582 582 "keyword('foo\\\\xe9')"
583 583 >>> b = lambda: b'default'
584 584 >>> b.branch = b
585 585 >>> formatspec(b'branch(%b)', b)
586 586 "branch('default')"
587 587 >>> formatspec(b'root(%ls)', [b'a', b'b', b'c', b'd'])
588 "root(_list('a\\x00b\\x00c\\x00d'))"
588 "root(_list('a\\\\x00b\\\\x00c\\\\x00d'))"
589 >>> formatspec('%ls', ['a', "'"])
590 "_list('a\\\\x00\\\\'')"
589 591 '''
590 592
591 593 def argtype(c, arg):
592 594 if c == 'd':
593 595 return '%d' % int(arg)
594 596 elif c == 's':
595 597 return _quote(arg)
596 598 elif c == 'r':
597 599 parse(arg) # make sure syntax errors are confined
598 600 return '(%s)' % arg
599 601 elif c == 'n':
600 602 return _quote(node.hex(arg))
601 603 elif c == 'b':
602 604 try:
603 605 return _quote(arg.branch())
604 606 except AttributeError:
605 607 raise TypeError
606 608 raise error.ParseError(_('unexpected revspec format character %s') % c)
607 609
608 610 def listexp(s, t):
609 611 l = len(s)
610 612 if l == 0:
611 613 return "_list('')"
612 614 elif l == 1:
613 615 return argtype(t, s[0])
614 616 elif t == 'd':
615 617 return "_intlist('%s')" % "\0".join('%d' % int(a) for a in s)
616 618 elif t == 's':
617 return "_list('%s')" % "\0".join(s)
619 return "_list(%s)" % _quote("\0".join(s))
618 620 elif t == 'n':
619 621 return "_hexlist('%s')" % "\0".join(node.hex(a) for a in s)
620 622 elif t == 'b':
621 623 try:
622 624 return "_list('%s')" % "\0".join(a.branch() for a in s)
623 625 except AttributeError:
624 626 raise TypeError
625 627
626 628 m = l // 2
627 629 return '(%s or %s)' % (listexp(s[:m], t), listexp(s[m:], t))
628 630
629 631 expr = pycompat.bytestr(expr)
630 632 argiter = iter(args)
631 633 ret = []
632 634 pos = 0
633 635 while pos < len(expr):
634 636 q = expr.find('%', pos)
635 637 if q < 0:
636 638 ret.append(expr[pos:])
637 639 break
638 640 ret.append(expr[pos:q])
639 641 pos = q + 1
640 642 try:
641 643 d = expr[pos]
642 644 except IndexError:
643 645 raise error.ParseError(_('incomplete revspec format character'))
644 646 if d == '%':
645 647 ret.append(d)
646 648 pos += 1
647 649 continue
648 650
649 651 try:
650 652 arg = next(argiter)
651 653 except StopIteration:
652 654 raise error.ParseError(_('missing argument for revspec'))
653 655 if d == 'l':
654 656 # a list of some type
655 657 pos += 1
656 658 try:
657 659 d = expr[pos]
658 660 except IndexError:
659 661 raise error.ParseError(_('incomplete revspec format character'))
660 662 try:
661 663 ret.append(listexp(list(arg), d))
662 664 except (TypeError, ValueError):
663 665 raise error.ParseError(_('invalid argument for revspec'))
664 666 else:
665 667 try:
666 668 ret.append(argtype(d, arg))
667 669 except (TypeError, ValueError):
668 670 raise error.ParseError(_('invalid argument for revspec'))
669 671 pos += 1
670 672
671 673 try:
672 674 next(argiter)
673 675 raise error.ParseError(_('too many revspec arguments specified'))
674 676 except StopIteration:
675 677 pass
676 678 return ''.join(ret)
677 679
678 680 def prettyformat(tree):
679 681 return parser.prettyformat(tree, ('string', 'symbol'))
680 682
681 683 def depth(tree):
682 684 if isinstance(tree, tuple):
683 685 return max(map(depth, tree)) + 1
684 686 else:
685 687 return 0
686 688
687 689 def funcsused(tree):
688 690 if not isinstance(tree, tuple) or tree[0] in ('string', 'symbol'):
689 691 return set()
690 692 else:
691 693 funcs = set()
692 694 for s in tree[1:]:
693 695 funcs |= funcsused(s)
694 696 if tree[0] == 'func':
695 697 funcs.add(tree[1][1])
696 698 return funcs
697 699
698 700 _hashre = util.re.compile('[0-9a-fA-F]{1,40}$')
699 701
700 702 def _ishashlikesymbol(symbol):
701 703 """returns true if the symbol looks like a hash"""
702 704 return _hashre.match(symbol)
703 705
704 706 def gethashlikesymbols(tree):
705 707 """returns the list of symbols of the tree that look like hashes
706 708
707 709 >>> gethashlikesymbols(('dagrange', ('symbol', '3'), ('symbol', 'abe3ff')))
708 710 ['3', 'abe3ff']
709 711 >>> gethashlikesymbols(('func', ('symbol', 'precursors'), ('symbol', '.')))
710 712 []
711 713 >>> gethashlikesymbols(('func', ('symbol', 'precursors'), ('symbol', '34')))
712 714 ['34']
713 715 >>> gethashlikesymbols(('symbol', 'abe3ffZ'))
714 716 []
715 717 """
716 718 if not tree:
717 719 return []
718 720
719 721 if tree[0] == "symbol":
720 722 if _ishashlikesymbol(tree[1]):
721 723 return [tree[1]]
722 724 elif len(tree) >= 3:
723 725 results = []
724 726 for subtree in tree[1:]:
725 727 results += gethashlikesymbols(subtree)
726 728 return results
727 729 return []
General Comments 0
You need to be logged in to leave comments. Login now