Skip to content

Commit 3380045

Browse files
committed
re-proofing the matches for special settings() functions
1 parent 0615f0f commit 3380045

3 files changed

Lines changed: 81 additions & 23 deletions

File tree

java/src/processing/mode/java/JavaBuild.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,7 @@ public String preprocess(File srcFolder,
257257
}
258258
//System.out.format("size() is '%s'%n", info[0]);
259259

260+
/*
260261
// Remove the size() statement (will be added back by writeFooter())
261262
if (sizeInfo != null) {
262263
String sizeStatement = sizeInfo.getStatement();
@@ -265,7 +266,7 @@ public String preprocess(File srcFolder,
265266
bigCode.delete(index, index + sizeStatement.length());
266267
}
267268
}
268-
269+
*/
269270

270271
PreprocessorResult result;
271272
try {

java/src/processing/mode/java/preproc/PdePreprocessor.java

Lines changed: 76 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -163,10 +163,10 @@ public static enum Mode {
163163
*/
164164
// public static final String SIZE_REGEX =
165165
// "(?:^|\\s|;)size\\s*\\(\\s*([^\\s,]+)\\s*,\\s*([^\\s,\\)]+)\\s*,?\\s*([^\\)]*)\\s*\\)\\s*\\;";
166-
static private final String SIZE_CONTENTS_REGEX =
167-
"(?:^|\\s|;)size\\s*\\(([^\\)]+)\\)\\s*\\;";
168-
static private final String FULL_SCREEN_CONTENTS_REGEX =
169-
"(?:^|\\s|;)fullScreen\\s*\\(([^\\)]+)\\)\\s*\\;";
166+
// static private final String SIZE_CONTENTS_REGEX =
167+
// "(?:^|\\s|;)size\\s*\\(([^\\)]+)\\)\\s*\\;";
168+
// static private final String FULL_SCREEN_CONTENTS_REGEX =
169+
// "(?:^|\\s|;)fullScreen\\s*\\(([^\\)]+)\\)\\s*\\;";
170170
// /** Test whether there's a void somewhere (the program has functions). */
171171
// static private final String VOID_REGEX =
172172
// "(?:^|\\s|;)void\\s";
@@ -208,9 +208,11 @@ public SurfaceInfo initSketchSize(String code,
208208
}
209209

210210

211-
// break on commas, except those inside quotes, e.g.:
212-
// size(300, 200, PDF, "output,weirdname.pdf");
213-
// no handling for escaped (\") quotes
211+
/**
212+
* Break on commas, except those inside quotes,
213+
* e.g.: size(300, 200, PDF, "output,weirdname.pdf");
214+
* No special handling implemented for escaped (\") quotes.
215+
*/
214216
static private StringList breakCommas(String contents) {
215217
StringList outgoing = new StringList();
216218

@@ -248,7 +250,8 @@ static private StringList breakCommas(String contents) {
248250

249251
/**
250252
* Parse a chunk of code and extract the size() command and its contents.
251-
* @param code Usually the code from the main tab in the sketch
253+
* Also goes after fullScreen(), smooth(), and noSmooth().
254+
* @param code The code from the main tab in the sketch
252255
* @param fussy true if it should show an error message if bad size()
253256
* @return null if there was an error, otherwise an array (might contain some/all nulls)
254257
*/
@@ -306,14 +309,28 @@ make sure that it uses numbers (or displayWidth/Height), copy into settings
306309
searchArea = sb.toString();
307310
}
308311

309-
// Get everything inside the parens for the size() method
310-
String[] contents = PApplet.match(searchArea, SIZE_CONTENTS_REGEX);
311-
if (contents != null) {
312-
//String[] matches = split on commas, but not commas inside quotes
312+
// First look for noSmooth() or smooth(N) so we can hoist it into settings.
313+
String smoothStatement = null;
314+
String[] smoothContents = matchMethod("smooth", searchArea);
315+
if (smoothContents != null) {
316+
smoothStatement = smoothContents[0];
317+
}
318+
String[] noContents = matchMethod("noSmooth", searchArea);
319+
if (noContents != null) {
320+
if (smoothStatement != null) {
321+
throw new SketchException("smooth() and noSmooth() cannot be used in the same sketch");
322+
} else {
323+
smoothStatement = noContents[0];
324+
}
325+
}
313326

314-
StringList args = breakCommas(contents[1]);
327+
// Get everything inside the parens for the size() method
328+
//String[] contents = PApplet.match(searchArea, SIZE_CONTENTS_REGEX);
329+
String[] sizeContents = matchMethod("size", searchArea);
330+
if (sizeContents != null) {
331+
StringList args = breakCommas(sizeContents[1]);
315332
SurfaceInfo info = new SurfaceInfo();
316-
info.statement = contents[0];
333+
info.statement = sizeContents[0];
317334
info.width = args.get(0).trim();
318335
info.height = args.get(1).trim();
319336
info.renderer = (args.size() >= 3) ? args.get(2).trim() : null;
@@ -340,30 +357,69 @@ make sure that it uses numbers (or displayWidth/Height), copy into settings
340357
throw new SketchException("Please fix the size() line to continue.", false);
341358
}
342359

360+
if (smoothStatement != null) {
361+
info.statement += smoothStatement;
362+
}
343363
info.checkEmpty();
344364
return info;
345365
//return new String[] { contents[0], width, height, renderer, path };
346366
}
347367
// if no size() found, check for fullScreen()
348-
contents = PApplet.match(searchArea, FULL_SCREEN_CONTENTS_REGEX);
349-
if (contents != null) {
368+
//contents = PApplet.match(searchArea, FULL_SCREEN_CONTENTS_REGEX);
369+
String[] fullContents = matchMethod("fullScreen", searchArea);
370+
if (fullContents != null) {
350371
SurfaceInfo info = new SurfaceInfo();
351-
info.statement = contents[0];
352-
StringList args = breakCommas(contents[1]);
353-
info.renderer = args.get(0).trim();
354-
info.display = args.size() > 1 ? args.get(1).trim() : null;
372+
info.statement = fullContents[0];
373+
StringList args = breakCommas(fullContents[1]);
374+
if (args.size() > 0) { // might have no args
375+
String args0 = args.get(0).trim();
376+
if (args.size() == 1) {
377+
// could be either fullScreen(1) or fullScreen(P2D), figure out which
378+
if (args0.equals("SPAN") || PApplet.parseInt(args0, -1) != -1) {
379+
// it's the display parameter, not the renderer
380+
info.display = args0;
381+
} else {
382+
info.renderer = args0;
383+
}
384+
} else if (args.size() == 2) {
385+
info.renderer = args0;
386+
info.display = args.get(1).trim();
387+
} else {
388+
throw new SketchException("That's too many parameters for fullScreen()");
389+
}
390+
}
355391
info.width = "displayWidth";
356392
info.height = "displayHeight";
393+
if (smoothStatement != null) {
394+
info.statement += smoothStatement;
395+
}
357396
info.checkEmpty();
358397
return info;
359398
}
360399

400+
// Made it this far, but no size() or fullScreen(), and still
401+
// need to pull out the noSmooth() and smooth(N) methods.
402+
if (smoothStatement != null) {
403+
SurfaceInfo info = new SurfaceInfo();
404+
info.statement = smoothStatement;
405+
return info;
406+
}
407+
361408
// not an error, just no size() specified
362409
//return new String[] { null, null, null, null, null };
363410
return new SurfaceInfo();
364411
}
365412

366413

414+
static protected String[] matchMethod(String methodName, String searchArea) {
415+
final String left = "(?:^|\\s|;)";
416+
// doesn't match empty pairs of parens
417+
//final String right = "\\s*\\(([^\\)]+)\\)\\s*\\;";
418+
final String right = "\\s*\\(([^\\)]*)\\)\\s*\\;";
419+
return PApplet.match(searchArea, left + methodName + right);
420+
}
421+
422+
367423
/**
368424
* Replace all commented portions of a given String as spaces.
369425
* Utility function used here and in the preprocessor.

java/src/processing/mode/java/preproc/SurfaceInfo.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
SizeInfo - parsed elements of a size() or fullScreen() call
55
Part of the Processing project - http://processing.org
66
7-
Copyright (c) 2015 Ben Fry and Casey Reas
7+
Copyright (c) 2015 The Processing Foundation
88
99
This program is free software; you can redistribute it and/or modify
1010
it under the terms of the GNU General Public License as published by
@@ -36,7 +36,8 @@ public class SurfaceInfo {
3636

3737
String display;
3838
/** null for nothing in setup(), 0 for noSmooth(), N for smooth(N) */
39-
Integer quality;
39+
//Integer quality;
40+
// String smooth;
4041

4142

4243
boolean hasOldSyntax() {

0 commit comments

Comments
 (0)