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

  1. // $Id: LexStream.java,v 1.3 1999/11/04 14:02:18 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.InputStream;
  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 exprsym
  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.     void dump()
  42.     {
  43.         for (int i = 1; i < tokens.size(); i++)
  44.         {
  45.             System.out.print(" (");
  46.             switch(((Token) tokens.elementAt(i)).kind)
  47.             {
  48.                 case TK_PLUS:      System.out.print("PLUS"); break;
  49.                 case TK_MINUS:     System.out.print("MINUS"); break;
  50.                 case TK_STAR:      System.out.print("STAR"); break;
  51.                 case TK_SLASH:     System.out.print("SLASH"); break;
  52.                 case TK_LPAREN:    System.out.print("LPAREN"); break;
  53.                 case TK_RPAREN:    System.out.print("RPAREN"); break;
  54.                 case TK_NUMBER:    System.out.print("NUMBER"); break;
  55.                 case TK_ERROR:     System.out.print("ERROR"); break;
  56.                 case TK_EOF:       System.out.print("EOF"); break;
  57.                 default:           System.out.print(((Token) tokens.elementAt(i)).kind);
  58.             }
  59.             System.out.print(") : \"");
  60.             System.out.print(((Token) tokens.elementAt(i)).name);
  61.             System.out.println("\"");
  62.         }
  63.     }
  64.  
  65.     Vector tokens = new Vector();
  66. }
  67.