From 3131dfa810e22cf3d416a599beed0980392ca618 Mon Sep 17 00:00:00 2001 From: "Emily.Bourne" Date: Wed, 11 Mar 2020 17:55:27 +0100 Subject: [PATCH 1/4] Change the parsing order of binary operators with non-commutative operators of the same precedence --- baron/grammator_operators.py | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/baron/grammator_operators.py b/baron/grammator_operators.py index b0661ce..0d0b2d4 100644 --- a/baron/grammator_operators.py +++ b/baron/grammator_operators.py @@ -188,20 +188,20 @@ def comparison_advanced_node(pack): "second_formatting": comparison_operator2.hidden_tokens_after, } - @pg.production("expr : xor_expr VBAR expr") + @pg.production("expr : expr VBAR xor_expr") @pg.production("xor_expr : and_expr CIRCUMFLEX xor_expr") @pg.production("and_expr : shift_expr AMPER and_expr") - @pg.production("shift_expr : arith_expr RIGHT_SHIFT shift_expr") - @pg.production("shift_expr : arith_expr LEFT_SHIFT shift_expr") - @pg.production("arith_expr : term PLUS arith_expr") - @pg.production("arith_expr : term MINUS arith_expr") - @pg.production("term : factor STAR term") - @pg.production("term : factor SLASH term") - @pg.production("term : factor PERCENT term") - @pg.production("term : factor DOUBLE_SLASH term") - @pg.production("term : factor AT term") - @pg.production("power : atom DOUBLE_STAR factor") - @pg.production("power : atom DOUBLE_STAR power") + @pg.production("shift_expr : shift_expr RIGHT_SHIFT arith_expr") + @pg.production("shift_expr : shift_expr LEFT_SHIFT arith_expr") + @pg.production("arith_expr : arith_expr PLUS term") + @pg.production("arith_expr : arith_expr MINUS term") + @pg.production("term : term STAR factor") + @pg.production("term : term SLASH factor") + @pg.production("term : term PERCENT factor") + @pg.production("term : term DOUBLE_SLASH factor") + @pg.production("term : term AT factor") + @pg.production("power : factor DOUBLE_STAR atom") + @pg.production("power : power DOUBLE_STAR atom") def binary_operator_node(pack): (first, operator, second) = pack return { From 84dcc257b6da94b49a7281812b70e5d765d1946b Mon Sep 17 00:00:00 2001 From: "Emily.Bourne" Date: Wed, 11 Mar 2020 18:06:45 +0100 Subject: [PATCH 2/4] Correct test_combine_div_modulo_mult test --- tests/test_grammator_operators.py | 320 +++++++++++++++--------------- 1 file changed, 160 insertions(+), 160 deletions(-) diff --git a/tests/test_grammator_operators.py b/tests/test_grammator_operators.py index 5772cbe..72cbbc0 100644 --- a/tests/test_grammator_operators.py +++ b/tests/test_grammator_operators.py @@ -2552,35 +2552,35 @@ def test_combine_div_modulo_mult(): ], [ { "type": "binary_operator", - "value": '/', + "value": '*', "first": { - "type": "name", - "value": 'a', - }, - "second": { "type": "binary_operator", "value": '%', "first": { - "type": "name", - "value": 'b' - }, - "second": { "type": "binary_operator", - "value": '*', + "value": '/', "first": { "type": "name", - "value": "c" + "value": "a" }, "second": { "type": "name", - "value": 'd' + "value": 'b' }, "first_formatting": [], "second_formatting": [], }, + "second": { + "type": "name", + "value": 'c' + }, "first_formatting": [], "second_formatting": [], }, + "second": { + "type": "name", + "value": 'd', + }, "first_formatting": [], "second_formatting": [] } @@ -2596,37 +2596,37 @@ def test_combine_div_modulo_mult(): ], [ { "type": "binary_operator", - "value": '/', + "value": '*', "first": { - "type": "name", - "value": 'a', - }, - "second": { "type": "binary_operator", "value": '%', "first": { - "type": "name", - "value": 'b' - }, - "second": { "type": "binary_operator", - "value": '*', + "value": '/', "first": { "type": "name", - "value": "c" + "value": "a" }, "second": { "type": "name", - "value": 'd' + "value": 'b' }, "first_formatting": [], "second_formatting": [], }, + "second": { + "type": "name", + "value": 'c' + }, "first_formatting": [], - "second_formatting": [] + "second_formatting": [], + }, + "second": { + "type": "name", + "value": 'd', }, "first_formatting": [], - "second_formatting": [], + "second_formatting": [] } ]) parse_simple([ @@ -2640,37 +2640,37 @@ def test_combine_div_modulo_mult(): ], [ { "type": "binary_operator", - "value": '/', + "value": '*', "first": { - "type": "name", - "value": 'a', - }, - "second": { "type": "binary_operator", "value": '%', "first": { - "type": "name", - "value": 'b' - }, - "second": { "type": "binary_operator", - "value": '*', + "value": '/', "first": { "type": "name", - "value": "c" + "value": "a" }, "second": { "type": "name", - "value": 'd' + "value": 'b' }, "first_formatting": [], - "second_formatting": [] + "second_formatting": [], + }, + "second": { + "type": "name", + "value": 'c' }, "first_formatting": [], "second_formatting": [], }, + "second": { + "type": "name", + "value": 'd', + }, "first_formatting": [], - "second_formatting": [], + "second_formatting": [] } ]) parse_simple([ @@ -2684,35 +2684,35 @@ def test_combine_div_modulo_mult(): ], [ { "type": "binary_operator", - "value": '/', + "value": '*', "first": { - "type": "name", - "value": 'a' - }, - "second": { "type": "binary_operator", "value": '%', "first": { - "type": "name", - "value": 'b', - }, - "second": { "type": "binary_operator", - "value": '*', + "value": '/', "first": { "type": "name", - "value": "c" + "value": "a" }, "second": { "type": "name", - "value": 'd' + "value": 'b' }, "first_formatting": [], "second_formatting": [], }, + "second": { + "type": "name", + "value": 'c' + }, "first_formatting": [], "second_formatting": [], }, + "second": { + "type": "name", + "value": 'd', + }, "first_formatting": [], "second_formatting": [] } @@ -2728,37 +2728,37 @@ def test_combine_div_modulo_mult(): ], [ { "type": "binary_operator", - "value": '/', + "value": '*', "first": { - "type": "name", - "value": 'a' - }, - "second": { "type": "binary_operator", "value": '%', "first": { - "type": "name", - "value": 'b', - }, - "second": { "type": "binary_operator", - "value": '*', + "value": '/', "first": { "type": "name", - "value": "c" + "value": "a" }, "second": { "type": "name", - "value": 'd' + "value": 'b' }, "first_formatting": [], "second_formatting": [], }, + "second": { + "type": "name", + "value": 'c' + }, "first_formatting": [], - "second_formatting": [] + "second_formatting": [], + }, + "second": { + "type": "name", + "value": 'd', }, "first_formatting": [], - "second_formatting": [], + "second_formatting": [] } ]) parse_simple([ @@ -2772,37 +2772,37 @@ def test_combine_div_modulo_mult(): ], [ { "type": "binary_operator", - "value": '/', + "value": '*', "first": { - "type": "name", - "value": 'a' - }, - "second": { "type": "binary_operator", "value": '%', "first": { - "type": "name", - "value": 'b', - }, - "second": { "type": "binary_operator", - "value": '*', + "value": '/', "first": { "type": "name", - "value": "c" + "value": "a" }, "second": { "type": "name", - "value": 'd' + "value": 'b' }, "first_formatting": [], - "second_formatting": [] + "second_formatting": [], + }, + "second": { + "type": "name", + "value": 'c' }, "first_formatting": [], "second_formatting": [], }, + "second": { + "type": "name", + "value": 'd', + }, "first_formatting": [], - "second_formatting": [], + "second_formatting": [] } ]) parse_simple([ @@ -2816,35 +2816,35 @@ def test_combine_div_modulo_mult(): ], [ { "type": "binary_operator", - "value": '/', + "value": '*', "first": { - "type": "name", - "value": 'a' - }, - "second": { "type": "binary_operator", "value": '%', "first": { - "type": "name", - "value": 'b' - }, - "second": { "type": "binary_operator", - "value": '*', + "value": '/', "first": { "type": "name", - "value": "c", + "value": "a" }, "second": { "type": "name", - "value": 'd' + "value": 'b' }, "first_formatting": [], "second_formatting": [], }, + "second": { + "type": "name", + "value": 'c' + }, "first_formatting": [], "second_formatting": [], }, + "second": { + "type": "name", + "value": 'd', + }, "first_formatting": [], "second_formatting": [] } @@ -2860,37 +2860,37 @@ def test_combine_div_modulo_mult(): ], [ { "type": "binary_operator", - "value": '/', + "value": '*', "first": { - "type": "name", - "value": 'a' - }, - "second": { "type": "binary_operator", "value": '%', "first": { - "type": "name", - "value": 'b' - }, - "second": { "type": "binary_operator", - "value": '*', + "value": '/', "first": { "type": "name", - "value": "c", + "value": "a" }, "second": { "type": "name", - "value": 'd' + "value": 'b' }, "first_formatting": [], "second_formatting": [], }, + "second": { + "type": "name", + "value": 'c' + }, "first_formatting": [], - "second_formatting": [] + "second_formatting": [], + }, + "second": { + "type": "name", + "value": 'd', }, "first_formatting": [], - "second_formatting": [], + "second_formatting": [] } ]) parse_simple([ @@ -2904,37 +2904,37 @@ def test_combine_div_modulo_mult(): ], [ { "type": "binary_operator", - "value": '/', + "value": '*', "first": { - "type": "name", - "value": 'a' - }, - "second": { "type": "binary_operator", "value": '%', "first": { - "type": "name", - "value": 'b' - }, - "second": { "type": "binary_operator", - "value": '*', + "value": '/', "first": { "type": "name", - "value": "c", + "value": "a" }, "second": { "type": "name", - "value": 'd' + "value": 'b' }, "first_formatting": [], - "second_formatting": [] + "second_formatting": [], + }, + "second": { + "type": "name", + "value": 'c' }, "first_formatting": [], "second_formatting": [], }, + "second": { + "type": "name", + "value": 'd', + }, "first_formatting": [], - "second_formatting": [], + "second_formatting": [] } ]) parse_simple([ @@ -2948,35 +2948,35 @@ def test_combine_div_modulo_mult(): ], [ { "type": "binary_operator", - "value": '/', + "value": '*', "first": { - "type": "name", - "value": 'a' - }, - "second": { "type": "binary_operator", "value": '%', "first": { - "type": "name", - "value": 'b' - }, - "second": { "type": "binary_operator", - "value": '*', + "value": '/', "first": { "type": "name", - "value": "c" + "value": "a" }, "second": { "type": "name", - "value": 'd', + "value": 'b' }, "first_formatting": [], "second_formatting": [], }, + "second": { + "type": "name", + "value": 'c' + }, "first_formatting": [], "second_formatting": [], }, + "second": { + "type": "name", + "value": 'd', + }, "first_formatting": [], "second_formatting": [] } @@ -2992,37 +2992,37 @@ def test_combine_div_modulo_mult(): ], [ { "type": "binary_operator", - "value": '/', + "value": '*', "first": { - "type": "name", - "value": 'a' - }, - "second": { "type": "binary_operator", "value": '%', "first": { - "type": "name", - "value": 'b' - }, - "second": { "type": "binary_operator", - "value": '*', + "value": '/', "first": { "type": "name", - "value": "c" + "value": "a" }, "second": { "type": "name", - "value": 'd', + "value": 'b' }, "first_formatting": [], "second_formatting": [], }, + "second": { + "type": "name", + "value": 'c' + }, "first_formatting": [], - "second_formatting": [] + "second_formatting": [], + }, + "second": { + "type": "name", + "value": 'd', }, "first_formatting": [], - "second_formatting": [], + "second_formatting": [] } ]) parse_simple([ @@ -3036,37 +3036,37 @@ def test_combine_div_modulo_mult(): ], [ { "type": "binary_operator", - "value": '/', + "value": '*', "first": { - "type": "name", - "value": 'a' - }, - "second": { "type": "binary_operator", "value": '%', "first": { - "type": "name", - "value": 'b' - }, - "second": { "type": "binary_operator", - "value": '*', + "value": '/', "first": { "type": "name", - "value": "c" + "value": "a" }, "second": { "type": "name", - "value": 'd', + "value": 'b' }, "first_formatting": [], - "second_formatting": [] + "second_formatting": [], + }, + "second": { + "type": "name", + "value": 'c' }, "first_formatting": [], "second_formatting": [], }, + "second": { + "type": "name", + "value": 'd', + }, "first_formatting": [], - "second_formatting": [], + "second_formatting": [] } ]) From c4837b9c1343d7f2879091614fd4d60ecf31de49 Mon Sep 17 00:00:00 2001 From: "Emily.Bourne" Date: Wed, 11 Mar 2020 18:41:02 +0100 Subject: [PATCH 3/4] Keep intuitive order where possible --- baron/grammator_operators.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/baron/grammator_operators.py b/baron/grammator_operators.py index 0d0b2d4..881e83f 100644 --- a/baron/grammator_operators.py +++ b/baron/grammator_operators.py @@ -188,7 +188,7 @@ def comparison_advanced_node(pack): "second_formatting": comparison_operator2.hidden_tokens_after, } - @pg.production("expr : expr VBAR xor_expr") + @pg.production("expr : xor_expr VBAR expr") @pg.production("xor_expr : and_expr CIRCUMFLEX xor_expr") @pg.production("and_expr : shift_expr AMPER and_expr") @pg.production("shift_expr : shift_expr RIGHT_SHIFT arith_expr") @@ -200,8 +200,8 @@ def comparison_advanced_node(pack): @pg.production("term : term PERCENT factor") @pg.production("term : term DOUBLE_SLASH factor") @pg.production("term : term AT factor") - @pg.production("power : factor DOUBLE_STAR atom") - @pg.production("power : power DOUBLE_STAR atom") + @pg.production("power : atom DOUBLE_STAR factor") + @pg.production("power : atom DOUBLE_STAR power") def binary_operator_node(pack): (first, operator, second) = pack return { From 709fe255514b270766766f8a2e86d726d4227ea3 Mon Sep 17 00:00:00 2001 From: "Emily.Bourne" Date: Wed, 11 Mar 2020 18:41:21 +0100 Subject: [PATCH 4/4] Correct remaining chained tests --- tests/test_grammator_operators.py | 204 +++++++++++++++--------------- 1 file changed, 102 insertions(+), 102 deletions(-) diff --git a/tests/test_grammator_operators.py b/tests/test_grammator_operators.py index 72cbbc0..b87f832 100644 --- a/tests/test_grammator_operators.py +++ b/tests/test_grammator_operators.py @@ -3730,25 +3730,25 @@ def test_chained_add_substract(): ], [ { "type": "binary_operator", - "value": '+', + "value": '-', "first": { - "type": "name", - "value": 'a', - }, - "second": { "type": "binary_operator", - "value": '-', + "value": '+', "first": { "type": "name", - "value": "b" + "value": "a" }, "second": { "type": "name", - "value": "c" + "value": "b" }, "first_formatting": [], "second_formatting": [] }, + "second": { + "type": "name", + "value": 'c', + }, "first_formatting": [], "second_formatting": [] } @@ -3762,27 +3762,27 @@ def test_chained_add_substract(): ], [ { "type": "binary_operator", - "value": '+', + "value": '-', "first": { - "type": "name", - "value": 'a', - }, - "second": { "type": "binary_operator", - "value": '-', + "value": '+', "first": { "type": "name", - "value": "b" + "value": "a" }, "second": { "type": "name", - "value": "c" + "value": "b" }, "first_formatting": [], "second_formatting": [] }, + "second": { + "type": "name", + "value": 'c', + }, "first_formatting": [], - "second_formatting": [], + "second_formatting": [] } ]) parse_simple([ @@ -3794,25 +3794,25 @@ def test_chained_add_substract(): ], [ { "type": "binary_operator", - "value": '+', + "value": '-', "first": { - "type": "name", - "value": 'a' - }, - "second": { "type": "binary_operator", - "value": '-', + "value": '+', "first": { "type": "name", - "value": "b", + "value": "a" }, "second": { "type": "name", - "value": "c" + "value": "b" }, "first_formatting": [], "second_formatting": [] }, + "second": { + "type": "name", + "value": 'c', + }, "first_formatting": [], "second_formatting": [] } @@ -3826,27 +3826,27 @@ def test_chained_add_substract(): ], [ { "type": "binary_operator", - "value": '+', + "value": '-', "first": { - "type": "name", - "value": 'a' - }, - "second": { "type": "binary_operator", - "value": '-', + "value": '+', "first": { "type": "name", - "value": "b", + "value": "a" }, "second": { "type": "name", - "value": "c" + "value": "b" }, "first_formatting": [], "second_formatting": [] }, + "second": { + "type": "name", + "value": 'c', + }, "first_formatting": [], - "second_formatting": [], + "second_formatting": [] } ]) parse_simple([ @@ -3858,25 +3858,25 @@ def test_chained_add_substract(): ], [ { "type": "binary_operator", - "value": '+', + "value": '-', "first": { - "type": "name", - "value": 'a' - }, - "second": { "type": "binary_operator", - "value": '-', + "value": '+', "first": { "type": "name", - "value": "b" + "value": "a" }, "second": { "type": "name", - "value": "c", + "value": "b" }, "first_formatting": [], "second_formatting": [] }, + "second": { + "type": "name", + "value": 'c', + }, "first_formatting": [], "second_formatting": [] } @@ -3890,27 +3890,27 @@ def test_chained_add_substract(): ], [ { "type": "binary_operator", - "value": '+', + "value": '-', "first": { - "type": "name", - "value": 'a' - }, - "second": { "type": "binary_operator", - "value": '-', + "value": '+', "first": { "type": "name", - "value": "b" + "value": "a" }, "second": { "type": "name", - "value": "c", + "value": "b" }, "first_formatting": [], "second_formatting": [] }, + "second": { + "type": "name", + "value": 'c', + }, "first_formatting": [], - "second_formatting": [], + "second_formatting": [] } ]) @@ -4358,25 +4358,25 @@ def test_chained_left_right_shift(): ], [ { "type": "binary_operator", - "value": '<<', + "value": '>>', "first": { - "type": "name", - "value": 'a', - }, - "second": { "type": "binary_operator", - "value": '>>', + "value": '<<', "first": { "type": "name", - "value": "b" + "value": "a" }, "second": { "type": "name", - "value": "c" + "value": "b" }, "first_formatting": [], "second_formatting": [] }, + "second": { + "type": "name", + "value": 'c', + }, "first_formatting": [], "second_formatting": [] } @@ -4390,27 +4390,27 @@ def test_chained_left_right_shift(): ], [ { "type": "binary_operator", - "value": '<<', + "value": '>>', "first": { - "type": "name", - "value": 'a', - }, - "second": { "type": "binary_operator", - "value": '>>', + "value": '<<', "first": { "type": "name", - "value": "b" + "value": "a" }, "second": { "type": "name", - "value": "c" + "value": "b" }, "first_formatting": [], "second_formatting": [] }, + "second": { + "type": "name", + "value": 'c', + }, "first_formatting": [], - "second_formatting": [], + "second_formatting": [] } ]) parse_simple([ @@ -4422,25 +4422,25 @@ def test_chained_left_right_shift(): ], [ { "type": "binary_operator", - "value": '<<', + "value": '>>', "first": { - "type": "name", - "value": 'a' - }, - "second": { "type": "binary_operator", - "value": '>>', + "value": '<<', "first": { "type": "name", - "value": "b", + "value": "a" }, "second": { "type": "name", - "value": "c" + "value": "b" }, "first_formatting": [], "second_formatting": [] }, + "second": { + "type": "name", + "value": 'c', + }, "first_formatting": [], "second_formatting": [] } @@ -4454,27 +4454,27 @@ def test_chained_left_right_shift(): ], [ { "type": "binary_operator", - "value": '<<', + "value": '>>', "first": { - "type": "name", - "value": 'a' - }, - "second": { "type": "binary_operator", - "value": '>>', + "value": '<<', "first": { "type": "name", - "value": "b", + "value": "a" }, "second": { "type": "name", - "value": "c" + "value": "b" }, "first_formatting": [], "second_formatting": [] }, + "second": { + "type": "name", + "value": 'c', + }, "first_formatting": [], - "second_formatting": [], + "second_formatting": [] } ]) parse_simple([ @@ -4486,25 +4486,25 @@ def test_chained_left_right_shift(): ], [ { "type": "binary_operator", - "value": '<<', + "value": '>>', "first": { - "type": "name", - "value": 'a' - }, - "second": { "type": "binary_operator", - "value": '>>', + "value": '<<', "first": { "type": "name", - "value": "b" + "value": "a" }, "second": { "type": "name", - "value": "c", + "value": "b" }, "first_formatting": [], "second_formatting": [] }, + "second": { + "type": "name", + "value": 'c', + }, "first_formatting": [], "second_formatting": [] } @@ -4518,27 +4518,27 @@ def test_chained_left_right_shift(): ], [ { "type": "binary_operator", - "value": '<<', + "value": '>>', "first": { - "type": "name", - "value": 'a' - }, - "second": { "type": "binary_operator", - "value": '>>', + "value": '<<', "first": { "type": "name", - "value": "b" + "value": "a" }, "second": { "type": "name", - "value": "c", + "value": "b" }, "first_formatting": [], "second_formatting": [] }, + "second": { + "type": "name", + "value": 'c', + }, "first_formatting": [], - "second_formatting": [], + "second_formatting": [] } ])