Class Preprocessor

java.lang.Object
io.github.douira.glsl_preprocessor.Preprocessor
All Implemented Interfaces:
Closeable, AutoCloseable

public class Preprocessor extends Object implements Closeable
A C Preprocessor. The Preprocessor outputs a token stream which does not need re-lexing for C or C++. Alternatively, the output text may be reconstructed by concatenating the text values of the returned Tokens.
  • Constructor Details

    • Preprocessor

      public Preprocessor()
    • Preprocessor

      public Preprocessor(@NonNull Source initial)
    • Preprocessor

      public Preprocessor(@NonNull Reader r)
    • Preprocessor

      public Preprocessor(@NonNull String r)
  • Method Details

    • setFileSystem

      public void setFileSystem(@NonNull VirtualFileSystem filesystem)
      Sets the VirtualFileSystem used by this Preprocessor.
    • getFileSystem

      @NonNull public VirtualFileSystem getFileSystem()
      Returns the VirtualFileSystem used by this Preprocessor.
    • setListener

      public void setListener(@NonNull PreprocessorListener listener)
      Sets the PreprocessorListener which handles events for this Preprocessor. The listener is notified of warnings, errors and source changes, amongst other things.
    • getListener

      @NonNull public PreprocessorListener getListener()
      Returns the PreprocessorListener which handles events for this Preprocessor.
    • getFeatures

      @NonNull public Set<Feature> getFeatures()
      Returns the feature-set for this Preprocessor. This set may be freely modified by user code.
    • addFeature

      public void addFeature(@NonNull Feature f)
      Adds a feature to the feature-set of this Preprocessor.
    • addFeatures

      public void addFeatures(@NonNull Collection<Feature> f)
      Adds features to the feature-set of this Preprocessor.
    • addFeatures

      public void addFeatures(Feature... f)
      Adds features to the feature-set of this Preprocessor.
    • getFeature

      public boolean getFeature(@NonNull Feature f)
      Returns true if the given feature is in the feature-set of this Preprocessor.
    • getWarnings

      @NonNull public Set<Warning> getWarnings()
      Returns the warning-set for this Preprocessor. This set may be freely modified by user code.
    • addWarning

      public void addWarning(@NonNull Warning w)
      Adds a warning to the warning-set of this Preprocessor.
    • addWarnings

      public void addWarnings(@NonNull Collection<Warning> w)
      Adds warnings to the warning-set of this Preprocessor.
    • getWarning

      public boolean getWarning(@NonNull Warning w)
      Returns true if the given warning is in the warning-set of this Preprocessor.
    • addInput

      public void addInput(@NonNull Source source)
      Adds input for the Preprocessor. Inputs are processed in the order in which they are added.
    • error

      protected void error(int line, int column, @NonNull String msg)
      Handles an error. If a PreprocessorListener is installed, it receives the error. Otherwise, an exception is thrown.
    • error

      protected void error(@NonNull Token tok, @NonNull String msg)
      Handles an error. If a PreprocessorListener is installed, it receives the error. Otherwise, an exception is thrown.
      See Also:
    • warning

      protected void warning(int line, int column, @NonNull String msg)
      Handles a warning. If a PreprocessorListener is installed, it receives the warning. Otherwise, an exception is thrown.
    • warning

      protected void warning(@NonNull Token tok, @NonNull String msg)
      Handles a warning. If a PreprocessorListener is installed, it receives the warning. Otherwise, an exception is thrown.
      See Also:
    • addMacro

      public void addMacro(@NonNull Macro m)
      Adds a Macro to this Preprocessor. The given Macro object encapsulates both the name and the expansion.
      Throws:
      LexerException - if the definition fails or is otherwise illegal.
    • addMacro

      public void addMacro(@NonNull String name, @NonNull String value)
      Defines the given name as a macro. The String value is lexed into a token stream, which is used as the macro expansion.
      Throws:
      LexerException - if the definition fails or is otherwise illegal.
    • addMacro

      public void addMacro(@NonNull String name)
      Defines the given name as a macro, with the value 1. This is a convenience method, and is equivalent to addMacro(name, "1").
      Throws:
      LexerException - if the definition fails or is otherwise illegal.
    • getMacros

      @NonNull public Map<String,Macro> getMacros()
      Returns the Map of Macros parsed during the run of this Preprocessor.
      Returns:
      The Map of macros currently defined.
    • getMacro

      @CheckForNull public Macro getMacro(@NonNull String name)
      Returns the named macro. While you can modify the returned object, unexpected things might happen if you do.
      Returns:
      the Macro object, or null if not found.
    • getIncludes

      @NonNull public List<? extends VirtualFile> getIncludes()
      Returns the list of VirtualFiles which have been included by this Preprocessor. This does not include any Source provided to the constructor or addInput(Source).
    • getSource

      protected Source getSource()
      Returns the top Source on the input stack.
      Returns:
      the top Source on the input stack.
      See Also:
    • push_source

      protected void push_source(@NonNull Source source, boolean autopop)
      Pushes a Source onto the input stack.
      Parameters:
      source - the new Source to push onto the top of the input stack.
      autopop - if true, the Source is automatically removed from the input stack at EOF.
      See Also:
    • pop_source

      @CheckForNull protected Token pop_source(boolean linemarker)
      Pops a Source from the input stack.
      Parameters:
      linemarker - TODO: currently ignored, might be a bug?
      See Also:
    • pop_source

      protected void pop_source()
    • include

      protected boolean include(@NonNull VirtualFile file)
      Attempts to include the given file. User code may override this method to implement a virtual file system.
      Parameters:
      file - The VirtualFile to attempt to include.
      Returns:
      true if the file was successfully included, false otherwise.
    • include

      protected boolean include(@NonNull Iterable<String> path, @NonNull String name)
      Attempts to include a file from an include path, by name.
      Parameters:
      path - The list of virtual directories to search for the given name.
      name - The name of the file to attempt to include.
      Returns:
      true if the file was successfully included, false otherwise.
    • pragma_once

      protected void pragma_once(@NonNull Token name)
    • pragma

      protected void pragma(@NonNull Token name, @NonNull List<Token> value)
    • token

      @NonNull public Token token()
      Returns the next preprocessor token.
      Returns:
      The next fully preprocessed token.
      See Also:
    • printTo

      public void printTo(StringBuilder builder)
    • print

      public StringBuilder print()
    • printToString

      public String printToString()
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • close

      public void close()
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable