Class TransformationManager

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

public class TransformationManager extends PhaseCollector
Implements the phase collector 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.
  • Field Details

    • INTERNAL

      public static final TransformationManager 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(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()
      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 anyways. The state and contents of the parser are setup correctly when the transformation is performed. Returns this phase collector's parser. How the parser is stored is up to the implementing class.
      Specified by:
      getParser in class PhaseCollector
      Returns:
      The parser
    • getLexer

      public io.github.douira.glsl_transformer.GLSLLexer getLexer()
      Description copied from class: PhaseCollector
      Returns the phase collector's lexer.
      Specified by:
      getLexer in class PhaseCollector
      Returns:
      The lexer
    • setTokenFilter

      public void setTokenFilter(TokenFilter tokenFilter)
      Sets the token filter to use for printing a tree. Set it to null to remove the filter.
      Parameters:
      tokenFilter - The token filter to use for printing. May be null.
    • addTokenFilter

      public void addTokenFilter(TokenFilter newFilter)
      Adds a token filter for printing. This will create a new multi token filter if another filter is already present.
      Parameters:
      newFilter - The token filter to also use for printing.
    • 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.
      Parameters:
      str - The string to be transformed
      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. 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.
      Parameters:
      stream - The input stream to be transformed
      Returns:
      The transformed string
      Throws:
      RecognitionException