/usr/lib/gdesklets/utils/QueryParser.py is in gdesklets 0.36.1-5.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 | try:
set
except NameError:
from sets import Set as set
#
# Class for parsing expressions in a simple query language.
#
# Grammar: CLAUSE -> ( CMD ARGS )
# ARGS -> ARG ARGS | e
# ARG -> CLAUSE | STRING
# STRING -> ".*" | '.*'
# CMD -> [A-Z]+
#
# Usage: myparser = QueryParser(mytrie)
# result = myparser.parse(query)
#
class QueryParser(object):
__slots__ = '__trie'
def __init__(self, trie):
self.__trie = trie
def parse(self, query):
query, result = self.__parse_CLAUSE(query)
if (query.strip()): raise SyntaxError("trailing garbage: %s" % (query))
return result
def __parse_CLAUSE(self, query):
query = query.lstrip()
token = query[0]
query = query[1:]
if (token != "("): raise SyntaxError("'(' expected.")
# read command
query = query.lstrip()
pos = query.find(" ")
cmd = query[:pos]
query = query[pos:]
if (cmd == "AND"):
query, r1 = self.__parse_ARG(query)
query, r2 = self.__parse_ARG(query)
result = set(r1)
result.intersection_update(r2)
result = list(result)
elif (cmd == "OR"):
query, r1 = self.__parse_ARG(query)
query, r2 = self.__parse_ARG(query)
result = set(r1)
result.union_update(r2)
result = list(result)
elif (cmd == "MATCH"):
query, r1 = self.__parse_ARG(query)
query, r2 = self.__parse_ARG(query)
result = self.__trie.retrieve([r1] + list(r2))
# read paren
query = query.lstrip()
token = query[0]
if (token != ")"): raise SyntaxError("')' expected. " + query)
query = query[1:]
return (query, result)
def __parse_ARG(self, query):
query = query.lstrip()
token = query[0]
if (token == "("):
query, result = self.__parse_CLAUSE(query)
elif (token in ["\"", "'"]):
query, result = self.__parse_STRING(query)
else:
raise SyntaxError("'(' or '\"' expected.")
return (query, result)
def __parse_STRING(self, query):
delimiter = query[0]
value = ""
cnt = 1
c = query[cnt]
while (c != delimiter):
value += c
cnt += 1
c = query[cnt]
#end while
query = query[cnt + 1:]
return (query, value)
|