new size() parsing, change how P2D/P3D are handled, onward...

This commit is contained in:
benfry
2012-04-03 14:57:13 +00:00
parent 0a560a2226
commit f0c2ef79b6
14 changed files with 3201 additions and 2971 deletions

View File

@@ -3,7 +3,7 @@
/*
Part of the Processing project - http://processing.org
Copyright (c) 2004-11 Ben Fry and Casey Reas
Copyright (c) 2004-12 Ben Fry and Casey Reas
Copyright (c) 2001-04 Massachusetts Institute of Technology
This program is free software; you can redistribute it and/or modify
@@ -34,16 +34,6 @@ import processing.mode.java.preproc.*;
public class JavaBuild {
/**
* Regular expression for parsing the size() method. This should match
* against any uses of the size() function, whether numbers or variables
* or whatever. This way, no warning is shown if size() isn't actually used
* in the sketch, which is the case especially for anyone who is cutting
* and pasting from the reference.
*/
public static final String SIZE_REGEX =
"(?:^|\\s|;)size\\s*\\(\\s*([^\\s,]+)\\s*,\\s*([^\\s,\\)]+),?\\s*([^\\)]*)\\s*\\)\\s*\\;";
//"(?:^|\\s|;)size\\s*\\(\\s*(\\S+)\\s*,\\s*([^\\s,\\)]+),?\\s*([^\\)]*)\\s*\\)\\s*\\;";
public static final String PACKAGE_REGEX =
"(?:^|\\s|;)package\\s+(\\S+)\\;";
@@ -249,20 +239,24 @@ public class JavaBuild {
}
}
// Automatically insert the OpenGL import line if P3D is used. Do this by
// modifying the code here instead of
String scrubbed = scrubComments(sketch.getCode(0).getProgram());
String[] matches = PApplet.match(scrubbed, SIZE_REGEX);
String renderer = "";
if (matches != null) {
// Adding back the trim() for 0136 to handle Bug #769
if (matches.length == 4) renderer = matches[3].trim();
// Actually, matches.length should always be 4...
}
// OpenGL import time! Really, this is for P3D, but may as well do it
// for OpenGL as well.
if (renderer.equals("P3D") || renderer.equals("OPENGL")) {
bigCode.insert(0, "import processing.opengl.*; ");
// initSketchSize() sets the internal sketchWidth/Height/Renderer vars
// in the preprocessor. Those are used in preproc.write() so that they
// can be turned into sketchXxxx() methods.
// This also returns the size info as an array so that we can figure out
// if this fella is OpenGL, and if so, to add the import. It's messy and
// gross and someday we'll just always include OpenGL.
String[] sizeInfo =
preprocessor.initSketchSize(sketch.getMainProgram());
//PdePreprocessor.parseSketchSize(sketch.getMainProgram(), false);
if (sizeInfo != null) {
String sketchRenderer = sizeInfo[3];
if (sketchRenderer != null) {
if (sketchRenderer.equals("P2D") ||
sketchRenderer.equals("P3D") ||
sketchRenderer.equals("OPENGL")) {
bigCode.insert(0, "import processing.opengl.*; ");
}
}
}
PreprocessorResult result;
@@ -298,7 +292,7 @@ public class JavaBuild {
// System.out.println(errorLine + " " + errorFile + " " + code[errorFile].getPreprocOffset());
String msg = re.getMessage();
//System.out.println(java.getAbsolutePath());
System.out.println(bigCode);
@@ -313,7 +307,7 @@ public class JavaBuild {
"without a } to match it.",
errorFile, errorLine, re.getColumn(), false);
}
if (msg.contains("expecting LCURLY")) {
System.err.println(msg);
String suffix = ".";
@@ -321,7 +315,7 @@ public class JavaBuild {
if (m != null) {
suffix = ", not " + m[1] + ".";
}
throw new SketchException("Was expecting a { character" + suffix,
throw new SketchException("Was expecting a { character" + suffix,
errorFile, errorLine, re.getColumn(), false);
}
@@ -365,7 +359,7 @@ public class JavaBuild {
// TODO not tested since removing ORO matcher.. ^ could be a problem
String mess = "^line (\\d+):(\\d+):\\s";
matches = PApplet.match(tsre.toString(), mess);
String[] matches = PApplet.match(tsre.toString(), mess);
if (matches != null) {
int errorLine = Integer.parseInt(matches[1]) - 1;
int errorColumn = Integer.parseInt(matches[2]);
@@ -503,7 +497,7 @@ public class JavaBuild {
sc.addPreprocOffset(result.headerOffset);
}
}
foundMain = preprocessor.getFoundMain();
foundMain = preprocessor.hasMethod("main");
return result.className;
}
@@ -730,12 +724,57 @@ public class JavaBuild {
return false;
}
String[] sizeInfo =
PdePreprocessor.parseSketchSize(sketch.getMainProgram(), false);
int sketchWidth = PApplet.DEFAULT_WIDTH;
int sketchHeight = PApplet.DEFAULT_HEIGHT;
boolean openglApplet = false;
boolean foundSize = false;
if (sizeInfo != null) {
try {
if (sizeInfo[1] != null && sizeInfo[2] != null) {
sketchWidth = Integer.parseInt(sizeInfo[1]);
sketchHeight = Integer.parseInt(sizeInfo[2]);
foundSize = true;
}
} catch (Exception e) {
e.printStackTrace();
// parsing errors, whatever; ignored
}
String sketchRenderer = sizeInfo[3];
if (sketchRenderer != null) {
if (sketchRenderer.equals("P2D") ||
sketchRenderer.equals("P3D") ||
sketchRenderer.equals("OPENGL")) {
openglApplet = true;
}
}
}
if (!foundSize) {
final String message =
"The size of this applet could not automatically be\n" +
"determined from your code. You'll have to edit the\n" +
"HTML file to set the size of the applet.\n" +
"Use only numeric values (not variables) for the size()\n" +
"command. See the size() reference for an explanation.";
Base.showWarning("Could not find applet size", message, null);
}
// // If the renderer is set to the built-in OpenGL library,
// // then it's definitely an OpenGL applet.
// if (sketchRenderer.equals("P3D") || sketchRenderer.equals("OPENGL")) {
// openglApplet = true;
// }
/*
int wide = PApplet.DEFAULT_WIDTH;
int high = PApplet.DEFAULT_HEIGHT;
String renderer = "";
String scrubbed = scrubComments(sketch.getCode(0).getProgram());
String[] matches = PApplet.match(scrubbed, SIZE_REGEX);
String scrubbed = PdePreprocessor.scrubComments(sketch.getCode(0).getProgram());
String[] matches = PApplet.match(scrubbed, PdePreprocessor.SIZE_REGEX);
if (matches != null) {
try {
@@ -759,6 +798,7 @@ public class JavaBuild {
Base.showWarning("Could not find applet size", message, null);
}
} // else no size() command found
*/
// Grab the Javadoc-style description from the main code.
String description = "";
@@ -847,7 +887,7 @@ public class JavaBuild {
// File openglLibraryFolder =
// new File(editor.getMode().getLibrariesFolder(), "opengl/library");
// String openglLibraryPath = openglLibraryFolder.getAbsolutePath();
boolean openglApplet = false;
// boolean openglApplet = false;
HashMap<String,Object> zipFileContents = new HashMap<String,Object>();
@@ -942,11 +982,6 @@ public class JavaBuild {
// for (File libraryFolder : importedLibraries) {
// System.out.println(libraryFolder + " " + libraryFolder.getAbsolutePath());
// }
// If the renderer is set to the built-in OpenGL library,
// then it's definitely an OpenGL applet.
if (renderer.equals("P3D") || renderer.equals("OPENGL")) {
openglApplet = true;
}
if (is == null) {
if (openglApplet) {
is = mode.getContentStream("applet/template-opengl.html");
@@ -975,11 +1010,11 @@ public class JavaBuild {
}
while ((index = sb.indexOf("@@width@@")) != -1) {
sb.replace(index, index + "@@width@@".length(),
String.valueOf(wide));
String.valueOf(sketchWidth));
}
while ((index = sb.indexOf("@@height@@")) != -1) {
sb.replace(index, index + "@@height@@".length(),
String.valueOf(high));
String.valueOf(sketchHeight));
}
while ((index = sb.indexOf("@@description@@")) != -1) {
sb.replace(index, index + "@@description@@".length(),
@@ -998,58 +1033,6 @@ public class JavaBuild {
}
/**
* Replace all commented portions of a given String as spaces.
* Utility function used here and in the preprocessor.
*/
static public String scrubComments(String what) {
char p[] = what.toCharArray();
int index = 0;
while (index < p.length) {
// for any double slash comments, ignore until the end of the line
if ((p[index] == '/') &&
(index < p.length - 1) &&
(p[index+1] == '/')) {
p[index++] = ' ';
p[index++] = ' ';
while ((index < p.length) &&
(p[index] != '\n')) {
p[index++] = ' ';
}
// check to see if this is the start of a new multiline comment.
// if it is, then make sure it's actually terminated somewhere.
} else if ((p[index] == '/') &&
(index < p.length - 1) &&
(p[index+1] == '*')) {
p[index++] = ' ';
p[index++] = ' ';
boolean endOfRainbow = false;
while (index < p.length - 1) {
if ((p[index] == '*') && (p[index+1] == '/')) {
p[index++] = ' ';
p[index++] = ' ';
endOfRainbow = true;
break;
} else {
// continue blanking this area
p[index++] = ' ';
}
}
if (!endOfRainbow) {
throw new RuntimeException("Missing the */ from the end of a " +
"/* comment */");
}
} else { // any old character, move along
index++;
}
}
return new String(p);
}
/**
* Export to application via GUI.
*/

View File

@@ -24,7 +24,7 @@ import antlr.collections.AST;
* other than System.out, and then call print(), passing the
* AST. Typically, the AST node that you pass would be the root of a
* tree - the ROOT_ID node that represents a Java file.
*
*
* Modified March 2010 to support Java 5 type arguments and for loops by
* @author Jonathan Feinberg &lt;jdf@pobox.com&gt;
*/
@@ -239,10 +239,11 @@ public class PdeEmitter implements PdeTokenTypes {
type = modifiers.getNextSibling();
}
final AST methodName = type.getNextSibling();
if (methodName.getText().equals("main")) {
pdePreprocessor.setFoundMain(true);
}
printChildren(ast);
// if (methodName.getText().equals("main")) {
// pdePreprocessor.setFoundMain(true);
// }
pdePreprocessor.addMethod(methodName.getText());
printChildren(ast);
}
private void printIfThenElse(final AST literalIf) throws SketchException {
@@ -265,10 +266,10 @@ public class PdeEmitter implements PdeTokenTypes {
dumpHiddenBefore(bestPrintableNode);
final CommonHiddenStreamToken hiddenBefore =
((CommonASTWithHiddenTokens) elsePath).getHiddenBefore();
if (elsePath.getType() == PdeTokenTypes.SLIST && elsePath.getNumberOfChildren() == 0 &&
if (elsePath.getType() == PdeTokenTypes.SLIST && elsePath.getNumberOfChildren() == 0 &&
hiddenBefore == null) {
out.print("{");
final CommonHiddenStreamToken hiddenAfter =
final CommonHiddenStreamToken hiddenAfter =
((CommonASTWithHiddenTokens) elsePath).getHiddenAfter();
if (hiddenAfter == null) {
out.print("}");
@@ -742,11 +743,11 @@ public class PdeEmitter implements PdeTokenTypes {
out.print("@");
printChildren(ast);
break;
case ANNOTATION_ARRAY_INIT:
printChildren(ast);
break;
case ANNOTATION_MEMBER_VALUE_PAIR:
print(ast.getFirstChild());
out.print("=");

View File

@@ -4,7 +4,7 @@
PdePreprocessor - wrapper for default ANTLR-generated parser
Part of the Processing project - http://processing.org
Copyright (c) 2004-10 Ben Fry and Casey Reas
Copyright (c) 2004-12 Ben Fry and Casey Reas
Copyright (c) 2001-04 Massachusetts Institute of Technology
ANTLR-generated parser and several supporting classes written
@@ -30,6 +30,8 @@ package processing.mode.java.preproc;
import java.io.*;
import java.util.*;
import java.util.regex.Pattern;
import processing.app.Base;
import processing.app.Preferences;
import processing.app.SketchException;
import processing.core.PApplet;
@@ -146,33 +148,33 @@ public class PdePreprocessor {
private TokenStreamCopyingHiddenTokenFilter filter;
private boolean foundMain;
public void setFoundMain(boolean foundMain) {
this.foundMain = foundMain;
}
public boolean getFoundMain() {
return foundMain;
}
// private boolean foundMain;
private String advClassName = "";
public void setAdvClassName(final String advClassName) {
this.advClassName = advClassName;
}
protected Mode mode;
HashMap<String, Object> foundMethods;
protected String sizeStatement;
protected String sketchWidth;
protected String sketchHeight;
protected String sketchRenderer;
/**
* Regular expression for parsing the size() method. This should match
* against any uses of the size() function, whether numbers or variables
* or whatever. This way, no warning is shown if size() isn't actually used
* in the sketch, which is the case especially for anyone who is cutting
* and pasting from the reference.
*/
public static final String SIZE_REGEX =
"(?:^|\\s|;)size\\s*\\(\\s*([^\\s,]+)\\s*,\\s*([^\\s,\\)]+),?\\s*([^\\)]*)\\s*\\)\\s*\\;";
//"(?:^|\\s|;)size\\s*\\(\\s*(\\S+)\\s*,\\s*([^\\s,\\)]+),?\\s*([^\\)]*)\\s*\\)\\s*\\;";
public void setMode(final Mode mode) {
// System.err.println("Setting mode to " + mode);
this.mode = mode;
}
public PdePreprocessor(final String sketchName) {
this(sketchName, Preferences.getInteger("editor.tabs.size"));
}
public PdePreprocessor(final String sketchName, final int tabSize) {
this.name = sketchName;
final char[] indentChars = new char[tabSize];
@@ -180,14 +182,176 @@ public class PdePreprocessor {
indent = new String(indentChars);
}
public String[] initSketchSize(String code) throws SketchException {
String[] info = parseSketchSize(code, true);
if (info != null) {
sizeStatement = info[0];
sketchWidth = info[1];
sketchHeight = info[2];
sketchRenderer = info[3];
}
return info;
}
/**
* Parse a chunk of code and extract the size() command and its contents.
* @param code Usually the code from the main tab in the sketch
* @param fussy true if it should show an error message if bad size()
* @return null if there was an error, otherwise an array (might contain some/all nulls)
*/
static public String[] parseSketchSize(String code, boolean fussy) {
// This matches against any uses of the size() function, whether numbers
// or variables or whatever. This way, no warning is shown if size() isn't
// actually used in the applet, which is the case especially for anyone
// who is cutting/pasting from the reference.
// String scrubbed = scrubComments(sketch.getCode(0).getProgram());
// String[] matches = PApplet.match(scrubbed, SIZE_REGEX);
String[] matches = PApplet.match(scrubComments(code), SIZE_REGEX);
if (matches != null) {
boolean badSize = false;
if (matches[1].equals("screenWidth") ||
matches[1].equals("screenHeight") ||
matches[2].equals("screenWidth") ||
matches[2].equals("screenHeight")) {
final String message =
"The screenWidth and screenHeight variables\n" +
"are named displayWidth and displayHeight\n" +
"in this release of Processing.";
Base.showWarning("Time for a quick update", message, null);
return null;
}
if (!matches[1].equals("displayWidth") &&
!matches[1].equals("displayHeight") &&
PApplet.parseInt(matches[1], -1) == -1) {
badSize = true;
}
if (!matches[2].equals("displayWidth") &&
!matches[2].equals("displayHeight") &&
PApplet.parseInt(matches[2], -1) == -1) {
badSize = true;
}
if (badSize && fussy) {
// found a reference to size, but it didn't seem to contain numbers
final String message =
"The size of this applet could not automatically\n" +
"be determined from your code. Use only numeric\n" +
"values (not variables) for the size() command.\n" +
"See the size() reference for an explanation.";
Base.showWarning("Could not find sketch size", message, null);
new Exception().printStackTrace(System.out);
return null;
}
// if the renderer entry is empty, set it to null
if (matches[3].trim().length() == 0) {
matches[3] = null;
}
return matches;
}
return new String[] { null, null, null, null }; // not an error, just empty
}
/**
* Replace all commented portions of a given String as spaces.
* Utility function used here and in the preprocessor.
*/
static public String scrubComments(String what) {
char p[] = what.toCharArray();
int index = 0;
while (index < p.length) {
// for any double slash comments, ignore until the end of the line
if ((p[index] == '/') &&
(index < p.length - 1) &&
(p[index+1] == '/')) {
p[index++] = ' ';
p[index++] = ' ';
while ((index < p.length) &&
(p[index] != '\n')) {
p[index++] = ' ';
}
// check to see if this is the start of a new multiline comment.
// if it is, then make sure it's actually terminated somewhere.
} else if ((p[index] == '/') &&
(index < p.length - 1) &&
(p[index+1] == '*')) {
p[index++] = ' ';
p[index++] = ' ';
boolean endOfRainbow = false;
while (index < p.length - 1) {
if ((p[index] == '*') && (p[index+1] == '/')) {
p[index++] = ' ';
p[index++] = ' ';
endOfRainbow = true;
break;
} else {
// continue blanking this area
p[index++] = ' ';
}
}
if (!endOfRainbow) {
throw new RuntimeException("Missing the */ from the end of a " +
"/* comment */");
}
} else { // any old character, move along
index++;
}
}
return new String(p);
}
public void addMethod(String methodName) {
foundMethods.put(methodName, new Object());
}
public boolean hasMethod(String methodName) {
return foundMethods.containsKey(methodName);
}
// public void setFoundMain(boolean foundMain) {
// this.foundMain = foundMain;
// }
// public boolean getFoundMain() {
// return foundMain;
// }
public void setAdvClassName(final String advClassName) {
this.advClassName = advClassName;
}
public void setMode(final Mode mode) {
// System.err.println("Setting mode to " + mode);
this.mode = mode;
}
CommonHiddenStreamToken getHiddenAfter(final CommonHiddenStreamToken t) {
return filter.getHiddenAfter(t);
}
CommonHiddenStreamToken getInitialHiddenToken() {
return filter.getInitialHiddenToken();
}
private static int countNewlines(final String s) {
int count = 0;
for (int pos = s.indexOf('\n', 0); pos >= 0; pos = s.indexOf('\n', pos + 1))
@@ -195,6 +359,7 @@ public class PdePreprocessor {
return count;
}
private static void checkForUnterminatedMultilineComment(final String program)
throws SketchException {
final int length = program.length();
@@ -291,11 +456,13 @@ public class PdePreprocessor {
}
}
public PreprocessorResult write(final Writer out, String program)
throws SketchException, RecognitionException, TokenStreamException {
return write(out, program, null);
}
public PreprocessorResult write(Writer out, String program,
String codeFolderPackages[])
throws SketchException, RecognitionException, TokenStreamException {
@@ -310,9 +477,10 @@ public class PdePreprocessor {
final ArrayList<String> codeFolderImports = new ArrayList<String>();
// need to reset whether or not this has a main()
foundMain = false;
// foundMain = false;
foundMethods = new HashMap<String, Object>();
// bug #5
// http://processing.org/bugs/bugzilla/5.html
if (!program.endsWith("\n"))
program += "\n";
@@ -663,7 +831,17 @@ public class PdePreprocessor {
}
if ((mode == Mode.STATIC) || (mode == Mode.ACTIVE)) {
if (!foundMain) {
if (sketchWidth != null && !hasMethod("sketchWidth")) {
out.println(indent + "public int sketchWidth() { return " + sketchWidth + "; }");
}
if (sketchHeight != null && !hasMethod("sketchHeight")) {
out.println(indent + "public int sketchHeight() { return " + sketchHeight + "; }");
}
if (sketchRenderer != null && !hasMethod("sketchRenderer")) {
out.println(indent + "public String sketchRenderer() { return " + sketchRenderer + "; }");
}
if (!hasMethod("main")) {
out.println(indent + "static public void main(String args[]) {");
out.print(indent + indent + "PApplet.main(new String[] { ");