From 9cbf47319545a9ba057318e4637c8b85d4301d72 Mon Sep 17 00:00:00 2001 From: Sam Harwell Date: Mon, 16 Jun 2014 12:05:10 -0500 Subject: [PATCH] Add regression tests for #625 --- .../org/antlr/v4/test/TestLeftRecursion.java | 47 +++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/tool/test/org/antlr/v4/test/TestLeftRecursion.java b/tool/test/org/antlr/v4/test/TestLeftRecursion.java index b7866cab4..34cab8d24 100644 --- a/tool/test/org/antlr/v4/test/TestLeftRecursion.java +++ b/tool/test/org/antlr/v4/test/TestLeftRecursion.java @@ -568,6 +568,53 @@ public class TestLeftRecursion extends BaseTest { assertEquals("(prog (statement (letterA a)) (statement (letterA a)) )\n", found); } + /** + * This is a regression test for antlr/antlr4#625 "Duplicate action breaks + * operator precedence" + * https://github.com/antlr/antlr4/issues/625 + */ + @Test public void testMultipleActions() throws Exception { + String grammar = + "grammar T;\n" + + "s @after {System.out.println($ctx.toStringTree(this));} : e ;\n" + + "e : a=e op=('*'|'/') b=e {}{}\n" + + " | INT {}{}\n" + + " | '(' x=e ')' {}{}\n" + + " ;\n" + + "INT : '0'..'9'+ ;\n" + + "WS : (' '|'\\n') -> skip ;\n"; + String[] tests = { + "4", "(s (e 4))", + "1*2/3", "(s (e (e (e 1) * (e 2)) / (e 3)))", + "(1/2)*3", "(s (e (e ( (e (e 1) / (e 2)) )) * (e 3)))", + }; + runTests(grammar, tests, "s"); + } + + /** + * This is a regression test for antlr/antlr4#625 "Duplicate action breaks + * operator precedence" + * https://github.com/antlr/antlr4/issues/625 + */ + @Test public void testMultipleActionsPredicatesOptions() throws Exception { + String grammar = + "grammar T;\n" + + "s @after {System.out.println($ctx.toStringTree(this));} : e ;\n" + + "e : a=e op=('*'|'/') b=e {}{true}?\n" + + " | a=e op=('+'|'-') b=e {}{true}?\n" + + " | INT {}{}\n" + + " | '(' x=e ')' {}{}\n" + + " ;\n" + + "INT : '0'..'9'+ ;\n" + + "WS : (' '|'\\n') -> skip ;\n"; + String[] tests = { + "4", "(s (e 4))", + "1*2/3", "(s (e (e (e 1) * (e 2)) / (e 3)))", + "(1/2)*3", "(s (e (e ( (e (e 1) / (e 2)) )) * (e 3)))", + }; + runTests(grammar, tests, "s"); + } + public void runTests(String grammar, String[] tests, String startRule) { rawGenerateAndBuildRecognizer("T.g4", grammar, "TParser", "TLexer"); writeRecognizerAndCompile("TParser",