fixed problem with empty paths in token analysis

empty paths are keep the state of the prefix including the empty flag
which causes empty paths in alternatives to be lost.

fix: create new token state with prefix (reset empty flag)
This commit is contained in:
overflowerror 2022-01-20 14:26:40 +01:00
parent aad95fa670
commit 2cd6e454d7
2 changed files with 25 additions and 3 deletions

View file

@ -1122,11 +1122,10 @@ public class HoistingProcessorTest extends AbstractXtextTests {
} }
@Test @Test
public void testRepeatableContextWithEmptyPath_bug_expectCorrectResult() throws Exception { public void testRepeatablyQuantifiedContextWithEmptyPath_bug_expectCorrectResult() throws Exception {
// @formatter:off // @formatter:off
String model = String model =
MODEL_PREAMBLE + MODEL_PREAMBLE +
"hoistingDebug\n" +
"S: A C+ ;\n" + "S: A C+ ;\n" +
"A: $$ p0 $$?=> 'a' " + "A: $$ p0 $$?=> 'a' " +
" | $$ p1 $$?=> 'a' 'b' ;\n" + " | $$ p1 $$?=> 'a' 'b' ;\n" +
@ -1142,4 +1141,26 @@ public class HoistingProcessorTest extends AbstractXtextTests {
assertTrue(guard.hasTerminal()); assertTrue(guard.hasTerminal());
assertEquals("(((" + getSyntaxForEofToken(2) + " && " + getSyntaxForKeywordToken("c", 2) + ") || (p0)) && (" + getSyntaxForKeywordToken("b", 2) + " || (p1)))", guard.render()); assertEquals("(((" + getSyntaxForEofToken(2) + " && " + getSyntaxForKeywordToken("c", 2) + ") || (p0)) && (" + getSyntaxForKeywordToken("b", 2) + " || (p1)))", guard.render());
} }
@Test
public void testRecursiveContextWithEmptyPath_bug_expectCorrectResult() throws Exception {
// @formatter:off
String model =
MODEL_PREAMBLE +
"S: a=A c=C ;\n" +
"A: $$ p0 $$?=> 'a' " +
" | $$ p1 $$?=> 'a' 'b' ;\n" +
"C: {C} \n" +
" | 'c' C ;\n";
// @formatter:off
XtextResource resource = getResourceFromString(model);
Grammar grammar = ((Grammar) resource.getContents().get(0));
hoistingProcessor.init(grammar);
AbstractRule rule = getRule(grammar, "A");
HoistingGuard guard = hoistingProcessor.findHoistingGuard(rule.getAlternatives());
assertFalse(guard.isTrivial());
assertTrue(guard.hasTerminal());
assertEquals("(((" + getSyntaxForEofToken(2) + " && " + getSyntaxForKeywordToken("c", 2) + ") || (p0)) && (" + getSyntaxForKeywordToken("b", 2) + " || (p1)))", guard.render());
}
} }

View file

@ -317,7 +317,8 @@ public class TokenAnalysis {
return result; return result;
} else { } else {
// Actions, Predicates, JavaActions, ... // Actions, Predicates, JavaActions, ...
return prefix; // create new state with out empty flag
return new TokenAnalysisPaths(prefix);
} }
}; };
}.doSwitch(path); }.doSwitch(path);