Skip to content

Commit 54426cb

Browse files
author
A Samuel Pottinger
committed
Fix size call with equation.
1 parent 1ca265a commit 54426cb

File tree

6 files changed

+167
-8
lines changed

6 files changed

+167
-8
lines changed

java/src/processing/mode/java/preproc/PdeParseTreeListener.java

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -693,17 +693,17 @@ protected void handleSizeCall(ParserRuleContext ctx) {
693693
if (isSize && argsContext.getChildCount() > 2) {
694694
thisRequiresRewrite = true;
695695

696-
sketchWidth = argsContext.getChild(0).getText();
697-
boolean invalidWidth = PApplet.parseInt(sketchWidth, -1) == -1;
698-
invalidWidth = invalidWidth && !sketchWidth.equals("displayWidth");
699-
if (invalidWidth) {
696+
boolean widthValid = sizeParamValid(argsContext.getChild(0));
697+
if (widthValid) {
698+
sketchWidth = argsContext.getChild(0).getText();
699+
} else {
700700
thisRequiresRewrite = false;
701701
}
702702

703-
sketchHeight = argsContext.getChild(2).getText();
704-
boolean invalidHeight = PApplet.parseInt(sketchHeight, -1) == -1;
705-
invalidHeight = invalidHeight && !sketchHeight.equals("displayHeight");
706-
if (invalidHeight) {
703+
boolean validHeight = sizeParamValid(argsContext.getChild(2));
704+
if (validHeight) {
705+
sketchHeight = argsContext.getChild(2).getText();
706+
} else {
707707
thisRequiresRewrite = false;
708708
}
709709

@@ -1535,4 +1535,36 @@ private ImportStatement createPlainImportStatementInfo(String fullyQualifiedName
15351535
return ImportStatement.parse(fullyQualifiedName);
15361536
}
15371537

1538+
private boolean isMethodCall(ParseTree ctx) {
1539+
return ctx instanceof ProcessingParser.MethodCallContext;
1540+
}
1541+
1542+
private boolean isVariable(ParseTree ctx) {
1543+
boolean isPrimary = ctx instanceof ProcessingParser.PrimaryContext;
1544+
if (!isPrimary) {
1545+
return false;
1546+
}
1547+
1548+
String text = ctx.getText();
1549+
boolean startsWithAlpha = text.length() > 0 && Character.isAlphabetic(text.charAt(0));
1550+
return startsWithAlpha;
1551+
}
1552+
1553+
private boolean sizeParamValid(ParseTree ctx) {
1554+
// Method calls and variables not allowed.
1555+
if (isMethodCall(ctx) || isVariable(ctx)) {
1556+
return false;
1557+
}
1558+
1559+
// If user passed an expression, check subexpressions.
1560+
for (int i = 0; i < ctx.getChildCount(); i++) {
1561+
if (!sizeParamValid(ctx.getChild(i))) {
1562+
return false;
1563+
}
1564+
}
1565+
1566+
// If all sub-expressions passed and not identifier, is valid.
1567+
return true;
1568+
}
1569+
15381570
}

java/test/processing/mode/java/ParserTests.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,22 @@ static void expectRecognitionException(final String id,
5050
}
5151
}
5252

53+
static void expectRunnerException(final String id) {
54+
try {
55+
preprocess(id, res(id + ".pde"));
56+
fail("Expected to fail");
57+
} catch (SketchException e) {
58+
assertNotNull(e);
59+
} catch (PdePreprocessIssueException e) {
60+
assertNotNull(e.getIssue().getMsg());
61+
} catch (Exception e) {
62+
if (!e.equals(e.getCause()) && e.getCause() != null)
63+
fail(e.getCause().toString());
64+
else
65+
fail(e.toString());
66+
}
67+
}
68+
5369
static void expectRunnerException(final String id,
5470
final int expectedLine) {
5571

@@ -453,4 +469,19 @@ public void testCustomRootClass() {
453469
expectGood("customrootclass");
454470
}
455471

472+
@Test
473+
public void testExpessionSize() {
474+
expectGood("expressionsize");
475+
}
476+
477+
@Test
478+
public void testExpessionSizeMethod() {
479+
expectRunnerException("expressionsizemethod");
480+
}
481+
482+
@Test
483+
public void testExpessionSizeVar() {
484+
expectRunnerException("expressionsizevar");
485+
}
486+
456487
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import processing.core.*;
2+
import processing.data.*;
3+
import processing.event.*;
4+
import processing.opengl.*;
5+
6+
import processing.pdf.*;
7+
8+
import java.util.HashMap;
9+
import java.util.ArrayList;
10+
import java.io.File;
11+
import java.io.BufferedReader;
12+
import java.io.PrintWriter;
13+
import java.io.InputStream;
14+
import java.io.OutputStream;
15+
import java.io.IOException;
16+
17+
public class expressionsize extends PApplet {
18+
19+
20+
21+
public void setup() {
22+
/* size commented out by preprocessor */;
23+
}
24+
25+
public void draw() {
26+
// Draw something good here
27+
line(0, 0, width/2, height);
28+
29+
// Exit the program
30+
println("Finished.");
31+
exit();
32+
}
33+
34+
35+
public void settings() { size(400*2,4e2/2); }
36+
37+
static public void main(String[] passedArgs) {
38+
String[] appletArgs = new String[] { "expressionsize" };
39+
if (passedArgs != null) {
40+
PApplet.main(concat(appletArgs, passedArgs));
41+
} else {
42+
PApplet.main(appletArgs);
43+
}
44+
}
45+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import processing.pdf.*;
2+
3+
void setup() {
4+
size(400*2, 4e2/2);
5+
}
6+
7+
void draw() {
8+
// Draw something good here
9+
line(0, 0, width/2, height);
10+
11+
// Exit the program
12+
println("Finished.");
13+
exit();
14+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import processing.pdf.*;
2+
3+
int getWidth() {
4+
return 400*2;
5+
}
6+
7+
void setup() {
8+
size(getWidth(), 400/2);
9+
}
10+
11+
void draw() {
12+
// Draw something good here
13+
line(0, 0, width/2, height);
14+
15+
// Exit the program
16+
println("Finished.");
17+
exit();
18+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import processing.pdf.*;
2+
3+
int getWidth() {
4+
return 400*2;
5+
}
6+
7+
void setup() {
8+
int newWidth = 400*2;
9+
size(newWidth, 400/2);
10+
}
11+
12+
void draw() {
13+
// Draw something good here
14+
line(0, 0, width/2, height);
15+
16+
// Exit the program
17+
println("Finished.");
18+
exit();
19+
}

0 commit comments

Comments
 (0)