Class Preprocessor

java.lang.Object
foundry.veil.lib.anarres.cpp.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(@NotNull @NotNull Source initial)
  • Method Details

    • setListener

      public void setListener(@NotNull @NotNull 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

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

      @NotNull public @NotNull Set<Feature> getFeatures()
      Returns the feature-set for this Preprocessor.

      This set may be freely modified by user code.

    • addFeature

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

      public void addFeatures(@NotNull @NotNull 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(@NotNull @NotNull Feature f)
      Returns true if the given feature is in the feature-set of this Preprocessor.
    • getWarnings

      @NotNull public @NotNull Set<Warning> getWarnings()
      Returns the warning-set for this Preprocessor.

      This set may be freely modified by user code.

    • addWarning

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

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

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

      public void addInput(@NotNull @NotNull 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, @NotNull @NotNull String msg) throws LexerException
      Handles an error.

      If a PreprocessorListener is installed, it receives the error. Otherwise, an exception is thrown.

      Throws:
      LexerException
    • error

      protected void error(@NotNull @NotNull Token tok, @NotNull @NotNull String msg) throws LexerException
      Handles an error.

      If a PreprocessorListener is installed, it receives the error. Otherwise, an exception is thrown.

      Throws:
      LexerException
      See Also:
    • warning

      protected void warning(int line, int column, @NotNull @NotNull String msg) throws LexerException
      Handles a warning.

      If a PreprocessorListener is installed, it receives the warning. Otherwise, an exception is thrown.

      Throws:
      LexerException
    • warning

      protected void warning(@NotNull @NotNull Token tok, @NotNull @NotNull String msg) throws LexerException
      Handles a warning.

      If a PreprocessorListener is installed, it receives the warning. Otherwise, an exception is thrown.

      Throws:
      LexerException
      See Also:
    • addMacro

      public void addMacro(@NotNull @NotNull Macro m) throws LexerException
      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(@NotNull @NotNull String name, @NotNull @NotNull String value) throws LexerException
      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(@NotNull @NotNull String name) throws LexerException
      Defines the given name as a macro, with the value 1.

      This is a convnience method, and is equivalent to addMacro(name, "1").

      Throws:
      LexerException - if the definition fails or is otherwise illegal.
    • setQuoteIncludePath

      public void setQuoteIncludePath(@NotNull @NotNull List<String> path)
      Sets the user include path used by this Preprocessor.
    • getQuoteIncludePath

      @NotNull public @NotNull List<String> getQuoteIncludePath()
      Returns the user include-path of this Preprocessor.

      This list may be freely modified by user code.

    • setSystemIncludePath

      public void setSystemIncludePath(@NotNull @NotNull List<String> path)
      Sets the system include path used by this Preprocessor.
    • getSystemIncludePath

      @NotNull public @NotNull List<String> getSystemIncludePath()
      Returns the system include-path of this Preprocessor.

      This list may be freely modified by user code.

    • setFrameworksPath

      public void setFrameworksPath(@NotNull @NotNull List<String> path)
      Sets the Objective-C frameworks path used by this Preprocessor.
    • getFrameworksPath

      @NotNull public @NotNull List<String> getFrameworksPath()
      Returns the Objective-C frameworks path used by this Preprocessor.

      This list may be freely modified by user code.

    • getMacros

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

      @Nullable public @Nullable Macro getMacro(@NotNull @NotNull 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.
    • 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(@NotNull @NotNull 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

      @Nullable protected @Nullable Token pop_source(boolean linemarker) throws IOException
      Pops a Source from the input stack.
      Parameters:
      linemarker - TODO: currently ignored, might be a bug?
      Throws:
      IOException - if an I/O error occurs.
      See Also:
    • pop_source

      protected void pop_source() throws IOException
      Throws:
      IOException
    • pragma

      protected void pragma(@NotNull @NotNull Token name, @NotNull @NotNull List<Token> value) throws IOException, LexerException
      Throws:
      IOException
      LexerException
    • token

      @NotNull public @NotNull Token token() throws IOException, LexerException
      Returns the next preprocessor token.
      Returns:
      The next fully preprocessed token.
      Throws:
      IOException - if an I/O error occurs.
      LexerException - if a preprocessing error occurs.
      InternalException - if an unexpected error condition arises.
      See Also:
    • toString

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

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