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
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 Summary
FieldsModifier and TypeFieldDescriptionprotected IntStreamThe last parsed input stream.static final TransformationManager<NonFixedJobParameters>An internal instance of this class that is used by other library-internal classes for parsing needs.protected BufferedTokenStreamThe last parsed tokens stream. -
Constructor Summary
ConstructorsConstructorDescriptionCreates a new transformation manager that throws parse errors by default.TransformationManager(boolean throwParseErrors) Creates a new transformation manager and specifies if parse errors should be thrown during parsing.TransformationManager(Transformation<T> rootTransformation) Creates a new transformation manager with a given root transformation that throws parse errors by default.TransformationManager(Transformation<T> rootTransformation, boolean throwParseErrors) Creates a new transformation manager with a given root transformation and parse error throwing behavior. -
Method Summary
Modifier and TypeMethodDescriptionio.github.douira.glsl_transformer.GLSLLexergetLexer()Returns the execution planner's lexer.io.github.douira.glsl_transformer.GLSLParserThe returned parser (and lexer) may contain no token stream or a wrong token stream.<RuleType extends ExtendedContext>
RuleTypeparse(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.<RuleType extends ExtendedContext>
RuleTypeParses a string using a parser method reference into a parse tree.<RuleType extends ExtendedContext>
RuleTypeparse(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.voidsetParseTokenFilter(TokenFilter<T> parseTokenFilter) Sets the token filter to use before parsing.voidsetPrintTokenFilter(TokenFilter<T> printTokenFilter) Sets the token filter to use before printing.Transforms the given string by parsing, transforming it with the already registered transformations and then re-printing it.Transforms the given string by parsing, transforming it with the already registered transformations and then re-printing it.transformStream(IntStream stream) Transforms a given input stream and re-prints it as a string.transformStream(IntStream stream, T parameters) Transforms a given input stream and re-prints it as a string.Methods inherited from class io.github.douira.glsl_transformer.transform.ExecutionPlanner
addConcurrent, getRootTransformation, planExecutionFor, setRootTransformation, transformTree, withJobParameters, withJobParameters
-
Field Details
-
INTERNAL
An internal instance of this class that is used by other library-internal classes for parsing needs. -
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
The last parsed tokens stream.- See Also:
-
-
Constructor Details
-
TransformationManager
Creates a new transformation manager with a given root transformation and parse error throwing behavior.- Parameters:
rootTransformation- The root transformation to usethrowParseErrors- 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- Iftrue, the transformation manager throw any parse errors encountered during parsing
-
TransformationManager
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:
getParserin classExecutionPlanner<T extends JobParameters>- Returns:
- The parser
-
getLexer
public io.github.douira.glsl_transformer.GLSLLexer getLexer()Description copied from class:ExecutionPlannerReturns the execution planner's lexer.- Specified by:
getLexerin classExecutionPlanner<T extends JobParameters>- Returns:
- The lexer
-
setPrintTokenFilter
Sets the token filter to use before printing.- Parameters:
printTokenFilter- The new print token filter
-
setParseTokenFilter
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 parseparseMethod- 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 parseparent- The parent to attach to the parsed nodeparseMethod- 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 parseparent- The parent to attach to the parsed nodeparseMethod- The parser method reference to use for parsing- Returns:
- The parsed string as a parse tree that has the given type
-
transform
Transforms the given string by parsing, transforming it with the already registered transformations and then re-printing it. The job parameters are set tonullsince none are passed.- Parameters:
str- The string to be transformed- Returns:
- The transformed string
- Throws:
RecognitionException
-
transform
Transforms the given string by parsing, transforming it with the already registered transformations and then re-printing it. The job parameters are set tonullsince none are passed.- Parameters:
str- The string to be transformedparameters- The job parameters- Returns:
- The transformed string
- Throws:
RecognitionException
-
transformStream
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
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'sCharStreamscan 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 transformedparameters- The job parameters- Returns:
- The transformed string
- Throws:
RecognitionException
-