Main Page   Class Hierarchy   Compound List   File List   Compound Members  

CharReader.java

Go to the documentation of this file.
00001 // file:    CharReader.java
00002 // author:  Robert Keller
00003 // purpose: Provide character reader as defined immediately below.
00004 
00005 import java.io.*;
00006 
00007 /**
00008  * A simple reader for reading chars one at a time from a String.
00009  * Both peek() and read() methods are provided.  Any whitespace is
00010  * is skipped.  For debugging purposes, a trace of characters read
00011  * can be turned on or off (default is off).
00012  */
00013 
00014 class CharReader extends java.io.StringReader
00015 {
00016 /**
00017  * Indicate whether tracing is on or off.
00018  */
00019 
00020 boolean traceOn = false;
00021 
00022 
00023 /**
00024  * Character returned when end of input reached.
00025  */
00026 
00027 static char nullchar = '\0';
00028 
00029 
00030 /**
00031  * Integer returned by StringReader for end of input.
00032  */
00033 
00034 static int eof = -1;
00035 
00036 
00037 /**
00038  * Construct a CharReader for a String.
00039  */
00040 
00041 CharReader(String s)
00042   {
00043   super(s);
00044   }
00045 
00046 
00047 /**
00048  * Get the next non-whitespace "character",
00049  * returning nullchar if there are no more characters.  
00050  */
00051 
00052 public char get()
00053   {
00054   int character = readNonWhiteSpace();
00055 
00056   if( character == eof )
00057     {
00058     if( traceOn )
00059       {
00060       System.out.println("geting at end of input, returning null character");
00061       }
00062     return nullchar;
00063     }
00064   else
00065     {
00066     if( traceOn )
00067       {
00068       System.out.println("geting: " + (char)character);
00069       }
00070     return (char)character;
00071     }
00072   }
00073 
00074 
00075 /**
00076  * Return the next non-whitespace character, but leave the 
00077  * character in the input to be gotten again.  If there are no more
00078  * characters, nullchar is returned.
00079  */
00080 
00081 public char peek()
00082   {
00083   try
00084     {
00085     mark(1);
00086     int character = readNonWhiteSpace();
00087     reset();  
00088 
00089     if( character == eof )
00090       {
00091       if( traceOn )
00092         {
00093         System.out.println("peeking at end of input, "
00094                          + "returning null character");
00095         }
00096       return nullchar;
00097       }
00098     else
00099       {
00100       if( traceOn )
00101         {
00102         System.out.println("peeking at: " + (char)character);
00103         }
00104       return (char)character;
00105       }
00106     }
00107   catch( IOException e )
00108     {
00109     return nullchar;
00110     }
00111   }
00112 
00113 
00114 /**
00115  * Return true if there are more characters to be get, false otherwise.
00116  */
00117 
00118 public boolean hasMore()
00119   {
00120   return peek() != nullchar;  
00121   }
00122 
00123 
00124 /**
00125  * Return the remaining input as a String.
00126  */
00127 
00128 public String toString()
00129   {
00130   StringBuffer buffer = new StringBuffer();
00131   
00132   int character;
00133 
00134   try
00135     {
00136     character = super.read();
00137 
00138     // Keep reading until a non-whitespace character is encountered or eof.
00139 
00140     while( character != eof )
00141       {
00142       buffer.append((char)character);
00143       character = super.read();    
00144       }
00145     }
00146   catch( IOException e )
00147     {
00148     }
00149 
00150   return buffer.toString();
00151   }
00152 
00153 
00154 /**
00155  * Get the next non-whitespace character,
00156  * returning nullchar if there are no more characters.  
00157  */
00158 
00159 private int readNonWhiteSpace()
00160   {
00161   int character;
00162 
00163   try
00164     {
00165     character = super.read();
00166 
00167     // Keep reading until a non-whitespace character is encountered or eof.
00168 
00169     while( character != eof && Character.isWhitespace((char)character) )
00170       {
00171       character = super.read();    
00172       }
00173     }
00174   catch( IOException e )
00175     {
00176     return nullchar;
00177     }
00178 
00179   return character;    
00180   }
00181 
00182 
00183 /**
00184  * Set the trace on or off.
00185  */
00186 
00187 public void setTrace(boolean traceOn)
00188   {
00189   this.traceOn = traceOn;
00190   }
00191 }      

Generated at Wed Mar 26 13:57:25 2003 for LogicParserSupportFiles by doxygen1.2.6 written by Dimitri van Heesch, © 1997-2001