Class PdeParseTreeListener

java.lang.Object
processing.mode.java.preproc.ProcessingBaseListener
processing.mode.java.preproc.PdeParseTreeListener
All Implemented Interfaces:
org.antlr.v4.runtime.tree.ParseTreeListener, ProcessingListener

public class PdeParseTreeListener extends ProcessingBaseListener
ANTLR tree traversal listener that preforms code rewrites as part of sketch preprocessing.

ANTLR tree traversal listener that preforms code rewrites as part of sketch preprocessing, turning sketch source into compilable Java code. Note that this emits both the Java source when using javac directly as part of {JavaBuild} as well as {TextTransform.Edit}s when using the JDT via the {PreprocessingService}.

  • Constructor Details

    • PdeParseTreeListener

      public PdeParseTreeListener(org.antlr.v4.runtime.TokenStream tokens, String newSketchName, int newTabSize, Optional<String> newDestinationPackageName)
      Create a new listener.
      Parameters:
      tokens - The tokens over which to rewrite.
      newSketchName - The name of the sketch being traversed.
      newTabSize - Size of tab / indent.
      newDestinationPackageName - The package to which generated code should be assigned (the package to which the sketch code java file should be assigned).
  • Method Details

    • setCodeFolderImports

      public void setCodeFolderImports(List<String> codeFolderImports)
      Indicate imports for code folders given those imports' fully qualified names.
      Parameters:
      codeFolderImports - List of imports for sources sitting in the sketch code folder. Note that these will be interpreted as non-static imports.
    • setCodeFolderImportInfo

      public void setCodeFolderImportInfo(List<ImportStatement> codeFolderImports)
      Indicate imports for code folders given full import statement information. names.
      Parameters:
      codeFolderImports - List of import statement info for sources sitting in the sketch code folder.
    • setCoreImports

      public void setCoreImports(String[] coreImports)
      Indicate list of imports required for all sketches to be inserted in preprocessing given those imports' fully qualified names.
      Parameters:
      coreImports - The list of imports required for all sketches. Note that these will be interpreted as non-static imports.
    • setCoreImports

      public void setCoreImports(List<String> coreImports)
      Indicate list of imports required for all sketches to be inserted in preprocessing given those imports' fully qualified names.
      Parameters:
      coreImports - The list of imports required for all sketches. Note that these will be interpreted as non-static imports.
    • setCoreImportInfo

      public void setCoreImportInfo(List<ImportStatement> coreImports)
    • setDefaultImports

      public void setDefaultImports(String[] defaultImports)
      Indicate list of default convenience imports.

      Indicate list of imports that are not required for sketch operation but included for the user's convenience regardless.

      Parameters:
      defaultImports - The list of imports to include for user convenience.
    • setDefaultImports

      public void setDefaultImports(List<String> defaultImports)
      Indicate list of default convenience imports.

      Indicate list of imports that are not required for sketch operation but included for the user's convenience regardless given those imports' fully qualified names.

      Parameters:
      defaultImports - The list of imports to include for user convenience. Note that these will be interpreted as non-static imports.
    • setDefaultImportInfo

      public void setDefaultImportInfo(List<ImportStatement> defaultImports)
    • setTesting

      public void setTesting(boolean isTesting)
      Indicate if running in unit tests.
      Parameters:
      isTesting - True if running as part of tests and false otherwise.
    • setTreeErrorListener

      public void setTreeErrorListener(PdeParseTreeListener.PdeParseTreeErrorListener newListener)
      Indicate which listener should be informed of parse tree processing issues.
      Parameters:
      newListener - listener to be informed when an issue is encoutnered in processing the parse tree.
    • foundMain

      public boolean foundMain()
      Determine if the user provided their own "main" method.
      Returns:
      True if the sketch code provides a main method. False otherwise.
    • getOutputProgram

      public String getOutputProgram()
      Get the sketch code transformed to grammatical Java.
      Returns:
      Complete sketch code as Java.
    • getRewriter

      public org.antlr.v4.runtime.TokenStreamRewriter getRewriter()
      Get the rewriter used by this listener.
      Returns:
      Listener's rewriter.
    • getResult

      public PreprocessorResult getResult()
      Get the result of the last preprocessing.
      Returns:
      The result of the last preprocessing.
    • getResult

      public PreprocessorResult getResult(List<PdePreprocessIssue> issues)
      Get the result of the last preprocessing with optional error.
      Parameters:
      issues - The errors (if any) encountered.
      Returns:
      The result of the last preprocessing.
    • exitProcessingSketch

      public void exitProcessingSketch(ProcessingParser.ProcessingSketchContext ctx)
      Endpoint for ANTLR to call when having finished parsing a processing sketch.
      Specified by:
      exitProcessingSketch in interface ProcessingListener
      Overrides:
      exitProcessingSketch in class ProcessingBaseListener
      Parameters:
      ctx - The context from ANTLR for the processing sketch.
    • enterWarnMixedModes

      public void enterWarnMixedModes(ProcessingParser.WarnMixedModesContext ctx)
      Detect if the user is programming with "mixed" modes.

      Detect if the user is programming with "mixed" modes where they are combining active and static mode features. This may be, for example, a method call followed by method definition.

      Specified by:
      enterWarnMixedModes in interface ProcessingListener
      Overrides:
      enterWarnMixedModes in class ProcessingBaseListener
      Parameters:
      ctx - The context from ANTLR for the mixed modes sketch.
    • exitMethodCall

      public void exitMethodCall(ProcessingParser.MethodCallContext ctx)
      Endpoint for ANTLR to call when finished parsing a method invocatino.
      Specified by:
      exitMethodCall in interface ProcessingListener
      Overrides:
      exitMethodCall in class ProcessingBaseListener
      Parameters:
      ctx - The ANTLR context for the method call.
    • exitImportDeclaration

      public void exitImportDeclaration(ProcessingParser.ImportDeclarationContext ctx)
      Endpoint for ANTLR to call when finished parsing an import declaration.

      Endpoint for ANTLR to call when finished parsing an import declaration, remvoing those declarations from sketch body so that they can be included in the header.

      Specified by:
      exitImportDeclaration in interface ProcessingListener
      Overrides:
      exitImportDeclaration in class ProcessingBaseListener
      Parameters:
      ctx - ANTLR context for the import declaration.
    • exitFloatLiteral

      public void exitFloatLiteral(ProcessingParser.FloatLiteralContext ctx)
      Endpoint for ANTLR to call after parsing a decimal point literal.

      Endpoint for ANTLR to call when finished parsing a floating point literal, adding an 'f' at the end to force it float instead of double for API compatability.

      Specified by:
      exitFloatLiteral in interface ProcessingListener
      Overrides:
      exitFloatLiteral in class ProcessingBaseListener
      Parameters:
      ctx - ANTLR context for the literal.
    • exitMultilineStringLiteral

      public void exitMultilineStringLiteral(ProcessingParser.MultilineStringLiteralContext ctx)
      Endpoint for ANTLR to call after parsing a String literal.

      Endpoint for ANTLR to call when finished parsing a string literal, simulating multiline strings if configured to do so.

      Specified by:
      exitMultilineStringLiteral in interface ProcessingListener
      Overrides:
      exitMultilineStringLiteral in class ProcessingBaseListener
      Parameters:
      ctx - ANTLR context for the literal.
    • exitStaticProcessingSketch

      public void exitStaticProcessingSketch(ProcessingParser.StaticProcessingSketchContext ctx)
      Endpoint for ANTLR to call after parsing a static processing sketch.

      Endpoint for ANTLR to call after parsing a static processing sketch, informing this parser that it is operating on a static sketch (no method or class declarations) so that it writes the correct header / footer.

      Specified by:
      exitStaticProcessingSketch in interface ProcessingListener
      Overrides:
      exitStaticProcessingSketch in class ProcessingBaseListener
      Parameters:
      ctx - ANTLR context for the sketch.
    • exitActiveProcessingSketch

      public void exitActiveProcessingSketch(ProcessingParser.ActiveProcessingSketchContext ctx)
      Endpoint for ANTLR to call after parsing a "active" processing sketch.

      Endpoint for ANTLR to call after parsing a "active" processing sketch, informing this parser that it is operating on an active sketch so that it writes the correct header / footer.

      Specified by:
      exitActiveProcessingSketch in interface ProcessingListener
      Overrides:
      exitActiveProcessingSketch in class ProcessingBaseListener
      Parameters:
      ctx - ANTLR context for the sketch.
    • exitMethodDeclaration

      public void exitMethodDeclaration(ProcessingParser.MethodDeclarationContext ctx)
      Endpoint for ANTLR to call after parsing a method declaration.

      Endpoint for ANTLR to call after parsing a method declaration, making any method "public" that has:

      • no other access modifier
      • return type "void"
      • is either in the context of the sketch class
      • is in the context of a class definition that extends PApplet

      Specified by:
      exitMethodDeclaration in interface ProcessingListener
      Overrides:
      exitMethodDeclaration in class ProcessingBaseListener
      Parameters:
      ctx - ANTLR context for the method declaration
    • exitFunctionWithPrimitiveTypeName

      public void exitFunctionWithPrimitiveTypeName(ProcessingParser.FunctionWithPrimitiveTypeNameContext ctx)
      Endpoint for ANTLR to call after parsing a primitive type name.

      Endpoint for ANTLR to call after parsing a primitive type name, possibly converting that type to a parse function as part of the Processing API.

      Specified by:
      exitFunctionWithPrimitiveTypeName in interface ProcessingListener
      Overrides:
      exitFunctionWithPrimitiveTypeName in class ProcessingBaseListener
      Parameters:
      ctx - ANTLR context for the primitive name token.
    • exitColorPrimitiveType

      public void exitColorPrimitiveType(ProcessingParser.ColorPrimitiveTypeContext ctx)
      Endpoint for ANTLR to call after parsing a color primitive token.

      Endpoint for ANTLR to call after parsing a color primitive token, fixing "color type" to be "int" as part of the processing API.

      Specified by:
      exitColorPrimitiveType in interface ProcessingListener
      Overrides:
      exitColorPrimitiveType in class ProcessingBaseListener
      Parameters:
      ctx - ANTLR context for the type token.
    • exitHexColorLiteral

      public void exitHexColorLiteral(ProcessingParser.HexColorLiteralContext ctx)
      Endpoint for ANTLR to call after parsing a hex color literal.
      Specified by:
      exitHexColorLiteral in interface ProcessingListener
      Overrides:
      exitHexColorLiteral in class ProcessingBaseListener
      Parameters:
      ctx - ANTLR context for the literal.
    • handleSizeCall

      protected void handleSizeCall(org.antlr.v4.runtime.ParserRuleContext ctx)
      Manage parsing out a size or fullscreen call.

      The size call will need to be rewritten if it is in global or setup, having it hoisted up to settings body.

      Parameters:
      ctx - The context of the call.
    • handlePixelDensityCall

      protected void handlePixelDensityCall(org.antlr.v4.runtime.ParserRuleContext ctx)
    • handleNoSmoothCall

      protected void handleNoSmoothCall(org.antlr.v4.runtime.ParserRuleContext ctx)
    • handleSmoothCall

      protected void handleSmoothCall(org.antlr.v4.runtime.ParserRuleContext ctx)
    • calledFromGlobalOrSetup

      protected boolean calledFromGlobalOrSetup(org.antlr.v4.runtime.ParserRuleContext callContext)
    • isMethodSetup

      protected boolean isMethodSetup(org.antlr.v4.runtime.ParserRuleContext declaration)
      Determine if a method declaration is for setup.
      Parameters:
      declaration - The method declaration to parse.
      Returns:
      True if setup and false otherwise.
    • isAnnotation

      protected boolean isAnnotation(ProcessingParser.ModifierContext context)
      Check if this contains an annation.
      Parameters:
      context - The modifier context to check.
      Returns:
      True if annotation. False otherwise
    • insertBefore

      protected void insertBefore(org.antlr.v4.runtime.Token location, String text)
      Insert text before a token.
      Parameters:
      location - The token before which code should be added.
      text - The text to add.
    • insertBefore

      protected void insertBefore(int locationToken, int locationOffset, String text)
      Insert text before a location in code.
      Parameters:
      locationToken - Character offset from start.
      locationOffset -
      text - Text to add.
    • insertAfter

      protected void insertAfter(org.antlr.v4.runtime.Token location, String text)
      Insert text after a location in code.
      Parameters:
      location - The token after which to insert code.
      text - The text to insert.
    • delete

      protected void delete(org.antlr.v4.runtime.Token start, org.antlr.v4.runtime.Token stop)
      Delete from a token to a token inclusive.
      Parameters:
      start - First token to delete.
      stop - Last token to delete.
    • delete

      protected void delete(org.antlr.v4.runtime.Token location)
      Delete a single token.
      Parameters:
      location - Token to delete.
    • prepareHeader

      protected RewriteResult prepareHeader(org.antlr.v4.runtime.TokenStreamRewriter headerWriter)
      Prepare preface code to wrap sketch code so that it is contained within a proper Java definition.
      Parameters:
      headerWriter - The writer into which the header should be written.
      Returns:
      Information about the completed rewrite.
    • prepareFooter

      protected RewriteResult prepareFooter(org.antlr.v4.runtime.TokenStreamRewriter footerWriter, int insertPoint)
      Prepare the footer for a sketch (finishes the constructs introduced in header like class def).
      Parameters:
      footerWriter - The writer through which the footer should be introduced.
      insertPoint - The loction at which the footer should be written.
      Returns:
      Information about the completed rewrite.
    • writeHeaderContents

      protected void writeHeaderContents(PdeParseTreeListener.PrintWriterWithEditGen decoratedWriter, RewriteResultBuilder resultBuilder)
      Write the contents of the header using a prebuilt print writer.
      Parameters:
      decoratedWriter - he writer though which the comment should be introduced.
      resultBuilder - Builder for reporting out results to the caller.
    • writeFooterContents

      protected void writeFooterContents(PdeParseTreeListener.PrintWriterWithEditGen decoratedWriter, RewriteResultBuilder resultBuilder)
      Write the contents of the footer using a prebuilt print writer.
      Parameters:
      decoratedWriter - he writer though which the comment should be introduced.
      resultBuilder - Builder for reporting out results to the caller.
    • writePreprocessorComment

      protected void writePreprocessorComment(PdeParseTreeListener.PrintWriterWithEditGen headerWriter, RewriteResultBuilder resultBuilder)
      Comment out sketch code before it is moved elsewhere in resulting Java.
      Parameters:
      headerWriter - The writer though which the comment should be introduced.
      resultBuilder - Builder for reporting out results to the caller.
    • writeImports

      protected void writeImports(PdeParseTreeListener.PrintWriterWithEditGen headerWriter, RewriteResultBuilder resultBuilder)
      Add imports as part of conversion from processing sketch to Java code.
      Parameters:
      headerWriter - The writer though which the imports should be introduced.
      resultBuilder - Builder for reporting out results to the caller.
    • writeImportList

      protected void writeImportList(PdeParseTreeListener.PrintWriterWithEditGen headerWriter, List<ImportStatement> imports, RewriteResultBuilder resultBuilder)
      Write a list of imports.
      Parameters:
      headerWriter - The writer though which the imports should be introduced.
      imports - Collection of imports to introduce.
      resultBuilder - Builder for reporting out results to the caller.
    • writeImportList

      protected void writeImportList(PdeParseTreeListener.PrintWriterWithEditGen headerWriter, ImportStatement[] imports, RewriteResultBuilder resultBuilder)
      Write a list of imports.
      Parameters:
      headerWriter - The writer though which the imports should be introduced.
      imports - Collection of imports to introduce.
      resultBuilder - Builder for reporting out results to the caller.
    • writeClassHeader

      protected void writeClassHeader(PdeParseTreeListener.PrintWriterWithEditGen headerWriter, RewriteResultBuilder resultBuilder)
      Write the prefix which defines the enclosing class for the sketch.
      Parameters:
      headerWriter - The writer through which the header should be introduced.
      resultBuilder - Builder for reporting out results to the caller.
    • writeStaticSketchHeader

      protected void writeStaticSketchHeader(PdeParseTreeListener.PrintWriterWithEditGen headerWriter, RewriteResultBuilder resultBuilder)
      Write the header for a static sketch (no methods).
      Parameters:
      headerWriter - The writer through which the header should be introduced.
      resultBuilder - Builder for reporting out results to the caller.
    • writeStaticSketchFooter

      protected void writeStaticSketchFooter(PdeParseTreeListener.PrintWriterWithEditGen footerWriter, RewriteResultBuilder resultBuilder)
      Write the bottom of the sketch code for static mode.
      Parameters:
      footerWriter - The footer into which the text should be written.
      resultBuilder - Builder for reporting out results to the caller.
    • writeExtraFieldsAndMethods

      protected void writeExtraFieldsAndMethods(PdeParseTreeListener.PrintWriterWithEditGen classBodyWriter, RewriteResultBuilder resultBuilder)
      Write code supporting special functions like size.
      Parameters:
      classBodyWriter - The writer into which the code should be written. Should be for class body.
      resultBuilder - Builder for reporting out results to the caller.
    • writeMain

      protected void writeMain(PdeParseTreeListener.PrintWriterWithEditGen footerWriter, RewriteResultBuilder resultBuilder)
      Write the main method.
      Parameters:
      footerWriter - The writer into which the footer should be written.
      resultBuilder - Builder for reporting out results to the caller.
    • writeClassFooter

      protected void writeClassFooter(PdeParseTreeListener.PrintWriterWithEditGen footerWriter, RewriteResultBuilder resultBuilder)
      Write the end of the class body for the footer.
      Parameters:
      footerWriter - The writer into which the footer should be written.
      resultBuilder - Builder for reporting out results to the caller.
    • createDelete

      protected static TextTransform.Edit createDelete(org.antlr.v4.runtime.Token start, org.antlr.v4.runtime.Token stop, org.antlr.v4.runtime.TokenStreamRewriter rewriter)
      Delete tokens between a start end end token inclusive.
      Parameters:
      start - The token to be deleted.
      stop - The final token to be deleted.
      rewriter - The rewriter with which to make the edit.
      Returns:
      The {TextTransform.Edit} corresponding to this change.
    • createInsertAfter

      protected static TextTransform.Edit createInsertAfter(org.antlr.v4.runtime.Token start, String text, org.antlr.v4.runtime.TokenStreamRewriter rewriter)
      Insert text after a token.
      Parameters:
      start - The token after which the text should be inserted.
      text - The text to insert.
      rewriter - The rewriter with which to make the edit.
      Returns:
      The {TextTransform.Edit} corresponding to this change.
    • createInsertBefore

      protected static TextTransform.Edit createInsertBefore(org.antlr.v4.runtime.Token before, String text, org.antlr.v4.runtime.TokenStreamRewriter rewriter)
      Insert text before a token.
      Parameters:
      before - Token before which the text should be inserted.
      text - The text to insert.
      rewriter - The rewriter with which to make the edit.
      Returns:
      The {TextTransform.Edit} corresponding to this change.
    • createInsertBefore

      protected static TextTransform.Edit createInsertBefore(int before, int beforeOffset, String text, org.antlr.v4.runtime.TokenStreamRewriter rewriter)
      Insert text before a position in code.
      Parameters:
      before - The location before which to insert the text in tokens.
      beforeOffset - THe location before which to insert the text in chars.
      text - The text to insert.
      rewriter - The rewriter with which to make the edit.
      Returns:
      The {TextTransform.Edit} corresponding to this change.
    • insertAfter

      protected static TextTransform.Edit insertAfter(int start, String text, org.antlr.v4.runtime.TokenStreamRewriter rewriter)
      Insert text after a token.
      Parameters:
      start - The position after which the text should be inserted.
      text - The text to insert.
      rewriter - The rewriter with which to make the edit.
      Returns:
      The {TextTransform.Edit} corresponding to this change.
    • createDelete

      protected static TextTransform.Edit createDelete(org.antlr.v4.runtime.Token start, org.antlr.v4.runtime.TokenStreamRewriter rewriter)
      Delete a single token.
      Parameters:
      start - The token to be deleted.
      rewriter - The rewriter with which to make the edit.
      Returns:
      The {TextTransform.Edit} corresponding to this change.