A semantic search engine for source code https://bitshift.benkurtovic.com/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

99 lines
2.4 KiB

  1. """
  2. This subpackage contains code to parse search queries received from the
  3. frontend into trees that can be used by the database backend.
  4. """
  5. from shlex import split
  6. from .nodes import * ## TODO
  7. from .tree import Tree
  8. from ..languages import LANGS
  9. __all__ = ["QueryParseException", "parse_query"]
  10. class QueryParseException(Exception):
  11. """Raised by parse_query() when a query is invalid."""
  12. pass
  13. class _QueryParser(object):
  14. """Wrapper class with methods to parse queries. Used as a singleton."""
  15. def __init__(self):
  16. prefixes = {
  17. "language": _parse_language,
  18. "author": _parse_author,
  19. "modified": _parse_modified,
  20. "created": _parse_created,
  21. "symbol": _parse_symbol,
  22. "function": _parse_function,
  23. "class": _parse_class,
  24. "variable": _parse_variable
  25. }
  26. def _parse_language(self, term):
  27. pass
  28. def _parse_author(self, term):
  29. pass
  30. def _parse_modified(self, term):
  31. pass
  32. def _parse_created(self, term):
  33. pass
  34. def _parse_symbol(self, term):
  35. pass
  36. def _parse_function(self, term):
  37. pass
  38. def _parse_class(self, term):
  39. pass
  40. def _parse_variable(self, term):
  41. pass
  42. def parse(self, query):
  43. """
  44. Parse a search query.
  45. The result is normalized with a sorting function so that ``"foo OR bar"``
  46. and ``"bar OR foo"`` result in the same tree. This is important for caching
  47. purposes.
  48. :param query: The query be converted.
  49. :type query: str
  50. :return: A tree storing the data in the query.
  51. :rtype: :py:class:`~.query.tree.Tree`
  52. :raises: :py:class:`.QueryParseException`
  53. """
  54. for term in split(query):
  55. if ":" in term and not term[0] == ":":
  56. prefix = term.split(":")[0]
  57. # language:"Python"
  58. # lang:
  59. # l:
  60. # author:"Ben Kurtovic"
  61. # modified:before
  62. # modified:after
  63. # created:before
  64. # created:after:"Jaunary 4, 2014"
  65. # func:"foobar"
  66. # func:re|gex:"foo?b|car"
  67. # "foo" -> Tree()
  68. # "foo bar" -> "foo bar" OR ("foo" or "bar")
  69. # "foo bar baz" -> ""foo bar baz" OR ("foo" OR "bar baz") OR ("foo" OR "bar baz") OR ('foo' OR 'bar' OR 'baz')"
  70. parse_query = _QueryParser().parse