Class EnhancedParser
java.lang.Object
io.github.douira.glsl_transformer.basic.EnhancedParser
- All Implemented Interfaces:
ParserInterface
- Direct Known Subclasses:
CachingParser
The enhanced parser does more than just parsing. It also does lexing,
token filtering and switching between parsing modes on demand. It also
handles error listeners.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic enumEnum for the parsing strategy. -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected IntStreamThe last parsed input stream.This is a debug property.protected BufferedTokenStreamThe last parsed tokens stream. -
Constructor Summary
ConstructorsConstructorDescriptionCreates a new parser that throws parse errors by default.EnhancedParser(boolean throwParseErrors) Creates a new parser and specifies if parse errors should be thrown during parsing. -
Method Summary
Modifier and TypeMethodDescriptionstatic EnhancedParserGets the internal singleton instance of the parser.io.github.douira.glsl_transformer.GLSLLexergetLexer()io.github.douira.glsl_transformer.GLSLParserThe returned parser (and lexer) may contain no token stream or a wrong token stream.TokenFilter<?>io.github.douira.glsl_transformer.GLSLParser.TranslationUnitContextParses a string as a translation unit.<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.voidSets the parsing strategy toEnhancedParser.ParsingStrategy.LL_ONLY.voidsetParseTokenFilter(TokenFilter<?> parseTokenFilter) Sets the token filter to use before parsing.voidsetParsingStrategy(EnhancedParser.ParsingStrategy parsingStrategy) Sets if the parser should be re-run in LL parsing mode if the SLL parsing mode return an error.voidSets the parsing strategy toEnhancedParser.ParsingStrategy.SLL_ONLY.voidsetThrowParseErrors(boolean throwParseErrors)
-
Field Details
-
internalErrorConsumer
This is a debug property. This consumer will be called with errors parse cancellation errors generated by the parser in sll and ll mode. -
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
-
EnhancedParser
public EnhancedParser(boolean throwParseErrors) Creates a new parser 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 parser throw any parse errors encountered during parsing
-
EnhancedParser
public EnhancedParser()Creates a new parser that throws parse errors by default.
-
-
Method Details
-
setThrowParseErrors
public void setThrowParseErrors(boolean throwParseErrors) - Specified by:
setThrowParseErrorsin interfaceParserInterface
-
getInternalInstance
Gets the internal singleton instance of the parser. This should generally not be used by external library users. -
setParsingStrategy
Sets if the parser should be re-run in LL parsing mode if the SLL parsing mode return an error. This is generally only necessary if it's important that errors are only reported if there are actually errors. Keep in mind that LL parsing mode is much slower than SLL.- Specified by:
setParsingStrategyin interfaceParserInterface- Parameters:
parsingStrategy- The parsing strategy to use
-
setSLLOnly
public void setSLLOnly()Sets the parsing strategy toEnhancedParser.ParsingStrategy.SLL_ONLY. This is the faster strategy.- Specified by:
setSLLOnlyin interfaceParserInterface
-
setLLOnly
public void setLLOnly()Sets the parsing strategy toEnhancedParser.ParsingStrategy.LL_ONLY. This is the slower strategy.- Specified by:
setLLOnlyin interfaceParserInterface
-
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.- Specified by:
getParserin interfaceParserInterface
-
getLexer
public io.github.douira.glsl_transformer.GLSLLexer getLexer()- Specified by:
getLexerin interfaceParserInterface
-
getTokenStream
-
setParseTokenFilter
Sets the token filter to use before parsing. It's placed between the lexer and the token stream.- Specified by:
setParseTokenFilterin interfaceParserInterface- Parameters:
parseTokenFilter- The new parse token filter
-
getParseTokenFilter
- Specified by:
getParseTokenFilterin interfaceParserInterface
-
parse
Parses a string as a translation unit.- Parameters:
str- The string to parse- Returns:
- The parsed string as a translation unit parse tree
-
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
-