mirror of
https://github.com/processing/processing4.git
synced 2026-01-28 02:41:08 +01:00
Fix size call with equation.
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
45
java/test/resources/expressionsize.expected
Normal file
45
java/test/resources/expressionsize.expected
Normal 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
14
java/test/resources/expressionsize.pde
Normal file
14
java/test/resources/expressionsize.pde
Normal 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();
|
||||
}
|
||||
18
java/test/resources/expressionsizemethod.pde
Normal file
18
java/test/resources/expressionsizemethod.pde
Normal 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();
|
||||
}
|
||||
19
java/test/resources/expressionsizevar.pde
Normal file
19
java/test/resources/expressionsizevar.pde
Normal 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();
|
||||
}
|
||||
Reference in New Issue
Block a user