Class PrintVisitor

java.lang.Object
org.antlr.v4.runtime.tree.AbstractParseTreeVisitor<Void>
io.github.douira.glsl_transformer.print.PrintVisitor
All Implemented Interfaces:
ParseTreeVisitor<Void>

public class PrintVisitor extends AbstractParseTreeVisitor<Void>
The print visitor visits the parse tree and reprints it while preserving the position and content of hidden tokens. This means it preserves all newlines even if the parsed nodes that contains them was removed. This is useful because it doesn't necessarily change the line numbers during patching. Local roots in the parse tree tell print visitor what changes have been made to the original parse tree and from which alternative token streams it has to read when it prints newly inserted nodes (=local roots) and their subtrees.
  • Method Details

    • printTree

      public static String printTree(BufferedTokenStream rootTokenStream, ExtendedContext tree, TokenFilter<?> tokenFilter)
      Prints the given parse tree that references the given token stream. Sets up the given node as a root node if that hasn't happened already. If the given token filter is not null, it will be used to check if each otherwise qualified token should be printed.
      Parameters:
      rootTokenStream - The token stream for the parse tree
      tree - The parse tree to print
      tokenFilter - An additional token filter to check before printing each otherwise qualified token
      Returns:
      The printed parse tree that includes the results of the transformations
    • printTree

      public static String printTree(BufferedTokenStream rootTokenStream, ExtendedContext tree)
      Prints the given parse tree that references the given token stream. Sets up the given node as a root node if that hasn't happened already.
      Parameters:
      rootTokenStream - The token stream for the parse tree
      tree - The parse tree to print
      Returns:
      The printed parse tree that includes the results of the transformations
      See Also:
    • visitChildren

      public Void visitChildren(RuleNode node)
      The visitor method that the print visitor overrides in order to collect each node's intervals. The current local root is updated when a local root node is found. Any created intervals for printing are attributed to the current local root in order to know which token stream the interval's indexes refer to.
      Specified by:
      visitChildren in interface ParseTreeVisitor<Void>
      Overrides:
      visitChildren in class AbstractParseTreeVisitor<Void>
      Implementation Note:
      intervals that cover tokens that are not part of children (which are only hidden tokens like whitespace because terminal nodes are children too) are moved after inserted nodes, which are local roots, through this process. this happens because fetchNext is not updated for local roots which causes the next non-local-root child (or at the after end of the child list) to add the whole interval covering the child after the child itself has been visited.
    • visitTerminal

      public Void visitTerminal(TerminalNode node)
      Specified by:
      visitTerminal in interface ParseTreeVisitor<Void>
      Overrides:
      visitTerminal in class AbstractParseTreeVisitor<Void>