org.anarres.cpp
Class Preprocessor

java.lang.Object
  extended by org.anarres.cpp.Preprocessor

public class Preprocessor
extends Object

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. (See CppReader, which does this.)


Constructor Summary
Preprocessor()
           
Preprocessor(File file)
          Equivalent to 'new Preprocessor(new FileLexerSource(file))'
Preprocessor(Source initial)
           
 
Method Summary
 void addFeature(Feature f)
          Adds a feature to the feature-set of this Preprocessor.
 void addFeatures(Collection<Feature> f)
          Adds features to the feature-set of this Preprocessor.
 void addInput(File file)
          Adds input for the Preprocessor.
 void addInput(Source source)
          Adds input for the Preprocessor.
 void addMacro(Macro m)
          Adds a Macro to this Preprocessor.
 void addMacro(String name)
          Defines the given name as a macro, with the value 1.
 void addMacro(String name, String value)
          Defines the given name as a macro.
 void addSource(Source source)
          Pushes a source onto the input stack.
 void addWarning(Warning w)
          Adds a warning to the warning-set of this Preprocessor.
 void addWarnings(Collection<Warning> w)
          Adds warnings to the warning-set of this Preprocessor.
protected  void error(int line, int column, String msg)
          Handles an error.
protected  void error(Token tok, String msg)
          Handles an error.
 boolean getFeature(Feature f)
          Returns true if the given feature is in the feature-set of this Preprocessor.
 Set<Feature> getFeatures()
          Returns the feature-set for this Preprocessor.
 PreprocessorListener getListener()
          Returns the PreprocessorListener which handles events for this Preprocessor.
 Macro getMacro(String name)
          Returns the named macro.
protected  Map<String,Macro> getMacros()
          Returns the Map of Macros parsed during the run of this Preprocessor.
 List<String> getQuoteIncludePath()
          Returns the user include-path of this Preprocessor.
protected  Source getSource()
          Returns the top Source on the input stack.
 List<String> getSystemIncludePath()
          Returns the system include-path of this Preprocessor.
 boolean getWarning(Warning w)
          Returns true if the given warning is in the warning-set of this Preprocessor.
 Set<Warning> getWarnings()
          Returns the warning-set for this Preprocessor.
protected  String include_dirname(String path)
           
protected  String include_filename(String dir, String name)
           
protected  boolean include(String path)
          Attempts to include the given file.
protected  void include(String parent, int line, String name, boolean quoted)
          Handles a include directive.
protected  void pop_source()
          Pops a Source from the input stack.
protected  void pragma(Token name, List<Token> value)
           
protected  void push_source(Source source, boolean autopop)
          Pushes a Source onto the input stack.
 void setListener(PreprocessorListener listener)
          Sets the PreprocessorListener which handles events for this Preprocessor.
 void setQuoteIncludePath(List<String> path)
          Sets the user include path used by this Preprocessor.
 void setSystemIncludePath(List<String> path)
          Sets the system include path used by this Preprocessor.
 Token token()
          Returns the next preprocessor token.
 String toString()
           
protected  void warning(int line, int column, String msg)
          Handles a warning.
protected  void warning(Token tok, String msg)
          Handles a warning.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

Preprocessor

public Preprocessor()

Preprocessor

public Preprocessor(Source initial)

Preprocessor

public Preprocessor(File file)
             throws IOException
Equivalent to 'new Preprocessor(new FileLexerSource(file))'

Throws:
IOException
Method Detail

setListener

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

public PreprocessorListener getListener()
Returns the PreprocessorListener which handles events for this Preprocessor.


getFeatures

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


addFeature

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


addFeatures

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


getFeature

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


getWarnings

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


addWarning

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


addWarnings

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


getWarning

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


addInput

public void addInput(Source source)
Adds input for the Preprocessor. XXX Inputs should be maintained off the source stack.

Inputs are processed in the order in which they are added.


addInput

public void addInput(File file)
              throws IOException
Adds input for the Preprocessor.

Throws:
IOException
See Also:
addInput(Source)

error

protected void error(int line,
                     int column,
                     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(Token tok,
                     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:
error(int, int, String)

warning

protected void warning(int line,
                       int column,
                       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(Token tok,
                       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:
warning(int, int, String)

addMacro

public void addMacro(Macro m)
              throws LexerException
Adds a Macro to this Preprocessor. The given Macro object encapsulates both the name and the expansion.

Throws:
LexerException

addMacro

public void addMacro(String name,
                     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

addMacro

public void addMacro(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

setQuoteIncludePath

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


getQuoteIncludePath

public List<String> getQuoteIncludePath()
Returns the user include-path of this Preprocessor. This list may be freely modified by user code.


setSystemIncludePath

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


getSystemIncludePath

public List<String> getSystemIncludePath()
Returns the system include-path of this Preprocessor. This list may be freely modified by user code.


getMacros

protected Map<String,Macro> getMacros()
Returns the Map of Macros parsed during the run of this Preprocessor.


getMacro

public Macro getMacro(String name)
Returns the named macro. While you can modify the returned object, unexpected things might happen if you do.


getSource

protected Source getSource()
Returns the top Source on the input stack.

See Also:
Source, push_source(Source,boolean), pop_source()

push_source

protected void push_source(Source source,
                           boolean autopop)
Pushes a Source onto the input stack.

See Also:
getSource(), pop_source()

pop_source

protected void pop_source()
Pops a Source from the input stack.

See Also:
getSource(), push_source(Source,boolean)

addSource

public void addSource(Source source)
Pushes a source onto the input stack. The top source on the input stack is the one which is currently being processed. It is unlikely that you will want to call this method. It is more likely that you want addInput(Source).

See Also:
addInput(Source)

include

protected boolean include(String path)
                   throws IOException,
                          LexerException
Attempts to include the given file. User code may override this method to implement a virtual file system.

Throws:
IOException
LexerException

include_dirname

protected String include_dirname(String path)

include_filename

protected String include_filename(String dir,
                                  String name)

include

protected void include(String parent,
                       int line,
                       String name,
                       boolean quoted)
                throws IOException,
                       LexerException
Handles a include directive. The user may override this to provide alternate semantics for the include directive, for example, creating a Source based on a virtual file system.

Throws:
IOException
LexerException

pragma

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

token

public Token token()
            throws IOException,
                   LexerException
Returns the next preprocessor token.

Throws:
LexerException - if a preprocessing error occurs.
InternalException - if an unexpected error condition arises.
IOException
See Also:
Token

toString

public String toString()
Overrides:
toString in class Object


Copyright © 2007 Shevek, Anarres. All Rights Reserved.