From 98bb519f8a6de001486efa4c372f44e1a77a5b20 Mon Sep 17 00:00:00 2001 From: Sam Harwell Date: Sun, 25 Jan 2015 16:26:49 -0600 Subject: [PATCH] TestSemPredEvalParser for Java is working --- .../antlr4/runtime/test/templates/Index.stg | 3 +- .../SemPredEvalParser/2UnpredicatedAlts.stg | 39 ++ .../2UnpredicatedAltsAndOneOrthogonalAlt.stg | 39 ++ .../SemPredEvalParser/ActionHidesPreds.stg | 24 + .../ActionsHidePredsInGlobalFOLLOW.stg | 39 ++ .../AtomWithClosureInTranslatedLRRule.stg | 26 + .../DepedentPredsInGlobalFOLLOW.stg | 38 ++ ...endentPredNotInOuterCtxShouldBeIgnored.stg | 24 + .../SemPredEvalParser/DisabledAlternative.stg | 31 ++ ...dNotPassedOuterCtxToAvoidCastException.stg | 32 ++ .../templates/SemPredEvalParser/Index.stg | 31 ++ .../NoTruePredsThrowsNoViableAlt.stg | 34 ++ .../templates/SemPredEvalParser/Order.stg | 37 ++ .../PredFromAltTestedInLoopBack.stg | 16 + .../PredFromAltTestedInLoopBack_1.stg | 12 + .../PredFromAltTestedInLoopBack_2.stg | 9 + .../PredTestedEvenWhenUnAmbig.stg | 14 + .../PredTestedEvenWhenUnAmbig_1.stg | 9 + .../PredTestedEvenWhenUnAmbig_2.stg | 10 + .../PredicateDependentOnArg.stg | 40 ++ .../PredicateDependentOnArg2.stg | 42 ++ .../SemPredEvalParser/PredsInGlobalFOLLOW.stg | 38 ++ .../RewindBeforePredEval.stg | 35 ++ .../templates/SemPredEvalParser/Simple.stg | 24 + .../SemPredEvalParser/SimpleValidate.stg | 30 ++ .../SemPredEvalParser/SimpleValidate2.stg | 33 ++ .../templates/SemPredEvalParser/ToLeft.stg | 32 ++ .../ToLeftWithVaryingPredicate.stg | 43 ++ .../UnpredicatedPathsInAlt.stg | 34 ++ .../SemPredEvalParser/ValidateInDFA.stg | 24 + .../testgen/Antlr4TestGeneratorMojo.java | 25 +- .../antlr4/testgen/STGroupModelAdaptor.java | 17 +- .../org/antlr/v4/test/rt/gen/Generator.java | 179 ------- .../SemPredEvalParser/2UnpredicatedAlts.st | 9 - .../2UnpredicatedAltsAndOneOrthogonalAlt.st | 10 - .../ActionsHidePredsInGlobalFOLLOW.st | 10 - .../AtomWithClosureInTranslatedLRRule.st | 6 - .../DepedentPredsInGlobalFOLLOW.st | 11 - .../SemPredEvalParser/DisabledAlternative.st | 5 - ...edNotPassedOuterCtxToAvoidCastException.st | 10 - .../NoTruePredsThrowsNoViableAlt.st | 8 - .../gen/grammars/SemPredEvalParser/Order.st | 10 - .../PredicateDependentOnArg.st | 10 - .../PredicateDependentOnArg2.st | 10 - .../SemPredEvalParser/PredsInGlobalFOLLOW.st | 10 - .../SemPredEvalParser/RewindBeforePredEval.st | 8 - .../SemPredEvalParser/SimpleValidate.st | 8 - .../SemPredEvalParser/SimpleValidate2.st | 8 - .../gen/grammars/SemPredEvalParser/ToLeft.st | 8 - .../ToLeftWithVaryingPredicate.st | 10 - .../UnpredicatedPathsInAlt.st | 12 - .../test/rt/java/TestSemPredEvalParser.java | 454 ------------------ .../antlr/v4/test/runtime/java/Java.test.stg | 2 +- 53 files changed, 880 insertions(+), 802 deletions(-) create mode 100644 antlr4-testgen-maven-plugin/resources/org/antlr4/runtime/test/templates/SemPredEvalParser/2UnpredicatedAlts.stg create mode 100644 antlr4-testgen-maven-plugin/resources/org/antlr4/runtime/test/templates/SemPredEvalParser/2UnpredicatedAltsAndOneOrthogonalAlt.stg rename tool/test/org/antlr/v4/test/rt/gen/grammars/SemPredEvalParser/ActionHidesPreds.st => antlr4-testgen-maven-plugin/resources/org/antlr4/runtime/test/templates/SemPredEvalParser/ActionHidesPreds.stg (56%) create mode 100644 antlr4-testgen-maven-plugin/resources/org/antlr4/runtime/test/templates/SemPredEvalParser/ActionsHidePredsInGlobalFOLLOW.stg create mode 100644 antlr4-testgen-maven-plugin/resources/org/antlr4/runtime/test/templates/SemPredEvalParser/AtomWithClosureInTranslatedLRRule.stg create mode 100644 antlr4-testgen-maven-plugin/resources/org/antlr4/runtime/test/templates/SemPredEvalParser/DepedentPredsInGlobalFOLLOW.stg rename tool/test/org/antlr/v4/test/rt/gen/grammars/SemPredEvalParser/DependentPredNotInOuterCtxShouldBeIgnored.st => antlr4-testgen-maven-plugin/resources/org/antlr4/runtime/test/templates/SemPredEvalParser/DependentPredNotInOuterCtxShouldBeIgnored.stg (53%) create mode 100644 antlr4-testgen-maven-plugin/resources/org/antlr4/runtime/test/templates/SemPredEvalParser/DisabledAlternative.stg create mode 100644 antlr4-testgen-maven-plugin/resources/org/antlr4/runtime/test/templates/SemPredEvalParser/IndependentPredNotPassedOuterCtxToAvoidCastException.stg create mode 100644 antlr4-testgen-maven-plugin/resources/org/antlr4/runtime/test/templates/SemPredEvalParser/Index.stg create mode 100644 antlr4-testgen-maven-plugin/resources/org/antlr4/runtime/test/templates/SemPredEvalParser/NoTruePredsThrowsNoViableAlt.stg create mode 100644 antlr4-testgen-maven-plugin/resources/org/antlr4/runtime/test/templates/SemPredEvalParser/Order.stg rename tool/test/org/antlr/v4/test/rt/gen/grammars/SemPredEvalParser/PredFromAltTestedInLoopBack.st => antlr4-testgen-maven-plugin/resources/org/antlr4/runtime/test/templates/SemPredEvalParser/PredFromAltTestedInLoopBack.stg (50%) create mode 100644 antlr4-testgen-maven-plugin/resources/org/antlr4/runtime/test/templates/SemPredEvalParser/PredFromAltTestedInLoopBack_1.stg create mode 100644 antlr4-testgen-maven-plugin/resources/org/antlr4/runtime/test/templates/SemPredEvalParser/PredFromAltTestedInLoopBack_2.stg rename tool/test/org/antlr/v4/test/rt/gen/grammars/SemPredEvalParser/PredTestedEvenWhenUnAmbig.st => antlr4-testgen-maven-plugin/resources/org/antlr4/runtime/test/templates/SemPredEvalParser/PredTestedEvenWhenUnAmbig.stg (62%) create mode 100644 antlr4-testgen-maven-plugin/resources/org/antlr4/runtime/test/templates/SemPredEvalParser/PredTestedEvenWhenUnAmbig_1.stg create mode 100644 antlr4-testgen-maven-plugin/resources/org/antlr4/runtime/test/templates/SemPredEvalParser/PredTestedEvenWhenUnAmbig_2.stg create mode 100644 antlr4-testgen-maven-plugin/resources/org/antlr4/runtime/test/templates/SemPredEvalParser/PredicateDependentOnArg.stg create mode 100644 antlr4-testgen-maven-plugin/resources/org/antlr4/runtime/test/templates/SemPredEvalParser/PredicateDependentOnArg2.stg create mode 100644 antlr4-testgen-maven-plugin/resources/org/antlr4/runtime/test/templates/SemPredEvalParser/PredsInGlobalFOLLOW.stg create mode 100644 antlr4-testgen-maven-plugin/resources/org/antlr4/runtime/test/templates/SemPredEvalParser/RewindBeforePredEval.stg rename tool/test/org/antlr/v4/test/rt/gen/grammars/SemPredEvalParser/Simple.st => antlr4-testgen-maven-plugin/resources/org/antlr4/runtime/test/templates/SemPredEvalParser/Simple.stg (55%) create mode 100644 antlr4-testgen-maven-plugin/resources/org/antlr4/runtime/test/templates/SemPredEvalParser/SimpleValidate.stg create mode 100644 antlr4-testgen-maven-plugin/resources/org/antlr4/runtime/test/templates/SemPredEvalParser/SimpleValidate2.stg create mode 100644 antlr4-testgen-maven-plugin/resources/org/antlr4/runtime/test/templates/SemPredEvalParser/ToLeft.stg create mode 100644 antlr4-testgen-maven-plugin/resources/org/antlr4/runtime/test/templates/SemPredEvalParser/ToLeftWithVaryingPredicate.stg create mode 100644 antlr4-testgen-maven-plugin/resources/org/antlr4/runtime/test/templates/SemPredEvalParser/UnpredicatedPathsInAlt.stg rename tool/test/org/antlr/v4/test/rt/gen/grammars/SemPredEvalParser/ValidateInDFA.st => antlr4-testgen-maven-plugin/resources/org/antlr4/runtime/test/templates/SemPredEvalParser/ValidateInDFA.stg (54%) delete mode 100644 tool/test/org/antlr/v4/test/rt/gen/grammars/SemPredEvalParser/2UnpredicatedAlts.st delete mode 100644 tool/test/org/antlr/v4/test/rt/gen/grammars/SemPredEvalParser/2UnpredicatedAltsAndOneOrthogonalAlt.st delete mode 100644 tool/test/org/antlr/v4/test/rt/gen/grammars/SemPredEvalParser/ActionsHidePredsInGlobalFOLLOW.st delete mode 100644 tool/test/org/antlr/v4/test/rt/gen/grammars/SemPredEvalParser/AtomWithClosureInTranslatedLRRule.st delete mode 100644 tool/test/org/antlr/v4/test/rt/gen/grammars/SemPredEvalParser/DepedentPredsInGlobalFOLLOW.st delete mode 100644 tool/test/org/antlr/v4/test/rt/gen/grammars/SemPredEvalParser/DisabledAlternative.st delete mode 100644 tool/test/org/antlr/v4/test/rt/gen/grammars/SemPredEvalParser/IndependentPredNotPassedOuterCtxToAvoidCastException.st delete mode 100644 tool/test/org/antlr/v4/test/rt/gen/grammars/SemPredEvalParser/NoTruePredsThrowsNoViableAlt.st delete mode 100644 tool/test/org/antlr/v4/test/rt/gen/grammars/SemPredEvalParser/Order.st delete mode 100644 tool/test/org/antlr/v4/test/rt/gen/grammars/SemPredEvalParser/PredicateDependentOnArg.st delete mode 100644 tool/test/org/antlr/v4/test/rt/gen/grammars/SemPredEvalParser/PredicateDependentOnArg2.st delete mode 100644 tool/test/org/antlr/v4/test/rt/gen/grammars/SemPredEvalParser/PredsInGlobalFOLLOW.st delete mode 100644 tool/test/org/antlr/v4/test/rt/gen/grammars/SemPredEvalParser/RewindBeforePredEval.st delete mode 100644 tool/test/org/antlr/v4/test/rt/gen/grammars/SemPredEvalParser/SimpleValidate.st delete mode 100644 tool/test/org/antlr/v4/test/rt/gen/grammars/SemPredEvalParser/SimpleValidate2.st delete mode 100644 tool/test/org/antlr/v4/test/rt/gen/grammars/SemPredEvalParser/ToLeft.st delete mode 100644 tool/test/org/antlr/v4/test/rt/gen/grammars/SemPredEvalParser/ToLeftWithVaryingPredicate.st delete mode 100644 tool/test/org/antlr/v4/test/rt/gen/grammars/SemPredEvalParser/UnpredicatedPathsInAlt.st delete mode 100644 tool/test/org/antlr/v4/test/rt/java/TestSemPredEvalParser.java diff --git a/antlr4-testgen-maven-plugin/resources/org/antlr4/runtime/test/templates/Index.stg b/antlr4-testgen-maven-plugin/resources/org/antlr4/runtime/test/templates/Index.stg index b5d0275b4..7ef16eb04 100644 --- a/antlr4-testgen-maven-plugin/resources/org/antlr4/runtime/test/templates/Index.stg +++ b/antlr4-testgen-maven-plugin/resources/org/antlr4/runtime/test/templates/Index.stg @@ -1,7 +1,8 @@ TestFolders ::= [ "CompositeLexers": [], "LexerExec": [], - "ParseTrees": [] + "ParseTrees": [], + "SemPredEvalParser": [] ] //TestTemplates ::= [ diff --git a/antlr4-testgen-maven-plugin/resources/org/antlr4/runtime/test/templates/SemPredEvalParser/2UnpredicatedAlts.stg b/antlr4-testgen-maven-plugin/resources/org/antlr4/runtime/test/templates/SemPredEvalParser/2UnpredicatedAlts.stg new file mode 100644 index 000000000..9b635dee6 --- /dev/null +++ b/antlr4-testgen-maven-plugin/resources/org/antlr4/runtime/test/templates/SemPredEvalParser/2UnpredicatedAlts.stg @@ -0,0 +1,39 @@ +// We have n-2 predicates for n alternatives. pick first alt + +TestType() ::= "Parser" + +Options ::= [ + "Debug": true +] + +Grammar ::= [ + "T": {} +] + +Input() ::= "x; y" + +Rule() ::= "s" + +Output() ::= << +alt 1 +alt 1<\n> +>> + +Errors() ::= << +line 1:0 reportAttemptingFullContext d=0 (a), input='x' +line 1:0 reportAmbiguity d=0 (a): ambigAlts={1, 2}, input='x' +line 1:3 reportAttemptingFullContext d=0 (a), input='y' +line 1:3 reportAmbiguity d=0 (a): ambigAlts={1, 2}, input='y'<\n> +>> + +grammar(grammarName) ::= << +grammar ; +s : {} a ';' a; // do 2x: once in ATN, next in DFA +a : ID {} + | ID {} + | {}? ID {} + ; +ID : 'a'..'z'+ ; +INT : '0'..'9'+; +WS : (' '|'\n') -> skip ; +>> diff --git a/antlr4-testgen-maven-plugin/resources/org/antlr4/runtime/test/templates/SemPredEvalParser/2UnpredicatedAltsAndOneOrthogonalAlt.stg b/antlr4-testgen-maven-plugin/resources/org/antlr4/runtime/test/templates/SemPredEvalParser/2UnpredicatedAltsAndOneOrthogonalAlt.stg new file mode 100644 index 000000000..fe96afe76 --- /dev/null +++ b/antlr4-testgen-maven-plugin/resources/org/antlr4/runtime/test/templates/SemPredEvalParser/2UnpredicatedAltsAndOneOrthogonalAlt.stg @@ -0,0 +1,39 @@ +TestType() ::= "Parser" + +Options ::= [ + "Debug": true +] + +Grammar ::= [ + "T": {} +] + +Input() ::= "34; x; y" + +Rule() ::= "s" + +Output() ::= << +alt 1 +alt 2 +alt 2<\n> +>> + +Errors() ::= << +line 1:4 reportAttemptingFullContext d=0 (a), input='x' +line 1:4 reportAmbiguity d=0 (a): ambigAlts={2, 3}, input='x' +line 1:7 reportAttemptingFullContext d=0 (a), input='y' +line 1:7 reportAmbiguity d=0 (a): ambigAlts={2, 3}, input='y'<\n> +>> + +grammar(grammarName) ::= << +grammar ; +s : {} a ';' a ';' a; +a : INT {} + | ID {} // must pick this one for ID since pred is false + | ID {} + | {}? ID {} + ; +ID : 'a'..'z'+ ; +INT : '0'..'9'+; +WS : (' '|'\n') -> skip ; +>> diff --git a/tool/test/org/antlr/v4/test/rt/gen/grammars/SemPredEvalParser/ActionHidesPreds.st b/antlr4-testgen-maven-plugin/resources/org/antlr4/runtime/test/templates/SemPredEvalParser/ActionHidesPreds.stg similarity index 56% rename from tool/test/org/antlr/v4/test/rt/gen/grammars/SemPredEvalParser/ActionHidesPreds.st rename to antlr4-testgen-maven-plugin/resources/org/antlr4/runtime/test/templates/SemPredEvalParser/ActionHidesPreds.stg index 90c052f6e..2b8ae1577 100644 --- a/tool/test/org/antlr/v4/test/rt/gen/grammars/SemPredEvalParser/ActionHidesPreds.st +++ b/antlr4-testgen-maven-plugin/resources/org/antlr4/runtime/test/templates/SemPredEvalParser/ActionHidesPreds.stg @@ -1,3 +1,26 @@ +TestType() ::= "Parser" + +Options ::= [ + "Debug": false +] + +Grammar ::= [ + "T": {} +] + +Input() ::= "x x y" + +Rule() ::= "s" + +Output() ::= << +alt 1 +alt 1 +alt 1<\n> +>> + +Errors() ::= "" + +grammar(grammarName) ::= << grammar ; @members {} s : a+ ; @@ -7,3 +30,4 @@ a : {} ID {}? { ID : 'a'..'z'+ ; INT : '0'..'9'+; WS : (' '|'\n') -> skip ; +>> diff --git a/antlr4-testgen-maven-plugin/resources/org/antlr4/runtime/test/templates/SemPredEvalParser/ActionsHidePredsInGlobalFOLLOW.stg b/antlr4-testgen-maven-plugin/resources/org/antlr4/runtime/test/templates/SemPredEvalParser/ActionsHidePredsInGlobalFOLLOW.stg new file mode 100644 index 000000000..e61091c05 --- /dev/null +++ b/antlr4-testgen-maven-plugin/resources/org/antlr4/runtime/test/templates/SemPredEvalParser/ActionsHidePredsInGlobalFOLLOW.stg @@ -0,0 +1,39 @@ +/** Regular non-forced actions can create side effects used by semantic + * predicates and so we cannot evaluate any semantic predicate + * encountered after having seen a regular action. This includes + * during global follow operations. + */ + +TestType() ::= "Parser" + +Options ::= [ + "Debug": true +] + +Grammar ::= [ + "T": {} +] + +Input() ::= "a!" + +Rule() ::= "s" + +Output() ::= << +eval=true +parse<\n> +>> + +Errors() ::= "" + +grammar(grammarName) ::= << +grammar ; +@members { + +} +s : e {} {}? {} '!' ; +t : e {} {}? ID ; +e : ID | ; // non-LL(1) so we use ATN +ID : 'a'..'z'+ ; +INT : '0'..'9'+; +WS : (' '|'\n') -> skip ; +>> diff --git a/antlr4-testgen-maven-plugin/resources/org/antlr4/runtime/test/templates/SemPredEvalParser/AtomWithClosureInTranslatedLRRule.stg b/antlr4-testgen-maven-plugin/resources/org/antlr4/runtime/test/templates/SemPredEvalParser/AtomWithClosureInTranslatedLRRule.stg new file mode 100644 index 000000000..dadc7bafe --- /dev/null +++ b/antlr4-testgen-maven-plugin/resources/org/antlr4/runtime/test/templates/SemPredEvalParser/AtomWithClosureInTranslatedLRRule.stg @@ -0,0 +1,26 @@ +TestType() ::= "Parser" + +Options ::= [ + "Debug": false +] + +Grammar ::= [ + "T": {} +] + +Input() ::= "a+b+a" + +Rule() ::= "start" + +Output() ::= "" + +Errors() ::= "" + +grammar(grammarName) ::= << +grammar ; +start : e[0] EOF; +e[int _p] + : ( 'a' | 'b'+ ) ( {3 >= $_p}? '+' e[4] )* + ; + +>> diff --git a/antlr4-testgen-maven-plugin/resources/org/antlr4/runtime/test/templates/SemPredEvalParser/DepedentPredsInGlobalFOLLOW.stg b/antlr4-testgen-maven-plugin/resources/org/antlr4/runtime/test/templates/SemPredEvalParser/DepedentPredsInGlobalFOLLOW.stg new file mode 100644 index 000000000..e035e01ce --- /dev/null +++ b/antlr4-testgen-maven-plugin/resources/org/antlr4/runtime/test/templates/SemPredEvalParser/DepedentPredsInGlobalFOLLOW.stg @@ -0,0 +1,38 @@ +/** We cannot collect predicates that are dependent on local context if + * we are doing a global follow. They appear as if they were not there at all. + */ + +TestType() ::= "Parser" + +Options ::= [ + "Debug": true +] + +Grammar ::= [ + "T": {} +] + +Input() ::= "a!" + +Rule() ::= "s" + +Output() ::= << +eval=true +parse<\n> +>> + +Errors() ::= "" + +grammar(grammarName) ::= << +grammar ; +@members { + +} +s : a[99] ; +a[int i] : e {}? {} '!' ; +b[int i] : e {}? ID ; +e : ID | ; // non-LL(1) so we use ATN +ID : 'a'..'z'+ ; +INT : '0'..'9'+; +WS : (' '|'\n') -> skip ; +>> diff --git a/tool/test/org/antlr/v4/test/rt/gen/grammars/SemPredEvalParser/DependentPredNotInOuterCtxShouldBeIgnored.st b/antlr4-testgen-maven-plugin/resources/org/antlr4/runtime/test/templates/SemPredEvalParser/DependentPredNotInOuterCtxShouldBeIgnored.stg similarity index 53% rename from tool/test/org/antlr/v4/test/rt/gen/grammars/SemPredEvalParser/DependentPredNotInOuterCtxShouldBeIgnored.st rename to antlr4-testgen-maven-plugin/resources/org/antlr4/runtime/test/templates/SemPredEvalParser/DependentPredNotInOuterCtxShouldBeIgnored.stg index 266a67208..42ac6b8b7 100644 --- a/tool/test/org/antlr/v4/test/rt/gen/grammars/SemPredEvalParser/DependentPredNotInOuterCtxShouldBeIgnored.st +++ b/antlr4-testgen-maven-plugin/resources/org/antlr4/runtime/test/templates/SemPredEvalParser/DependentPredNotInOuterCtxShouldBeIgnored.stg @@ -1,3 +1,26 @@ +// uses ID ';' or ID '.' lookahead to solve s. preds not tested. + +TestType() ::= "Parser" + +Options ::= [ + "Debug": true +] + +Grammar ::= [ + "T": {} +] + +Input() ::= "a;" + +Rule() ::= "s" + +Output() ::= << +alt 2<\n> +>> + +Errors() ::= "" + +grammar(grammarName) ::= << grammar ; s : b[2] ';' | b[2] '.' ; // decision in s drills down to ctx-dependent pred in a; b[int i] : a[i] ; @@ -9,3 +32,4 @@ ID : 'a'..'z'+ ; INT : '0'..'9'+; WS : (' '|'\n') -> skip ; +>> diff --git a/antlr4-testgen-maven-plugin/resources/org/antlr4/runtime/test/templates/SemPredEvalParser/DisabledAlternative.stg b/antlr4-testgen-maven-plugin/resources/org/antlr4/runtime/test/templates/SemPredEvalParser/DisabledAlternative.stg new file mode 100644 index 000000000..5af1398ba --- /dev/null +++ b/antlr4-testgen-maven-plugin/resources/org/antlr4/runtime/test/templates/SemPredEvalParser/DisabledAlternative.stg @@ -0,0 +1,31 @@ +/** + * This is a regression test for antlr/antlr4#218 "ANTLR4 EOF Related Bug". + * https://github.com/antlr/antlr4/issues/218 + */ + +TestType() ::= "Parser" + +Options ::= [ + "Debug": true +] + +Grammar ::= [ + "T": {} +] + +Input() ::= "hello" + +Rule() ::= "cppCompilationUnit" + +Output() ::= << +>> + +Errors() ::= "" + +grammar(grammarName) ::= << +grammar ; +cppCompilationUnit : content+ EOF; +content: anything | {}? .; +anything: ANY_CHAR; +ANY_CHAR: [_a-zA-Z0-9]; +>> diff --git a/antlr4-testgen-maven-plugin/resources/org/antlr4/runtime/test/templates/SemPredEvalParser/IndependentPredNotPassedOuterCtxToAvoidCastException.stg b/antlr4-testgen-maven-plugin/resources/org/antlr4/runtime/test/templates/SemPredEvalParser/IndependentPredNotPassedOuterCtxToAvoidCastException.stg new file mode 100644 index 000000000..7740b7240 --- /dev/null +++ b/antlr4-testgen-maven-plugin/resources/org/antlr4/runtime/test/templates/SemPredEvalParser/IndependentPredNotPassedOuterCtxToAvoidCastException.stg @@ -0,0 +1,32 @@ +TestType() ::= "Parser" + +Options ::= [ + "Debug": true +] + +Grammar ::= [ + "T": {} +] + +Input() ::= "a;" + +Rule() ::= "s" + +Output() ::= << +alt 2<\n> +>> + +Errors() ::= "" + +grammar(grammarName) ::= << +grammar ; +s : b ';' | b '.' ; +b : a ; +a + : {}? ID {} + | {}? ID {} + ; +ID : 'a'..'z'+ ; +INT : '0'..'9'+; +WS : (' '|'\n') -> skip ; +>> diff --git a/antlr4-testgen-maven-plugin/resources/org/antlr4/runtime/test/templates/SemPredEvalParser/Index.stg b/antlr4-testgen-maven-plugin/resources/org/antlr4/runtime/test/templates/SemPredEvalParser/Index.stg new file mode 100644 index 000000000..6ebb56c49 --- /dev/null +++ b/antlr4-testgen-maven-plugin/resources/org/antlr4/runtime/test/templates/SemPredEvalParser/Index.stg @@ -0,0 +1,31 @@ +//TestFolders ::= [ +//] + +TestTemplates ::= [ + "SimpleValidate": [], + "SimpleValidate2": [], + "AtomWithClosureInTranslatedLRRule": [], + "ValidateInDFA": [], + "Simple": [], + "Order": [], + "2UnpredicatedAlts": [], + "2UnpredicatedAltsAndOneOrthogonalAlt": [], + "RewindBeforePredEval": [], + "NoTruePredsThrowsNoViableAlt": [], + "ToLeft": [], + "UnpredicatedPathsInAlt": [], + "ActionHidesPreds": [], + "ToLeftWithVaryingPredicate": [], + "PredicateDependentOnArg": [], + "PredicateDependentOnArg2": [], + "DependentPredNotInOuterCtxShouldBeIgnored": [], + "IndependentPredNotPassedOuterCtxToAvoidCastException": [], + "PredsInGlobalFOLLOW": [], + "DepedentPredsInGlobalFOLLOW": [], + "ActionsHidePredsInGlobalFOLLOW": [], + "PredTestedEvenWhenUnAmbig_1": [], + "PredTestedEvenWhenUnAmbig_2": [], + "DisabledAlternative": [], + "PredFromAltTestedInLoopBack_1": [], + "PredFromAltTestedInLoopBack_2": [] +] diff --git a/antlr4-testgen-maven-plugin/resources/org/antlr4/runtime/test/templates/SemPredEvalParser/NoTruePredsThrowsNoViableAlt.stg b/antlr4-testgen-maven-plugin/resources/org/antlr4/runtime/test/templates/SemPredEvalParser/NoTruePredsThrowsNoViableAlt.stg new file mode 100644 index 000000000..7114684cf --- /dev/null +++ b/antlr4-testgen-maven-plugin/resources/org/antlr4/runtime/test/templates/SemPredEvalParser/NoTruePredsThrowsNoViableAlt.stg @@ -0,0 +1,34 @@ +// checks that we throw exception if all alts +// are covered with a predicate and none succeeds + +TestType() ::= "Parser" + +Options ::= [ + "Debug": false +] + +Grammar ::= [ + "T": {} +] + +Input() ::= "y 3 x 4" + +Rule() ::= "s" + +Output() ::= << +>> + +Errors() ::= << +line 1:0 no viable alternative at input 'y'<\n> +>> + +grammar(grammarName) ::= << +grammar ; +s : a a; +a : {}? ID INT {} + | {}? ID INT {} + ; +ID : 'a'..'z'+ ; +INT : '0'..'9'+; +WS : (' '|'\n') -> skip ; +>> diff --git a/antlr4-testgen-maven-plugin/resources/org/antlr4/runtime/test/templates/SemPredEvalParser/Order.stg b/antlr4-testgen-maven-plugin/resources/org/antlr4/runtime/test/templates/SemPredEvalParser/Order.stg new file mode 100644 index 000000000..4837a3a78 --- /dev/null +++ b/antlr4-testgen-maven-plugin/resources/org/antlr4/runtime/test/templates/SemPredEvalParser/Order.stg @@ -0,0 +1,37 @@ +// Under new predicate ordering rules (see antlr/antlr4#29), the first +// alt with an acceptable config (unpredicated, or predicated and evaluates +// to true) is chosen. + +TestType() ::= "Parser" + +Options ::= [ + "Debug": false +] + +Grammar ::= [ + "T": {} +] + +Input() ::= "x y" + +Rule() ::= "s" + +Output() ::= << +alt 1 +alt 1<\n> +>> + +Errors() ::= "" + +grammar(grammarName) ::= << +grammar ; +s : a {} a; // do 2x: once in ATN, next in DFA; +// action blocks lookahead from falling off of 'a' +// and looking into 2nd 'a' ref. !ctx dependent pred +a : ID {} + | {}? ID {} + ; +ID : 'a'..'z'+ ; +INT : '0'..'9'+; +WS : (' '|'\n') -> skip ; +>> diff --git a/tool/test/org/antlr/v4/test/rt/gen/grammars/SemPredEvalParser/PredFromAltTestedInLoopBack.st b/antlr4-testgen-maven-plugin/resources/org/antlr4/runtime/test/templates/SemPredEvalParser/PredFromAltTestedInLoopBack.stg similarity index 50% rename from tool/test/org/antlr/v4/test/rt/gen/grammars/SemPredEvalParser/PredFromAltTestedInLoopBack.st rename to antlr4-testgen-maven-plugin/resources/org/antlr4/runtime/test/templates/SemPredEvalParser/PredFromAltTestedInLoopBack.stg index 697cd9a66..b18869cc7 100644 --- a/tool/test/org/antlr/v4/test/rt/gen/grammars/SemPredEvalParser/PredFromAltTestedInLoopBack.st +++ b/antlr4-testgen-maven-plugin/resources/org/antlr4/runtime/test/templates/SemPredEvalParser/PredFromAltTestedInLoopBack.stg @@ -1,3 +1,18 @@ +/** Loopback doesn't eval predicate at start of alt */ + +TestType() ::= "Parser" + +Options ::= [ + "Debug": true +] + +Grammar ::= [ + "T": {} +] + +Rule() ::= "file_" + +grammar(grammarName) ::= << grammar ; file_ @after {} @@ -7,3 +22,4 @@ paraContent : ('s'|'x'|{}? NL)+ ; NL : '\n' ; s : 's' ; X : 'x' ; +>> diff --git a/antlr4-testgen-maven-plugin/resources/org/antlr4/runtime/test/templates/SemPredEvalParser/PredFromAltTestedInLoopBack_1.stg b/antlr4-testgen-maven-plugin/resources/org/antlr4/runtime/test/templates/SemPredEvalParser/PredFromAltTestedInLoopBack_1.stg new file mode 100644 index 000000000..d28d02c1c --- /dev/null +++ b/antlr4-testgen-maven-plugin/resources/org/antlr4/runtime/test/templates/SemPredEvalParser/PredFromAltTestedInLoopBack_1.stg @@ -0,0 +1,12 @@ +import "PredFromAltTestedInLoopBack.stg" + +Input() ::= "s<\n><\n><\n>x<\n>" + +Output() ::= << +(file_ (para (paraContent s) \n \n) (para (paraContent \n x \n)) \)<\n> +>> + +Errors() ::= << +line 5:0 mismatched input '\' expecting ' +'<\n> +>> diff --git a/antlr4-testgen-maven-plugin/resources/org/antlr4/runtime/test/templates/SemPredEvalParser/PredFromAltTestedInLoopBack_2.stg b/antlr4-testgen-maven-plugin/resources/org/antlr4/runtime/test/templates/SemPredEvalParser/PredFromAltTestedInLoopBack_2.stg new file mode 100644 index 000000000..f77ec7080 --- /dev/null +++ b/antlr4-testgen-maven-plugin/resources/org/antlr4/runtime/test/templates/SemPredEvalParser/PredFromAltTestedInLoopBack_2.stg @@ -0,0 +1,9 @@ +import "PredFromAltTestedInLoopBack.stg" + +Input() ::= "s<\n><\n><\n>x<\n><\n>" + +Output() ::= << +(file_ (para (paraContent s) \n \n) (para (paraContent \n x) \n \n) \)<\n> +>> + +Errors() ::= "" diff --git a/tool/test/org/antlr/v4/test/rt/gen/grammars/SemPredEvalParser/PredTestedEvenWhenUnAmbig.st b/antlr4-testgen-maven-plugin/resources/org/antlr4/runtime/test/templates/SemPredEvalParser/PredTestedEvenWhenUnAmbig.stg similarity index 62% rename from tool/test/org/antlr/v4/test/rt/gen/grammars/SemPredEvalParser/PredTestedEvenWhenUnAmbig.st rename to antlr4-testgen-maven-plugin/resources/org/antlr4/runtime/test/templates/SemPredEvalParser/PredTestedEvenWhenUnAmbig.stg index 7b724ad2f..c8b07769c 100644 --- a/tool/test/org/antlr/v4/test/rt/gen/grammars/SemPredEvalParser/PredTestedEvenWhenUnAmbig.st +++ b/antlr4-testgen-maven-plugin/resources/org/antlr4/runtime/test/templates/SemPredEvalParser/PredTestedEvenWhenUnAmbig.stg @@ -1,3 +1,16 @@ +TestType() ::= "Parser" + +Options ::= [ + "Debug": true +] + +Grammar ::= [ + "T": {} +] + +Rule() ::= "primary" + +grammar(grammarName) ::= << grammar ; @members {} primary @@ -6,3 +19,4 @@ primary ; ID : [a-z]+ ; WS : [ \t\n\r]+ -> skip ; +>> diff --git a/antlr4-testgen-maven-plugin/resources/org/antlr4/runtime/test/templates/SemPredEvalParser/PredTestedEvenWhenUnAmbig_1.stg b/antlr4-testgen-maven-plugin/resources/org/antlr4/runtime/test/templates/SemPredEvalParser/PredTestedEvenWhenUnAmbig_1.stg new file mode 100644 index 000000000..dc0698d38 --- /dev/null +++ b/antlr4-testgen-maven-plugin/resources/org/antlr4/runtime/test/templates/SemPredEvalParser/PredTestedEvenWhenUnAmbig_1.stg @@ -0,0 +1,9 @@ +import "PredTestedEvenWhenUnAmbig.stg" + +Input() ::= "abc" + +Output() ::= << +ID abc<\n> +>> + +Errors() ::= "" diff --git a/antlr4-testgen-maven-plugin/resources/org/antlr4/runtime/test/templates/SemPredEvalParser/PredTestedEvenWhenUnAmbig_2.stg b/antlr4-testgen-maven-plugin/resources/org/antlr4/runtime/test/templates/SemPredEvalParser/PredTestedEvenWhenUnAmbig_2.stg new file mode 100644 index 000000000..e8c185bad --- /dev/null +++ b/antlr4-testgen-maven-plugin/resources/org/antlr4/runtime/test/templates/SemPredEvalParser/PredTestedEvenWhenUnAmbig_2.stg @@ -0,0 +1,10 @@ +import "PredTestedEvenWhenUnAmbig.stg" + +Input() ::= "enum" + +Output() ::= << +>> + +Errors() ::= << +line 1:0 no viable alternative at input 'enum'<\n> +>> diff --git a/antlr4-testgen-maven-plugin/resources/org/antlr4/runtime/test/templates/SemPredEvalParser/PredicateDependentOnArg.stg b/antlr4-testgen-maven-plugin/resources/org/antlr4/runtime/test/templates/SemPredEvalParser/PredicateDependentOnArg.stg new file mode 100644 index 000000000..a06dfad91 --- /dev/null +++ b/antlr4-testgen-maven-plugin/resources/org/antlr4/runtime/test/templates/SemPredEvalParser/PredicateDependentOnArg.stg @@ -0,0 +1,40 @@ +/** + * In this case, we're passing a parameter into a rule that uses that + * information to predict the alternatives. This is the special case + * where we know exactly which context we are in. The context stack + * is empty and we have not dipped into the outer context to make a decision. + */ + +TestType() ::= "Parser" + +Options ::= [ + "Debug": true +] + +Grammar ::= [ + "T": {} +] + +Input() ::= "a b" + +Rule() ::= "s" + +Output() ::= << +alt 2 +alt 1<\n> +>> + +Errors() ::= "" + +grammar(grammarName) ::= << +grammar ; +@members {} +s : a[2] a[1]; +a[int i] + : {}? ID {} + | {}? ID {} + ; +ID : 'a'..'z'+ ; +INT : '0'..'9'+; +WS : (' '|'\n') -> skip ; +>> diff --git a/antlr4-testgen-maven-plugin/resources/org/antlr4/runtime/test/templates/SemPredEvalParser/PredicateDependentOnArg2.stg b/antlr4-testgen-maven-plugin/resources/org/antlr4/runtime/test/templates/SemPredEvalParser/PredicateDependentOnArg2.stg new file mode 100644 index 000000000..2dac96b88 --- /dev/null +++ b/antlr4-testgen-maven-plugin/resources/org/antlr4/runtime/test/templates/SemPredEvalParser/PredicateDependentOnArg2.stg @@ -0,0 +1,42 @@ +/** + * In this case, we have to ensure that the predicates are not tested + * during the closure after recognizing the 1st ID. The closure will + * fall off the end of 'a' 1st time and reach into the a[1] rule + * invocation. It should not execute predicates because it does not know + * what the parameter is. The context stack will not be empty and so + * they should be ignored. It will not affect recognition, however. We + * are really making sure the ATN simulation doesn't crash with context + * object issues when it encounters preds during FOLLOW. + */ + +TestType() ::= "Parser" + +Options ::= [ + "Debug": true +] + +Grammar ::= [ + "T": {} +] + +Input() ::= "a b" + +Rule() ::= "s" + +Output() ::= << +>> + +Errors() ::= "" + +grammar(grammarName) ::= << +grammar ; +@members {} +s : a[2] a[1]; +a[int i] + : {}? ID + | {}? ID + ; +ID : 'a'..'z'+ ; +INT : '0'..'9'+; +WS : (' '|'\n') -> skip ; +>> diff --git a/antlr4-testgen-maven-plugin/resources/org/antlr4/runtime/test/templates/SemPredEvalParser/PredsInGlobalFOLLOW.stg b/antlr4-testgen-maven-plugin/resources/org/antlr4/runtime/test/templates/SemPredEvalParser/PredsInGlobalFOLLOW.stg new file mode 100644 index 000000000..66af3d509 --- /dev/null +++ b/antlr4-testgen-maven-plugin/resources/org/antlr4/runtime/test/templates/SemPredEvalParser/PredsInGlobalFOLLOW.stg @@ -0,0 +1,38 @@ +/** During a global follow operation, we still collect semantic + * predicates as long as they are not dependent on local context + */ + +TestType() ::= "Parser" + +Options ::= [ + "Debug": true +] + +Grammar ::= [ + "T": {} +] + +Input() ::= "a!" + +Rule() ::= "s" + +Output() ::= << +eval=true + +parse<\n> +>> + +Errors() ::= "" + +grammar(grammarName) ::= << +grammar ; +@members { + +} +s : e {}? {} '!' ; +t : e {}? ID ; +e : ID | ; // non-LL(1) so we use ATN +ID : 'a'..'z'+ ; +INT : '0'..'9'+; +WS : (' '|'\n') -> skip ; +>> diff --git a/antlr4-testgen-maven-plugin/resources/org/antlr4/runtime/test/templates/SemPredEvalParser/RewindBeforePredEval.stg b/antlr4-testgen-maven-plugin/resources/org/antlr4/runtime/test/templates/SemPredEvalParser/RewindBeforePredEval.stg new file mode 100644 index 000000000..aef9f484b --- /dev/null +++ b/antlr4-testgen-maven-plugin/resources/org/antlr4/runtime/test/templates/SemPredEvalParser/RewindBeforePredEval.stg @@ -0,0 +1,35 @@ +// The parser consumes ID and moves to the 2nd token INT. +// To properly evaluate the predicates after matching ID INT, +// we must correctly see come back to starting index so LT(1) works + +TestType() ::= "Parser" + +Options ::= [ + "Debug": true +] + +Grammar ::= [ + "T": {} +] + +Input() ::= "y 3 x 4" + +Rule() ::= "s" + +Output() ::= << +alt 2 +alt 1<\n> +>> + +Errors() ::= "" + +grammar(grammarName) ::= << +grammar ; +s : a a; +a : {}? ID INT {} + | {}? ID INT {} + ; +ID : 'a'..'z'+ ; +INT : '0'..'9'+; +WS : (' '|'\n') -> skip ; +>> diff --git a/tool/test/org/antlr/v4/test/rt/gen/grammars/SemPredEvalParser/Simple.st b/antlr4-testgen-maven-plugin/resources/org/antlr4/runtime/test/templates/SemPredEvalParser/Simple.stg similarity index 55% rename from tool/test/org/antlr/v4/test/rt/gen/grammars/SemPredEvalParser/Simple.st rename to antlr4-testgen-maven-plugin/resources/org/antlr4/runtime/test/templates/SemPredEvalParser/Simple.stg index 5285fcf83..edae9b3ee 100644 --- a/tool/test/org/antlr/v4/test/rt/gen/grammars/SemPredEvalParser/Simple.st +++ b/antlr4-testgen-maven-plugin/resources/org/antlr4/runtime/test/templates/SemPredEvalParser/Simple.stg @@ -1,3 +1,26 @@ +TestType() ::= "Parser" + +Options ::= [ + "Debug": true +] + +Grammar ::= [ + "T": {} +] + +Input() ::= "x y 3" + +Rule() ::= "s" + +Output() ::= << +alt 2 +alt 2 +alt 3<\n> +>> + +Errors() ::= "" + +grammar(grammarName) ::= << grammar ; s : a a a; // do 3x: once in ATN, next in DFA then INT in ATN a : {}? ID {} @@ -7,3 +30,4 @@ a : {}? ID {} ID : 'a'..'z'+ ; INT : '0'..'9'+; WS : (' '|'\n') -> skip ; +>> diff --git a/antlr4-testgen-maven-plugin/resources/org/antlr4/runtime/test/templates/SemPredEvalParser/SimpleValidate.stg b/antlr4-testgen-maven-plugin/resources/org/antlr4/runtime/test/templates/SemPredEvalParser/SimpleValidate.stg new file mode 100644 index 000000000..89ccf09b1 --- /dev/null +++ b/antlr4-testgen-maven-plugin/resources/org/antlr4/runtime/test/templates/SemPredEvalParser/SimpleValidate.stg @@ -0,0 +1,30 @@ +TestType() ::= "Parser" + +Options ::= [ + "Debug": true +] + +Grammar ::= [ + "T": {} +] + +Input() ::= "x" + +Rule() ::= "s" + +Output() ::= "" + +Errors() ::= << +line 1:0 no viable alternative at input 'x'<\n> +>> + +grammar(grammarName) ::= << +grammar ; +s : a ; +a : {}? ID {} + | {}? INT {} + ; +ID : 'a'..'z'+ ; +INT : '0'..'9'+; +WS : (' '|'\n') -> skip ; +>> diff --git a/antlr4-testgen-maven-plugin/resources/org/antlr4/runtime/test/templates/SemPredEvalParser/SimpleValidate2.stg b/antlr4-testgen-maven-plugin/resources/org/antlr4/runtime/test/templates/SemPredEvalParser/SimpleValidate2.stg new file mode 100644 index 000000000..5cc7c4a97 --- /dev/null +++ b/antlr4-testgen-maven-plugin/resources/org/antlr4/runtime/test/templates/SemPredEvalParser/SimpleValidate2.stg @@ -0,0 +1,33 @@ +TestType() ::= "Parser" + +Options ::= [ + "Debug": true +] + +Grammar ::= [ + "T": {} +] + +Input() ::= "3 4 x" + +Rule() ::= "s" + +Output() ::= << +alt 2 +alt 2<\n> +>> + +Errors() ::= << +line 1:4 no viable alternative at input 'x'<\n> +>> + +grammar(grammarName) ::= << +grammar ; +s : a a a; +a : {}? ID {} + | {}? INT {} + ; +ID : 'a'..'z'+ ; +INT : '0'..'9'+; +WS : (' '|'\n') -> skip ; +>> diff --git a/antlr4-testgen-maven-plugin/resources/org/antlr4/runtime/test/templates/SemPredEvalParser/ToLeft.stg b/antlr4-testgen-maven-plugin/resources/org/antlr4/runtime/test/templates/SemPredEvalParser/ToLeft.stg new file mode 100644 index 000000000..c47f4c8cf --- /dev/null +++ b/antlr4-testgen-maven-plugin/resources/org/antlr4/runtime/test/templates/SemPredEvalParser/ToLeft.stg @@ -0,0 +1,32 @@ +TestType() ::= "Parser" + +Options ::= [ + "Debug": true +] + +Grammar ::= [ + "T": {} +] + +Input() ::= "x x y" + +Rule() ::= "s" + +Output() ::= << +alt 2 +alt 2 +alt 2<\n> +>> + +Errors() ::= "" + +grammar(grammarName) ::= << +grammar ; + s : a+ ; +a : {}? ID {} + | {}? ID {} + ; +ID : 'a'..'z'+ ; +INT : '0'..'9'+; +WS : (' '|'\n') -> skip ; +>> diff --git a/antlr4-testgen-maven-plugin/resources/org/antlr4/runtime/test/templates/SemPredEvalParser/ToLeftWithVaryingPredicate.stg b/antlr4-testgen-maven-plugin/resources/org/antlr4/runtime/test/templates/SemPredEvalParser/ToLeftWithVaryingPredicate.stg new file mode 100644 index 000000000..882ec127d --- /dev/null +++ b/antlr4-testgen-maven-plugin/resources/org/antlr4/runtime/test/templates/SemPredEvalParser/ToLeftWithVaryingPredicate.stg @@ -0,0 +1,43 @@ +/** In this case, we use predicates that depend on global information + * like we would do for a symbol table. We simply execute + * the predicates assuming that all necessary information is available. + * The i++ action is done outside of the prediction and so it is executed. + */ + +TestType() ::= "Parser" + +Options ::= [ + "Debug": true +] + +Grammar ::= [ + "T": {} +] + +Input() ::= "x x y" + +Rule() ::= "s" + +Output() ::= << +i=1 +alt 2 +i=2 +alt 1 +i=3 +alt 2<\n> +>> + +Errors() ::= "" + +grammar(grammarName) ::= << +grammar ; +@members {} +s : ({ +} a)+ ; +a : {}? ID {} + | {}? ID {} + ; +ID : 'a'..'z'+ ; +INT : '0'..'9'+; +WS : (' '|'\n') -> skip ; +>> diff --git a/antlr4-testgen-maven-plugin/resources/org/antlr4/runtime/test/templates/SemPredEvalParser/UnpredicatedPathsInAlt.stg b/antlr4-testgen-maven-plugin/resources/org/antlr4/runtime/test/templates/SemPredEvalParser/UnpredicatedPathsInAlt.stg new file mode 100644 index 000000000..baab3cd2f --- /dev/null +++ b/antlr4-testgen-maven-plugin/resources/org/antlr4/runtime/test/templates/SemPredEvalParser/UnpredicatedPathsInAlt.stg @@ -0,0 +1,34 @@ +TestType() ::= "Parser" + +Options ::= [ + "Debug": true +] + +Grammar ::= [ + "T": {} +] + +Input() ::= "x 4" + +Rule() ::= "s" + +Output() ::= << +alt 1<\n> +>> + +Errors() ::= "" + +grammar(grammarName) ::= << +grammar ; +s : a {} + | b {} + ; +a : {}? ID INT + | ID INT + ; +b : ID ID + ; +ID : 'a'..'z'+ ; +INT : '0'..'9'+; +WS : (' '|'\n') -> skip ; +>> diff --git a/tool/test/org/antlr/v4/test/rt/gen/grammars/SemPredEvalParser/ValidateInDFA.st b/antlr4-testgen-maven-plugin/resources/org/antlr4/runtime/test/templates/SemPredEvalParser/ValidateInDFA.stg similarity index 54% rename from tool/test/org/antlr/v4/test/rt/gen/grammars/SemPredEvalParser/ValidateInDFA.st rename to antlr4-testgen-maven-plugin/resources/org/antlr4/runtime/test/templates/SemPredEvalParser/ValidateInDFA.stg index 47b10e627..d1ba43393 100644 --- a/tool/test/org/antlr/v4/test/rt/gen/grammars/SemPredEvalParser/ValidateInDFA.st +++ b/antlr4-testgen-maven-plugin/resources/org/antlr4/runtime/test/templates/SemPredEvalParser/ValidateInDFA.stg @@ -1,3 +1,26 @@ +TestType() ::= "Parser" + +Options ::= [ + "Debug": true +] + +Grammar ::= [ + "T": {} +] + +Input() ::= "x ; y" + +Rule() ::= "s" + +Output() ::= << +>> + +Errors() ::= << +line 1:0 no viable alternative at input 'x' +line 1:4 no viable alternative at input 'y'<\n> +>> + +grammar(grammarName) ::= << grammar ; s : a ';' a; // ';' helps us to resynchronize without consuming @@ -9,3 +32,4 @@ a : {}? ID {} ID : 'a'..'z'+ ; INT : '0'..'9'+; WS : (' '|'\n') -> skip ; +>> diff --git a/antlr4-testgen-maven-plugin/src/main/java/org/antlr/mojo/antlr4/testgen/Antlr4TestGeneratorMojo.java b/antlr4-testgen-maven-plugin/src/main/java/org/antlr/mojo/antlr4/testgen/Antlr4TestGeneratorMojo.java index 5e16982a3..44a6a4243 100644 --- a/antlr4-testgen-maven-plugin/src/main/java/org/antlr/mojo/antlr4/testgen/Antlr4TestGeneratorMojo.java +++ b/antlr4-testgen-maven-plugin/src/main/java/org/antlr/mojo/antlr4/testgen/Antlr4TestGeneratorMojo.java @@ -109,7 +109,7 @@ public class Antlr4TestGeneratorMojo extends AbstractMojo { List templates = new ArrayList(); for (String template : testTemplates) { STGroup testGroup = new STGroupFile(templateFolder + "/" + template + STGroup.GROUP_FILE_EXTENSION); - testGroup.importTemplates(targetGroup); + importLanguageTemplates(testGroup, targetGroup); ST testType = testGroup.getInstanceOf("TestType"); if (testType == null) { getLog().warn(String.format("Unable to generate tests for %s: no TestType specified.", template)); @@ -143,6 +143,29 @@ public class Antlr4TestGeneratorMojo extends AbstractMojo { } } + private void importLanguageTemplates(STGroup testGroup, STGroup languageGroup) { + // make sure the test group is loaded + testGroup.load(); + + if (testGroup == languageGroup) { + assert false : "Attempted to import the language group into itself."; + return; + } + + if (testGroup.getImportedGroups().isEmpty()) { + testGroup.importTemplates(languageGroup); + return; + } + + if (testGroup.getImportedGroups().contains(languageGroup)) { + return; + } + + for (STGroup importedGroup : testGroup.getImportedGroups()) { + importLanguageTemplates(importedGroup, languageGroup); + } + } + public void writeFile(File file, String content) throws IOException { file.getParentFile().mkdirs(); diff --git a/antlr4-testgen-maven-plugin/src/main/java/org/antlr/mojo/antlr4/testgen/STGroupModelAdaptor.java b/antlr4-testgen-maven-plugin/src/main/java/org/antlr/mojo/antlr4/testgen/STGroupModelAdaptor.java index ca8424f07..78a43c9b6 100644 --- a/antlr4-testgen-maven-plugin/src/main/java/org/antlr/mojo/antlr4/testgen/STGroupModelAdaptor.java +++ b/antlr4-testgen-maven-plugin/src/main/java/org/antlr/mojo/antlr4/testgen/STGroupModelAdaptor.java @@ -6,16 +6,16 @@ package org.antlr.mojo.antlr4.testgen; import org.stringtemplate.v4.Interpreter; +import org.stringtemplate.v4.ModelAdaptor; import org.stringtemplate.v4.ST; import org.stringtemplate.v4.STGroup; -import org.stringtemplate.v4.misc.ObjectModelAdaptor; import org.stringtemplate.v4.misc.STNoSuchPropertyException; /** * * @author sam */ -public class STGroupModelAdaptor extends ObjectModelAdaptor { +public class STGroupModelAdaptor implements ModelAdaptor { @Override public Object getProperty(Interpreter interp, ST self, Object o, Object property, String propertyName) throws STNoSuchPropertyException { @@ -29,7 +29,18 @@ public class STGroupModelAdaptor extends ObjectModelAdaptor { return template; } - return super.getProperty(interp, self, o, property, propertyName); + if ("name".equalsIgnoreCase(propertyName)) { + return group.getName(); + } + + for (STGroup importedGroup : group.getImportedGroups()) { + Object result = getProperty(interp, self, importedGroup, property, propertyName); + if (result != null) { + return result; + } + } + + return null; } } diff --git a/tool/test/org/antlr/v4/test/rt/gen/Generator.java b/tool/test/org/antlr/v4/test/rt/gen/Generator.java index e8bcb9ffa..22f2dc7c4 100644 --- a/tool/test/org/antlr/v4/test/rt/gen/Generator.java +++ b/tool/test/org/antlr/v4/test/rt/gen/Generator.java @@ -158,188 +158,9 @@ public class Generator { list.add(buildParserErrors()); list.add(buildParserExec()); list.add(buildSemPredEvalLexer()); - list.add(buildSemPredEvalParser()); return list; } - private JUnitTestFile buildSemPredEvalParser() throws Exception { - JUnitTestFile file = new JUnitTestFile("SemPredEvalParser"); - JUnitTestMethod tm = file.addParserTest(input, "SimpleValidate", "T", "s", - "x", - "", - "line 1:0 no viable alternative at input 'x'\n"); - tm.debug = true; - tm = file.addParserTest(input, "SimpleValidate2", "T", "s", - "3 4 x", - "alt 2\n" + "alt 2\n", - "line 1:4 no viable alternative at input 'x'\n"); - tm.debug = true; - file.addParserTest(input, "AtomWithClosureInTranslatedLRRule", "T", "start", - "a+b+a", - "", - null); - tm = file.addParserTest(input, "ValidateInDFA", "T", "s", - "x ; y", - "", - "line 1:0 no viable alternative at input 'x'\n" + - "line 1:4 no viable alternative at input 'y'\n"); - tm.debug = true; - tm = file.addParserTest(input, "Simple", "T", "s", - "x y 3", - "alt 2\n" + "alt 2\n" + "alt 3\n", - null); - // Under new predicate ordering rules (see antlr/antlr4#29), the first - // alt with an acceptable config (unpredicated, or predicated and evaluates - // to true) is chosen. - tm.debug = true; - file.addParserTest(input, "Order", "T", "s", - "x y", - "alt 1\n" + "alt 1\n", - null); - // We have n-2 predicates for n alternatives. pick first alt - tm = file.addParserTest(input, "2UnpredicatedAlts", "T", "s", - "x; y", - "alt 1\n" + - "alt 1\n", - "line 1:0 reportAttemptingFullContext d=0 (a), input='x'\n" + - "line 1:0 reportAmbiguity d=0 (a): ambigAlts={1, 2}, input='x'\n" + - "line 1:3 reportAttemptingFullContext d=0 (a), input='y'\n" + - "line 1:3 reportAmbiguity d=0 (a): ambigAlts={1, 2}, input='y'\n"); - tm.debug = true; - tm = file.addParserTest(input, "2UnpredicatedAltsAndOneOrthogonalAlt", "T", "s", - "34; x; y", - "alt 1\n" + "alt 2\n" + "alt 2\n", - "line 1:4 reportAttemptingFullContext d=0 (a), input='x'\n" + - "line 1:4 reportAmbiguity d=0 (a): ambigAlts={2, 3}, input='x'\n" + - "line 1:7 reportAttemptingFullContext d=0 (a), input='y'\n" + - "line 1:7 reportAmbiguity d=0 (a): ambigAlts={2, 3}, input='y'\n"); - // The parser consumes ID and moves to the 2nd token INT. - // To properly evaluate the predicates after matching ID INT, - // we must correctly see come back to starting index so LT(1) works - tm.debug = true; - tm = file.addParserTest(input, "RewindBeforePredEval", "T", "s", - "y 3 x 4", - "alt 2\n" + "alt 1\n", - null); - // checks that we throw exception if all alts - // are covered with a predicate and none succeeds - tm.debug = true; - file.addParserTest(input, "NoTruePredsThrowsNoViableAlt", "T", "s", - "y 3 x 4", - "", - "line 1:0 no viable alternative at input 'y'\n"); - tm = file.addParserTest(input, "ToLeft", "T", "s", - "x x y", - "alt 2\n" + "alt 2\n" + "alt 2\n", - null); - tm.debug = true; - tm = file.addParserTest(input, "UnpredicatedPathsInAlt", "T", "s", - "x 4", - "alt 1\n", - null); - tm.debug = true; - file.addParserTest(input, "ActionHidesPreds", "T", "s", - "x x y", - "alt 1\n" + "alt 1\n" + "alt 1\n", - null); - /** In this case, we use predicates that depend on global information - * like we would do for a symbol table. We simply execute - * the predicates assuming that all necessary information is available. - * The i++ action is done outside of the prediction and so it is executed. - */ - tm = file.addParserTest(input, "ToLeftWithVaryingPredicate", "T", "s", - "x x y", - "i=1\n" + "alt 2\n" + "i=2\n" + "alt 1\n" + "i=3\n" + "alt 2\n", - null); - tm.debug = true; - /** - * In this case, we're passing a parameter into a rule that uses that - * information to predict the alternatives. This is the special case - * where we know exactly which context we are in. The context stack - * is empty and we have not dipped into the outer context to make a decision. - */ - tm = file.addParserTest(input, "PredicateDependentOnArg", "T", "s", - "a b", - "alt 2\n" + "alt 1\n", - null); - tm.debug = true; - /** In this case, we have to ensure that the predicates are not - tested during the closure after recognizing the 1st ID. The - closure will fall off the end of 'a' 1st time and reach into the - a[1] rule invocation. It should not execute predicates because it - does not know what the parameter is. The context stack will not - be empty and so they should be ignored. It will not affect - recognition, however. We are really making sure the ATN - simulation doesn't crash with context object issues when it - encounters preds during FOLLOW. - */ - tm = file.addParserTest(input, "PredicateDependentOnArg2", "T", "s", - "a b", - "", - null); - tm.debug = true; - // uses ID ';' or ID '.' lookahead to solve s. preds not tested. - tm = file.addParserTest(input, "DependentPredNotInOuterCtxShouldBeIgnored", "T", "s", - "a;", - "alt 2\n", - null); - tm.debug = true; - tm = file.addParserTest(input, "IndependentPredNotPassedOuterCtxToAvoidCastException", "T", "s", - "a;", - "alt 2\n", - null); - tm.debug = true; - /** During a global follow operation, we still collect semantic - * predicates as long as they are not dependent on local context - */ - tm = file.addParserTest(input, "PredsInGlobalFOLLOW", "T", "s", - "a!", - "eval=true\n" + /* now we are parsing */ "parse\n", - null); - tm.debug = true; - /** We cannot collect predicates that are dependent on local context if - * we are doing a global follow. They appear as if they were not there at all. - */ - tm = file.addParserTest(input, "DepedentPredsInGlobalFOLLOW","T", "s", - "a!", - "eval=true\n" + "parse\n", - null); - tm.debug = true; - /** Regular non-forced actions can create side effects used by semantic - * predicates and so we cannot evaluate any semantic predicate - * encountered after having seen a regular action. This includes - * during global follow operations. - */ - tm = file.addParserTest(input, "ActionsHidePredsInGlobalFOLLOW", "T", "s", - "a!", - "eval=true\n" + "parse\n", - null); - tm.debug = true; - tm = file.addParserTestsWithErrors(input, "PredTestedEvenWhenUnAmbig", "T", "primary", - "abc", "ID abc\n", null, - "enum", "", "line 1:0 no viable alternative at input 'enum'\n"); - tm.debug = true; - /** - * This is a regression test for antlr/antlr4#218 "ANTLR4 EOF Related Bug". - * https://github.com/antlr/antlr4/issues/218 - */ - tm = file.addParserTest(input, "DisabledAlternative", "T", "cppCompilationUnit", - "hello", - "", - null); - tm.debug = true; - /** Loopback doesn't eval predicate at start of alt */ - tm = file.addParserTestsWithErrors(input, "PredFromAltTestedInLoopBack", "T", "file_", - "s\n\n\nx\n", - "(file_ (para (paraContent s) \\n \\n) (para (paraContent \\n x \\n)) )\n", - "line 5:0 mismatched input '' expecting '\n'\n", - "s\n\n\nx\n\n", - "(file_ (para (paraContent s) \\n \\n) (para (paraContent \\n x) \\n \\n) )\n", - null); - tm.debug = true; - return file; - } - private JUnitTestFile buildSemPredEvalLexer() throws Exception { JUnitTestFile file = new JUnitTestFile("SemPredEvalLexer"); LexerTestMethod tm = file.addLexerTest(input, "DisableRule", "L", diff --git a/tool/test/org/antlr/v4/test/rt/gen/grammars/SemPredEvalParser/2UnpredicatedAlts.st b/tool/test/org/antlr/v4/test/rt/gen/grammars/SemPredEvalParser/2UnpredicatedAlts.st deleted file mode 100644 index 0982d1d67..000000000 --- a/tool/test/org/antlr/v4/test/rt/gen/grammars/SemPredEvalParser/2UnpredicatedAlts.st +++ /dev/null @@ -1,9 +0,0 @@ -grammar ; -s : {} a ';' a; // do 2x: once in ATN, next in DFA -a : ID {} - | ID {} - | {}? ID {} - ; -ID : 'a'..'z'+ ; -INT : '0'..'9'+; -WS : (' '|'\n') -> skip ; diff --git a/tool/test/org/antlr/v4/test/rt/gen/grammars/SemPredEvalParser/2UnpredicatedAltsAndOneOrthogonalAlt.st b/tool/test/org/antlr/v4/test/rt/gen/grammars/SemPredEvalParser/2UnpredicatedAltsAndOneOrthogonalAlt.st deleted file mode 100644 index 4ab1601dc..000000000 --- a/tool/test/org/antlr/v4/test/rt/gen/grammars/SemPredEvalParser/2UnpredicatedAltsAndOneOrthogonalAlt.st +++ /dev/null @@ -1,10 +0,0 @@ -grammar ; -s : {} a ';' a ';' a; -a : INT {} - | ID {} // must pick this one for ID since pred is false - | ID {} - | {}? ID {} - ; -ID : 'a'..'z'+ ; -INT : '0'..'9'+; -WS : (' '|'\n') -> skip ; diff --git a/tool/test/org/antlr/v4/test/rt/gen/grammars/SemPredEvalParser/ActionsHidePredsInGlobalFOLLOW.st b/tool/test/org/antlr/v4/test/rt/gen/grammars/SemPredEvalParser/ActionsHidePredsInGlobalFOLLOW.st deleted file mode 100644 index 8316cf133..000000000 --- a/tool/test/org/antlr/v4/test/rt/gen/grammars/SemPredEvalParser/ActionsHidePredsInGlobalFOLLOW.st +++ /dev/null @@ -1,10 +0,0 @@ -grammar ; -@members { - -} -s : e {} {}? {} '!' ; -t : e {} {}? ID ; -e : ID | ; // non-LL(1) so we use ATN -ID : 'a'..'z'+ ; -INT : '0'..'9'+; -WS : (' '|'\n') -> skip ; diff --git a/tool/test/org/antlr/v4/test/rt/gen/grammars/SemPredEvalParser/AtomWithClosureInTranslatedLRRule.st b/tool/test/org/antlr/v4/test/rt/gen/grammars/SemPredEvalParser/AtomWithClosureInTranslatedLRRule.st deleted file mode 100644 index a782d480b..000000000 --- a/tool/test/org/antlr/v4/test/rt/gen/grammars/SemPredEvalParser/AtomWithClosureInTranslatedLRRule.st +++ /dev/null @@ -1,6 +0,0 @@ -grammar ; -start : e[0] EOF; -e[int _p] - : ( 'a' | 'b'+ ) ( {3 >= $_p}? '+' e[4] )* - ; - diff --git a/tool/test/org/antlr/v4/test/rt/gen/grammars/SemPredEvalParser/DepedentPredsInGlobalFOLLOW.st b/tool/test/org/antlr/v4/test/rt/gen/grammars/SemPredEvalParser/DepedentPredsInGlobalFOLLOW.st deleted file mode 100644 index 06b5a3735..000000000 --- a/tool/test/org/antlr/v4/test/rt/gen/grammars/SemPredEvalParser/DepedentPredsInGlobalFOLLOW.st +++ /dev/null @@ -1,11 +0,0 @@ -grammar ; -@members { - -} -s : a[99] ; -a[int i] : e {}? {} '!' ; -b[int i] : e {}? ID ; -e : ID | ; // non-LL(1) so we use ATN -ID : 'a'..'z'+ ; -INT : '0'..'9'+; -WS : (' '|'\n') -> skip ; diff --git a/tool/test/org/antlr/v4/test/rt/gen/grammars/SemPredEvalParser/DisabledAlternative.st b/tool/test/org/antlr/v4/test/rt/gen/grammars/SemPredEvalParser/DisabledAlternative.st deleted file mode 100644 index 2cfbbab6c..000000000 --- a/tool/test/org/antlr/v4/test/rt/gen/grammars/SemPredEvalParser/DisabledAlternative.st +++ /dev/null @@ -1,5 +0,0 @@ -grammar ; -cppCompilationUnit : content+ EOF; -content: anything | {}? .; -anything: ANY_CHAR; -ANY_CHAR: [_a-zA-Z0-9]; diff --git a/tool/test/org/antlr/v4/test/rt/gen/grammars/SemPredEvalParser/IndependentPredNotPassedOuterCtxToAvoidCastException.st b/tool/test/org/antlr/v4/test/rt/gen/grammars/SemPredEvalParser/IndependentPredNotPassedOuterCtxToAvoidCastException.st deleted file mode 100644 index 4143ec3b3..000000000 --- a/tool/test/org/antlr/v4/test/rt/gen/grammars/SemPredEvalParser/IndependentPredNotPassedOuterCtxToAvoidCastException.st +++ /dev/null @@ -1,10 +0,0 @@ -grammar ; -s : b ';' | b '.' ; -b : a ; -a - : {}? ID {} - | {}? ID {} - ; -ID : 'a'..'z'+ ; -INT : '0'..'9'+; -WS : (' '|'\n') -> skip ; diff --git a/tool/test/org/antlr/v4/test/rt/gen/grammars/SemPredEvalParser/NoTruePredsThrowsNoViableAlt.st b/tool/test/org/antlr/v4/test/rt/gen/grammars/SemPredEvalParser/NoTruePredsThrowsNoViableAlt.st deleted file mode 100644 index dd7b05895..000000000 --- a/tool/test/org/antlr/v4/test/rt/gen/grammars/SemPredEvalParser/NoTruePredsThrowsNoViableAlt.st +++ /dev/null @@ -1,8 +0,0 @@ -grammar ; -s : a a; -a : {}? ID INT {} - | {}? ID INT {} - ; -ID : 'a'..'z'+ ; -INT : '0'..'9'+; -WS : (' '|'\n') -> skip ; diff --git a/tool/test/org/antlr/v4/test/rt/gen/grammars/SemPredEvalParser/Order.st b/tool/test/org/antlr/v4/test/rt/gen/grammars/SemPredEvalParser/Order.st deleted file mode 100644 index 1d83ac807..000000000 --- a/tool/test/org/antlr/v4/test/rt/gen/grammars/SemPredEvalParser/Order.st +++ /dev/null @@ -1,10 +0,0 @@ -grammar ; -s : a {} a; // do 2x: once in ATN, next in DFA; -// action blocks lookahead from falling off of 'a' -// and looking into 2nd 'a' ref. !ctx dependent pred -a : ID {} - | {}? ID {} - ; -ID : 'a'..'z'+ ; -INT : '0'..'9'+; -WS : (' '|'\n') -> skip ; diff --git a/tool/test/org/antlr/v4/test/rt/gen/grammars/SemPredEvalParser/PredicateDependentOnArg.st b/tool/test/org/antlr/v4/test/rt/gen/grammars/SemPredEvalParser/PredicateDependentOnArg.st deleted file mode 100644 index 8a91793ab..000000000 --- a/tool/test/org/antlr/v4/test/rt/gen/grammars/SemPredEvalParser/PredicateDependentOnArg.st +++ /dev/null @@ -1,10 +0,0 @@ -grammar ; -@members {} -s : a[2] a[1]; -a[int i] - : {}? ID {} - | {}? ID {} - ; -ID : 'a'..'z'+ ; -INT : '0'..'9'+; -WS : (' '|'\n') -> skip ; diff --git a/tool/test/org/antlr/v4/test/rt/gen/grammars/SemPredEvalParser/PredicateDependentOnArg2.st b/tool/test/org/antlr/v4/test/rt/gen/grammars/SemPredEvalParser/PredicateDependentOnArg2.st deleted file mode 100644 index df1142e63..000000000 --- a/tool/test/org/antlr/v4/test/rt/gen/grammars/SemPredEvalParser/PredicateDependentOnArg2.st +++ /dev/null @@ -1,10 +0,0 @@ -grammar ; -@members {} -s : a[2] a[1]; -a[int i] - : {}? ID - | {}? ID - ; -ID : 'a'..'z'+ ; -INT : '0'..'9'+; -WS : (' '|'\n') -> skip ; diff --git a/tool/test/org/antlr/v4/test/rt/gen/grammars/SemPredEvalParser/PredsInGlobalFOLLOW.st b/tool/test/org/antlr/v4/test/rt/gen/grammars/SemPredEvalParser/PredsInGlobalFOLLOW.st deleted file mode 100644 index 27c61d473..000000000 --- a/tool/test/org/antlr/v4/test/rt/gen/grammars/SemPredEvalParser/PredsInGlobalFOLLOW.st +++ /dev/null @@ -1,10 +0,0 @@ -grammar ; -@members { - -} -s : e {}? {} '!' ; -t : e {}? ID ; -e : ID | ; // non-LL(1) so we use ATN -ID : 'a'..'z'+ ; -INT : '0'..'9'+; -WS : (' '|'\n') -> skip ; diff --git a/tool/test/org/antlr/v4/test/rt/gen/grammars/SemPredEvalParser/RewindBeforePredEval.st b/tool/test/org/antlr/v4/test/rt/gen/grammars/SemPredEvalParser/RewindBeforePredEval.st deleted file mode 100644 index a13cd7220..000000000 --- a/tool/test/org/antlr/v4/test/rt/gen/grammars/SemPredEvalParser/RewindBeforePredEval.st +++ /dev/null @@ -1,8 +0,0 @@ -grammar ; -s : a a; -a : {}? ID INT {} - | {}? ID INT {} - ; -ID : 'a'..'z'+ ; -INT : '0'..'9'+; -WS : (' '|'\n') -> skip ; diff --git a/tool/test/org/antlr/v4/test/rt/gen/grammars/SemPredEvalParser/SimpleValidate.st b/tool/test/org/antlr/v4/test/rt/gen/grammars/SemPredEvalParser/SimpleValidate.st deleted file mode 100644 index 5169d43b6..000000000 --- a/tool/test/org/antlr/v4/test/rt/gen/grammars/SemPredEvalParser/SimpleValidate.st +++ /dev/null @@ -1,8 +0,0 @@ -grammar ; -s : a ; -a : {}? ID {} - | {}? INT {} - ; -ID : 'a'..'z'+ ; -INT : '0'..'9'+; -WS : (' '|'\n') -> skip ; diff --git a/tool/test/org/antlr/v4/test/rt/gen/grammars/SemPredEvalParser/SimpleValidate2.st b/tool/test/org/antlr/v4/test/rt/gen/grammars/SemPredEvalParser/SimpleValidate2.st deleted file mode 100644 index 606c0083e..000000000 --- a/tool/test/org/antlr/v4/test/rt/gen/grammars/SemPredEvalParser/SimpleValidate2.st +++ /dev/null @@ -1,8 +0,0 @@ -grammar ; -s : a a a; -a : {}? ID {} - | {}? INT {} - ; -ID : 'a'..'z'+ ; -INT : '0'..'9'+; -WS : (' '|'\n') -> skip ; diff --git a/tool/test/org/antlr/v4/test/rt/gen/grammars/SemPredEvalParser/ToLeft.st b/tool/test/org/antlr/v4/test/rt/gen/grammars/SemPredEvalParser/ToLeft.st deleted file mode 100644 index a69e500a8..000000000 --- a/tool/test/org/antlr/v4/test/rt/gen/grammars/SemPredEvalParser/ToLeft.st +++ /dev/null @@ -1,8 +0,0 @@ -grammar ; - s : a+ ; -a : {}? ID {} - | {}? ID {} - ; -ID : 'a'..'z'+ ; -INT : '0'..'9'+; -WS : (' '|'\n') -> skip ; diff --git a/tool/test/org/antlr/v4/test/rt/gen/grammars/SemPredEvalParser/ToLeftWithVaryingPredicate.st b/tool/test/org/antlr/v4/test/rt/gen/grammars/SemPredEvalParser/ToLeftWithVaryingPredicate.st deleted file mode 100644 index f67afc57e..000000000 --- a/tool/test/org/antlr/v4/test/rt/gen/grammars/SemPredEvalParser/ToLeftWithVaryingPredicate.st +++ /dev/null @@ -1,10 +0,0 @@ -grammar ; -@members {} -s : ({ -} a)+ ; -a : {}? ID {} - | {}? ID {} - ; -ID : 'a'..'z'+ ; -INT : '0'..'9'+; -WS : (' '|'\n') -> skip ; diff --git a/tool/test/org/antlr/v4/test/rt/gen/grammars/SemPredEvalParser/UnpredicatedPathsInAlt.st b/tool/test/org/antlr/v4/test/rt/gen/grammars/SemPredEvalParser/UnpredicatedPathsInAlt.st deleted file mode 100644 index 207df7b63..000000000 --- a/tool/test/org/antlr/v4/test/rt/gen/grammars/SemPredEvalParser/UnpredicatedPathsInAlt.st +++ /dev/null @@ -1,12 +0,0 @@ -grammar ; -s : a {} - | b {} - ; -a : {}? ID INT - | ID INT - ; -b : ID ID - ; -ID : 'a'..'z'+ ; -INT : '0'..'9'+; -WS : (' '|'\n') -> skip ; diff --git a/tool/test/org/antlr/v4/test/rt/java/TestSemPredEvalParser.java b/tool/test/org/antlr/v4/test/rt/java/TestSemPredEvalParser.java deleted file mode 100644 index cce325d08..000000000 --- a/tool/test/org/antlr/v4/test/rt/java/TestSemPredEvalParser.java +++ /dev/null @@ -1,454 +0,0 @@ -package org.antlr.v4.test.rt.java; - -import org.junit.Test; -import static org.junit.Assert.*; - -public class TestSemPredEvalParser extends BaseTest { - - /* this file and method are generated, any edit will be overwritten by the next generation */ - @Test - public void testSimpleValidate() throws Exception { - String grammar = "grammar T;\n" + - "s : a ;\n" + - "a : {false}? ID {System.out.println(\"alt 1\");}\n" + - " | {true}? INT {System.out.println(\"alt 2\");}\n" + - " ;\n" + - "ID : 'a'..'z'+ ;\n" + - "INT : '0'..'9'+;\n" + - "WS : (' '|'\\n') -> skip ;"; - String found = execParser("T.g4", grammar, "TParser", "TLexer", "s", "x", true); - assertEquals("", found); - assertEquals("line 1:0 no viable alternative at input 'x'\n", this.stderrDuringParse); - } - - /* this file and method are generated, any edit will be overwritten by the next generation */ - @Test - public void testSimpleValidate2() throws Exception { - String grammar = "grammar T;\n" + - "s : a a a;\n" + - "a : {false}? ID {System.out.println(\"alt 1\");}\n" + - " | {true}? INT {System.out.println(\"alt 2\");}\n" + - " ;\n" + - "ID : 'a'..'z'+ ;\n" + - "INT : '0'..'9'+;\n" + - "WS : (' '|'\\n') -> skip ;"; - String found = execParser("T.g4", grammar, "TParser", "TLexer", "s", "3 4 x", true); - assertEquals("alt 2\nalt 2\n", found); - assertEquals("line 1:4 no viable alternative at input 'x'\n", this.stderrDuringParse); - } - - /* this file and method are generated, any edit will be overwritten by the next generation */ - @Test - public void testAtomWithClosureInTranslatedLRRule() throws Exception { - String grammar = "grammar T;\n" + - "start : e[0] EOF;\n" + - "e[int _p]\n" + - " : ( 'a' | 'b'+ ) ( {3 >= $_p}? '+' e[4] )*\n" + - " ;"; - String found = execParser("T.g4", grammar, "TParser", "TLexer", "start", "a+b+a", false); - assertEquals("", found); - assertNull(this.stderrDuringParse); - } - - /* this file and method are generated, any edit will be overwritten by the next generation */ - @Test - public void testValidateInDFA() throws Exception { - String grammar = "grammar T;\n" + - "s : a ';' a;\n" + - "// ';' helps us to resynchronize without consuming\n" + - "// 2nd 'a' reference. We our testing that the DFA also\n" + - "// throws an exception if the validating predicate fails\n" + - "a : {false}? ID {System.out.println(\"alt 1\");}\n" + - " | {true}? INT {System.out.println(\"alt 2\");}\n" + - " ;\n" + - "ID : 'a'..'z'+ ;\n" + - "INT : '0'..'9'+;\n" + - "WS : (' '|'\\n') -> skip ;"; - String found = execParser("T.g4", grammar, "TParser", "TLexer", "s", "x ; y", true); - assertEquals("", found); - assertEquals("line 1:0 no viable alternative at input 'x'\nline 1:4 no viable alternative at input 'y'\n", this.stderrDuringParse); - } - - /* this file and method are generated, any edit will be overwritten by the next generation */ - @Test - public void testSimple() throws Exception { - String grammar = "grammar T;\n" + - "s : a a a; // do 3x: once in ATN, next in DFA then INT in ATN\n" + - "a : {false}? ID {System.out.println(\"alt 1\");}\n" + - " | {true}? ID {System.out.println(\"alt 2\");}\n" + - " | INT {System.out.println(\"alt 3\");}\n" + - " ;\n" + - "ID : 'a'..'z'+ ;\n" + - "INT : '0'..'9'+;\n" + - "WS : (' '|'\\n') -> skip ;"; - String found = execParser("T.g4", grammar, "TParser", "TLexer", "s", "x y 3", true); - assertEquals("alt 2\nalt 2\nalt 3\n", found); - assertNull(this.stderrDuringParse); - } - - /* this file and method are generated, any edit will be overwritten by the next generation */ - @Test - public void testOrder() throws Exception { - String grammar = "grammar T;\n" + - "s : a {} a; // do 2x: once in ATN, next in DFA;\n" + - "// action blocks lookahead from falling off of 'a'\n" + - "// and looking into 2nd 'a' ref. !ctx dependent pred\n" + - "a : ID {System.out.println(\"alt 1\");}\n" + - " | {true}? ID {System.out.println(\"alt 2\");}\n" + - " ;\n" + - "ID : 'a'..'z'+ ;\n" + - "INT : '0'..'9'+;\n" + - "WS : (' '|'\\n') -> skip ;"; - String found = execParser("T.g4", grammar, "TParser", "TLexer", "s", "x y", false); - assertEquals("alt 1\nalt 1\n", found); - assertNull(this.stderrDuringParse); - } - - /* this file and method are generated, any edit will be overwritten by the next generation */ - @Test - public void test2UnpredicatedAlts() throws Exception { - String grammar = "grammar T;\n" + - "s : {_interp.setPredictionMode(PredictionMode.LL_EXACT_AMBIG_DETECTION);} a ';' a; // do 2x: once in ATN, next in DFA\n" + - "a : ID {System.out.println(\"alt 1\");}\n" + - " | ID {System.out.println(\"alt 2\");}\n" + - " | {false}? ID {System.out.println(\"alt 3\");}\n" + - " ;\n" + - "ID : 'a'..'z'+ ;\n" + - "INT : '0'..'9'+;\n" + - "WS : (' '|'\\n') -> skip ;"; - String found = execParser("T.g4", grammar, "TParser", "TLexer", "s", "x; y", true); - assertEquals("alt 1\nalt 1\n", found); - assertEquals("line 1:0 reportAttemptingFullContext d=0 (a), input='x'\nline 1:0 reportAmbiguity d=0 (a): ambigAlts={1, 2}, input='x'\nline 1:3 reportAttemptingFullContext d=0 (a), input='y'\nline 1:3 reportAmbiguity d=0 (a): ambigAlts={1, 2}, input='y'\n", this.stderrDuringParse); - } - - /* this file and method are generated, any edit will be overwritten by the next generation */ - @Test - public void test2UnpredicatedAltsAndOneOrthogonalAlt() throws Exception { - String grammar = "grammar T;\n" + - "s : {_interp.setPredictionMode(PredictionMode.LL_EXACT_AMBIG_DETECTION);} a ';' a ';' a;\n" + - "a : INT {System.out.println(\"alt 1\");}\n" + - " | ID {System.out.println(\"alt 2\");} // must pick this one for ID since pred is false\n" + - " | ID {System.out.println(\"alt 3\");}\n" + - " | {false}? ID {System.out.println(\"alt 4\");}\n" + - " ;\n" + - "ID : 'a'..'z'+ ;\n" + - "INT : '0'..'9'+;\n" + - "WS : (' '|'\\n') -> skip ;"; - String found = execParser("T.g4", grammar, "TParser", "TLexer", "s", "34; x; y", true); - assertEquals("alt 1\nalt 2\nalt 2\n", found); - assertEquals("line 1:4 reportAttemptingFullContext d=0 (a), input='x'\nline 1:4 reportAmbiguity d=0 (a): ambigAlts={2, 3}, input='x'\nline 1:7 reportAttemptingFullContext d=0 (a), input='y'\nline 1:7 reportAmbiguity d=0 (a): ambigAlts={2, 3}, input='y'\n", this.stderrDuringParse); - } - - /* this file and method are generated, any edit will be overwritten by the next generation */ - @Test - public void testRewindBeforePredEval() throws Exception { - String grammar = "grammar T;\n" + - "s : a a;\n" + - "a : {this._input.LT(1).getText().equals(\"x\")}? ID INT {System.out.println(\"alt 1\");}\n" + - " | {this._input.LT(1).getText().equals(\"y\")}? ID INT {System.out.println(\"alt 2\");}\n" + - " ;\n" + - "ID : 'a'..'z'+ ;\n" + - "INT : '0'..'9'+;\n" + - "WS : (' '|'\\n') -> skip ;"; - String found = execParser("T.g4", grammar, "TParser", "TLexer", "s", "y 3 x 4", true); - assertEquals("alt 2\nalt 1\n", found); - assertNull(this.stderrDuringParse); - } - - /* this file and method are generated, any edit will be overwritten by the next generation */ - @Test - public void testNoTruePredsThrowsNoViableAlt() throws Exception { - String grammar = "grammar T;\n" + - "s : a a;\n" + - "a : {false}? ID INT {System.out.println(\"alt 1\");}\n" + - " | {false}? ID INT {System.out.println(\"alt 2\");}\n" + - " ;\n" + - "ID : 'a'..'z'+ ;\n" + - "INT : '0'..'9'+;\n" + - "WS : (' '|'\\n') -> skip ;"; - String found = execParser("T.g4", grammar, "TParser", "TLexer", "s", "y 3 x 4", false); - assertEquals("", found); - assertEquals("line 1:0 no viable alternative at input 'y'\n", this.stderrDuringParse); - } - - /* this file and method are generated, any edit will be overwritten by the next generation */ - @Test - public void testToLeft() throws Exception { - String grammar = "grammar T;\n" + - " s : a+ ;\n" + - "a : {false}? ID {System.out.println(\"alt 1\");}\n" + - " | {true}? ID {System.out.println(\"alt 2\");}\n" + - " ;\n" + - "ID : 'a'..'z'+ ;\n" + - "INT : '0'..'9'+;\n" + - "WS : (' '|'\\n') -> skip ;"; - String found = execParser("T.g4", grammar, "TParser", "TLexer", "s", "x x y", true); - assertEquals("alt 2\nalt 2\nalt 2\n", found); - assertNull(this.stderrDuringParse); - } - - /* this file and method are generated, any edit will be overwritten by the next generation */ - @Test - public void testUnpredicatedPathsInAlt() throws Exception { - String grammar = "grammar T;\n" + - "s : a {System.out.println(\"alt 1\");}\n" + - " | b {System.out.println(\"alt 2\");}\n" + - " ;\n" + - "a : {false}? ID INT\n" + - " | ID INT\n" + - " ;\n" + - "b : ID ID\n" + - " ;\n" + - "ID : 'a'..'z'+ ;\n" + - "INT : '0'..'9'+;\n" + - "WS : (' '|'\\n') -> skip ;"; - String found = execParser("T.g4", grammar, "TParser", "TLexer", "s", "x 4", true); - assertEquals("alt 1\n", found); - assertNull(this.stderrDuringParse); - } - - /* this file and method are generated, any edit will be overwritten by the next generation */ - @Test - public void testActionHidesPreds() throws Exception { - String grammar = "grammar T;\n" + - "@members {int i = 0;}\n" + - "s : a+ ;\n" + - "a : {this.i = 1;} ID {this.i == 1}? {System.out.println(\"alt 1\");}\n" + - " | {this.i = 2;} ID {this.i == 2}? {System.out.println(\"alt 2\");}\n" + - " ;\n" + - "ID : 'a'..'z'+ ;\n" + - "INT : '0'..'9'+;\n" + - "WS : (' '|'\\n') -> skip ;"; - String found = execParser("T.g4", grammar, "TParser", "TLexer", "s", "x x y", false); - assertEquals("alt 1\nalt 1\nalt 1\n", found); - assertNull(this.stderrDuringParse); - } - - /* this file and method are generated, any edit will be overwritten by the next generation */ - @Test - public void testToLeftWithVaryingPredicate() throws Exception { - String grammar = "grammar T;\n" + - "@members {int i = 0;}\n" + - "s : ({this.i += 1;\n" + - "System.out.println(\"i=\" + this.i);} a)+ ;\n" + - "a : {this.i % 2 == 0}? ID {System.out.println(\"alt 1\");}\n" + - " | {this.i % 2 != 0}? ID {System.out.println(\"alt 2\");}\n" + - " ;\n" + - "ID : 'a'..'z'+ ;\n" + - "INT : '0'..'9'+;\n" + - "WS : (' '|'\\n') -> skip ;"; - String found = execParser("T.g4", grammar, "TParser", "TLexer", "s", "x x y", true); - assertEquals("i=1\nalt 2\ni=2\nalt 1\ni=3\nalt 2\n", found); - assertNull(this.stderrDuringParse); - } - - /* this file and method are generated, any edit will be overwritten by the next generation */ - @Test - public void testPredicateDependentOnArg() throws Exception { - String grammar = "grammar T;\n" + - "@members {int i = 0;}\n" + - "s : a[2] a[1];\n" + - "a[int i]\n" + - " : {$i==1}? ID {System.out.println(\"alt 1\");}\n" + - " | {$i==2}? ID {System.out.println(\"alt 2\");}\n" + - " ;\n" + - "ID : 'a'..'z'+ ;\n" + - "INT : '0'..'9'+;\n" + - "WS : (' '|'\\n') -> skip ;"; - String found = execParser("T.g4", grammar, "TParser", "TLexer", "s", "a b", true); - assertEquals("alt 2\nalt 1\n", found); - assertNull(this.stderrDuringParse); - } - - /* this file and method are generated, any edit will be overwritten by the next generation */ - @Test - public void testPredicateDependentOnArg2() throws Exception { - String grammar = "grammar T;\n" + - "@members {int i = 0;}\n" + - "s : a[2] a[1];\n" + - "a[int i]\n" + - " : {$i==1}? ID \n" + - " | {$i==2}? ID \n" + - " ;\n" + - "ID : 'a'..'z'+ ;\n" + - "INT : '0'..'9'+;\n" + - "WS : (' '|'\\n') -> skip ;"; - String found = execParser("T.g4", grammar, "TParser", "TLexer", "s", "a b", true); - assertEquals("", found); - assertNull(this.stderrDuringParse); - } - - /* this file and method are generated, any edit will be overwritten by the next generation */ - @Test - public void testDependentPredNotInOuterCtxShouldBeIgnored() throws Exception { - String grammar = "grammar T;\n" + - "s : b[2] ';' | b[2] '.' ; // decision in s drills down to ctx-dependent pred in a;\n" + - "b[int i] : a[i] ;\n" + - "a[int i]\n" + - " : {$i==1}? ID {System.out.println(\"alt 1\");}\n" + - " | {$i==2}? ID {System.out.println(\"alt 2\");}\n" + - " ;\n" + - "ID : 'a'..'z'+ ;\n" + - "INT : '0'..'9'+;\n" + - "WS : (' '|'\\n') -> skip ;"; - String found = execParser("T.g4", grammar, "TParser", "TLexer", "s", "a;", true); - assertEquals("alt 2\n", found); - assertNull(this.stderrDuringParse); - } - - /* this file and method are generated, any edit will be overwritten by the next generation */ - @Test - public void testIndependentPredNotPassedOuterCtxToAvoidCastException() throws Exception { - String grammar = "grammar T;\n" + - "s : b ';' | b '.' ;\n" + - "b : a ;\n" + - "a\n" + - " : {false}? ID {System.out.println(\"alt 1\");}\n" + - " | {true}? ID {System.out.println(\"alt 2\");}\n" + - " ;\n" + - "ID : 'a'..'z'+ ;\n" + - "INT : '0'..'9'+;\n" + - "WS : (' '|'\\n') -> skip ;"; - String found = execParser("T.g4", grammar, "TParser", "TLexer", "s", "a;", true); - assertEquals("alt 2\n", found); - assertNull(this.stderrDuringParse); - } - - /* this file and method are generated, any edit will be overwritten by the next generation */ - @Test - public void testPredsInGlobalFOLLOW() throws Exception { - String grammar = "grammar T;\n" + - "@members {\n" + - "boolean pred(boolean v) {\n" + - " System.out.println(\"eval=\"+v);\n" + - " return v;\n" + - "}\n" + - "}\n" + - "s : e {this.pred(true)}? {System.out.println(\"parse\");} '!' ;\n" + - "t : e {this.pred(false)}? ID ;\n" + - "e : ID | ; // non-LL(1) so we use ATN\n" + - "ID : 'a'..'z'+ ;\n" + - "INT : '0'..'9'+;\n" + - "WS : (' '|'\\n') -> skip ;"; - String found = execParser("T.g4", grammar, "TParser", "TLexer", "s", "a!", true); - assertEquals("eval=true\nparse\n", found); - assertNull(this.stderrDuringParse); - } - - /* this file and method are generated, any edit will be overwritten by the next generation */ - @Test - public void testDepedentPredsInGlobalFOLLOW() throws Exception { - String grammar = "grammar T;\n" + - "@members {\n" + - "boolean pred(boolean v) {\n" + - " System.out.println(\"eval=\"+v);\n" + - " return v;\n" + - "}\n" + - "}\n" + - "s : a[99] ;\n" + - "a[int i] : e {this.pred($i==99)}? {System.out.println(\"parse\");} '!' ;\n" + - "b[int i] : e {this.pred($i==99)}? ID ;\n" + - "e : ID | ; // non-LL(1) so we use ATN\n" + - "ID : 'a'..'z'+ ;\n" + - "INT : '0'..'9'+;\n" + - "WS : (' '|'\\n') -> skip ;"; - String found = execParser("T.g4", grammar, "TParser", "TLexer", "s", "a!", true); - assertEquals("eval=true\nparse\n", found); - assertNull(this.stderrDuringParse); - } - - /* this file and method are generated, any edit will be overwritten by the next generation */ - @Test - public void testActionsHidePredsInGlobalFOLLOW() throws Exception { - String grammar = "grammar T;\n" + - "@members {\n" + - "boolean pred(boolean v) {\n" + - " System.out.println(\"eval=\"+v);\n" + - " return v;\n" + - "}\n" + - "}\n" + - "s : e {} {this.pred(true)}? {System.out.println(\"parse\");} '!' ;\n" + - "t : e {} {this.pred(false)}? ID ;\n" + - "e : ID | ; // non-LL(1) so we use ATN\n" + - "ID : 'a'..'z'+ ;\n" + - "INT : '0'..'9'+;\n" + - "WS : (' '|'\\n') -> skip ;"; - String found = execParser("T.g4", grammar, "TParser", "TLexer", "s", "a!", true); - assertEquals("eval=true\nparse\n", found); - assertNull(this.stderrDuringParse); - } - - /* this file and method are generated, any edit will be overwritten by the next generation */ - String testPredTestedEvenWhenUnAmbig(String input) throws Exception { - String grammar = "grammar T;\n" + - "@members {boolean enumKeyword = true;}\n" + - "primary\n" + - " : ID {System.out.println(\"ID \"+$ID.text);}\n" + - " | {!this.enumKeyword}? 'enum' {System.out.println(\"enum\");}\n" + - " ;\n" + - "ID : [a-z]+ ;\n" + - "WS : [ \\t\\n\\r]+ -> skip ;"; - return execParser("T.g4", grammar, "TParser", "TLexer", "primary", input, true); - } - - /* this file and method are generated, any edit will be overwritten by the next generation */ - @Test - public void testPredTestedEvenWhenUnAmbig_1() throws Exception { - String found = testPredTestedEvenWhenUnAmbig("abc"); - assertEquals("ID abc\n", found); - assertNull(this.stderrDuringParse); - } - - /* this file and method are generated, any edit will be overwritten by the next generation */ - @Test - public void testPredTestedEvenWhenUnAmbig_2() throws Exception { - String found = testPredTestedEvenWhenUnAmbig("enum"); - assertEquals("", found); - assertEquals("line 1:0 no viable alternative at input 'enum'\n", this.stderrDuringParse); - } - - /* this file and method are generated, any edit will be overwritten by the next generation */ - @Test - public void testDisabledAlternative() throws Exception { - String grammar = "grammar T;\n" + - "cppCompilationUnit : content+ EOF;\n" + - "content: anything | {false}? .;\n" + - "anything: ANY_CHAR;\n" + - "ANY_CHAR: [_a-zA-Z0-9];"; - String found = execParser("T.g4", grammar, "TParser", "TLexer", "cppCompilationUnit", "hello", true); - assertEquals("", found); - assertNull(this.stderrDuringParse); - } - - /* this file and method are generated, any edit will be overwritten by the next generation */ - String testPredFromAltTestedInLoopBack(String input) throws Exception { - String grammar = "grammar T;\n" + - "file_\n" + - "@after {System.out.println($ctx.toStringTree(this));}\n" + - " : para para EOF ;\n" + - "para: paraContent NL NL ;\n" + - "paraContent : ('s'|'x'|{this._input.LA(2)!=NL}? NL)+ ;\n" + - "NL : '\\n' ;\n" + - "s : 's' ;\n" + - "X : 'x' ;"; - return execParser("T.g4", grammar, "TParser", "TLexer", "file_", input, true); - } - - /* this file and method are generated, any edit will be overwritten by the next generation */ - @Test - public void testPredFromAltTestedInLoopBack_1() throws Exception { - String found = testPredFromAltTestedInLoopBack("s\n\n\nx\n"); - assertEquals("(file_ (para (paraContent s) \\n \\n) (para (paraContent \\n x \\n)) )\n", found); - assertEquals("line 5:0 mismatched input '' expecting '\n'\n", this.stderrDuringParse); - } - - /* this file and method are generated, any edit will be overwritten by the next generation */ - @Test - public void testPredFromAltTestedInLoopBack_2() throws Exception { - String found = testPredFromAltTestedInLoopBack("s\n\n\nx\n\n"); - assertEquals("(file_ (para (paraContent s) \\n \\n) (para (paraContent \\n x) \\n \\n) )\n", found); - assertNull(this.stderrDuringParse); - } - - -} \ No newline at end of file diff --git a/tool/test/org/antlr/v4/test/runtime/java/Java.test.stg b/tool/test/org/antlr/v4/test/runtime/java/Java.test.stg index 64acf662c..78e4d03eb 100644 --- a/tool/test/org/antlr/v4/test/runtime/java/Java.test.stg +++ b/tool/test/org/antlr/v4/test/runtime/java/Java.test.stg @@ -98,7 +98,7 @@ public void test() throws Exception { String input =; - String found = execParser(".g4", grammar, "Parser", "Lexer", "", input, ); + String found = execParser(".g4", grammar, "Parser", "Lexer", "", input, ); assertEquals(, found); assertEquals(, this.stderrDuringParse);