Debian Codegraph @

src/smetana/core/Bucket.java

raw symbols SCIP
1 /* ========================================================================
2 * PlantUML : a free UML diagram generator
3 * ========================================================================
4 *
5 * Project Info: http://plantuml.com
6 *
7 * If you like this project or if you find it useful, you can support us at:
8 *
9 * http://plantuml.com/patreon (only 1$ per month!)
10 * http://plantuml.com/paypal
11 *
12 * This file is part of Smetana.
13 * Smetana is a partial translation of Graphviz/Dot sources from C to Java.
14 *
15 * (C) Copyright 2009-2020, Arnaud Roques
16 *
17 * This translation is distributed under the same Licence as the original C program.
18 *
19 * THE ACCOMPANYING PROGRAM IS PROVIDED UNDER THE TERMS OF THIS ECLIPSE PUBLIC
20 * LICENSE ("AGREEMENT"). [Eclipse Public License - v 1.0]
21 *
22 * ANY USE, REPRODUCTION OR DISTRIBUTION OF THE PROGRAM CONSTITUTES
23 * RECIPIENT'S ACCEPTANCE OF THIS AGREEMENT.
24 *
25 * You may obtain a copy of the License at
26 *
27 * http://www.eclipse.org/legal/epl-v10.html
28 *
29 * Unless required by applicable law or agreed to in writing, software
30 * distributed under the License is distributed on an "AS IS" BASIS,
31 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
32 * See the License for the specific language governing permissions and
33 * limitations under the License.
34 *
35 */
36
37 package smetana.core;
38
39 import java.util.ArrayList;
40 import java.util.Iterator;
41 import java.util.LinkedHashMap;
42 import java.util.List;
43 import java.util.Map;
44 import java.util.regex.Matcher;
45 import java.util.regex.Pattern;
46
47 public class Bucket {
48
49 final public String name;
50 final public CType ctype;
51 final private boolean inlineStruct;
52
53 private Bucket addPrefix(String prefix) {
54 return new Bucket(prefix + "." + name, ctype, inlineStruct);
55 }
56
57 public static Bucket buildEnum(String name, List<String> definition) {
58 return new Bucket(name, new CType("enum"), false);
59 }
60
61 Bucket(String name, String type, boolean inlineStruct) {
62 this(name, new CType(type), inlineStruct);
63 }
64
65 private Bucket(String name, CType ctype, boolean inlineStruct) {
66 this.name = name;
67 this.ctype = ctype;
68 this.inlineStruct = inlineStruct;
69 }
70
71 @Override
72 public String toString() {
73 return "[" + ctype + "==" + name + "]";
74 }
75
76 public boolean inlineStruct() {
77 return inlineStruct;
78 }
79
80 public boolean functionPointer() {
81 if (ctype == null) {
82 return false;
83 }
84 return ctype.functionPointer();
85 }
86
87 public static Map<String, Bucket> buildSome(Iterator<String> it) {
88
89 String def = it.next();
90 JUtils.LOG("DEF1=" + def);
91 def = def.replaceAll("unsigned int", "int");
92 def = def.replaceAll("unsigned long", "int");
93 def = def.replaceAll("unsigned short", "short");
94 def = def.replaceAll("unsigned char", "char");
95 def = def.replaceAll("const ", "");
96 def = def.replaceAll("struct ", "");
97 def = def.replaceAll("\\[\\s*[+]1\\s*[+]1\\s*[+]1\\s*[+]1\\s*[+]1\\s*\\]", "[5]");
98 // JUtils.LOG("DEF2=" + def);
99 // int foo
100
101 if (def.equals("union")) {
102 return createStruct(it);
103 } else if (def.equals("struct")) {
104 return createStruct(it);
105 }
106
107 final Map<String, Bucket> result = new LinkedHashMap<String, Bucket>();
108
109 // Dummy foo;
110 Pattern p1 = Pattern.compile("^(\\w+)\\s+(\\w+)$");
111 Matcher m1 = p1.matcher(def);
112 if (m1.find()) {
113 final String type = m1.group(1);
114 final boolean inline = CType.isPrimitive(type) == false;
115 result.put(m1.group(2), new Bucket(m1.group(2), type, inline));
116 return result;
117 }
118 // Dummy A, B
119 p1 = Pattern.compile("^(\\w+)\\s+(\\w+),\\s*(\\w+)$");
120 m1 = p1.matcher(def);
121 if (m1.find()) {
122 final String type = m1.group(1);
123 final boolean inline = CType.isPrimitive(type) == false;
124 result.put(m1.group(2), new Bucket(m1.group(2), type, inline));
125 result.put(m1.group(3), new Bucket(m1.group(3), type, inline));
126 return result;
127 }
128 // Dummy A, B, C
129 p1 = Pattern.compile("^(\\w+)\\s+(\\w+),\\s*(\\w+),\\s*(\\w+)$");
130 m1 = p1.matcher(def);
131 if (m1.find()) {
132 final String type = m1.group(1);
133 final boolean inline = CType.isPrimitive(type) == false;
134 result.put(m1.group(2), new Bucket(m1.group(2), type, inline));
135 result.put(m1.group(3), new Bucket(m1.group(3), type, inline));
136 result.put(m1.group(4), new Bucket(m1.group(4), type, inline));
137 return result;
138 }
139 // Dummy A, B, C, D
140 p1 = Pattern.compile("^(\\w+)\\s+(\\w+),\\s*(\\w+),\\s*(\\w+),\\s*(\\w+)$");
141 m1 = p1.matcher(def);
142 if (m1.find()) {
143 final String type = m1.group(1);
144 final boolean inline = CType.isPrimitive(type) == false;
145 result.put(m1.group(2), new Bucket(m1.group(2), type, inline));
146 result.put(m1.group(3), new Bucket(m1.group(3), type, inline));
147 result.put(m1.group(4), new Bucket(m1.group(4), type, inline));
148 result.put(m1.group(5), new Bucket(m1.group(5), type, inline));
149 return result;
150 }
151 // Dummy A, B, C, D, E
152 p1 = Pattern.compile("^(\\w+)\\s+(\\w+),\\s*(\\w+),\\s*(\\w+),\\s*(\\w+),\\s*(\\w+)$");
153 m1 = p1.matcher(def);
154 if (m1.find()) {
155 final String type = m1.group(1);
156 final boolean inline = CType.isPrimitive(type) == false;
157 result.put(m1.group(2), new Bucket(m1.group(2), type, inline));
158 result.put(m1.group(3), new Bucket(m1.group(3), type, inline));
159 result.put(m1.group(4), new Bucket(m1.group(4), type, inline));
160 result.put(m1.group(5), new Bucket(m1.group(5), type, inline));
161 result.put(m1.group(6), new Bucket(m1.group(6), type, inline));
162 return result;
163 }
164 // int (*foo)()
165 p1 = Pattern.compile("^.*(\\(\\*(\\w+)\\)).*$");
166 m1 = p1.matcher(def);
167 if (m1.find()) {
168 result.put(m1.group(1), new Bucket(m1.group(2), def, false));
169 return result;
170 }
171 // void *data
172 p1 = Pattern.compile("^(void)\\s+\\*(\\w+)$");
173 m1 = p1.matcher(def);
174 if (m1.find()) {
175 result.put(m1.group(2), new Bucket(m1.group(2), "void*", false));
176 return result;
177 }
178 // char *data
179 p1 = Pattern.compile("^(char)\\s+\\*(\\w+)$");
180 m1 = p1.matcher(def);
181 if (m1.find()) {
182 result.put(m1.group(2), new Bucket(m1.group(2), "CString", false));
183 return result;
184 }
185 // int *data
186 p1 = Pattern.compile("^(int)\\s+\\*(\\w+)$");
187 m1 = p1.matcher(def);
188 if (m1.find()) {
189 result.put(m1.group(2), new Bucket(m1.group(2), "int*", false));
190 return result;
191 }
192 // char **data
193 p1 = Pattern.compile("^(char)\\s+\\*\\*(\\w+)$");
194 m1 = p1.matcher(def);
195 if (m1.find()) {
196 result.put(m1.group(2), new Bucket(m1.group(2), "CString[]", false));
197 return result;
198 }
199 // void* foo
200 p1 = Pattern.compile("^(void)(\\s+\\*|\\*\\s+)(\\w+)$");
201 m1 = p1.matcher(def);
202 if (m1.find()) {
203 final String type = "void*";
204 // final Class cl = CType.getClassFrom(type);
205 result.put(m1.group(3), new Bucket(m1.group(3), type, false));
206 return result;
207 }
208 // dummy* foo
209 p1 = Pattern.compile("^(\\w+)(\\s+\\*|\\*\\s+)(\\w+)$");
210 m1 = p1.matcher(def);
211 if (m1.find()) {
212 final String type = m1.group(1);
213 // final Class cl = CType.getClassFrom(type);
214 result.put(m1.group(3), new Bucket(m1.group(3), type, false));
215 return result;
216 }
217 // foo *A, *B
218 p1 = Pattern.compile("^(\\w+)\\s+\\*(\\w+),\\s*\\*(\\w+)$");
219 m1 = p1.matcher(def);
220 if (m1.find()) {
221 final String type = m1.group(1);
222 // final Class cl = CType.getClassFrom(type);
223 result.put(m1.group(2), new Bucket(m1.group(2), type, false));
224 result.put(m1.group(3), new Bucket(m1.group(3), type, false));
225 return result;
226 }
227 // foo *A, *B, *C
228 p1 = Pattern.compile("^(\\w+)\\s+\\*(\\w+),\\s*\\*(\\w+),\\s*\\*(\\w+)$");
229 m1 = p1.matcher(def);
230 if (m1.find()) {
231 final String type = m1.group(1);
232 // final Class cl = CType.getClassFrom(type);
233 result.put(m1.group(2), new Bucket(m1.group(2), type, false));
234 result.put(m1.group(3), new Bucket(m1.group(3), type, false));
235 result.put(m1.group(4), new Bucket(m1.group(4), type, false));
236 return result;
237 }
238 // foo *A, *B, *C, *D
239 p1 = Pattern.compile("^(\\w+)\\s+\\*(\\w+),\\s*\\*(\\w+),\\s*\\*(\\w+),\\s*\\*(\\w+)$");
240 m1 = p1.matcher(def);
241 if (m1.find()) {
242 final String type = m1.group(1);
243 // final Class cl = CType.getClassFrom(type);
244 result.put(m1.group(2), new Bucket(m1.group(2), type, false));
245 result.put(m1.group(3), new Bucket(m1.group(3), type, false));
246 result.put(m1.group(4), new Bucket(m1.group(4), type, false));
247 result.put(m1.group(5), new Bucket(m1.group(5), type, false));
248 return result;
249 }
250 p1 = Pattern.compile("^(unsigned|int)\\s+(\\w+):([-sizeof ntunsged()0-9*]+)$");
251 m1 = p1.matcher(def);
252 if (m1.find()) {
253 result.put(m1.group(2), new Bucket(m1.group(2), "int", false));
254 return result;
255 }
256 // Dummy foo[3]
257 p1 = Pattern.compile("^(\\w+)\\s+(\\w+)\\[\\d+\\]$");
258 m1 = p1.matcher(def);
259 if (m1.find()) {
260 result.put(m1.group(1), new Bucket(m1.group(2), def, true));
261 return result;
262 }
263 // Dummy *foo[3]
264 p1 = Pattern.compile("^(\\w+)\\s+\\*(\\w+)\\[\\d+\\]$");
265 m1 = p1.matcher(def);
266 if (m1.find()) {
267 final Bucket tmp = new Bucket(m1.group(2), def, false);
268 JUtils.LOG("size=" + tmp.getArrayLength());
269 result.put(m1.group(1), tmp);
270 return result;
271 }
272 // graph_t **clust
273 p1 = Pattern.compile("^(\\w+)\\s+\\*\\*(\\w+)$");
274 m1 = p1.matcher(def);
275 if (m1.find()) {
276 final String type = m1.group(1) + "*";
277 result.put(m1.group(2), new Bucket(m1.group(2), type, false));
278 return result;
279 }
280 // node_t **store, **limit, **head, **tail
281 p1 = Pattern.compile("^(\\w+)\\s+\\*\\*(\\w+),\\s*\\*\\*(\\w+),\\s*\\*\\*(\\w+),\\s*\\*\\*(\\w+)$");
282 m1 = p1.matcher(def);
283 if (m1.find()) {
284 final String type = m1.group(1) + "*";
285 result.put(m1.group(2), new Bucket(m1.group(2), type, false));
286 result.put(m1.group(3), new Bucket(m1.group(3), type, false));
287 result.put(m1.group(4), new Bucket(m1.group(4), type, false));
288 result.put(m1.group(5), new Bucket(m1.group(5), type, false));
289 return result;
290 }
291 // foo *A, **B
292 p1 = Pattern.compile("^(\\w+)\\s+\\*(\\w+),\\s*\\*\\*(\\w+)$");
293 m1 = p1.matcher(def);
294 if (m1.find()) {
295 final String type = m1.group(1);
296 result.put(m1.group(2), new Bucket(m1.group(2), type, false));
297 result.put(m1.group(3), new Bucket(m1.group(3), type + "*", false));
298 return result;
299 }
300 // foo **B
301 p1 = Pattern.compile("^(\\w+)\\*\\*\\s+(\\w+)$");
302 m1 = p1.matcher(def);
303 if (m1.find()) {
304 final String type = m1.group(1) + "*";
305 result.put(m1.group(2), new Bucket(m1.group(2), type, false));
306 return result;
307 }
308 if (def.equals("double **dist, **spring, **sum_t, ***t")) {
309 // Let's skip this one...
310 return result;
311 }
312 if (def.equals("double *pos, dist")) {
313 result.put("pos", new Bucket("pos", "double*", false));
314 result.put("dist", new Bucket("dist", "double", false));
315 return result;
316 }
317 // p1 = Pattern.compile("^unsigned\\s+long\\s+(\\w+)$");
318 // m1 = p1.matcher(def);
319 // if (m1.find()) {
320 // mem.put(m1.group(1), new Bucket(m1.group(1), "int", false, 4, false));
321 // return;
322 // }
323 JUtils.LOG("def=" + def);
324 throw new UnsupportedOperationException(def);
325 }
326
327 private static Map<String, Bucket> createStruct(Iterator<String> it) {
328 String current = it.next();
329 if (current.equals("{") == false) {
330 throw new UnsupportedOperationException("current=" + current);
331 }
332 final Map<String, Bucket> tmp = new LinkedHashMap<String, Bucket>();
333 final List<String> inner = new ArrayList<String>();
334 while (true) {
335 current = it.next();
336 if (current.equals("union") || current.equals("struct")) {
337 Map<String, Bucket> internal = createStruct(it);
338 JUtils.LOG("internal=" + internal);
339 tmp.putAll(internal);
340 } else if (current.endsWith("}")) {
341 final String name = it.next();
342 JUtils.LOG("name=" + name);
343 JUtils.LOG("inner=" + inner);
344 // final Map<String, Bucket> some = new LinkedHashMap<String, Bucket>();
345 final Iterator<String> innerIt = inner.iterator();
346 while (innerIt.hasNext()) {
347 tmp.putAll(buildSome(innerIt));
348 }
349 final Map<String, Bucket> result = new LinkedHashMap<String, Bucket>();
350 for (Map.Entry<String, Bucket> ent : tmp.entrySet()) {
351 final String n = ent.getKey();
352 final Bucket bucket = ent.getValue().addPrefix(name);
353 JUtils.LOG("n=" + n);
354 JUtils.LOG("bucket=" + bucket);
355 result.put(name + "." + n, bucket);
356 }
357 return result;
358 } else {
359 inner.add(current);
360 }
361 }
362 // JUtils.LOG("some=" + some);
363 // throw new UnsupportedOperationException();
364
365 }
366
367 public int getArrayLength() {
368 if (ctype == null) {
369 return 0;
370 }
371 return ctype.getArrayLength();
372 }
373
374 }