1
  B   %ˆl–äY>”JeJe@†à¸Û*bÑ¿�þ]ÙY¹BáYI#MùÖ„à|?Õ_ŠI|¥�+Wé3ž¯ÀÔ ?÷     %# revsetlang.py - parser, tokenizer and utility for revision set language
#
# Copyright 2010 Olivia Mackall <olivia@selenic.com>
#
# 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 _
from .pycompat import getattr
from .node import hex
from . import (
    error,
    parser,
    pycompat,
    smartset,
    util,
)
from .utils import stringutil

elements = {
    # token-type: binding-strength, primary, prefix, infix, suffix
    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"::": (
        17,
        b"dagrangeall",
        (b"dagrangepre", 17),
        (b"dagrange", 17),
        b"dagrangepost",
    ),
    b"..": (
        17,
        b"dagrangeall",
        (b"dagrangepre", 17),
        (b"dagrange", 17),
        b"dagrangepost",
    ),
    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),
}

keywords = {b'and', b'or', b'not'}

symbols = {}

_quoteletters = {b'"', b"'"}
_simpleopletters = set(pycompat.iterbytestr(b"()[]#:=,-|&+!~^%"))

# default set of valid characters for the initial letter of symbols
_syminitletters = set(
    pycompat.iterbytestr(
        pycompat.sysbytes(string.ascii_letters)
        + pycompat.sysbytes(string.digits)
        + b'._@'
    )
) | set(map(pycompat.bytechr, range(128, 256)))

# default set of valid characters for non-initial letters of symbols
_symletters = _syminitletters | set(pycompat.iterbytestr(b'-/'))


def tokenize(program, lookup=None, syminitletters=None, symletters=None):
    """
    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):
    >>> list(tokenize(b"@::"))
    [('symbol', '@', 0), ('::', None, 1), ('end', None, 3)]

    """
    if not isinstance(program, bytes):
        raise error.ProgrammingError(
            b'revset statement must be bytes, got %r' % program
        )
    program = pycompat.bytestr(program)
    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
        parts = program.split(b':', 1)
        if all(lookup(sym) for sym in parts if sym):
            if parts[0]:
                yield (b'symbol', parts[0], 0)
            if len(parts