From 721046b5934719b6068ba75182a5d2c92d7f6bfe Mon Sep 17 00:00:00 2001 From: A Pottinger Date: Sat, 21 Nov 2020 11:59:25 -0800 Subject: [PATCH] Finish fix of preproc handeling of smooth. Fixes #149 as reported by @benfry. --- build/shared/lib/languages/PDE.properties | 2 + .../java/preproc/PdeParseTreeListener.java | 55 +++++++++++++++++++ .../processing/mode/java/ParserTests.java | 4 +- java/test/resources/bug315g.expected | 5 +- java/test/resources/smoothnoparam.expected | 28 ++++------ java/test/resources/smoothparam.expected | 28 ++++------ 6 files changed, 84 insertions(+), 38 deletions(-) diff --git a/build/shared/lib/languages/PDE.properties b/build/shared/lib/languages/PDE.properties index b81fcd11e..a4dc37d2b 100644 --- a/build/shared/lib/languages/PDE.properties +++ b/build/shared/lib/languages/PDE.properties @@ -410,6 +410,8 @@ editor.status.bad.generic = Possibly missing type in generic near '%s'? editor.status.bad.identifier = Bad identifier? Did you forget a variable or start an identifier with digits near '%s'? editor.status.bad.parameter = Error on parameter or method declaration near '%s'? editor.status.bad.import = Import not allowed here. +editor.status.bad.size = Too many arguments to size. +editor.status.bad.smooth = Too many arguments to smooth. editor.status.extraneous = Incomplete statement or extra code near '%s'? editor.status.mismatched = Missing operator, semicolon, or '}' near '%s'? editor.status.missing.name = Missing name or ; near '%s'? diff --git a/java/src/processing/mode/java/preproc/PdeParseTreeListener.java b/java/src/processing/mode/java/preproc/PdeParseTreeListener.java index 50a11a3cf..314d7b0c2 100644 --- a/java/src/processing/mode/java/preproc/PdeParseTreeListener.java +++ b/java/src/processing/mode/java/preproc/PdeParseTreeListener.java @@ -75,6 +75,7 @@ public class PdeParseTreeListener extends ProcessingBaseListener { private String sketchWidth; private String sketchHeight; private String pixelDensity; + private String smoothParam; private String sketchRenderer = null; private String sketchOutputFilename = null; @@ -82,6 +83,7 @@ public class PdeParseTreeListener extends ProcessingBaseListener { private boolean pixelDensityRequiresRewrite = false; private boolean sizeIsFullscreen = false; private boolean noSmoothRequiresRewrite = false; + private boolean smoothRequiresRewrite = false; private RewriteResult headerResult; private RewriteResult footerResult; @@ -317,6 +319,8 @@ public class PdeParseTreeListener extends ProcessingBaseListener { handlePixelDensityCall(ctx); } else if (NO_SMOOTH_METHOD_NAME.equals(methodName)) { handleNoSmoothCall(ctx); + } else if (SMOOTH_METHOD_NAME.equals(methodName)) { + handleSmoothCall(ctx); } } @@ -624,6 +628,20 @@ public class PdeParseTreeListener extends ProcessingBaseListener { if (argsContext.getChildCount() > 5) { sketchOutputFilename = argsContext.getChild(6).getText(); } + + if (argsContext.getChildCount() > 7) { + pdeParseTreeErrorListenerMaybe.ifPresent((listener) -> { + Token token = ctx.getStart(); + int line = token.getLine(); + int charOffset = token.getCharPositionInLine(); + + listener.onError(new PdePreprocessIssue( + line, + charOffset, + PreprocessIssueMessageSimplifier.getLocalStr("editor.status.bad.size") + )); + }); + } } if (isFullscreen) { @@ -669,6 +687,38 @@ public class PdeParseTreeListener extends ProcessingBaseListener { noSmoothRequiresRewrite = true; } + protected void handleSmoothCall(ParserRuleContext ctx) { + // Check that this is a call for processing and not a user defined size method. + if (!calledFromGlobalOrSetup(ctx)) { + return; + } + + ParseTree argsContext = ctx.getChild(2); + if (argsContext.getChildCount() > 0) { + smoothParam = argsContext.getChild(0).getText(); + } else { + smoothParam = ""; + } + + if (argsContext.getChildCount() > 2) { + pdeParseTreeErrorListenerMaybe.ifPresent((listener) -> { + Token token = ctx.getStart(); + int line = token.getLine(); + int charOffset = token.getCharPositionInLine(); + + listener.onError(new PdePreprocessIssue( + line, + charOffset, + PreprocessIssueMessageSimplifier.getLocalStr("editor.status.bad.smooth") + )); + }); + } + + delete(ctx.start, ctx.stop); + insertAfter(ctx.stop, "/* smooth commented out by preprocessor */"); + smoothRequiresRewrite = true; + } + protected boolean calledFromGlobalOrSetup(ParserRuleContext callContext) { ParserRuleContext outerContext = callContext.getParent() .getParent() @@ -1017,6 +1067,7 @@ public class PdeParseTreeListener extends ProcessingBaseListener { boolean noRewriteRequired = !sizeRequiresRewrite; noRewriteRequired = noRewriteRequired && !pixelDensityRequiresRewrite; noRewriteRequired = noRewriteRequired && !noSmoothRequiresRewrite; + noRewriteRequired = noRewriteRequired && !smoothRequiresRewrite; if (noRewriteRequired) { return; } @@ -1060,6 +1111,10 @@ public class PdeParseTreeListener extends ProcessingBaseListener { settingsInner.add("noSmooth();"); } + if (smoothRequiresRewrite) { + settingsInner.add(String.format("smooth(%s);", smoothParam)); + } + String newCode = String.format(settingsOuterTemplate, settingsInner.toString()); classBodyWriter.addEmptyLine(); diff --git a/java/test/processing/mode/java/ParserTests.java b/java/test/processing/mode/java/ParserTests.java index 6c2a5c1f5..6fd29e368 100644 --- a/java/test/processing/mode/java/ParserTests.java +++ b/java/test/processing/mode/java/ParserTests.java @@ -370,7 +370,7 @@ public class ParserTests { expectGood("nosmooth"); } - /*@Test + @Test public void testSmooth() { expectGood("smoothnoparam"); } @@ -378,6 +378,6 @@ public class ParserTests { @Test public void testSmoothWithParam() { expectGood("smoothparam"); - }*/ + } } diff --git a/java/test/resources/bug315g.expected b/java/test/resources/bug315g.expected index 54de9127e..e771f41b6 100644 --- a/java/test/resources/bug315g.expected +++ b/java/test/resources/bug315g.expected @@ -16,7 +16,7 @@ public class bug315g extends PApplet { public void setup() { /* size commented out by preprocessor */; -smooth(); +/* smooth commented out by preprocessor */; int y; y = 60; int d; @@ -25,7 +25,8 @@ ellipse(75, y, d, d); noLoop(); } - public void settings() { size(480,120); } + public void settings() { size(480,120); +smooth();} static public void main(String[] passedArgs) { String[] appletArgs = new String[] { "bug315g" }; diff --git a/java/test/resources/smoothnoparam.expected b/java/test/resources/smoothnoparam.expected index a5c2f7d39..2d87ddbea 100644 --- a/java/test/resources/smoothnoparam.expected +++ b/java/test/resources/smoothnoparam.expected @@ -14,28 +14,22 @@ import java.io.IOException; public class smoothnoparam extends PApplet { -public void setup() { - /* size commented out by preprocessor */; - /* smooth commented out by preprocessor */; + public void setup(){ + /* size commented out by preprocessor */; + /* smooth 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); - smooth(); - } + public void settings() { size(300, 300, P2D); +smooth(); } static public void main(String[] passedArgs) { String[] appletArgs = new String[] { "smoothnoparam" }; diff --git a/java/test/resources/smoothparam.expected b/java/test/resources/smoothparam.expected index f7efe8ec0..99543096a 100644 --- a/java/test/resources/smoothparam.expected +++ b/java/test/resources/smoothparam.expected @@ -14,28 +14,22 @@ import java.io.IOException; public class smoothparam extends PApplet { -public void setup() { - /* size commented out by preprocessor */; - /* smooth commented out by preprocessor */; + public void setup(){ + /* size commented out by preprocessor */; + /* smooth 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); - smooth(4); - } + public void settings() { size(300, 300, P2D); +smooth(4); } static public void main(String[] passedArgs) { String[] appletArgs = new String[] { "smoothparam" };