Ejemplo n.º 1
0
def parse_boolean(q):
    lhs, rest = parse_expression(q)
    if rest:
        for op, kls in sorted(BOOLEAN_OPERATORS.iteritems(), key=len, reverse=True):
            if rest[0] == op:
                rhs, rest = parse_boolean(rest[1:])
                lhs = kls(lhs, rhs)
                break
        else:
            if rest[0] == ")":
                return lhs, rest
            else:
                raise UnexpectedTokenError(rest)
    return lhs, rest
Ejemplo n.º 2
0
def parse_boolean(q):
    lhs, rest = parse_expression(q)
    if rest:
        for op, kls in sorted(BOOLEAN_OPERATORS.iteritems(),
                              key=len,
                              reverse=True):
            if rest[0] == op:
                rhs, rest = parse_boolean(rest[1:])
                lhs = kls(lhs, rhs)
                break
        else:
            if rest[0] == ")":
                return lhs, rest
            else:
                raise UnexpectedTokenError(rest)
    return lhs, rest
Ejemplo n.º 3
0
def lex(q):
    keywords = ["attr"]
    keywords.extend(SEARCH_KEYWORDS)
    keywords.extend(BOOLEAN_OPERATORS.keys())
    keywords.extend(UNARY_BOOLEAN_OPERATORS.keys())
    keywords.extend(INFIX_OPERATORS.keys())
    keywords.extend(("(", ")"))
    keywords.sort(key=len, reverse=True)
    results = []
    while q:
        q = q.lstrip()
        for keyword in keywords:
            if q.startswith(keyword):
                if _unquoted_string_re.match(keyword):
                    if not _separator_re.match(consume(keyword, q)):
                        continue
                results.append(keyword)
                q = consume(keyword, q)
                break
        else:
            result, q = lex_string(q)
            results.append(result)
    return results
Ejemplo n.º 4
0
def lex(q):
    keywords = ["attr"]
    keywords.extend(SEARCH_KEYWORDS)
    keywords.extend(BOOLEAN_OPERATORS.keys())
    keywords.extend(UNARY_BOOLEAN_OPERATORS.keys())
    keywords.extend(INFIX_OPERATORS.keys())
    keywords.extend(("(", ")"))
    keywords.sort(key=len, reverse=True)
    results = []
    while q:
        q = q.lstrip()
        for keyword in keywords:
            if q.startswith(keyword):
                if _unquoted_string_re.match(keyword):
                    if not _separator_re.match(consume(keyword, q)):
                        continue
                results.append(keyword)
                q = consume(keyword, q)
                break
        else:
            result, q = lex_string(q)
            results.append(result)
    return results