Fix size call with equation.

This commit is contained in:
A Samuel Pottinger
2022-11-27 19:04:56 +00:00
parent 1ca265aa43
commit 54426cb8a7
6 changed files with 167 additions and 8 deletions

View File

@@ -693,17 +693,17 @@ public class PdeParseTreeListener extends ProcessingBaseListener {
if (isSize && argsContext.getChildCount() > 2) {
thisRequiresRewrite = true;
sketchWidth = argsContext.getChild(0).getText();
boolean invalidWidth = PApplet.parseInt(sketchWidth, -1) == -1;
invalidWidth = invalidWidth && !sketchWidth.equals("displayWidth");
if (invalidWidth) {
boolean widthValid = sizeParamValid(argsContext.getChild(0));
if (widthValid) {
sketchWidth = argsContext.getChild(0).getText();
} else {
thisRequiresRewrite = false;
}
sketchHeight = argsContext.getChild(2).getText();
boolean invalidHeight = PApplet.parseInt(sketchHeight, -1) == -1;
invalidHeight = invalidHeight && !sketchHeight.equals("displayHeight");
if (invalidHeight) {
boolean validHeight = sizeParamValid(argsContext.getChild(2));
if (validHeight) {
sketchHeight = argsContext.getChild(2).getText();
} else {
thisRequiresRewrite = false;
}
@@ -1535,4 +1535,36 @@ public class PdeParseTreeListener extends ProcessingBaseListener {
return ImportStatement.parse(fullyQualifiedName);
}
private boolean isMethodCall(ParseTree ctx) {
return ctx instanceof ProcessingParser.MethodCallContext;
}
private boolean isVariable(ParseTree ctx) {
boolean isPrimary = ctx instanceof ProcessingParser.PrimaryContext;
if (!isPrimary) {
return false;
}
String text = ctx.getText();
boolean startsWithAlpha = text.length() > 0 && Character.isAlphabetic(text.charAt(0));
return startsWithAlpha;
}
private boolean sizeParamValid(ParseTree ctx) {
// Method calls and variables not allowed.
if (isMethodCall(ctx) || isVariable(ctx)) {
return false;
}
// If user passed an expression, check subexpressions.
for (int i = 0; i < ctx.getChildCount(); i++) {
if (!sizeParamValid(ctx.getChild(i))) {
return false;
}
}
// If all sub-expressions passed and not identifier, is valid.
return true;
}
}

View File

@@ -50,6 +50,22 @@ public class ParserTests {
}
}
static void expectRunnerException(final String id) {
try {
preprocess(id, res(id + ".pde"));
fail("Expected to fail");
} catch (SketchException e) {
assertNotNull(e);
} catch (PdePreprocessIssueException e) {
assertNotNull(e.getIssue().getMsg());
} catch (Exception e) {
if (!e.equals(e.getCause()) && e.getCause() != null)
fail(e.getCause().toString());
else
fail(e.toString());
}
}
static void expectRunnerException(final String id,
final int expectedLine) {
@@ -453,4 +469,19 @@ public class ParserTests {
expectGood("customrootclass");
}
@Test
public void testExpessionSize() {
expectGood("expressionsize");
}
@Test
public void testExpessionSizeMethod() {
expectRunnerException("expressionsizemethod");
}
@Test
public void testExpessionSizeVar() {
expectRunnerException("expressionsizevar");
}
}

View File

@@ -0,0 +1,45 @@
import processing.core.*;
import processing.data.*;
import processing.event.*;
import processing.opengl.*;
import processing.pdf.*;
import java.util.HashMap;
import java.util.ArrayList;
import java.io.File;
import java.io.BufferedReader;
import java.io.PrintWriter;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.IOException;
public class expressionsize extends PApplet {
public void setup() {
/* size commented out by preprocessor */;
}
public void draw() {
// Draw something good here
line(0, 0, width/2, height);
// Exit the program
println("Finished.");
exit();
}
public void settings() { size(400*2,4e2/2); }
static public void main(String[] passedArgs) {
String[] appletArgs = new String[] { "expressionsize" };
if (passedArgs != null) {
PApplet.main(concat(appletArgs, passedArgs));
} else {
PApplet.main(appletArgs);
}
}
}

View File

@@ -0,0 +1,14 @@
import processing.pdf.*;
void setup() {
size(400*2, 4e2/2);
}
void draw() {
// Draw something good here
line(0, 0, width/2, height);
// Exit the program
println("Finished.");
exit();
}

View File

@@ -0,0 +1,18 @@
import processing.pdf.*;
int getWidth() {
return 400*2;
}
void setup() {
size(getWidth(), 400/2);
}
void draw() {
// Draw something good here
line(0, 0, width/2, height);
// Exit the program
println("Finished.");
exit();
}

View File

@@ -0,0 +1,19 @@
import processing.pdf.*;
int getWidth() {
return 400*2;
}
void setup() {
int newWidth = 400*2;
size(newWidth, 400/2);
}
void draw() {
// Draw something good here
line(0, 0, width/2, height);
// Exit the program
println("Finished.");
exit();
}