Class TransformationManager<T extends JobParameters>

java.lang.Object
io.github.douira.glsl_transformer.transform.ExecutionPlanner<T>
io.github.douira.glsl_transformer.transform.TransformationManager<T>
Type Parameters:
T - The job parameters type

public class TransformationManager<T extends JobParameters> extends ExecutionPlanner<T>
Implements the execution planner by providing the boilerplate code for setting up an input, a lexer and a parser. The transformation manager is meant to be used to transform many strings and be re-used for many transformation jobs of the same kind. For entirely different jobs a new manager should be created. Common transformations can be shared between them. For printing a tree without transforming it, a manager without and transformations can be used. Creating manager instances isn't costly as ANTLR's parser and lexer instantiation is efficient. The transformation manager also manages job parameters. It has a private field that it stores the job parameters, that are passed to it along with the string to be transformed, during transformation. This means that the entire chain of objects that are involved in the transformation have to be generic in order to be able to make use of job parameters. If no job parameter is used the Void type can be used and object constructors can use the default parameter <> which assigns an Object. This is fine as long as the anonymous class being constructed doesn't need to use the job parameters. If it does need to use them, the whole chain of participating objects needs to be properly parameterized. (transformation manager -> transformation -> transformation phase)
  • Field Details

    • INTERNAL

      public static final TransformationManager<NonFixedJobParameters> INTERNAL
      An internal instance of this class that is used by other library-internal classes for parsing needs.
    • input

      protected IntStream input
      The last parsed input stream. This property can be used together with the parse methods since they don't give direct access to the internally created input stream and token stream.
    • tokenStream

      protected BufferedTokenStream tokenStream
      The last parsed tokens stream.
      See Also:
  • Constructor Details

    • TransformationManager

      public TransformationManager(Transformation<T> rootTransformation, boolean throwParseErrors)
      Creates a new transformation manager with a given root transformation and parse error throwing behavior.
      Parameters:
      rootTransformation - The root transformation to use
      throwParseErrors - If parse errors should be thrown
    • TransformationManager

      public TransformationManager(boolean throwParseErrors)
      Creates a new transformation manager and specifies if parse errors should be thrown during parsing. If they should not be thrown they will not be reported or printed to the console. ANTLR will attempt to recover from errors during parsing any construct a parse tree containing error nodes. These nodes can mess up transformation and printing. Do not expect anything to function properly if an error was encountered during parsing. Custom error handlers can be registered on the parser and lexer manually. For example, an error handler similar to ConsoleErrorListener that allows recovery and only collects the errors instead of printing them could be created.
      Parameters:
      throwParseErrors - If true, the transformation manager throw any parse errors encountered during parsing
    • TransformationManager

      public TransformationManager(Transformation<T> rootTransformation)
      Creates a new transformation manager with a given root transformation that throws parse errors by default.
      Parameters:
      rootTransformation - The root transformation to use
    • TransformationManager

      public TransformationManager()
      Creates a new transformation manager that throws parse errors by default.
  • Method Details

    • getParser

      public io.github.douira.glsl_transformer.GLSLParser getParser()
      The returned parser (and lexer) may contain no token stream or a wrong token stream. However, the parser should not be used for parsing manually anyway. The state and contents of the parser are set up correctly when the transformation is performed. Returns this execution planner's parser. How the parser is stored is up to the implementing class.
      Specified by:
      getParser in class ExecutionPlanner<T extends JobParameters>
      Returns:
      The parser
    • getLexer

      public io.github.douira.glsl_transformer.GLSLLexer getLexer()
      Description copied from class: ExecutionPlanner
      Returns the execution planner's lexer.
      Specified by:
      getLexer in class ExecutionPlanner<T extends JobParameters>
      Returns:
      The lexer
    • setPrintTokenFilter

      public void setPrintTokenFilter(TokenFilter<T> printTokenFilter)
      Sets the token filter to use before printing.
      Parameters:
      printTokenFilter - The new print token filter
    • setParseTokenFilter

      public void setParseTokenFilter(TokenFilter<T> parseTokenFilter)
      Sets the token filter to use before parsing. It's placed between the lexer and the token stream.
      Parameters:
      parseTokenFilter - The new parse token filter
    • parse

      public <RuleType extends ExtendedContext> RuleType parse(String str, Function<io.github.douira.glsl_transformer.GLSLParser,RuleType> parseMethod)
      Parses a string using a parser method reference into a parse tree.
      Type Parameters:
      RuleType - The type of the resulting parsed node
      Parameters:
      str - The string to parse
      parseMethod - The parser method reference to use for parsing
      Returns:
      The parsed string as a parse tree that has the given type
    • parse

      public <RuleType extends ExtendedContext> RuleType parse(String str, ExtendedContext parent, Function<io.github.douira.glsl_transformer.GLSLParser,RuleType> parseMethod)
      Parses a string using a parser method reference into a parse tree.
      Type Parameters:
      RuleType - The type of the resulting parsed node
      Parameters:
      str - The string to parse
      parent - The parent to attach to the parsed node
      parseMethod - The parser method reference to use for parsing
      Returns:
      The parsed string as a parse tree that has the given type
    • parse

      public <RuleType extends ExtendedContext> RuleType parse(IntStream stream, ExtendedContext parent, Function<io.github.douira.glsl_transformer.GLSLParser,RuleType> parseMethod)
      Parses an int stream (which is similar to a string) using a parser method reference into a parse tree. This method exists so non-string streams can also be parsed.
      Type Parameters:
      RuleType - The type of the resulting parsed node
      Parameters:
      stream - The int stream to parse
      parent - The parent to attach to the parsed node
      parseMethod - The parser method reference to use for parsing
      Returns:
      The parsed string as a parse tree that has the given type
    • transform

      public String transform(String str) throws RecognitionException
      Transforms the given string by parsing, transforming it with the already registered transformations and then re-printing it. The job parameters are set to null since none are passed.
      Parameters:
      str - The string to be transformed
      Returns:
      The transformed string
      Throws:
      RecognitionException
    • transform

      public String transform(String str, T parameters) throws RecognitionException
      Transforms the given string by parsing, transforming it with the already registered transformations and then re-printing it. The job parameters are set to null since none are passed.
      Parameters:
      str - The string to be transformed
      parameters - The job parameters
      Returns:
      The transformed string
      Throws:
      RecognitionException
    • transformStream

      public String transformStream(IntStream stream) throws RecognitionException
      Transforms a given input stream and re-prints it as a string.
      Parameters:
      stream - The input stream to be transformed
      Returns:
      The transformed string
      Throws:
      RecognitionException
      See Also:
    • transformStream

      public String transformStream(IntStream stream, T parameters) throws RecognitionException
      Transforms a given input stream and re-prints it as a string. This is useful if the input isn't a string yet. Then ANTLR's CharStreams can be used to generate a stream, not necessarily from a string. This is the entry point for job parameters that can be accessed by transformation phases during transformation. It's called "job" parameters because it may change for each transformation job. A transformation job is a single instance of parsing and transformation. There is only one job parameter reference. If multiple values are required, they have to be packed into an object or an array. (This is a good place to use a Record)
      Parameters:
      stream - The input stream to be transformed
      parameters - The job parameters
      Returns:
      The transformed string
      Throws:
      RecognitionException