Skip to content

Commit c310896

Browse files
authored
Merge pull request #609 from processing/preproc_fix_space_equation
2 parents b807259 + c6f9b6f commit c310896

File tree

10 files changed

+336
-10
lines changed

10 files changed

+336
-10
lines changed

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

Lines changed: 42 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -589,9 +589,9 @@ public void exitMethodDeclaration(ProcessingParser.MethodDeclarationContext ctx)
589589
// Insert at start of method or after annoation
590590
if (!hasVisibilityModifier) {
591591
if (annoationPoint == null) {
592-
insertBefore(possibleModifiers.getStart(), " public ");
592+
insertBefore(possibleModifiers.getStart(), "public ");
593593
} else {
594-
insertAfter(annoationPoint.getStop(), " public ");
594+
insertAfter(annoationPoint.getStop(), "public ");
595595
}
596596
}
597597

@@ -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: 36 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,24 @@ 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+
expectGood("expressionsizemethod");
480+
}
481+
482+
@Test
483+
public void testExpessionSizeVar() {
484+
expectGood("expressionsizevar");
485+
}
486+
487+
@Test
488+
public void testWhitespace() {
489+
expectGood("whitespace", false);
490+
}
491+
456492
}
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: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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 expressionsizemethod extends PApplet {
18+
19+
public int getWidth() {
20+
return 400*2;
21+
}
22+
23+
public void setup() {
24+
size(getWidth(), 400/2);
25+
}
26+
27+
public void draw() {
28+
// Draw something good here
29+
line(0, 0, width/2, height);
30+
31+
// Exit the program
32+
println("Finished.");
33+
exit();
34+
}
35+
36+
static public void main(String[] passedArgs) {
37+
String[] appletArgs = new String[] { "expressionsizemethod" };
38+
if (passedArgs != null) {
39+
PApplet.main(concat(appletArgs, passedArgs));
40+
} else {
41+
PApplet.main(appletArgs);
42+
}
43+
}
44+
}
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: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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 expressionsizevar extends PApplet {
18+
19+
20+
21+
public void setup() {
22+
int newWidth = 400*2;
23+
size(newWidth, 400/2);
24+
}
25+
26+
public void draw() {
27+
// Draw something good here
28+
line(0, 0, width/2, height);
29+
30+
// Exit the program
31+
println("Finished.");
32+
exit();
33+
}
34+
35+
static public void main(String[] passedArgs) {
36+
String[] appletArgs = new String[] { "expressionsizevar" };
37+
if (passedArgs != null) {
38+
PApplet.main(concat(appletArgs, passedArgs));
39+
} else {
40+
PApplet.main(appletArgs);
41+
}
42+
}
43+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import processing.pdf.*;
2+
3+
void setup() {
4+
int newWidth = 400*2;
5+
size(newWidth, 400/2);
6+
}
7+
8+
void draw() {
9+
// Draw something good here
10+
line(0, 0, width/2, height);
11+
12+
// Exit the program
13+
println("Finished.");
14+
exit();
15+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import processing.core.*;
2+
import processing.data.*;
3+
import processing.event.*;
4+
import processing.opengl.*;
5+
6+
import java.util.HashMap;
7+
import java.util.ArrayList;
8+
import java.io.File;
9+
import java.io.BufferedReader;
10+
import java.io.PrintWriter;
11+
import java.io.InputStream;
12+
import java.io.OutputStream;
13+
import java.io.IOException;
14+
15+
public class whitespace extends PApplet {
16+
17+
public void setup() {
18+
/* size commented out by preprocessor */;
19+
stroke(255);
20+
noFill();
21+
}
22+
23+
public void drawBezier(int i) {
24+
bezier(
25+
mouseX - (i/2.0f),
26+
40+i,
27+
410,
28+
20,
29+
440,
30+
300,
31+
240 - (i/16.0f),
32+
300 + (i/8.0f)
33+
);
34+
}
35+
36+
public void draw() {
37+
background(0);
38+
for (int i = 0; i < 200; i += 20) {
39+
drawBezier(i);
40+
}
41+
}
42+
43+
44+
public void settings() { size(640, 360); }
45+
46+
static public void main(String[] passedArgs) {
47+
String[] appletArgs = new String[] { "whitespace" };
48+
if (passedArgs != null) {
49+
PApplet.main(concat(appletArgs, passedArgs));
50+
} else {
51+
PApplet.main(appletArgs);
52+
}
53+
}
54+
}

java/test/resources/whitespace.pde

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
void setup() {
2+
size(640, 360);
3+
stroke(255);
4+
noFill();
5+
}
6+
7+
void drawBezier(int i) {
8+
bezier(
9+
mouseX - (i/2.0),
10+
40+i,
11+
410,
12+
20,
13+
440,
14+
300,
15+
240 - (i/16.0),
16+
300 + (i/8.0)
17+
);
18+
}
19+
20+
void draw() {
21+
background(0);
22+
for (int i = 0; i < 200; i += 20) {
23+
drawBezier(i);
24+
}
25+
}

0 commit comments

Comments
 (0)