home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / jikepg12.zip / jikespg / examples / bnf / Scanner.java < prev    next >
Encoding:
Text File  |  1999-11-04  |  2.2 KB  |  84 lines

  1. // $Id: Scanner.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. //
  10. // The Scanner object
  11. //
  12. class Scanner implements bnfsym
  13. {
  14.     int next_byte;
  15.     Option option;
  16.     LexStream lex_stream;
  17.  
  18.     Scanner(Option option, LexStream lex_stream)
  19.     {
  20.         this.lex_stream = lex_stream;
  21.         this.option = option;
  22.     }
  23.  
  24.     //
  25.     //
  26.     //
  27.     void skip_spaces() throws java.io.IOException
  28.     {
  29.         while (next_byte >= 0 && Character.isSpace((char) next_byte))
  30.             next_byte = lex_stream.srcfile.read();
  31.         return;
  32.     }
  33.  
  34.     //
  35.     //
  36.     //
  37.     String scan_symbol() throws java.io.IOException
  38.     {
  39.         StringBuffer buffer = new StringBuffer();
  40.         while (next_byte >= 0 && (! Character.isSpace((char) next_byte)))
  41.         {
  42.             buffer.append((char) next_byte);
  43.             next_byte = lex_stream.srcfile.read();
  44.         }
  45.  
  46.         return buffer.toString();
  47.     }
  48.  
  49.     //
  50.     //
  51.     //
  52.     void scan() throws java.io.IOException
  53.     {
  54.         //
  55.         // Do not use token indexed at location 0.
  56.         //
  57.         Token start_token = new Token();
  58.         start_token.kind = 0;
  59.         start_token.name = "";
  60.         lex_stream.tokens.addElement(start_token);
  61.  
  62.         next_byte = lex_stream.srcfile.read();
  63.         for (skip_spaces(); next_byte >= 0; skip_spaces())
  64.         {
  65.             Token token = new Token();
  66.             token.name = scan_symbol();
  67.             lex_stream.tokens.addElement(token);
  68.  
  69.             if (token.name.equals("::="))
  70.                  token.kind = TK_PRODUCES;
  71.             else if (token.name.equals("|"))
  72.                  token.kind = TK_OR;
  73.             else token.kind = TK_SYMBOL;
  74.         }
  75.  
  76.         Token end_token = new Token();
  77.         end_token.kind = TK_EOF;
  78.         end_token.name = "";
  79.         lex_stream.tokens.addElement(end_token);
  80.  
  81.         return;
  82.     }
  83. }
  84.