| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 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 |
|
| 99 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 200 |
p1 = Pattern.compile("^(void)(\\s+\\*|\\*\\s+)(\\w+)$"); |
| 201 |
m1 = p1.matcher(def); |
| 202 |
if (m1.find()) { |
| 203 |
final String type = "void*"; |
| 204 |
|
| 205 |
result.put(m1.group(3), new Bucket(m1.group(3), type, false)); |
| 206 |
return result; |
| 207 |
} |
| 208 |
|
| 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 |
|
| 214 |
result.put(m1.group(3), new Bucket(m1.group(3), type, false)); |
| 215 |
return result; |
| 216 |
} |
| 217 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 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 |
|
| 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 |
|
| 363 |
|
| 364 |
|
| 365 |
} |
| 366 |
|
| 367 |
public int getArrayLength() { |
| 368 |
if (ctype == null) { |
| 369 |
return 0; |
| 370 |
} |
| 371 |
return ctype.getArrayLength(); |
| 372 |
} |
| 373 |
|
| 374 |
} |