Skip to content

Commit

Permalink
negative range seems to work
Browse files Browse the repository at this point in the history
  • Loading branch information
mesemus committed Apr 9, 2024
1 parent 879acff commit 4687acf
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 52 deletions.
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

0 comments on commit 4687acf

Please sign in to comment.