1 | /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
|
---|
2 | * jflex 1.4 *
|
---|
3 | * Copyright (C) 1998-2009 Gerwin Klein <[email protected]> *
|
---|
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 | * An emitter for an array encoded as count/value pairs in a string.
|
---|
25 | *
|
---|
26 | * @author Gerwin Klein
|
---|
27 | * @version $Revision: 433 $, $Date: 2009-01-31 19:52:34 +1100 (Sat, 31 Jan 2009) $
|
---|
28 | */
|
---|
29 | public class CountEmitter extends PackEmitter {
|
---|
30 | /** number of entries in expanded array */
|
---|
31 | private int numEntries;
|
---|
32 |
|
---|
33 | /** translate all values by this amount */
|
---|
34 | private int translate = 0;
|
---|
35 |
|
---|
36 |
|
---|
37 | /**
|
---|
38 | * Create a count/value emitter for a specific field.
|
---|
39 | *
|
---|
40 | * @param name name of the generated array
|
---|
41 | */
|
---|
42 | protected CountEmitter(String name) {
|
---|
43 | super(name);
|
---|
44 | }
|
---|
45 |
|
---|
46 | /**
|
---|
47 | * Emits count/value unpacking code for the generated array.
|
---|
48 | *
|
---|
49 | * @see JFlex.PackEmitter#emitUnPack()
|
---|
50 | */
|
---|
51 | public void emitUnpack() {
|
---|
52 | // close last string chunk:
|
---|
53 | println("\";");
|
---|
54 |
|
---|
55 | nl();
|
---|
56 | println(" private static int [] zzUnpack"+name+"() {");
|
---|
57 | println(" int [] result = new int["+numEntries+"];");
|
---|
58 | println(" int offset = 0;");
|
---|
59 |
|
---|
60 | for (int i = 0; i < chunks; i++) {
|
---|
61 | println(" offset = zzUnpack"+name+"("+constName()+"_PACKED_"+i+", offset, result);");
|
---|
62 | }
|
---|
63 |
|
---|
64 | println(" return result;");
|
---|
65 | println(" }");
|
---|
66 | nl();
|
---|
67 |
|
---|
68 | println(" private static int zzUnpack"+name+"(String packed, int offset, int [] result) {");
|
---|
69 | println(" int i = 0; /* index in packed string */");
|
---|
70 | println(" int j = offset; /* index in unpacked array */");
|
---|
71 | println(" int l = packed.length();");
|
---|
72 | println(" while (i < l) {");
|
---|
73 | println(" int count = packed.charAt(i++);");
|
---|
74 | println(" int value = packed.charAt(i++);");
|
---|
75 | if (translate == 1) {
|
---|
76 | println(" value--;");
|
---|
77 | }
|
---|
78 | else if (translate != 0) {
|
---|
79 | println(" value-= "+translate);
|
---|
80 | }
|
---|
81 | println(" do result[j++] = value; while (--count > 0);");
|
---|
82 | println(" }");
|
---|
83 | println(" return j;");
|
---|
84 | println(" }");
|
---|
85 | }
|
---|
86 |
|
---|
87 | /**
|
---|
88 | * Translate all values by given amount.
|
---|
89 | *
|
---|
90 | * Use to move value interval from [0, 0xFFFF] to something different.
|
---|
91 | *
|
---|
92 | * @param i amount the value will be translated by.
|
---|
93 | * Example: <code>i = 1</code> allows values in [-1, 0xFFFE].
|
---|
94 | */
|
---|
95 | public void setValTranslation(int i) {
|
---|
96 | this.translate = i;
|
---|
97 | }
|
---|
98 |
|
---|
99 | /**
|
---|
100 | * Emit one count/value pair.
|
---|
101 | *
|
---|
102 | * Automatically translates value by the <code>translate</code> value.
|
---|
103 | *
|
---|
104 | * @param count
|
---|
105 | * @param value
|
---|
106 | *
|
---|
107 | * @see CountEmitter#setValTranslation(int)
|
---|
108 | */
|
---|
109 | public void emit(int count, int value) {
|
---|
110 | numEntries+= count;
|
---|
111 | breaks();
|
---|
112 | emitUC(count);
|
---|
113 | emitUC(value+translate);
|
---|
114 | }
|
---|
115 | }
|
---|