diff --git a/app/grammars/pde.g b/app/grammars/pde.g index b1c9997db..2ef7277c3 100644 --- a/app/grammars/pde.g +++ b/app/grammars/pde.g @@ -23,15 +23,33 @@ tokens { CONSTRUCTOR_CAST; EMPTY_FIELD; } +{ + // this clause copied from java15.g! ANTLR does not copy this + // section from the super grammar. + /** + * Counts the number of LT seen in the typeArguments production. + * It is used in semantic predicates to ensure we have seen + * enough closing '>' characters; which actually may have been + * either GT, SR or BSR tokens. + */ + private int ltCounter = 0; + + private PdePreprocessor pp; + public PdeRecognizer(final PdePreprocessor pp, final TokenStream ts) { + this(ts); + this.pp = pp; + } +} + pdeProgram // only java mode programs will have their own public classes or // imports (and they must have at least one) : ( "public" "class" | "import" ) => javaProgram - { PdePreprocessor.setProgramType(PdePreprocessor.ProgramType.JAVA); } + { pp.setProgramType(PdePreprocessor.ProgramType.JAVA); } | ((statement)*) => staticProgram - { PdePreprocessor.setProgramType(PdePreprocessor.ProgramType.STATIC); } + { pp.setProgramType(PdePreprocessor.ProgramType.STATIC); } | activeProgram - { PdePreprocessor.setProgramType(PdePreprocessor.ProgramType.ACTIVE); } + { pp.setProgramType(PdePreprocessor.ProgramType.ACTIVE); } ; @@ -226,7 +244,7 @@ classDefinition![AST modifiers] cb:classBlock {#classDefinition = #(#[CLASS_DEF,"CLASS_DEF"], modifiers,i,tp,sc,ic,cb); - PdePreprocessor.advClassName = i.getText();} + pp.setAdvClassName(i.getText());} ; possiblyEmptyField diff --git a/app/src/processing/app/Sketch.java b/app/src/processing/app/Sketch.java index 75235d9cd..1a9548a6a 100644 --- a/app/src/processing/app/Sketch.java +++ b/app/src/processing/app/Sketch.java @@ -47,6 +47,7 @@ public class Sketch { static private File tempBuildFolder; private Editor editor; + private boolean foundMain = false; /** main pde file for this sketch. */ private File primaryFile; @@ -1444,9 +1445,13 @@ public class Sketch { sc.addPreprocOffset(headerOffset); } } + foundMain = preprocessor.getFoundMain(); return primaryClassName; } + public boolean getFoundMain() { + return foundMain; + } public ArrayList getImportedLibraries() { return importedLibraries; diff --git a/app/src/processing/app/debug/Runner.java b/app/src/processing/app/debug/Runner.java index b2bd084a1..519099678 100644 --- a/app/src/processing/app/debug/Runner.java +++ b/app/src/processing/app/debug/Runner.java @@ -24,7 +24,6 @@ package processing.app.debug; import processing.app.*; -import processing.app.preproc.PdePreprocessor; import processing.core.*; import java.awt.Point; @@ -201,9 +200,8 @@ public class Runner implements MessageConsumer { // It's dangerous to add your own main() to your code, // but if you've done it, we'll respect your right to hang yourself. // http://dev.processing.org/bugs/show_bug.cgi?id=1446 - if (PdePreprocessor.foundMain) { + if (sketch.getFoundMain()) { params.add(appletClassName); - } else { params.add("processing.core.PApplet"); diff --git a/app/src/processing/app/preproc/PdeEmitter.java b/app/src/processing/app/preproc/PdeEmitter.java index 02bea9aef..060d20668 100644 --- a/app/src/processing/app/preproc/PdeEmitter.java +++ b/app/src/processing/app/preproc/PdeEmitter.java @@ -4,9 +4,11 @@ package processing.app.preproc; import java.io.PrintStream; import java.util.BitSet; +import java.util.Stack; import processing.app.Preferences; import processing.app.antlr.PdeTokenTypes; import processing.app.debug.RunnerException; +import antlr.CommonHiddenStreamToken; import antlr.collections.AST; /* Based on original code copyright (c) 2003 Andy Tripp . @@ -26,17 +28,16 @@ import antlr.collections.AST; @SuppressWarnings("unused") public class PdeEmitter implements PdeTokenTypes { - private PrintStream out = System.out; + private final PdePreprocessor pdePreprocessor; + private final PrintStream out; private final PrintStream debug = System.err; - //private static int ALL = -1; - private final java.util.Stack stack = new java.util.Stack(); + + private final Stack stack = new Stack(); private final static int ROOT_ID = 0; - /** - * Specify a PrintStream to print to. System.out is the default. - * @param out the PrintStream to print to - */ - public void setOut(final PrintStream out) { + + public PdeEmitter(final PdePreprocessor pdePreprocessor, final PrintStream out) { + this.pdePreprocessor = pdePreprocessor; this.out = out; } @@ -98,8 +99,8 @@ public class PdeEmitter implements PdeTokenTypes { /** * Dump the list of hidden tokens linked to from the token passed in. */ - private void dumpHiddenTokens(antlr.CommonHiddenStreamToken t) { - for (; t != null; t = PdePreprocessor.filter.getHiddenAfter(t)) { + private void dumpHiddenTokens(CommonHiddenStreamToken t) { + for (; t != null; t = pdePreprocessor.getHiddenAfter(t)) { out.print(t.getText()); } } @@ -281,7 +282,7 @@ public class PdeEmitter implements PdeTokenTypes { // imports // class definition case ROOT_ID: - dumpHiddenTokens(PdePreprocessor.filter.getInitialHiddenToken()); + dumpHiddenTokens(pdePreprocessor.getInitialHiddenToken()); printChildren(ast); break; @@ -385,7 +386,7 @@ public class PdeEmitter implements PdeTokenTypes { final String methodName = methodNameChild.getText(); if (methodName.equals("main")) { - PdePreprocessor.foundMain = true; + pdePreprocessor.setFoundMain(true); } // if this method doesn't have a specifier, make it public diff --git a/app/src/processing/app/preproc/PdePreprocessor.java b/app/src/processing/app/preproc/PdePreprocessor.java index 7e0afb372..7054c0c40 100644 --- a/app/src/processing/app/preproc/PdePreprocessor.java +++ b/app/src/processing/app/preproc/PdePreprocessor.java @@ -125,6 +125,10 @@ import antlr.collections.*; *

*/ public class PdePreprocessor { + + // used for calling the ASTFactory to get the root node + private static final int ROOT_ID = 0; + // these ones have the .* at the end, since a class name might be at the end // instead of .* which would make trouble other classes using this can lop @@ -139,47 +143,51 @@ public class PdePreprocessor { STATIC, ACTIVE, JAVA } - // static to make it easier for the antlr preproc to get at it - static private ProgramType programType; - static public boolean foundMain; - - static public void setProgramType(final ProgramType programType) { - // System.err.println("Setting program type to " + programType); - PdePreprocessor.programType = programType; - } - - String indent; - - PrintStream stream; - Reader programReader; - String buildPath; + private String indent; + private PrintStream stream; + private Reader programReader; // starts as sketch name, ends as main class name - String name; + private String name; - // used for calling the ASTFactory to get the root node - private static final int ROOT_ID = 0; - /** - * Used by PdeEmitter.dumpHiddenTokens() - */ - public static TokenStreamCopyingHiddenTokenFilter filter; + private TokenStreamCopyingHiddenTokenFilter filter; - public static String advClassName = ""; - - /** - * Setup a new preprocessor. - */ + private boolean foundMain; + public void setFoundMain(boolean foundMain) { + this.foundMain = foundMain; + } + public boolean getFoundMain() { + return foundMain; + } + + private String advClassName = ""; + public void setAdvClassName(final String advClassName) { + this.advClassName = advClassName; + } + + private ProgramType programType; + public void setProgramType(final ProgramType programType) { + // System.err.println("Setting program type to " + programType); + this.programType = programType; + } + public PdePreprocessor() { - int tabSize = Preferences.getInteger("editor.tabs.size"); - char[] indentChars = new char[tabSize]; + char[] indentChars = new char[Preferences.getInteger("editor.tabs.size")]; Arrays.fill(indentChars, ' '); indent = new String(indentChars); } + CommonHiddenStreamToken getHiddenAfter(final CommonHiddenStreamToken t) { + return filter.getHiddenAfter(t); + } + + CommonHiddenStreamToken getInitialHiddenToken() { + return filter.getInitialHiddenToken(); + } + public int writePrefix(String program, String buildPath, String sketchName, String codeFolderPackages[]) throws FileNotFoundException { - this.buildPath = buildPath; this.name = sketchName; // need to reset whether or not this has a main() @@ -339,7 +347,7 @@ public class PdePreprocessor { // create a parser and set what sort of AST should be generated // - PdeRecognizer parser = new PdeRecognizer(filter); + PdeRecognizer parser = new PdeRecognizer(this, filter); // use our extended AST class // @@ -376,12 +384,8 @@ public class PdePreprocessor { return null; // output the code - // - PdeEmitter emitter = new PdeEmitter(); - //writeHeader(stream, extraImports, name); + PdeEmitter emitter = new PdeEmitter(this, stream); writeDeclaration(stream, name); - - emitter.setOut(stream); emitter.print(rootNode); // debugAST(rootNode, true); @@ -484,7 +488,7 @@ public class PdePreprocessor { if ((programType == ProgramType.STATIC) || (programType == ProgramType.ACTIVE)) { - if (!PdePreprocessor.foundMain) { + if (!foundMain) { out.println(indent + "static public void main(String args[]) {"); out.print(indent + indent + "PApplet.main(new String[] { "); @@ -593,7 +597,7 @@ public class PdePreprocessor { private String debugHiddenTokens(antlr.CommonHiddenStreamToken t) { final StringBuilder sb = new StringBuilder(); - for (; t != null; t = PdePreprocessor.filter.getHiddenAfter(t)) { + for (; t != null; t = filter.getHiddenAfter(t)) { if (sb.length() == 0) sb.append("["); sb.append(t.getText().replace("\n", "\\n"));