Kaynağa Gözat

Literal parsing; term parsing hooks; unit test stubs.

tags/v1.0^2
Ben Kurtovic 10 yıl önce
ebeveyn
işleme
c9258f35e4
3 değiştirilmiş dosya ile 49 ekleme ve 13 silme
  1. +32
    -13
      bitshift/query/__init__.py
  2. +0
    -0
     
  3. +17
    -0
      test/test_query_parser.py

+ 32
- 13
bitshift/query/__init__.py Dosyayı Görüntüle

@@ -19,15 +19,15 @@ class _QueryParser(object):
"""Wrapper class with methods to parse queries. Used as a singleton."""

def __init__(self):
prefixes = {
"language": _parse_language,
"author": _parse_author,
"modified": _parse_modified,
"created": _parse_created,
"symbol": _parse_symbol,
"function": _parse_function,
"class": _parse_class,
"variable": _parse_variable
self._prefixes = {
self._parse_language: ["l", "lang", "language"],
self._parse_author: ["a", "author"],
self._parse_modified: ["m", "mod", "modified", "modify"],
self._parse_created: ["cr", "create", "created"],
self._parse_symbol: ["s", "sym", "symb", "symbol"],
self._parse_function: ["f", "fn", "func", "function"],
self._parse_class: ["cl", "class", "clss"],
self._parse_variable: ["v", "var", "variable"]
}

def _parse_language(self, term):
@@ -54,6 +54,21 @@ class _QueryParser(object):
def _parse_variable(self, term):
pass

def _parse_literal(self, literal):
"""Parse part of a search query into a string or regular expression."""
if literal.startswith(("r:", "re:", "regex:", "regexp:")):
return Regex(literal.split(":", 1)[1])
return String(literal)

def _parse_term(self, term):
"""Parse a query term into a tree node and return it."""
if ":" in term and not term[0] == ":":
prefix, arg = term.split(":", 1)
for meth, prefixes in self._prefixes.iteritems():
if prefix in prefixes:
return meth(arg)
return Text(self._parse_literal(term))

def parse(self, query):
"""
Parse a search query.
@@ -70,11 +85,15 @@ class _QueryParser(object):

:raises: :py:class:`.QueryParseException`
"""
print "input:", query
for term in split(query):
if ":" in term and not term[0] == ":":
prefix = term.split(":")[0]


print "term: ", term
node = self._parse_term(term)
print "parse:", node
tree = Tree(node)
print "tree: ", tree
return tree
## TODO

# language:"Python"
# lang:



+ 17
- 0
test/test_query_parser.py Dosyayı Görüntüle

@@ -0,0 +1,17 @@
from __future__ import unicode_literals
import unittest

from bitshift.query import parse_query

class TestQueryParser(unittest.TestCase):
"""Unit tests for the query parser in :py:mod:`bitshift.query`."""

def test_parse(self):
"""test basic query parsing"""
pq = lambda s: parse_query(s).serialize()
self.assertEqual("Tree(Text(String('test')))", pq("test"))
self.assertEqual("Tree(Text(Regex('test')))", pq("re:test"))


if __name__ == "__main__":
unittest.main(verbosity=2)

Yükleniyor…
İptal
Kaydet