home *** CD-ROM | disk | FTP | other *** search
Java Source | 1996-08-14 | 4.9 KB | 184 lines |
- /*---------------------------------------------------------------------------
-
- Written by the Personal Journal developers of Dow Jones & Company, Inc.
-
- Dow Jones makes no representations or warranties about
- the suitability of this software, either express or
- implied, including but not limited to the implied warranties
- of merchantability, fitness for a particular purpose,
- or non-infringement. Dow Jones will not be liable for
- any damages suffered by a user as a result of using,
- modifying or distributing this software or its derivatives.
-
-
-
- @(#)StoryScanner.java 0.00 20-Dec-95
-
- A simple scanner that reads from an InputStream.
- See also StdinScanner.java and StoryParser.java
-
-
- Authors:
-
- rphall Rick Hall
-
-
- Version Ident:
-
- $Header: /PjJavaClient/src/pj/io/StoryScanner.java 2 1/21/96 5:27p Rphall $
-
-
- History:
-
- 12/20/95 rphall Initial Creation
-
- ---------------------------------------------------------------------------*/
-
- package pj.io;
-
- import pj.io.PaperStory;
- import pj.io.StoryParserSymbols;
-
-
-
- import java.io.InputStream;
- import java.io.IOException;
- import java_cup.runtime.*;
-
-
- /*
- * A simple scanner that reads from an InputStream.
- *
- * @version 0.00 20-Dec-95
- * @author rphall
- */
-
-
-
-
- public class StoryScanner
- {
- // --- Instance variables
- private String strNextLine;
- private InputStream isInput;
-
-
- // --- Public constructors
- public StoryScanner(InputStream is)
- {
- isInput = is;
- strNextLine = null;
- }
-
- public StoryScanner()
- {
- this(System.in);
- }
-
-
- // --- Public operators
-
- /* initialize the StoryScanner */
- public void init()
- {
- strNextLine = "";
- advance();
- }
-
- /** Advance input by one line.
- **/
- protected void advance()
- {
- int in = -1;
- try {
- in = isInput.read();
- }
- catch(IOException e)
- {
- in = -1;
- }
-
- while ( in!='\r' && in!='\n' && in!=-1 )
- {
- strNextLine += (char)in ;
- try { in = isInput.read(); }
- catch(IOException e) { in = -1; }
-
- } // while
-
- // Eat the '\n' in CRLF
- if (in == '\r' )
- {
- try {
- in = isInput.read();
- }
- catch(IOException e)
- {
- in = -1;
- }
- }
-
- if (in == -1)
- strNextLine = null;
-
- } // advance
-
- /* recognize and return the next complete token */
- public token next_token()
- {
- token tok = null;
-
- if ( strNextLine == null )
- return new token(StoryParserSymbols.EOF);
-
- else if ( strNextLine.startsWith(PaperStory.markStartStory) )
- tok = new token(StoryParserSymbols.START_STORY);
-
- else if ( strNextLine.startsWith(PaperStory.markStartTitle) )
- tok = new token(StoryParserSymbols.START_TITLE);
-
- else if ( strNextLine.startsWith(PaperStory.markStartBody) )
- tok = new token(StoryParserSymbols.START_BODY);
-
- else if ( strNextLine.startsWith(PaperStory.markStopStory) )
- tok = new token(StoryParserSymbols.STOP_STORY);
-
- else if ( strNextLine.startsWith(PaperStory.markStopTitle) )
- tok = new token(StoryParserSymbols.STOP_TITLE);
-
- else if ( strNextLine.startsWith(PaperStory.markStopBody) )
- tok = new token(StoryParserSymbols.STOP_BODY);
-
- else if ( strNextLine.startsWith(PaperStory.markAttribSection) )
- tok = new str_token(
- StoryParserSymbols.ATTRIB_SECTION,
- strNextLine.substring(PaperStory.markAttribSection.length()));
-
- else if ( strNextLine.startsWith(PaperStory.markAttribSubsect) )
- tok = new str_token(
- StoryParserSymbols.ATTRIB_SUBSECT,
- strNextLine.substring(PaperStory.markAttribSubsect.length()));
-
- else if ( strNextLine.startsWith(PaperStory.markAttribType) )
- tok = new str_token(
- StoryParserSymbols.ATTRIB_TYPE,
- strNextLine.substring(PaperStory.markAttribType.length()));
-
- else if ( strNextLine.startsWith(PaperStory.markAttribKeys) )
- tok = new str_token(
- StoryParserSymbols.ATTRIB_KEYS,
- strNextLine.substring(PaperStory.markAttribKeys.length()));
-
- else
- tok = new str_token(
- StoryParserSymbols.LINE,
- strNextLine + PaperStory.markEOL);
-
- strNextLine = "";
- advance();
- return tok;
-
- } // next_token
-
- }; // StoryScanner
-