home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / jikepg12.zip / jikespg / examples / bnf / LexStream.java < prev    next >
Encoding:
Java Source  |  1999-11-04  |  2.3 KB  |  72 lines

  1. // $Id: LexStream.java,v 1.3 1999/11/04 14:02:16 shields Exp $
  2. // This software is subject to the terms of the IBM Jikes Compiler
  3. // License Agreement available at the following URL:
  4. // http://www.ibm.com/research/jikes.
  5. // Copyright (C) 1983, 1999, International Business Machines Corporation
  6. // and others.  All Rights Reserved.
  7. // You must accept the terms of that agreement to use this software.
  8.  
  9. import java.util.Vector;
  10. import java.io.FileInputStream;
  11.  
  12. //
  13. // LexStream holds a stream of tokens generated from an input and 
  14. // provides methods to retrieve information from the stream.
  15. //
  16. public class LexStream implements bnfsym
  17. {
  18.     private int index;
  19.     final static int INFINITY = Integer.MAX_VALUE;
  20.  
  21.     int Next(int i) { return (++i < tokens.size() ? i : tokens.size() - 1); }
  22.  
  23.     int Previous(int i) { return (i <= 0 ? 0 : i - 1); }
  24.  
  25.     int Peek() { return Next(index); }
  26.  
  27.     void Reset(int i) { index = Previous(i); }
  28.     void Reset() { index = 0; }
  29.  
  30.     int Gettoken() { return index = Next(index); }
  31.  
  32.     int Gettoken(int end_token)
  33.          { return index = (index < end_token ? Next(index) : tokens.size() - 1); }
  34.  
  35.     int Badtoken() { return 0; }
  36.  
  37.     int Kind(int i)   { return ((Token) tokens.elementAt(i)).kind; }
  38.  
  39.     String Name(int i) { return ((Token) tokens.elementAt(i)).name; }
  40.  
  41.     //*
  42.     //* Constructors and Destructor.
  43.     //*
  44.     LexStream(String filename) throws java.io.FileNotFoundException
  45.     {
  46.         srcfile = new FileInputStream(filename);
  47.     }
  48.  
  49.  
  50.     void dump()
  51.     {
  52.         for (int i = 1; i < tokens.size(); i++)
  53.         {
  54.             System.out.print(" (");
  55.             switch(((Token) tokens.elementAt(i)).kind)
  56.             {
  57.                 case TK_SYMBOL:        System.out.print("SYMBOL"); break;
  58.                 case TK_OR:            System.out.print("|"); break;
  59.                 case TK_PRODUCES:      System.out.print("::="); break;
  60.                 case TK_EOF:           System.out.print("EOF"); break;
  61.                 default:               System.out.print(((Token) tokens.elementAt(i)).kind);
  62.             }
  63.             System.out.print(") : \"");
  64.             System.out.print(((Token) tokens.elementAt(i)).name);
  65.             System.out.println("\"");
  66.         }
  67.     }
  68.  
  69.     Vector tokens = new Vector();
  70.     FileInputStream srcfile;
  71. }
  72.