Class Parser

java.lang.Object
software.coley.sourcesolver.Parser

public class Parser extends Object
Initiates the parsing of source code and provides CompilationUnitModel in return.
Author:
Matt Coley
  • Constructor Details

    • Parser

      public Parser()
      New parser.
  • Method Details

    • setMappingContextFactory

      public void setMappingContextFactory(@Nonnull MappingContextProvider mappingContextFactory)
      Allows overriding MappingContext behavior by providing a factory that can create user-defined subclasses.
      Parameters:
      mappingContextFactory - New factory to produce MappingContext instances.
    • setJavacContext

      public void setJavacContext(@Nonnull com.sun.tools.javac.util.Context context)
      Allows controlling lower level javac aspects by providing a full context.
      Parameters:
      context - New javac context object.
    • getContextProperty

      @Nullable public <T> T getContextProperty(@Nonnull Class<T> key)
      Get a value in the javac context.
      Type Parameters:
      T - Content value type.
      Parameters:
      key - Javac context key.
      Returns:
      Javac context value.
    • putContextProperty

      public <T> void putContextProperty(@Nonnull Class<T> key, @Nonnull T value)
      Set a value in the javac context.
      Type Parameters:
      T - Content value type.
      Parameters:
      key - Javac context key.
      value - Value to assign.
    • putContextProperty

      public <T> void putContextProperty(@Nonnull Class<T> key, @Nonnull com.sun.tools.javac.util.Context.Factory<T> factory)
      Set a value in the javac context.
      Type Parameters:
      T - Content value type.
      Parameters:
      key - Javac context key.
      factory - Value factory to assign.
    • regenerateFactory

      protected void regenerateFactory()
      Regenerates the javac parser factory.
    • parse

      @Nonnull public CompilationUnitModel parse(@Nonnull String source)
      Maps Java source code to our compilation unit model.
      Parameters:
      source - Java source code.
      Returns:
      Parsed model.
      Throws:
      IllegalArgumentException - When the source is null, or if a critical error occurs.
    • mapCompilationUnit

      @Nonnull protected CompilationUnitModel mapCompilationUnit(@Nonnull String source, @Nonnull com.sun.tools.javac.tree.EndPosTable table, @Nonnull CompilationUnitTree unit)
      Maps the javac model to our own.
      Parameters:
      source - Java source code.
      table - Table containing offsets of javac tree elements.
      unit - Root tree element.
      Returns:
      Parsed model.
    • extractEndPosTable

      @Nonnull protected com.sun.tools.javac.tree.EndPosTable extractEndPosTable(@Nonnull com.sun.tools.javac.parser.JavacParser parser) throws Exception
      Retrieves the private table information we use for mapping our model's ranges.
      Parameters:
      parser - Context to reflect from.
      Returns:
      End position table within the given parser.
      Throws:
      Exception - When the table cannot be reflected.