Fix parsing of quoted words

This commit is contained in:
Kovid Goyal 2022-04-12 19:31:56 +05:30
parent 8f92c594f2
commit 93a7b220c9
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
2 changed files with 10 additions and 5 deletions

View File

@ -223,13 +223,15 @@ class Parser:
return self.base_token() return self.base_token()
def base_token(self) -> SearchTreeNode: def base_token(self) -> SearchTreeNode:
if self.token_type() is TokenType.QUOTED_WORD:
tt = self.token(advance=True) tt = self.token(advance=True)
assert tt is not None assert tt is not None
if self.token_type() is TokenType.QUOTED_WORD:
if self.allow_no_location: if self.allow_no_location:
return TokenNode('all', tt) return TokenNode('all', tt)
raise ParseException(f'No location specified before {tt}') raise ParseException(f'No location specified before {tt}')
tt = self.token(advance=True)
assert tt is not None
words = tt.split(':') words = tt.split(':')
# The complexity here comes from having colon-separated search # The complexity here comes from having colon-separated search
# values. That forces us to check that the first "word" in a colon- # values. That forces us to check that the first "word" in a colon-

View File

@ -8,20 +8,23 @@ from . import BaseTest
class TestSQP(BaseTest): class TestSQP(BaseTest):
def test_search_query_parser(self): def test_search_query_parser(self):
from kitty.search_query_parser import search from kitty.search_query_parser import search, ParseException
locations = 'id' locations = 'id'
universal_set = {1, 2, 3, 4, 5} universal_set = {1, 2, 3, 4, 5}
def get_matches(location, query, candidates): def get_matches(location, query, candidates):
return {x for x in candidates if query == str(x)} return {x for x in candidates if query == str(x)}
def t(q, expected): def t(q, expected=set()):
actual = search(q, locations, universal_set, get_matches) actual = search(q, locations, universal_set, get_matches)
self.ae(actual, expected) self.ae(actual, expected)
t('id:1', {1}) t('id:1', {1})
t('id:"1"', {1})
t('id:1 and id:1', {1}) t('id:1 and id:1', {1})
t('id:1 or id:2', {1, 2}) t('id:1 or id:2', {1, 2})
t('id:1 and id:2', set()) t('id:1 and id:2')
t('not id:1', universal_set - {1}) t('not id:1', universal_set - {1})
t('(id:1 or id:2) and id:1', {1}) t('(id:1 or id:2) and id:1', {1})
self.assertRaises(ParseException, t, '1')
self.assertRaises(ParseException, t, '"id:1"')