From 14107cc40a13d338864bf7a1c998389ddaaac5e8 Mon Sep 17 00:00:00 2001 From: Sam Harwell Date: Fri, 17 Jan 2014 08:54:43 -0600 Subject: [PATCH] Add regression test for expression performance (closes #192) --- .../org/antlr/v4/test/TestPerformance.java | 54 +++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/tool/test/org/antlr/v4/test/TestPerformance.java b/tool/test/org/antlr/v4/test/TestPerformance.java index 2aa6420a4..099f6be43 100644 --- a/tool/test/org/antlr/v4/test/TestPerformance.java +++ b/tool/test/org/antlr/v4/test/TestPerformance.java @@ -60,6 +60,7 @@ import org.antlr.v4.runtime.misc.Interval; import org.antlr.v4.runtime.misc.NotNull; import org.antlr.v4.runtime.misc.Nullable; import org.antlr.v4.runtime.misc.ParseCancellationException; +import org.antlr.v4.runtime.misc.Utils; import org.antlr.v4.runtime.tree.ErrorNode; import org.antlr.v4.runtime.tree.ParseTree; import org.antlr.v4.runtime.tree.ParseTreeListener; @@ -1963,4 +1964,57 @@ public class TestPerformance extends BaseTest { return referent; } } + + /** + * This is a regression test for antlr/antlr4#192 "Poor performance of + * expression parsing". + * https://github.com/antlr/antlr4/issues/192 + */ + @Test(timeout = 60000) + public void testExpressionGrammar() { + String grammar = + "grammar Expr;\n" + + "\n" + + "program: expr EOF;\n" + + "\n" + + "expr: ID\n" + + " | 'not' expr\n" + + " | expr 'and' expr\n" + + " | expr 'or' expr\n" + + " ;\n" + + "\n" + + "ID: [a-zA-Z_][a-zA-Z_0-9]*;\n" + + "WS: [ \\t\\n\\r\\f]+ -> skip;\n" + + "ERROR: .;\n"; + String input = + "not X1 and not X2 and not X3 and not X4 and not X5 and not X6 and not X7 and not X8 and not X9 and not X10 and not X11 and not X12 or\n" + + " X1 and not X2 and not X3 and not X4 and not X5 and not X6 and not X7 and not X8 and not X9 and not X10 and not X11 and not X12 or\n" + + "not X1 and X2 and not X3 and not X4 and not X5 and not X6 and not X7 and not X8 and not X9 and not X10 and not X11 and not X12 or\n" + + "not X1 and not X2 and X3 and not X4 and not X5 and not X6 and not X7 and not X8 and not X9 and not X10 and not X11 and not X12 or\n" + + "not X1 and not X2 and not X3 and X4 and not X5 and not X6 and not X7 and not X8 and not X9 and not X10 and not X11 and not X12 or\n" + + "not X1 and not X2 and not X3 and not X4 and X5 and not X6 and not X7 and not X8 and not X9 and not X10 and not X11 and not X12 or\n" + + "not X1 and not X2 and not X3 and not X4 and not X5 and X6 and not X7 and not X8 and not X9 and not X10 and not X11 and not X12 or\n" + + "not X1 and not X2 and not X3 and not X4 and not X5 and not X6 and X7 and not X8 and not X9 and not X10 and not X11 and not X12 or\n" + + "not X1 and not X2 and not X3 and not X4 and not X5 and not X6 and not X7 and X8 and not X9 and not X10 and not X11 and not X12 or\n" + + "not X1 and not X2 and not X3 and not X4 and not X5 and not X6 and not X7 and not X8 and X9 and not X10 and not X11 and not X12 or\n" + + "not X1 and not X2 and not X3 and not X4 and not X5 and not X6 and not X7 and not X8 and not X9 and X10 and not X11 and not X12 or\n" + + "not X1 and not X2 and not X3 and not X4 and not X5 and not X6 and not X7 and not X8 and not X9 and not X10 and X11 and not X12 or\n" + + "not X1 and not X2 and not X3 and not X4 and not X5 and not X6 and not X7 and not X8 and not X9 and not X10 and not X11 and X12\n"; + + String found = execParser("Expr.g4", grammar, "ExprParser", "ExprLexer", "program", + input, false); + Assert.assertEquals("", found); + Assert.assertEquals(null, stderrDuringParse); + + List inputs = new ArrayList(); + for (int i = 0; i < 10; i++) { + inputs.add(input); + } + + input = Utils.join(inputs.iterator(), " or\n"); + found = execParser("Expr.g4", grammar, "ExprParser", "ExprLexer", "program", + input, false); + Assert.assertEquals("", found); + Assert.assertEquals(null, stderrDuringParse); + } }