Main Page   Packages   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members  

UnicodePreprocessor.java

00001 package edu.ksu.cis.bandera.jjjc.unicodepreprocessor;
00002 
00003 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
00004  * Bandera, a Java(TM) analysis and transformation toolkit           *
00005  * Copyright (C) 1998-2001 SAnToS Laboratories (santos@cis.ksu.edu)  *
00006 
00007  * All rights reserved.                                              *
00008  *                                                                   *
00009  * This work was done as a project in the SAnToS Laboratory,         *
00010  * Department of Computing and Information Sciences, Kansas State    *
00011  * University, USA (http://www.cis.ksu.edu/santos).                  *
00012  * It is understood that any modification not identified as such is  *
00013  * not covered by the preceding statement.                           *
00014  *                                                                   *
00015  * This work is free software; you can redistribute it and/or        *
00016  * modify it under the terms of the GNU Library General Public       *
00017  * License as published by the Free Software Foundation; either      *
00018  * version 2 of the License, or (at your option) any later version.  *
00019  *                                                                   *
00020  * This work is distributed in the hope that it will be useful,      *
00021  * but WITHOUT ANY WARRANTY; without even the implied warranty of    *
00022  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU *
00023  * Library General Public License for more details.                  *
00024  *                                                                   *
00025  * You should have received a copy of the GNU Library General Public *
00026  * License along with this toolkit; if not, write to the             *
00027  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,      *
00028  * Boston, MA  02111-1307, USA.                                      *
00029  *                                                                   *
00030  * Java is a trademark of Sun Microsystems, Inc.                     *
00031  *                                                                   *
00032  * To submit a bug report, send a comment, or get the latest news on *
00033  * this project and other SAnToS projects, please visit the web-site *
00034  *                http://www.cis.ksu.edu/santos                      *
00035  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
00036 import java.io.*;
00037 
00038 import edu.ksu.cis.bandera.jjjc.unicodepreprocessor.lexer.*;
00039 import edu.ksu.cis.bandera.jjjc.unicodepreprocessor.node.*;
00040 import edu.ksu.cis.bandera.jjjc.unicodepreprocessor.analysis.*;
00041 
00042 public class UnicodePreprocessor extends Reader {
00043     private UnicodeLexer lexer;
00044     private ProcessToken processor = new ProcessToken();
00045 
00046     int available;
00047     char[] buffer = new char[2];
00048 
00049     private class ProcessToken extends AnalysisAdapter {
00050         public void caseTEvenBackslash(TEvenBackslash node) {
00051             buffer[0] = '\\';
00052             buffer[1] = '\\';
00053             available = 2;
00054         }
00055 
00056         public void caseTUnicodeEscape(TUnicodeEscape node) {
00057             String text = node.getText();
00058             buffer[0] = (char) Integer.parseInt(
00059                     text.substring(text.length() - 4), 16);
00060             available = 1;
00061         }
00062 
00063         public void caseTErroneousEscape(TErroneousEscape node) {
00064             throw new RuntimeException("Erroneous escape: " + node);
00065         }
00066 
00067         public void caseTSub(TSub node) {
00068             buffer[0] = node.getText().charAt(0);
00069             available = 1;
00070         }
00071 
00072         public void caseTRawInputCharacter(TRawInputCharacter node) {
00073             buffer[0] = node.getText().charAt(0);
00074             available = 1;
00075         }
00076 
00077         public void caseEOF(EOF node) {
00078             available = 0;
00079         }
00080     }
00081 
00082     public UnicodePreprocessor(PushbackReader in) {
00083         lexer = new UnicodeLexer(in);
00084     }
00085     public void close() {
00086     }
00087     public int read() throws IOException {
00088         if (available == 0) {
00089             try {
00090                 lexer.next().apply(processor);
00091             } catch (LexerException e) {
00092                 throw new RuntimeException(e.toString());
00093             }
00094         }
00095 
00096         if (available == 0) {
00097             return -1;
00098         }
00099 
00100         char c = buffer[0];
00101         buffer[0] = buffer[1];
00102         available--;
00103 
00104         return c;
00105     }
00106     public int read(char cbuf[], int off, int len) throws IOException {
00107         for (int i = 0; i < len; i++) {
00108             int c = read();
00109 
00110             if (c == -1) {
00111                 if (i == 0) {
00112                     return -1;
00113                 } else {
00114                     return i;
00115                 }
00116             }
00117 
00118             cbuf[off + i] = (char) c;
00119         }
00120 
00121         return len;
00122     }
00123 }

Generated at Thu Feb 7 06:58:56 2002 for Bandera by doxygen1.2.10 written by Dimitri van Heesch, © 1997-2001