Class EnhancedParser

java.lang.Object
io.github.douira.glsl_transformer.parser.EnhancedParser
All Implemented Interfaces:
ParserInterface
Direct Known Subclasses:
CachingParser

public class EnhancedParser extends Object implements ParserInterface
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.
  • 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

      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

    • 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 - If true, 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:
      setThrowParseErrors in interface ParserInterface
    • setParsingStrategy

      public void setParsingStrategy(EnhancedParser.ParsingStrategy parsingStrategy)
      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:
      setParsingStrategy in interface ParserInterface
      Parameters:
      parsingStrategy - The parsing strategy to use
    • setSLLOnly

      public void setSLLOnly()
      Sets the parsing strategy to EnhancedParser.ParsingStrategy.SLL_ONLY. This is the faster strategy.
      Specified by:
      setSLLOnly in interface ParserInterface
    • setLLOnly

      public void setLLOnly()
      Sets the parsing strategy to EnhancedParser.ParsingStrategy.LL_ONLY. This is the slower strategy.
      Specified by:
      setLLOnly in interface ParserInterface
    • 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:
      getParser in interface ParserInterface
    • getLexer

      public io.github.douira.glsl_transformer.GLSLLexer getLexer()
      Specified by:
      getLexer in interface ParserInterface
    • getTokenStream

      public BufferedTokenStream getTokenStream()
    • setTokenFilter

      public void setTokenFilter(TokenFilter<?> tokenFilter)
      Sets the token filter to use before parsing. It's placed between the lexer and the token stream.
      Specified by:
      setTokenFilter in interface ParserInterface
      Parameters:
      tokenFilter - The new token filter
    • parse

      public io.github.douira.glsl_transformer.GLSLParser.TranslationUnitContext parse(String str)
      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 <C extends ParserRuleContext> C parse(String str, Function<io.github.douira.glsl_transformer.GLSLParser,C> parseMethod)
      Parses a string using a parser method reference into a parse tree.
      Type Parameters:
      C - 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 <C extends ParserRuleContext> C parse(String str, ParseShape<C,?> parseShape)
    • parse

      public <C extends ParserRuleContext> C parse(String str, ParserRuleContext parent, Function<io.github.douira.glsl_transformer.GLSLParser,C> parseMethod)
      Parses a string using a parser method reference into a parse tree.
      Type Parameters:
      C - 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 <C extends ParserRuleContext> C parse(String str, ParserRuleContext parent, ParseShape<C,?> parseShape)