Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Including 2 new terms to be able to parse logical and tuple terms inside braces. #18

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 12 additions & 10 deletions py_rql/grammar.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,28 @@

RQL_GRAMMAR = r"""
start: _top_term?

_top_term: term
| term_and_comma

term: expr_term
| logical
| tuple

| _L_BRACE logical _R_BRACE
| _L_BRACE tuple _R_BRACE

expr_term: comp
| listing
| searching
| ordering
| select
| _L_BRACE expr_term _R_BRACE

logical: and_op
| or_op
| not_op
and_op: _and

and_op: _and
| _L_BRACE _and _R_BRACE

_and: _AND? _logical_exp
Expand All @@ -57,15 +59,15 @@
comp: comp_term _L_BRACE prop _COMMA val _R_BRACE
| prop _EQUALITY comp_term _EQUALITY val
| prop _EQUALITY val

listing: list_term _L_BRACE prop _COMMA _L_BRACE val (_COMMA val)* _R_BRACE _R_BRACE
searching: search_term _L_BRACE prop _COMMA val _R_BRACE

ordering: ordering_term _signed_props
select: select_term _signed_props
_signed_props: _L_BRACE _R_BRACE
| _L_BRACE sign_prop (_COMMA sign_prop)* _R_BRACE

val: prop
| tuple
| QUOTED_VAL
Expand All @@ -78,19 +80,19 @@
| ordering_term
| select_term
| PROP

tuple: _TUPLE _L_BRACE (comp|searching) (_COMMA (comp|searching))* _R_BRACE

!sign_prop: ["+"|"-"] prop

!comp_term: "eq" | "ne" | "gt" | "ge" | "lt" | "le"
!logical_term: _AND | _OR | _NOT
!list_term: "in" | "out"
!search_term: "like" | "ilike"
!ordering_term: "ordering"
!select_term: "select"


PROP: /[a-zA-Z]/ /[\w\-\.]/*
QUOTED_VAL: /"[^"]*"/
| /'[^']*'/
Expand Down
45 changes: 45 additions & 0 deletions tests/test_parser/test_logical.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,3 +199,48 @@ def test_or_chain(query):
(ComparisonOperators.EQ, 'p4', 'v4'),
],
}


def test_logical_tuple_and_not():
result = logical_transform('(not(id=fg))')

not_grammar_key = LogicalOperators.get_grammar_key(LogicalOperators.NOT)

assert result == {
not_grammar_key: [(ComparisonOperators.EQ, 'id', 'fg')],
}


def test_logical_tuple_and_not_w_and_nesting():
result = logical_transform('((not(eq(id,fg)))&(not(ge(external_id,fg))))')

and_grammar_key = LogicalOperators.get_grammar_key(LogicalOperators.AND)
not_grammar_key = LogicalOperators.get_grammar_key(LogicalOperators.NOT)

assert result == {
and_grammar_key: [
{not_grammar_key: [(ComparisonOperators.EQ, 'id', 'fg')]},
{not_grammar_key: [(ComparisonOperators.GE, 'external_id', 'fg')]},
],
}


def test_logical_tuple_nesting_ands_and_ors():
result = logical_transform(
'((((eq(id,*baba*))&(eq(external_id,*baba*))))|(((eq(id,*dziad*)))))',
)

and_grammar_key = LogicalOperators.get_grammar_key(LogicalOperators.AND)
or_grammar_key = LogicalOperators.get_grammar_key(LogicalOperators.OR)

assert result == {
or_grammar_key: [
{
and_grammar_key: [
(ComparisonOperators.EQ, 'id', '*baba*'),
(ComparisonOperators.EQ, 'external_id', '*baba*'),
],
},
(ComparisonOperators.EQ, 'id', '*dziad*'),
],
}
Loading