home *** CD-ROM | disk | FTP | other *** search
/ The Net: Ultimate Internet Guide / WWLCD1.ISO / pc / java / un2maiq4 / pjjava / src / pj / io / storyscanner.java < prev   
Encoding:
Java Source  |  1996-08-14  |  4.9 KB  |  184 lines

  1. /*---------------------------------------------------------------------------
  2.  
  3.     Written by the Personal Journal developers of Dow Jones & Company, Inc.
  4.  
  5.     Dow Jones makes no representations or warranties about 
  6.     the suitability of this software, either express or 
  7.     implied, including but not limited to the implied warranties 
  8.     of merchantability, fitness for a particular purpose, 
  9.     or non-infringement.  Dow Jones will not be liable for 
  10.     any damages suffered by a user as a result of using, 
  11.     modifying or distributing this software or its derivatives.
  12.  
  13.  
  14.  
  15.     @(#)StoryScanner.java  0.00 20-Dec-95
  16.  
  17.         A simple scanner that reads from an InputStream.
  18.         See also StdinScanner.java and StoryParser.java
  19.  
  20.  
  21.     Authors:
  22.  
  23.         rphall      Rick Hall
  24.  
  25.  
  26.     Version Ident:
  27.  
  28.         $Header: /PjJavaClient/src/pj/io/StoryScanner.java 2     1/21/96 5:27p Rphall $
  29.  
  30.  
  31.     History:
  32.  
  33.         12/20/95    rphall      Initial Creation
  34.  
  35. ---------------------------------------------------------------------------*/
  36.  
  37. package pj.io;
  38.  
  39. import pj.io.PaperStory;
  40. import pj.io.StoryParserSymbols;
  41.  
  42.  
  43.  
  44. import java.io.InputStream;
  45. import java.io.IOException;
  46. import java_cup.runtime.*;
  47.  
  48.  
  49. /*
  50.  * A simple scanner that reads from an InputStream.
  51.  *
  52.  * @version 0.00 20-Dec-95
  53.  * @author  rphall
  54.  */  
  55.  
  56.  
  57.  
  58.  
  59. public class StoryScanner 
  60.     {
  61.     // --- Instance variables
  62.     private String      strNextLine;
  63.     private InputStream isInput;
  64.  
  65.  
  66.     // --- Public constructors
  67.     public StoryScanner(InputStream is)
  68.         {
  69.         isInput = is;
  70.         strNextLine = null;
  71.         }
  72.  
  73.     public StoryScanner()
  74.         { 
  75.         this(System.in); 
  76.         }
  77.  
  78.  
  79.     // --- Public operators
  80.  
  81.     /* initialize the StoryScanner */
  82.     public void init()
  83.         {
  84.         strNextLine = "";
  85.         advance();
  86.         }
  87.  
  88.     /** Advance input by one line.
  89.     **/
  90.     protected void advance()
  91.         {
  92.         int in = -1;
  93.         try { 
  94.             in = isInput.read(); 
  95.             }
  96.         catch(IOException e) 
  97.             { 
  98.             in = -1; 
  99.             }
  100.  
  101.         while ( in!='\r' && in!='\n' && in!=-1 )
  102.             {
  103.             strNextLine += (char)in ;
  104.             try { in = isInput.read(); }
  105.             catch(IOException e) { in = -1; }
  106.  
  107.             } // while
  108.  
  109.         // Eat the '\n' in CRLF
  110.         if (in == '\r' )
  111.             {
  112.             try { 
  113.                 in = isInput.read(); 
  114.                 }
  115.             catch(IOException e) 
  116.                 { 
  117.                 in = -1; 
  118.                 }
  119.             }
  120.  
  121.         if (in == -1) 
  122.             strNextLine = null;
  123.  
  124.         } // advance
  125.  
  126.     /* recognize and return the next complete token */
  127.     public token next_token()
  128.         {
  129.         token tok = null;
  130.  
  131.         if ( strNextLine == null )
  132.             return new token(StoryParserSymbols.EOF);
  133.  
  134.         else if ( strNextLine.startsWith(PaperStory.markStartStory) )
  135.             tok = new token(StoryParserSymbols.START_STORY);
  136.  
  137.         else if ( strNextLine.startsWith(PaperStory.markStartTitle) )
  138.             tok = new token(StoryParserSymbols.START_TITLE);
  139.  
  140.         else if ( strNextLine.startsWith(PaperStory.markStartBody) )
  141.             tok = new token(StoryParserSymbols.START_BODY);
  142.  
  143.         else if ( strNextLine.startsWith(PaperStory.markStopStory) )
  144.             tok = new token(StoryParserSymbols.STOP_STORY);
  145.  
  146.         else if ( strNextLine.startsWith(PaperStory.markStopTitle) )
  147.             tok = new token(StoryParserSymbols.STOP_TITLE);
  148.  
  149.         else if ( strNextLine.startsWith(PaperStory.markStopBody) )
  150.             tok = new token(StoryParserSymbols.STOP_BODY);
  151.  
  152.         else if ( strNextLine.startsWith(PaperStory.markAttribSection) )
  153.             tok = new str_token(
  154.                     StoryParserSymbols.ATTRIB_SECTION,
  155.                     strNextLine.substring(PaperStory.markAttribSection.length()));
  156.  
  157.         else if ( strNextLine.startsWith(PaperStory.markAttribSubsect) )
  158.             tok = new str_token(
  159.                     StoryParserSymbols.ATTRIB_SUBSECT,
  160.                     strNextLine.substring(PaperStory.markAttribSubsect.length()));
  161.  
  162.         else if ( strNextLine.startsWith(PaperStory.markAttribType) )
  163.             tok = new str_token(
  164.                     StoryParserSymbols.ATTRIB_TYPE,
  165.                     strNextLine.substring(PaperStory.markAttribType.length()));
  166.  
  167.         else if ( strNextLine.startsWith(PaperStory.markAttribKeys) )
  168.             tok = new str_token(
  169.                     StoryParserSymbols.ATTRIB_KEYS,
  170.                     strNextLine.substring(PaperStory.markAttribKeys.length()));
  171.  
  172.         else
  173.             tok = new str_token(
  174.                     StoryParserSymbols.LINE,
  175.                     strNextLine + PaperStory.markEOL);
  176.  
  177.         strNextLine = "";
  178.         advance();
  179.         return tok;
  180.  
  181.         } // next_token
  182.  
  183. }; // StoryScanner
  184.