SimpleByteLexer.java

  1. /* $Id$
  2.  *****************************************************************************
  3.  * Copyright (c) 2009 Contributors - see below
  4.  * All rights reserved. This program and the accompanying materials
  5.  * are made available under the terms of the Eclipse Public License v1.0
  6.  * which accompanies this distribution, and is available at
  7.  * http://www.eclipse.org/legal/epl-v10.html
  8.  *
  9.  * Contributors:
  10.  *    thn
  11.  *****************************************************************************
  12.  *
  13.  * Some portions of this file was previously release using the BSD License:
  14.  */

  15. // Copyright (c) 1996-2006 The Regents of the University of California. All
  16. // Rights Reserved. Permission to use, copy, modify, and distribute this
  17. // software and its documentation without fee, and without a written
  18. // agreement is hereby granted, provided that the above copyright notice
  19. // and this paragraph appear in all copies.  This software program and
  20. // documentation are copyrighted by The Regents of the University of
  21. // California. The software program and documentation are supplied "AS
  22. // IS", without any accompanying services from The Regents. The Regents
  23. // does not warrant that the operation of the program will be
  24. // uninterrupted or error-free. The end-user understands that the program
  25. // was developed for research purposes and is advised not to rely
  26. // exclusively on the program for any reason.  IN NO EVENT SHALL THE
  27. // UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT,
  28. // SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS,
  29. // ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
  30. // THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
  31. // SUCH DAMAGE. THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY
  32. // WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  33. // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
  34. // PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
  35. // CALIFORNIA HAS NO OBLIGATIONS TO PROVIDE MAINTENANCE, SUPPORT,
  36. // UPDATES, ENHANCEMENTS, OR MODIFICATIONS.

  37. package org.argouml.language.java.reveng.classfile;

  38. import java.io.IOException;
  39. import java.io.InputStream;

  40. import antlr.Token;
  41. import antlr.TokenStream;
  42. import antlr.TokenStreamException;
  43. import antlr.TokenStreamIOException;



  44. /**************************************
  45.  * A simple lexer to scan a bytestream
  46.  * Using a generated scanner results in
  47.  * too much overhead and performance
  48.  * issues.
  49.  **************************************/
  50. public class SimpleByteLexer implements ClassfileTokenTypes, TokenStream {

  51.     //////////////////////
  52.     // Instance variables.

  53.     private InputStream input = null;


  54.     ///////////////
  55.     // Constructors

  56.     /**
  57.      * Create a new bytestream scanner.
  58.      * The constructor.
  59.      *
  60.      * @param in the given inputstream
  61.      */
  62.     public SimpleByteLexer(InputStream in) {
  63.         input = in;
  64.     }


  65.     //////////
  66.     // Methods

  67.     /**
  68.      * Return the next byte as a token.
  69.      *
  70.      * @return The next byte as a token or a EOF token.
  71.      * @throws TokenStreamException if the next byte cannot be read
  72.      * @see antlr.TokenStream#nextToken()
  73.      */
  74.     public final Token nextToken() throws TokenStreamException {
  75.     int nextByte;

  76.     try {
  77.         nextByte = input.read();
  78.     } catch (IOException ie) {
  79.         throw new TokenStreamIOException(ie);
  80.     }

  81.     // System.out.println("Generating token for: " + nextByte);

  82.     return (nextByte == -1)
  83.         ? new ByteToken( Token.EOF_TYPE)
  84.                 : new ByteToken( BYTE, (byte) nextByte);
  85.     }
  86. }