Class PhaseCollector

java.lang.Object
io.github.douira.glsl_transformer.transform.PhaseCollector

public class PhaseCollector extends Object
The phase collector holds the registered transformations and manages their execution. After creating an instance, transformations should be registered with it. During registration the phase collector organizes the phases each transformation contributes. In order to reduce the number of times the tree has to be walked, it processes all walk phases of each level at the same time. A level of phases consists of all phases that were added to their transformation at the same index.
  • Field Details

  • Constructor Details

    • PhaseCollector

      public PhaseCollector(org.antlr.v4.runtime.Parser parser)
      Creates a new phase collector for a parser.
      Parameters:
      parser - The parser to create this phase collector for
  • Method Details

    • registerTransformation

      public void registerTransformation(Transformation transformation)
      Registers a single transformation with this phase collector. When the phase collector transforms a tree, the phases contributed by this transformation will be run.
      Parameters:
      transformation - The transformation to collect the phases from
    • registerTransformationMultiple

      public void registerTransformationMultiple(Consumer<PhaseCollector> groupRegisterer)
      Registers multiple transformations by calling a function that consumes a phase collector. This can be used together with transformation groups by having them register many transformations with a phase collector.
      Parameters:
      groupRegisterer - The function that registers transformations on the phase collector it is given
      See Also:
    • getParser

      public org.antlr.v4.runtime.Parser getParser()
      Returns this phase collector's parser
      Returns:
      The parser
    • addPhaseAt

      protected void addPhaseAt(io.github.douira.glsl_transformer.transform.Phase phase, int index)
      Collects a phase of a transformation and inserts it at a given level index. During transformations the phases of each level are executed in the order they were added.
      Parameters:
      phase - The phase to collect into the specified level
      index - The level this phase should be added to
    • transformTree

      public EditContext transformTree(GLSLParser.TranslationUnitContext ctx, org.antlr.v4.runtime.BufferedTokenStream tokenStream)
      Transforms the given parse tree with the registered transformations. The generated edit context has to be passed along to the printer in order for the applied transformations to be printed correctly.
      Parameters:
      ctx - The root node of the parse tree to be transformed
      tokenStream - The token stream of the parse tree
      Returns:
      An edit context containing the changes made by the applied transformations
      See Also:
    • transformTree

      public void transformTree(GLSLParser.TranslationUnitContext ctx, EditContext editContext)
      Transforms the given parse tree with the registered transformations recording the changes to the given edit context. The edit context is sealed after transformation and cannot be used in another transformation since all transformations should be performed using a single phase collector.
      Parameters:
      ctx - The root node of the parse tree to be transformed
      editContext - The edit context to which the transformation's changes should be recorded