QueryParser.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. import pyparsing as pp
  2. class QueryParser:
  3. def __init__(self):
  4. """
  5. Constructs a parser for all named queries using PyParsing.
  6. """
  7. # TODO: allow lists as input, like: ipaddress(macaddress in [1,2,3])
  8. extractor = pp.Keyword("random") ^ pp.Keyword("first") ^ pp.Keyword("last")
  9. # Valid selectors - except "avg", because not all attributes can be combined with it
  10. selector_no_avg = pp.Keyword("most_used") ^ pp.Keyword("least_used") ^ pp.Keyword("all")
  11. # All attributes that cannot be combined with "avg"
  12. attributes_no_avg = pp.Keyword("ipaddress") ^ pp.Keyword("macaddress") ^ pp.Keyword("portnumber") ^\
  13. pp.Keyword("protocolname") ^ pp.Keyword("winsize") ^ pp.Keyword("ipclass")
  14. # All attributes that can be combined with "avg"
  15. attributes_avg = pp.Keyword("ttlvalue") ^ pp.Keyword("mssvalue") ^\
  16. pp.Keyword("pktssent") ^ pp.Keyword("pktsreceived") ^ pp.Keyword("mss") ^\
  17. pp.Keyword("kbytesreceived") ^ pp.Keyword("kbytessent")
  18. # Collection of all attributes for simpler specification
  19. attributes_all = attributes_no_avg ^ attributes_avg
  20. # Simple selector + attribute query, only allowing "avg" with compatible attributes
  21. simple_selector_query = (selector_no_avg + pp.Suppress("(") + attributes_all + pp.Suppress(")")) ^\
  22. (pp.Keyword("avg") + pp.Suppress("(") + attributes_avg + pp.Suppress(")"))
  23. # Selectors for parameterized queries - they are replaced in the result to avoid ambiguity
  24. param_selectors = pp.Keyword("ipaddress").setParseAction(pp.replaceWith("ipaddress_param")) ^\
  25. pp.Keyword("macaddress").setParseAction(pp.replaceWith("macaddress_param"))
  26. # All operators allowed in parameterized queries
  27. operators = pp.Literal("<=") ^ pp.Literal("<") ^ pp.Literal("=") ^\
  28. pp.Literal(">=") ^ pp.Literal(">") ^ pp.CaselessLiteral("in")
  29. # Placeholder for nesting in parameterized queries
  30. expr = pp.Forward()
  31. # One "attribute-operator-value" triplet. Value can be alphanumeric plus dot and colon, or a nested query
  32. comparison = pp.Group(attributes_all + operators + (pp.Word(pp.alphanums + ".:") ^ expr))
  33. # A full parameterized query, consisting of a parameterized selector and a comma-separated list of comparisons
  34. parameterized_query = param_selectors + pp.Suppress("(") + pp.Group(pp.delimitedList(comparison)) + pp.Suppress(")")
  35. # Combination of simple and parameterized queries
  36. all_selector_queries = (simple_selector_query ^ parameterized_query)
  37. # All queries can be combined with an extractor
  38. extractor_selector_query = extractor + pp.Suppress("(") + all_selector_queries + pp.Suppress(")")
  39. # Queries can be used with an extractor or without
  40. named_query = (extractor_selector_query ^ all_selector_queries)
  41. # The placeholder can be replaced with any query
  42. expr << pp.Group(named_query)
  43. # Make sure all queries end with a semicolon, and we're done
  44. self.full_query = named_query + pp.Suppress(";")
  45. def parse_query(self, querystring: str) -> pp.ParseResults:
  46. """
  47. Parses the passed query with a pre-constructed parser.
  48. :param querystring: The named query to be executed
  49. :return: A ParseResults-object, which essentially is a list of tokens
  50. """
  51. return self.full_query.parseString(querystring)