From 8b7cfbf8897c7b118bc4f8bc13b461e90db18f43 Mon Sep 17 00:00:00 2001 From: jdf Date: Thu, 18 Mar 2010 15:26:55 +0000 Subject: [PATCH] Call a mode a mode, and not a programType. Also, further dancing around with pde.g heuristics for detecting mode. --- app/grammars/pde.g | 19 ++++++++++++--- .../app/preproc/PdePreprocessor.java | 24 +++++++++---------- .../app/preproc/PreprocessResult.java | 5 +++- 3 files changed, 32 insertions(+), 16 deletions(-) diff --git a/app/grammars/pde.g b/app/grammars/pde.g index 2946635b9..f728fc9b6 100644 --- a/app/grammars/pde.g +++ b/app/grammars/pde.g @@ -18,6 +18,7 @@ options { // in the editor. defaultErrorHandler = false; //true; + // permit mode-detection heuristics, below k=3; } @@ -52,7 +53,7 @@ pdeProgram // only java mode programs will have their own public classes or // imports (and they must have at least one) : ( "public" "class" | "import" ) => javaProgram - { pp.setProgramType(PdePreprocessor.ProgramType.JAVA); } + { pp.setMode(PdePreprocessor.Mode.JAVA); } // the syntactic predicate here looks for any minimal (thus // the non-greedy qualifier) number of fields, followed by @@ -67,10 +68,22 @@ pdeProgram // of that in practice. Please report such a case! | ( (options{greedy=false;}: possiblyEmptyField)* typeSpec[false] IDENT LPAREN ) => activeProgram - { pp.setProgramType(PdePreprocessor.ProgramType.ACTIVE); } + { pp.setMode(PdePreprocessor.Mode.ACTIVE); } + // Some programs can be equally well interpreted as STATIC or ACTIVE; + // this forces the parser to prefer the STATIC interpretation. + | (staticProgram) => staticProgram + { pp.setMode(PdePreprocessor.Mode.STATIC); } + + // Some correct ACTIVE programs (using lots of modifiers and/or + // type arguments) are not caught by the heuristic predicate above, + // so we at least catch those here so that the STATIC catch-all, below, + // won't choke on it. + | (activeProgram) => activeProgram + { pp.setMode(PdePreprocessor.Mode.ACTIVE); } + | staticProgram - { pp.setProgramType(PdePreprocessor.ProgramType.STATIC); } + { pp.setMode(PdePreprocessor.Mode.STATIC); } ; // advanced mode is really just a normal java file diff --git a/app/src/processing/app/preproc/PdePreprocessor.java b/app/src/processing/app/preproc/PdePreprocessor.java index bc5fc4189..3089e9fdc 100644 --- a/app/src/processing/app/preproc/PdePreprocessor.java +++ b/app/src/processing/app/preproc/PdePreprocessor.java @@ -153,7 +153,7 @@ public class PdePreprocessor implements PdeTokenTypes { private final String indent; private final String name; - public static enum ProgramType { + public static enum Mode { STATIC, ACTIVE, JAVA } @@ -175,11 +175,11 @@ public class PdePreprocessor implements PdeTokenTypes { this.advClassName = advClassName; } - private ProgramType programType; + private Mode mode; - public void setProgramType(final ProgramType programType) { + public void setMode(final Mode mode) { // System.err.println("Setting program type to " + programType); - this.programType = programType; + this.mode = mode; } public PdePreprocessor(final String sketchName) { @@ -326,7 +326,7 @@ public class PdePreprocessor implements PdeTokenTypes { final PrintWriter stream = new PrintWriter(out); final int headerOffset = writeImports(stream, programImports, codeFolderImports); - return new PreprocessResult(headerOffset + 2, write(program, stream), + return new PreprocessResult(mode, headerOffset + 2, write(program, stream), programImports); } @@ -438,7 +438,7 @@ public class PdePreprocessor implements PdeTokenTypes { CommonAST.setVerboseStringConversion(true, parser.getTokenNames()); final String className; - if (programType == ProgramType.JAVA) { + if (mode == Mode.JAVA) { // if this is an advanced program, the classname is already defined. className = getFirstClassName(parserAST); } else { @@ -570,17 +570,17 @@ public class PdePreprocessor implements PdeTokenTypes { * @param className Name of the class being created. */ protected void writeDeclaration(PrintWriter out, String className) { - if (programType == ProgramType.JAVA) { + if (mode == Mode.JAVA) { // Print two blank lines so that the offset doesn't change out.println(); out.println(); - } else if (programType == ProgramType.ACTIVE) { + } else if (mode == Mode.ACTIVE) { // Print an extra blank line so the offset is identical to the others out.println("public class " + className + " extends PApplet {"); out.println(); - } else if (programType == ProgramType.STATIC) { + } else if (mode == Mode.STATIC) { out.println("public class " + className + " extends PApplet {"); out.println(indent + "public void setup() {"); } @@ -592,14 +592,14 @@ public class PdePreprocessor implements PdeTokenTypes { * @param out PrintStream to write it to. */ protected void writeFooter(PrintWriter out, String className) { - if (programType == ProgramType.STATIC) { + if (mode == Mode.STATIC) { // close off draw() definition out.println(indent + "noLoop();"); out.println("} "); } - if ((programType == ProgramType.STATIC) - || (programType == ProgramType.ACTIVE)) { + if ((mode == Mode.STATIC) + || (mode == Mode.ACTIVE)) { if (!foundMain) { out.println(indent + "static public void main(String args[]) {"); out.print(indent + indent + "PApplet.main(new String[] { "); diff --git a/app/src/processing/app/preproc/PreprocessResult.java b/app/src/processing/app/preproc/PreprocessResult.java index becfd9f46..f7a52e8b8 100644 --- a/app/src/processing/app/preproc/PreprocessResult.java +++ b/app/src/processing/app/preproc/PreprocessResult.java @@ -14,8 +14,10 @@ public class PreprocessResult { public final int headerOffset; public final String className; public final List extraImports; + public final PdePreprocessor.Mode programType; - public PreprocessResult(int headerOffset, String className, + public PreprocessResult(PdePreprocessor.Mode programType, + int headerOffset, String className, final List extraImports) throws RunnerException { if (className == null) @@ -24,6 +26,7 @@ public class PreprocessResult { this.className = className; this.extraImports = Collections .unmodifiableList(new ArrayList(extraImports)); + this.programType = programType; } }