Class TransformationManager
java.lang.Object
io.github.douira.glsl_transformer.transform.PhaseCollector
io.github.douira.glsl_transformer.transform.TransformationManager
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 Summary
FieldsModifier and TypeFieldDescriptionprotected IntStreamThe last parsed input stream.static final TransformationManagerAn 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. -
Method Summary
Modifier and TypeMethodDescriptionvoidaddTokenFilter(TokenFilter newFilter) Adds a token filter for printing.io.github.douira.glsl_transformer.GLSLLexergetLexer()Returns the phase collector'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.voidsetTokenFilter(TokenFilter tokenFilter) Sets the token filter to use for printing a tree.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.Methods inherited from class io.github.douira.glsl_transformer.transform.PhaseCollector
registerTransformation, registerTransformationMultiple, transformTree
-
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
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
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:
getParserin classPhaseCollector- Returns:
- The parser
-
getLexer
public io.github.douira.glsl_transformer.GLSLLexer getLexer()Description copied from class:PhaseCollectorReturns the phase collector's lexer.- Specified by:
getLexerin classPhaseCollector- Returns:
- The lexer
-
setTokenFilter
Sets the token filter to use for printing a tree. Set it tonullto remove the filter.- Parameters:
tokenFilter- The token filter to use for printing. May benull.
-
addTokenFilter
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 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.- Parameters:
str- The string to be transformed- Returns:
- The transformed string
- Throws:
RecognitionException
-
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.- Parameters:
stream- The input stream to be transformed- Returns:
- The transformed string
- Throws:
RecognitionException
-