KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > JFlex > HiLowEmitter


1 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2  * jflex *
3  * Copyright (C) 1998-2004 Gerwin Klein <lsf@jflex.de> *
4  * All rights reserved. *
5  * *
6  * This program is free software; you can redistribute it and/or modify *
7  * it under the terms of the GNU General Public License. See the file *
8  * COPYRIGHT for more information. *
9  * *
10  * This program is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU General Public License along *
16  * with this program; if not, write to the Free Software Foundation, Inc., *
17  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
18  * *
19  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

20
21 package JFlex;
22
23 /**
24  * HiLowEmitter
25  *
26  * @author Gerwin Klein
27  * @version $Revision: 1.5 $, $Date: 2004/04/12 10:07:48 $
28  */

29 public class HiLowEmitter extends PackEmitter {
30
31   /** number of entries in expanded array */
32   private int numEntries;
33
34   /**
35    * Create new emitter for values in [0, 0xFFFFFFFF] using hi/low encoding.
36    *
37    * @param name the name of the generated array
38    */

39   public HiLowEmitter(String JavaDoc name) {
40     super(name);
41   }
42
43   /**
44    * Emits hi/low pair unpacking code for the generated array.
45    *
46    * @see JFlex.PackEmitter#emitUnPack()
47    */

48   public void emitUnpack() {
49     // close last string chunk:
50
println("\";");
51     nl();
52     println(" private static int [] zzUnpack"+name+"() {");
53     println(" int [] result = new int["+numEntries+"];");
54     println(" int offset = 0;");
55
56     for (int i = 0; i < chunks; i++) {
57       println(" offset = zzUnpack"+name+"("+constName()+"_PACKED_"+i+", offset, result);");
58     }
59
60     println(" return result;");
61     println(" }");
62
63     nl();
64     println(" private static int zzUnpack"+name+"(String packed, int offset, int [] result) {");
65     println(" int i = 0; /* index in packed string */");
66     println(" int j = offset; /* index in unpacked array */");
67     println(" int l = packed.length();");
68     println(" while (i < l) {");
69     println(" int high = packed.charAt(i++) << 16;");
70     println(" result[j++] = high | packed.charAt(i++);");
71     println(" }");
72     println(" return j;");
73     println(" }");
74   }
75
76   /**
77    * Emit one value using two characters.
78    *
79    * @param val the value to emit
80    * @prec 0 <= val <= 0xFFFFFFFF
81    */

82   public void emit(int val) {
83     numEntries+= 1;
84     breaks();
85     emitUC(val >> 16);
86     emitUC(val & 0xFFFF);
87   }
88 }
89
Popular Tags