Class PrintVisitor

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

public class PrintVisitor extends org.antlr.v4.runtime.tree.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. An edit context tells the 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.
  • Method Summary

    Modifier and Type
    Method
    Description
    static String
    printTree(org.antlr.v4.runtime.BufferedTokenStream rootTokenStream, org.antlr.v4.runtime.tree.ParseTree tree)
    Prints the given parse tree that references the given token stream.
    static String
    printTree(org.antlr.v4.runtime.BufferedTokenStream rootTokenStream, org.antlr.v4.runtime.tree.ParseTree tree, EditContext editContext)
    Prints the given parse tree that references the given token stream.
    visitAndJoin(org.antlr.v4.runtime.BufferedTokenStream rootTokenStream, org.antlr.v4.runtime.tree.ParseTree rootNode, org.antlr.v4.runtime.misc.Interval bounds, EditContext editContext)
    This is the lowest level printing method that should be used publicly.
    visitChildren(org.antlr.v4.runtime.tree.RuleNode node)
    The visitor method that the print visitor overrides in order to collect each node's intervals.
    visitTerminal(org.antlr.v4.runtime.tree.TerminalNode node)
     

    Methods inherited from class org.antlr.v4.runtime.tree.AbstractParseTreeVisitor

    aggregateResult, defaultResult, shouldVisitNextChild, visit, visitErrorNode

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • printTree

      public static String printTree(org.antlr.v4.runtime.BufferedTokenStream rootTokenStream, org.antlr.v4.runtime.tree.ParseTree tree)
      Prints the given parse tree that references the given token stream. An edit context is constructed internally. Use this method only if you're not doing any patching.
      Parameters:
      rootTokenStream - The token stream for the parse tree
      tree - The parse tree to print
      Returns:
      The printed parse tree
      See Also:
    • printTree

      public static String printTree(org.antlr.v4.runtime.BufferedTokenStream rootTokenStream, org.antlr.v4.runtime.tree.ParseTree tree, EditContext editContext)
      Prints the given parse tree that references the given token stream. Additionally an edit context must be passed that contains the tokens streams and locations of added or removed nodes. This edit context will have been generated by a transformation such as when the method PhaseCollector.transformTree(io.github.douira.glsl_transformer.GLSLParser.TranslationUnitContext, BufferedTokenStream) is used.
      Parameters:
      rootTokenStream - The token stream for the parse tree
      tree - The parse tree to print
      editContext - The edit context generated by transformations performed on the tree
      Returns:
      The printed parse tree that includes the results of the transformations
    • visitAndJoin

      public String visitAndJoin(org.antlr.v4.runtime.BufferedTokenStream rootTokenStream, org.antlr.v4.runtime.tree.ParseTree rootNode, org.antlr.v4.runtime.misc.Interval bounds, EditContext editContext)
      This is the lowest level printing method that should be used publicly. It takes a parse tree and a token stream and prints it. Additionally an interval is given that determines which tokens of the string should be printed.
      Parameters:
      rootTokenStream - The token stream for the parse tree
      rootNode - The parse tree to print
      bounds - An token index interval of the tokens that should be included in the printed string
      editContext - The edit context generated by transformations
      Returns:
      The printed parse tree that is within the bounds and contains the results of the transformations applied to the tree
      See Also:
    • visitChildren

      public Void visitChildren(org.antlr.v4.runtime.tree.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 org.antlr.v4.runtime.tree.ParseTreeVisitor<Void>
      Overrides:
      visitChildren in class org.antlr.v4.runtime.tree.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(org.antlr.v4.runtime.tree.TerminalNode node)
      Specified by:
      visitTerminal in interface org.antlr.v4.runtime.tree.ParseTreeVisitor<Void>
      Overrides:
      visitTerminal in class org.antlr.v4.runtime.tree.AbstractParseTreeVisitor<Void>