Skip to content

Commit

Permalink
Fixed preproc of noSmooth as part of #149.
Browse files Browse the repository at this point in the history
  • Loading branch information
sampottinger committed Nov 21, 2020
1 parent 3e7aab9 commit c41e4d2
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 84 deletions.
141 changes: 76 additions & 65 deletions java/src/processing/mode/java/preproc/PdeParseTreeListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@
public class PdeParseTreeListener extends ProcessingBaseListener {

private static final String SIZE_METHOD_NAME = "size";
private static final String SMOOTH_METHOD_NAME = "smooth";
private static final String NO_SMOOTH_METHOD_NAME = "noSmooth";
private static final String PIXEL_DENSITY_METHOD_NAME = "pixelDensity";
private static final String FULLSCREEN_METHOD_NAME = "fullScreen";

Expand Down Expand Up @@ -79,6 +81,7 @@ public class PdeParseTreeListener extends ProcessingBaseListener {
private boolean sizeRequiresRewrite = false;
private boolean pixelDensityRequiresRewrite = false;
private boolean sizeIsFullscreen = false;
private boolean noSmoothRequiresRewrite = false;
private RewriteResult headerResult;
private RewriteResult footerResult;

Expand Down Expand Up @@ -310,10 +313,10 @@ public void exitMethodCall(ProcessingParser.MethodCallContext ctx) {

if (SIZE_METHOD_NAME.equals(methodName) || FULLSCREEN_METHOD_NAME.equals(methodName)) {
handleSizeCall(ctx);
}

if (PIXEL_DENSITY_METHOD_NAME.equals(methodName)) {
} else if (PIXEL_DENSITY_METHOD_NAME.equals(methodName)) {
handlePixelDensityCall(ctx);
} else if (NO_SMOOTH_METHOD_NAME.equals(methodName)) {
handleNoSmoothCall(ctx);
}
}

Expand Down Expand Up @@ -585,22 +588,9 @@ public void exitHexColorLiteral(ProcessingParser.HexColorLiteralContext ctx) {
* @param ctx The context of the call.
*/
protected void handleSizeCall(ParserRuleContext ctx) {
ParserRuleContext testCtx = ctx.getParent()
.getParent()
.getParent()
.getParent();

boolean isInGlobal =
testCtx instanceof ProcessingParser.StaticProcessingSketchContext;

boolean isInSetup;
if (!isInGlobal) {
ParserRuleContext methodDeclaration = testCtx.getParent()
.getParent();

isInSetup = isMethodSetup(methodDeclaration);
} else {
isInSetup = false;
// Check that this is the size call for processing and not a user defined size method.
if (!calledFromGlobalOrSetup(ctx)) {
return;
}

ParseTree argsContext = ctx.getChild(2);
Expand All @@ -610,41 +600,41 @@ protected void handleSizeCall(ParserRuleContext ctx) {
boolean isSize = ctx.getChild(0).getText().equals(SIZE_METHOD_NAME);
boolean isFullscreen = ctx.getChild(0).getText().equals(FULLSCREEN_METHOD_NAME);

if (isInGlobal || isInSetup) {
if (isSize && argsContext.getChildCount() > 2) {
thisRequiresRewrite = true;

if (isSize && argsContext.getChildCount() > 2) {
sketchWidth = argsContext.getChild(0).getText();
if (PApplet.parseInt(sketchWidth, -1) == -1 &&
!sketchWidth.equals("displayWidth")) {
thisRequiresRewrite = false;
}
sketchWidth = argsContext.getChild(0).getText();
boolean invalidWidth = PApplet.parseInt(sketchWidth, -1) == -1;
invalidWidth = invalidWidth && !sketchWidth.equals("displayWidth");
if (invalidWidth) {
thisRequiresRewrite = false;
}

sketchHeight = argsContext.getChild(2).getText();
if (PApplet.parseInt(sketchHeight, -1) == -1 &&
!sketchHeight.equals("displayHeight")) {
thisRequiresRewrite = false;
}
sketchHeight = argsContext.getChild(2).getText();
boolean invalidHeight = PApplet.parseInt(sketchHeight, -1) == -1;
invalidHeight = invalidHeight && !sketchHeight.equals("displayHeight");
if (invalidHeight) {
thisRequiresRewrite = false;
}

if (argsContext.getChildCount() > 3) {
sketchRenderer = argsContext.getChild(4).getText();
}
if (argsContext.getChildCount() > 3) {
sketchRenderer = argsContext.getChild(4).getText();
}

if (argsContext.getChildCount() > 5) {
sketchOutputFilename = argsContext.getChild(6).getText();
}
if (argsContext.getChildCount() > 5) {
sketchOutputFilename = argsContext.getChild(6).getText();
}
}

if (isFullscreen) {
sketchWidth = "displayWidth";
sketchWidth = "displayHeight";
if (isFullscreen) {
sketchWidth = "displayWidth";
sketchWidth = "displayHeight";

thisRequiresRewrite = true;
sizeIsFullscreen = true;
thisRequiresRewrite = true;
sizeIsFullscreen = true;

if (argsContext.getChildCount() > 0) {
sketchRenderer = argsContext.getChild(0).getText();
}
if (argsContext.getChildCount() > 0) {
sketchRenderer = argsContext.getChild(0).getText();
}
}

Expand All @@ -656,32 +646,45 @@ protected void handleSizeCall(ParserRuleContext ctx) {
}

protected void handlePixelDensityCall(ParserRuleContext ctx) {
ParserRuleContext testCtx = ctx.getParent()
// Check that this is a call for processing and not a user defined method.
if (!calledFromGlobalOrSetup(ctx)) {
return;
}

ParseTree argsContext = ctx.getChild(2);
pixelDensity = argsContext.getChild(0).getText();
delete(ctx.start, ctx.stop);
insertAfter(ctx.stop, "/* pixelDensity commented out by preprocessor */");
pixelDensityRequiresRewrite = true;
}

protected void handleNoSmoothCall(ParserRuleContext ctx) {
// Check that this is a call for processing and not a user defined method.
if (!calledFromGlobalOrSetup(ctx)) {
return;
}

delete(ctx.start, ctx.stop);
insertAfter(ctx.stop, "/* noSmooth commented out by preprocessor */");
noSmoothRequiresRewrite = true;
}

protected boolean calledFromGlobalOrSetup(ParserRuleContext callContext) {
ParserRuleContext outerContext = callContext.getParent()
.getParent()
.getParent()
.getParent();

boolean isInGlobal =
testCtx instanceof ProcessingParser.StaticProcessingSketchContext;

boolean isInSetup;
if (!isInGlobal) {
ParserRuleContext methodDeclaration = testCtx.getParent()
.getParent();

isInSetup = isMethodSetup(methodDeclaration);
} else {
isInSetup = false;
// Check static context first (global)
if (outerContext instanceof ProcessingParser.StaticProcessingSketchContext) {
return true;
}

ParseTree argsContext = ctx.getChild(2);
// Otherwise check if method called form setup
ParserRuleContext methodDeclaration = outerContext.getParent()
.getParent();

if (isInGlobal || isInSetup) {
pixelDensity = argsContext.getChild(0).getText();
delete(ctx.start, ctx.stop);
insertAfter(ctx.stop, "/* pixelDensity commented out by preprocessor */");
pixelDensityRequiresRewrite = true;
}
return isMethodSetup(methodDeclaration);
}

/**
Expand Down Expand Up @@ -1010,10 +1013,15 @@ protected void writeStaticSketchFooter(PrintWriterWithEditGen footerWriter,
protected void writeExtraFieldsAndMethods(PrintWriterWithEditGen classBodyWriter,
RewriteResultBuilder resultBuilder) {

if (!sizeRequiresRewrite && !pixelDensityRequiresRewrite) {
// First check that a settings method is required at all
boolean noRewriteRequired = !sizeRequiresRewrite;
noRewriteRequired = noRewriteRequired && !pixelDensityRequiresRewrite;
noRewriteRequired = noRewriteRequired && !noSmoothRequiresRewrite;
if (noRewriteRequired) {
return;
}

// If needed, add the components via a string joiner.
String settingsOuterTemplate = indent1 + "public void settings() { %s }";

StringJoiner settingsInner = new StringJoiner("\n");
Expand Down Expand Up @@ -1048,6 +1056,9 @@ protected void writeExtraFieldsAndMethods(PrintWriterWithEditGen classBodyWriter
settingsInner.add(String.format("pixelDensity(%s);", pixelDensity));
}

if (noSmoothRequiresRewrite) {
settingsInner.add("noSmooth();");
}

String newCode = String.format(settingsOuterTemplate, settingsInner.toString());

Expand Down
4 changes: 2 additions & 2 deletions java/test/processing/mode/java/ParserTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -370,14 +370,14 @@ public void testNoSmooth() {
expectGood("nosmooth");
}

@Test
/*@Test
public void testSmooth() {
expectGood("smoothnoparam");
}
@Test
public void testSmoothWithParam() {
expectGood("smoothparam");
}
}*/

}
28 changes: 11 additions & 17 deletions java/test/resources/nosmooth.expected
Original file line number Diff line number Diff line change
Expand Up @@ -14,28 +14,22 @@ import java.io.IOException;

public class nosmooth extends PApplet {

public void setup() {
/* size commented out by preprocessor */;
/* noSmooth commented out by preprocessor */;
public void setup(){
/* size commented out by preprocessor */;
/* noSmooth commented out by preprocessor */;
}

@Override public void draw() {
background(0);
fill(255,0,0);
ellipse(100,100,100,100);
fill(0,255,0);
ellipse(150,150,100,100);
}

class Test {
private void draw() {}
public void draw(){
background(0);
fill(255,0,0);
ellipse(100,100,100,100);
fill(0,255,0);
ellipse(150,150,100,100);
}


public void settings() {
size(300,300, P2D);
noSmooth();
}
public void settings() { size(300, 300, P2D);
noSmooth(); }

static public void main(String[] passedArgs) {
String[] appletArgs = new String[] { "nosmooth" };
Expand Down

0 comments on commit c41e4d2

Please sign in to comment.