|
@@ -3,17 +3,20 @@ import pyparsing as pp
|
|
|
|
|
|
class QueryParser:
|
|
class QueryParser:
|
|
def __init__(self):
|
|
def __init__(self):
|
|
- # TODO: Try to disallow invalid combinations
|
|
|
|
- # TODO: Have tests for invalid combinations
|
|
|
|
# TODO: allow lists as input, like: ipaddress(macaddress in [1,2,3])
|
|
# TODO: allow lists as input, like: ipaddress(macaddress in [1,2,3])
|
|
extractor = pp.Keyword("random") ^ pp.Keyword("first") ^ pp.Keyword("last")
|
|
extractor = pp.Keyword("random") ^ pp.Keyword("first") ^ pp.Keyword("last")
|
|
- selector = pp.Keyword("most_used") ^ pp.Keyword("least_used") ^ pp.Keyword("avg") ^ pp.Keyword("all")
|
|
|
|
- attribute = pp.Keyword("ipaddress") ^ pp.Keyword("macaddress") ^ pp.Keyword("portnumber") ^\
|
|
|
|
- pp.Keyword("protocolname") ^ pp.Keyword("ttlvalue") ^ pp.Keyword("mssvalue") ^\
|
|
|
|
- pp.Keyword("winsize") ^ pp.Keyword("ipclass") ^ pp.Keyword("pktssent") ^\
|
|
|
|
- pp.Keyword("pktsreceived") ^ pp.Keyword("mss") ^ pp.Keyword("kbytesreceived") ^\
|
|
|
|
- pp.Keyword("kbytessent")
|
|
|
|
- simple_selector_query = selector + pp.Suppress("(") + attribute + pp.Suppress(")")
|
|
|
|
|
|
+ # Valid selectors - except "avg", because not all attributes can be combined with it
|
|
|
|
+ selector_no_avg = pp.Keyword("most_used") ^ pp.Keyword("least_used") ^ pp.Keyword("all")
|
|
|
|
+ attributes_no_avg = pp.Keyword("ipaddress") ^ pp.Keyword("macaddress") ^ pp.Keyword("portnumber") ^\
|
|
|
|
+ pp.Keyword("protocolname") ^ pp.Keyword("winsize") ^ pp.Keyword("ipclass")
|
|
|
|
+
|
|
|
|
+ attributes_avg = pp.Keyword("ttlvalue") ^ pp.Keyword("mssvalue") ^\
|
|
|
|
+ pp.Keyword("pktssent") ^ pp.Keyword("pktsreceived") ^ pp.Keyword("mss") ^\
|
|
|
|
+ pp.Keyword("kbytesreceived") ^ pp.Keyword("kbytessent")
|
|
|
|
+
|
|
|
|
+ attributes_all = attributes_no_avg ^ attributes_avg
|
|
|
|
+ simple_selector_query = (selector_no_avg + pp.Suppress("(") + attributes_all + pp.Suppress(")")) ^\
|
|
|
|
+ (pp.Keyword("avg") + pp.Suppress("(") + attributes_avg + pp.Suppress(")"))
|
|
|
|
|
|
param_selectors = pp.Keyword("ipaddress").setParseAction(pp.replaceWith("ipaddress_param")) ^\
|
|
param_selectors = pp.Keyword("ipaddress").setParseAction(pp.replaceWith("ipaddress_param")) ^\
|
|
pp.Keyword("macaddress").setParseAction(pp.replaceWith("macaddress_param"))
|
|
pp.Keyword("macaddress").setParseAction(pp.replaceWith("macaddress_param"))
|
|
@@ -21,9 +24,8 @@ class QueryParser:
|
|
operators = pp.Literal("<=") ^ pp.Literal("<") ^ pp.Literal("=") ^\
|
|
operators = pp.Literal("<=") ^ pp.Literal("<") ^ pp.Literal("=") ^\
|
|
pp.Literal(">=") ^ pp.Literal(">") ^ pp.CaselessLiteral("in")
|
|
pp.Literal(">=") ^ pp.Literal(">") ^ pp.CaselessLiteral("in")
|
|
expr = pp.Forward()
|
|
expr = pp.Forward()
|
|
- comparison = pp.Group(attribute + operators + (pp.Word(pp.alphanums + ".:") ^ expr))
|
|
|
|
|
|
+ comparison = pp.Group(attributes_all + operators + (pp.Word(pp.alphanums + ".:") ^ expr))
|
|
parameterized_query = param_selectors + pp.Suppress("(") + pp.Group(pp.delimitedList(comparison)) + pp.Suppress(")")
|
|
parameterized_query = param_selectors + pp.Suppress("(") + pp.Group(pp.delimitedList(comparison)) + pp.Suppress(")")
|
|
- # parameterized_query = param_selectors + pp.Suppress("(") + comparison + pp.Suppress(")")
|
|
|
|
|
|
|
|
all_selector_queries = (simple_selector_query ^ parameterized_query)
|
|
all_selector_queries = (simple_selector_query ^ parameterized_query)
|
|
extractor_selector_query = extractor + pp.Suppress("(") + all_selector_queries + pp.Suppress(")")
|
|
extractor_selector_query = extractor + pp.Suppress("(") + all_selector_queries + pp.Suppress(")")
|