scale.frontend.c
Class CPreprocessor

java.lang.Object
  extended by java.io.Reader
      extended by scale.frontend.c.CPreprocessor
All Implemented Interfaces:
java.io.Closeable, java.lang.Readable

public class CPreprocessor
extends java.io.Reader

This class reads a file and transforms it using the C preprocessor conventions.

$Id: CPreprocessor.java,v 1.51 2007-06-12 20:21:20 burrill Exp $

Copyright 2008 by the Scale Compiler Group,
Department of Computer Science
University of Massachusetts,
Amherst MA. 01003, USA
All Rights Reserved.

This class will send warning messages to System.err when any problem in the source file is discovered.


Field Summary
protected  boolean allowGNUExtensions
          Set true if GNU extensions are allowed.
static boolean classTrace
          True if traces are to be performed.
protected  boolean strictANSIC
          Set true if strict ANSI C required.
 
Fields inherited from class java.io.Reader
lock
 
Constructor Summary
CPreprocessor(java.lang.String filename, Vector<java.lang.String> userMacros, Vector<java.lang.String> userDirs, Vector<java.lang.String> systemDirs, Vector<java.lang.String> macroText)
          Create a reader for C programs that does the C pre-processing.
 
Method Summary
 void addMacro(java.lang.String name, java.lang.String text)
          Define a simple macro to be used by the pre-processor.
 void close()
          Close the files associated with this preprocessor.
 CReader getCReader()
           
 int getLineNumber()
           
 void mark(int readAheadLimit)
          Mark the present position in the stream.
 int read()
          Read a single character.
 int read(char[] cbuf, int off, int len)
          Read characters into a portion of an array.
 java.lang.String readLine()
          Read a line of text.
 void reset()
          Reset the stream to the most recent mark.
 void setCurrentFtn(java.lang.String currentFtn)
          Specify the current function being processed.
 long skip(long n)
          Skip characters.
 
Methods inherited from class java.io.Reader
markSupported, read, read, ready
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

classTrace

public static boolean classTrace
True if traces are to be performed.


allowGNUExtensions

protected boolean allowGNUExtensions
Set true if GNU extensions are allowed.


strictANSIC

protected boolean strictANSIC
Set true if strict ANSI C required.

Constructor Detail

CPreprocessor

public CPreprocessor(java.lang.String filename,
                     Vector<java.lang.String> userMacros,
                     Vector<java.lang.String> userDirs,
                     Vector<java.lang.String> systemDirs,
                     Vector<java.lang.String> macroText)
              throws java.io.IOException
Create a reader for C programs that does the C pre-processing. This reader reads from the file specified nad processes it according to the standard C preprocessor rules.

A set of user-defined macros can be specified. They must be in one of the forms

   NAME
   NAME=TEXT
 

Directories to be searched for include files are specified using two arguments. The userDirs parameter specifies a Vector of String instances that are searched when the include file name is specified in the source file as a string delimited by double quotes. The pre-processor also includes the directory containing the source file in the list of directories to be searched.

The systemDirs parameter specifies an array of String instances that are searched when the include file name is specified as a string delimited by angle-brackets.

Parameters:
filename - is the source file name
userMacros - is a list of user defined macros in the form of name=text or null
userDirs - is a list of directory pathnames to use in finding user include files or null
systemDirs - is a list of directory pathnames to use in finding system include files or null
macroText - is null or is used to collect macro definitions as text strings
Throws:
java.io.IOException
Method Detail

getCReader

public CReader getCReader()

getLineNumber

public int getLineNumber()

addMacro

public void addMacro(java.lang.String name,
                     java.lang.String text)
Define a simple macro to be used by the pre-processor. A simple macro has no parameters.

Parameters:
name - is the macro name
text - is the replacement text

close

public void close()
           throws java.io.IOException
Close the files associated with this preprocessor.

Specified by:
close in interface java.io.Closeable
Specified by:
close in class java.io.Reader
Throws:
java.io.IOException

read

public int read(char[] cbuf,
                int off,
                int len)
         throws java.io.IOException
Read characters into a portion of an array.

Specified by:
read in class java.io.Reader
Parameters:
cbuf - - destination buffer
off - - offset at which to start storing characters
len - - maximum number of characters to read
Returns:
the number of bytes read, or -1 if the end of the stream has already been reached
Throws:
java.io.IOException

readLine

public java.lang.String readLine()
                          throws java.io.IOException
Read a line of text. A line is considered to be terminated by any one of a line feed ('\n'), a carriage return ('\r'), or a carriage return followed immediately by a linefeed.

Returns:
a String containing the contents of the line, not including any line-termination characters, or null if the end of the stream has been reached
Throws:
java.io.IOException

read

public int read()
         throws java.io.IOException
Read a single character. Line terminators are compressed into single newline ('\n') characters.

Overrides:
read in class java.io.Reader
Returns:
the character read, or -1 if the end of the stream has been reached
Throws:
java.io.IOException

setCurrentFtn

public void setCurrentFtn(java.lang.String currentFtn)
Specify the current function being processed.


skip

public long skip(long n)
          throws java.io.IOException
Skip characters.

Overrides:
skip in class java.io.Reader
Parameters:
n - - the number of characters to skip
Returns:
the number of characters actually skipped
Throws:
java.io.IOException

mark

public void mark(int readAheadLimit)
          throws java.io.IOException
Mark the present position in the stream. Subsequent calls to reset() will attempt to reposition the stream to this point, and will also reset the line number appropriately.

Overrides:
mark in class java.io.Reader
Throws:
java.io.IOException

reset

public void reset()
           throws java.io.IOException
Reset the stream to the most recent mark.

Overrides:
reset in class java.io.Reader
Throws:
java.io.IOException