handling another kind of statement

This commit is contained in:
rusefillc 2022-01-01 23:36:25 -05:00
parent 7c3f7f5675
commit baa7c050b8
4 changed files with 46 additions and 22 deletions

View File

@ -52,6 +52,12 @@ public class CodeWalkthrough {
colorStatement(ctx, painter); colorStatement(ctx, painter);
} }
@Override
public void enterExpressionStatement(CPP14Parser.ExpressionStatementContext ctx) {
super.enterExpressionStatement(ctx);
colorStatement(ctx, painter);
}
@Override @Override
public void enterJumpStatement(CPP14Parser.JumpStatementContext ctx) { public void enterJumpStatement(CPP14Parser.JumpStatementContext ctx) {
super.enterJumpStatement(ctx); super.enterJumpStatement(ctx);
@ -126,7 +132,10 @@ public class CodeWalkthrough {
boolean isAlive = getOverallState(currentState); boolean isAlive = getOverallState(currentState);
color = isAlive ? ACTIVE_STATEMENT : INACTIVE_BRANCH; color = isAlive ? ACTIVE_STATEMENT : INACTIVE_BRANCH;
} }
painter.paintBackground(color, new Range(ctx)); Range range = new Range(ctx);
if (log.debugEnabled())
log.info(color + " for " + sourceCode.substring(range.getStart(), range.getStop()));
painter.paintBackground(color, range);
} }
}, tree); }, tree);

View File

@ -17,7 +17,7 @@ public class LiveDataParserSandbox {
values.put("engineTooSlow", Boolean.TRUE); values.put("engineTooSlow", Boolean.TRUE);
values.put("engineTooFast", Boolean.FALSE); values.put("engineTooFast", Boolean.FALSE);
VariableValueSource valueSource = name -> values.get(name); VariableValueSource valueSource = values::get;
new FrameHelper(JDialog.EXIT_ON_CLOSE).showFrame(new LiveDataParserPanel(new UIContext(), valueSource, "ac_control.cpp").getContent()); new FrameHelper(JDialog.EXIT_ON_CLOSE).showFrame(new LiveDataParserPanel(new UIContext(), valueSource, "ac_control.cpp").getContent());
} }

View File

@ -21,6 +21,24 @@ import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.*; import static org.mockito.Mockito.*;
public class LiveDataParserTest { public class LiveDataParserTest {
@Test
public void testGreenCode() {
String sourceCode = "void AcController::onSlowCallback() {\n" +
"\tbool isEnabled = getAcState();\n" +
"\n" +
"\tm_acEnabled = isEnabled;\n" +
"\n" +
"\tenginePins.acRelay.setValue(isEnabled);\n" +
"\n" +
"#if EFI_TUNER_STUDIO\n" +
"\tengine->outputChannels.acState = isEnabled;\n" +
"#endif // EFI_TUNER_STUDIO\n" +
"}\n";
SourceCodePainter painter = run(name -> null, sourceCode);
verify(painter, times(4)).paintBackground(eq(CodeWalkthrough.ACTIVE_STATEMENT), any());
}
@Test @Test
public void testParsing() { public void testParsing() {
Map<String, Object> values = new TreeMap<>(String.CASE_INSENSITIVE_ORDER); Map<String, Object> values = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
@ -48,16 +66,8 @@ public class LiveDataParserTest {
"return ff;\n" + "return ff;\n" +
"}\n"; "}\n";
SourceCodePainter painter = mock(SourceCodePainter.class); SourceCodePainter painter = run(valueSource, sourceCode);
ParseTree tree = LiveDataParserPanel.getParseTree(sourceCode);
System.out.println("******************************************* Just print everything for educational purposes");
new ParseTreeWalker().walk(new PrintCPP14ParserListener(), tree);
System.out.println("******************************************* Now running FOR REAL");
CodeWalkthrough.applyVariables(valueSource, sourceCode, painter, tree);
verify(painter, times(2)).paintForeground(eq(Color.blue), any()); verify(painter, times(2)).paintForeground(eq(Color.blue), any());
verify(painter).paintBackground(eq(Color.red), any()); verify(painter).paintBackground(eq(Color.red), any());
@ -69,6 +79,17 @@ public class LiveDataParserTest {
verify(painter, times(3)).paintBackground(eq(CodeWalkthrough.PASSIVE_CODE), any()); verify(painter, times(3)).paintBackground(eq(CodeWalkthrough.PASSIVE_CODE), any());
} }
private SourceCodePainter run(VariableValueSource valueSource, String sourceCode) {
SourceCodePainter painter = mock(SourceCodePainter.class);
ParseTree tree = LiveDataParserPanel.getParseTree(sourceCode);
System.out.println("******************************************* Just print everything for educational purposes");
new ParseTreeWalker().walk(new PrintCPP14ParserListener(), tree);
System.out.println("******************************************* Now running FOR REAL");
CodeWalkthrough.applyVariables(valueSource, sourceCode, painter, tree);
return painter;
}
@Test @Test
public void testConfigurationInRealSourceCode() throws IOException, URISyntaxException { public void testConfigurationInRealSourceCode() throws IOException, URISyntaxException {
String sourceCode = LiveDataParserPanel.getContent(LiveDataParserPanel.class, LiveDataView.BOOST_CONTROL.getFileName()); String sourceCode = LiveDataParserPanel.getContent(LiveDataParserPanel.class, LiveDataView.BOOST_CONTROL.getFileName());

View File

@ -601,7 +601,7 @@ public class PrintCPP14ParserListener implements CPP14ParserListener {
@Override @Override
public void enterLabeledStatement(CPP14Parser.LabeledStatementContext ctx) { public void enterLabeledStatement(CPP14Parser.LabeledStatementContext ctx) {
printWithCounter("enterLabeledStatement"); printWithCounter("enterLabeledStatement " + ctx.getText());
} }
@ -613,8 +613,7 @@ public class PrintCPP14ParserListener implements CPP14ParserListener {
@Override @Override
public void enterExpressionStatement(CPP14Parser.ExpressionStatementContext ctx) { public void enterExpressionStatement(CPP14Parser.ExpressionStatementContext ctx) {
printWithCounter("enterExpressionStatement"); printWithCounter("enterExpressionStatement " + ctx.getText());
} }
@Override @Override
@ -650,7 +649,6 @@ public class PrintCPP14ParserListener implements CPP14ParserListener {
@Override @Override
public void enterSelectionStatement(CPP14Parser.SelectionStatementContext ctx) { public void enterSelectionStatement(CPP14Parser.SelectionStatementContext ctx) {
printWithCounter("CONDITIONAL enterSelectionStatement " + ctx.getText() + " statements" + ctx.statement()); printWithCounter("CONDITIONAL enterSelectionStatement " + ctx.getText() + " statements" + ctx.statement());
} }
@Override @Override
@ -672,26 +670,22 @@ public class PrintCPP14ParserListener implements CPP14ParserListener {
@Override @Override
public void enterIterationStatement(CPP14Parser.IterationStatementContext ctx) { public void enterIterationStatement(CPP14Parser.IterationStatementContext ctx) {
printWithCounter("enterIterationStatement"); printWithCounter("enterIterationStatement " + ctx.getText());
} }
@Override @Override
public void exitIterationStatement(CPP14Parser.IterationStatementContext ctx) { public void exitIterationStatement(CPP14Parser.IterationStatementContext ctx) {
printWithCounter("exitIterationStatement"); printWithCounter("exitIterationStatement");
} }
@Override @Override
public void enterForInitStatement(CPP14Parser.ForInitStatementContext ctx) { public void enterForInitStatement(CPP14Parser.ForInitStatementContext ctx) {
printWithCounter("enterForInitStatement"); printWithCounter("enterForInitStatement");
} }
@Override @Override
public void exitForInitStatement(CPP14Parser.ForInitStatementContext ctx) { public void exitForInitStatement(CPP14Parser.ForInitStatementContext ctx) {
printWithCounter("exitForInitStatement"); printWithCounter("exitForInitStatement");
} }
@Override @Override
@ -732,7 +726,7 @@ public class PrintCPP14ParserListener implements CPP14ParserListener {
@Override @Override
public void enterDeclarationStatement(CPP14Parser.DeclarationStatementContext ctx) { public void enterDeclarationStatement(CPP14Parser.DeclarationStatementContext ctx) {
printWithCounter("enterDeclarationStatement"); printWithCounter("enterDeclarationStatement " + ctx.getText());
} }
@ -756,7 +750,7 @@ public class PrintCPP14ParserListener implements CPP14ParserListener {
@Override @Override
public void enterDeclaration(CPP14Parser.DeclarationContext ctx) { public void enterDeclaration(CPP14Parser.DeclarationContext ctx) {
printWithCounter("enterDeclaration"); printWithCounter("enterDeclaration " + ctx);
} }