From 65a269a9c44b0b1e13a3f26c67eeab33d57877be Mon Sep 17 00:00:00 2001 From: Kaushik Kulkarni Date: Sun, 22 Dec 2019 12:57:31 -0600 Subject: [PATCH 1/2] parses tuples with trailing commas correctly --- pymbolic/parser.py | 6 +++++- test/test_pymbolic.py | 3 +-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/pymbolic/parser.py b/pymbolic/parser.py index 85f75af..da55e1e 100644 --- a/pymbolic/parser.py +++ b/pymbolic/parser.py @@ -469,7 +469,11 @@ class Parser(object): pstate.advance() if pstate.is_at_end() or pstate.next_tag() is _closepar: - left_exp = (left_exp,) + if isinstance(left_exp, (tuple, list)) \ + and not isinstance(left_exp, FinalizedContainer): + pass + else: + left_exp = (left_exp,) else: new_el = self.parse_expression(pstate, _PREC_COMMA) if isinstance(left_exp, (tuple, list)) \ diff --git a/test/test_pymbolic.py b/test/test_pymbolic.py index 2d6f3f7..e199c76 100644 --- a/test/test_pymbolic.py +++ b/test/test_pymbolic.py @@ -253,8 +253,6 @@ def test_parser(): assert_parse_roundtrip("(3,)") assert_parse_roundtrip("[x + 3, 3, 5]") - # FIXME: trailing commas not allowed - # assert parse("[x + 3, 3, 5]") == parse("[x + 3, 3, 5]") assert_parse_roundtrip("[]") assert_parse_roundtrip("[x]") @@ -289,6 +287,7 @@ def test_parser(): assert_parsed_same_as_python('5+i if i>=0 else (0 if i<-1 else 10)') assert_parsed_same_as_python("0 if 1 if 2 else 3 else 4") assert_parsed_same_as_python("0 if (1 if 2 else 3) else 4") + assert_parsed_same_as_python("(2, 3,)") with pytest.deprecated_call(): parse('1+if(0, 1, 2)') -- GitLab From 83bf966e5f4b52e2a76a1cc654c9471f15fe12bf Mon Sep 17 00:00:00 2001 From: Kaushik Kulkarni Date: Mon, 30 Dec 2019 18:36:54 -0600 Subject: [PATCH 2/2] explains https://gitlab.tiker.net/inducer/pymbolic/merge_requests/48 --- pymbolic/parser.py | 1 + 1 file changed, 1 insertion(+) diff --git a/pymbolic/parser.py b/pymbolic/parser.py index da55e1e..26cabd4 100644 --- a/pymbolic/parser.py +++ b/pymbolic/parser.py @@ -471,6 +471,7 @@ class Parser(object): if pstate.is_at_end() or pstate.next_tag() is _closepar: if isinstance(left_exp, (tuple, list)) \ and not isinstance(left_exp, FinalizedContainer): + # left_expr is a container with trailing commas pass else: left_exp = (left_exp,) -- GitLab