Call a mode a mode, and not a programType.

Also, further dancing around with pde.g heuristics for detecting mode.
This commit is contained in:
jdf
2010-03-18 15:26:55 +00:00
parent c106742938
commit 8b7cfbf889
3 changed files with 32 additions and 16 deletions
+16 -3
View File
@@ -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
@@ -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[] { ");
@@ -14,8 +14,10 @@ public class PreprocessResult {
public final int headerOffset;
public final String className;
public final List<String> extraImports;
public final PdePreprocessor.Mode programType;
public PreprocessResult(int headerOffset, String className,
public PreprocessResult(PdePreprocessor.Mode programType,
int headerOffset, String className,
final List<String> extraImports)
throws RunnerException {
if (className == null)
@@ -24,6 +26,7 @@ public class PreprocessResult {
this.className = className;
this.extraImports = Collections
.unmodifiableList(new ArrayList<String>(extraImports));
this.programType = programType;
}
}