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

negative range seems to work #101

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
19 changes: 18 additions & 1 deletion luqum/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
('nonassoc', 'PLUS', 'MINUS'),
('nonassoc', 'BOOST'),
('nonassoc', 'TO'),
('right', 'UMINUS')
)

# term
Expand Down Expand Up @@ -95,6 +96,7 @@
)*
)
'''.format(time_re=TIME_RE)

# phrase
PHRASE_RE = r'''
(?P<phrase> # phrase
Expand Down Expand Up @@ -296,12 +298,25 @@ def p_grouping(p):


def p_range(p):
'''unary_expression : LBRACKET phrase_or_term TO phrase_or_term RBRACKET'''
'''unary_expression : LBRACKET phrase_or_possibly_negative_term TO phrase_or_possibly_negative_term RBRACKET'''
include_low = p[1].value == "["
include_high = p[5].value == "]"
p[0] = Range(p[2], p[4], include_low, include_high)
head_tail.range(p)

def p_possibly_negative_term(p):
'''possibly_negative_term : MINUS phrase_or_term %prec UMINUS
| phrase_or_term'''
if len(p) == 3:
p[0] = Prohibit(p[2])
head_tail.unary(p)
else:
p[0] = p[1]

def p_phrase_or_possibly_negative_term(p):
'''phrase_or_possibly_negative_term : possibly_negative_term
| PHRASE'''
p[0] = p[1]

def p_lessthan(p):
'''unary_expression : LESSTHAN phrase_or_term'''
Expand Down Expand Up @@ -390,3 +405,5 @@ def p_error(p):
**Note**: The parser by itself is not thread safe (because PLY is not).
Use :py:func:`luqum.thread.parse` instead
"""


51 changes: 0 additions & 51 deletions luqum/parsetab.py

This file was deleted.

5 changes: 5 additions & 0 deletions tests/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -526,3 +526,8 @@ def test_illegal_character_exception(self):
str(raised.exception),
"Illegal character '\\' at position 0",
)

def test_negative_values_in_ranges(self):
parser.parse("[-1 TO 5]")
parser.parse("[-10 TO -1]")
parser.parse("[5 TO -1]") # semantically incorrect but correct from the parser's perspective
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should test the resulting tree is that expected, (as we do in other non raising tests).

Loading