java.lang.Object
io.github.douira.glsl_transformer.GLSLParserBaseListener
io.github.douira.glsl_transformer.transform.RunPhase
All Implemented Interfaces:
GLSLParserListener, org.antlr.v4.runtime.tree.ParseTreeListener

public abstract class RunPhase extends GLSLParserBaseListener
A run phase simply executes one method when it is executed in a level by the phase collector. Even though it extends Phase, no listener methods on it are executed.
  • Constructor Details

    • RunPhase

      public RunPhase()
  • Method Details

    • run

      protected abstract void run(GLSLParser.TranslationUnitContext ctx)
    • getEditContext

      protected EditContext getEditContext()
    • getParser

      protected org.antlr.v4.runtime.Parser getParser()
    • getSiblings

      protected static List<org.antlr.v4.runtime.tree.ParseTree> getSiblings(org.antlr.v4.runtime.ParserRuleContext node)
      Gets the sibling nodes of a given node. It looks up the parent and then returns the parent's children.
      Parameters:
      node - The node to get the siblings for
      Returns:
      The siblings of the given node. null if the node has no parent.
    • replaceNode

      protected void replaceNode(org.antlr.v4.runtime.ParserRuleContext node, String newContents, Function<GLSLParser,org.antlr.v4.runtime.ParserRuleContext> parseMethod)
      Replaces the given node in its parent with a new node generated by parsing the given string with the given method of the parser. See createLocalRoot(String, RuleContext, Function) for details of creating parsed nodes.
      Parameters:
      node - The node to be replaced
      newContents - The string from which a new node is generated
      parseMethod - The method with which the string will be parsed
    • removeNode

      protected void removeNode(org.antlr.v4.runtime.ParserRuleContext node)
      Removes the given node from its parent's child list.
      Parameters:
      node - The node to remove
      Implementation Note:
      The empty space is filled in with an empty terminal node in order to not mess up the walker's iteration.
    • compilePath

      protected org.antlr.v4.runtime.tree.xpath.XPath compilePath(String xpath)
    • compilePattern

      protected org.antlr.v4.runtime.tree.pattern.ParseTreePattern compilePattern(String pattern, int rootRule)
    • findAndMatch

      public List<org.antlr.v4.runtime.tree.pattern.ParseTreeMatch> findAndMatch(org.antlr.v4.runtime.tree.ParseTree tree, org.antlr.v4.runtime.tree.xpath.XPath xpath, org.antlr.v4.runtime.tree.pattern.ParseTreePattern pattern)
      This method uses a statically constructed xpath so it doesn't need to be repeatedly constructed. The subtrees yielded by the xpath need to start with the rule that the pattern was constructed with or nothing will match. Adapted from ANTLR's implementation of ParseTreePattern.findAll(ParseTree, String).
      Parameters:
      tree - The parse tree to find and match in
      xpath - The xpath that leads to a subtree for matching
      pattern - The pattern that tests the subtrees for matches
      Returns:
      A list of all matches resulting from the subtrees
    • isActive

      protected boolean isActive()
      Overwrite this method to add a check of if this phase should be run at all. Especially for WalkPhase this is important since it reduces the number of listeners that need to be processed.
      Returns:
      If the phase should run. true by default.
    • init

      protected void init()
      This method is called right after this phase is collected by the phase collector. It can be used to compile xpaths and pattern matchers.
    • createLocalRoot

      public org.antlr.v4.runtime.ParserRuleContext createLocalRoot(String str, org.antlr.v4.runtime.RuleContext parent, Function<GLSLParser,org.antlr.v4.runtime.ParserRuleContext> parseMethod)
      Parses the given string using the given parser method. Since the parser doesn't know which part of the parse tree any string would be part of, we need to tell it. In many cases multiple methods would produce a correct result. However, this can lead to a truncated parse tree when the resulting node is inserted into a bigger parse tree. The parsing method should be chosen such that when the resulting node is inserted into a parse tree, the tree has the same structure as if it had been parsed as one piece. For example, the code fragment foo() could be parsed as a functionCall, a primaryExpression, an expression or other enclosing parse rules. If it's inserted into an expression, it should be parsed as an expression so that this rule isn't missing from the parse tree. Using the wrong parse method often doesn't matter but it can cause tree matchers to not find the node if they are, for example, looking for an expression specifically. All nodes inserted into the parse tree must have properly configured parent references or looking up a node's local root won't work. Other things in ANTLR may also break if non-root nodes are missing their parent references.
      Parameters:
      str - The string to be parsed
      parent - The parent to be set on the node. All nodes will eventually end up in the a main tree so some parent will be available. Getting the siblings of the new node will not work if no parent is set.
      parseMethod - The parser method with which the string is parsed
      Returns:
      The resulting parsed node