Commit c1860d7f authored by Milhomme Allan's avatar Milhomme Allan
Browse files

Add array null deletion

parent 03b00723
No preview for this file type
T = {"a" [b:"b" [c:"c"]]}
......@@ -163,11 +163,11 @@ public class ClassGenerator
builder.append(", ");
}
if (a.minSize.length != 0 || a.maxSize.length != 0)
if (a.minSize != null || a.maxSize != null)
{
builder.append(a.minSize.length == 0 ? 1 : Integer.valueOf(Token.getStringFromTokens(a.minSize)));
builder.append(a.minSize == null ? 1 : Integer.valueOf(Token.getStringFromTokens(a.minSize)));
builder.append(", ");
builder.append(a.maxSize.length == 0 ? "Integer.MAX_VALUE" : Integer.valueOf(Token.getStringFromTokens(a.maxSize)));
builder.append(a.maxSize == null ? "Integer.MAX_VALUE" : Integer.valueOf(Token.getStringFromTokens(a.maxSize)));
builder.append(", ");
}
}
......
......@@ -190,12 +190,12 @@ public class Compiler
{
ArraySearchToken ats = (ArraySearchToken)token;
int min, max;
if (ats.minSize.length != 0)
if (ats.minSize != null)
min = Integer.valueOf(Token.getStringFromTokens(ats.minSize));
else
min = 1;
if (ats.maxSize.length != 0)
if (ats.maxSize != null)
max = Integer.valueOf(Token.getStringFromTokens(ats.maxSize));
else
max = Integer.MAX_VALUE;
......
......@@ -2,6 +2,7 @@ package parser;
import java.lang.reflect.Array;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
......@@ -20,7 +21,7 @@ public class TokenCreator
{
if (ss[i].getSearchName() != null)
write(build, ss[i].getSearchName(), pos, max, null);
if (ss[i] instanceof ArraySearch)
/*if (ss[i] instanceof ArraySearch)
{
pos.add(i);
max.add(0);
......@@ -30,10 +31,10 @@ public class TokenCreator
max.removeLast();
pos.removeLast();
}
}*/
}
}
if (result.isToken())
{
if (result.getSearch().getSearchName() != null)
......@@ -66,7 +67,7 @@ public class TokenCreator
}
}
private static void write(Token build, String to, List<Integer> pos, List<Integer> max, Result toWrite) throws Exception
private static void write(Token build, String to, LinkedList<Integer> pos, LinkedList<Integer> max, Result toWrite) throws Exception
{
if (pos.isEmpty())
{
......@@ -74,17 +75,17 @@ public class TokenCreator
f.setAccessible(true);
boolean assignable = Token.class.isAssignableFrom(f.getType());
if (assignable && toWrite != null && toWrite.isToken())
f.set(build, toWrite.getToken());
setFieldValue(build, f, toWrite.getToken());
else if (assignable && toWrite != null && toWrite.getSearch() instanceof BracketSearch && toWrite.getArray().length == 1)
f.set(build, toWrite.getArray()[0].getToken());
setFieldValue(build, f, toWrite.getArray()[0].getToken());
else
f.set(build, toWrite);
setFieldValue(build, f, toWrite);
}
else
{
Object[] current, total;
Class<?> currentType = build.getClass().getDeclaredField(to).getType().getComponentType();
Field f = build.getClass().getDeclaredField(to);
Class<?> currentType = f.getType().getComponentType();
f.setAccessible(true);
current = total = (Object[])f.get(build);
......@@ -112,21 +113,51 @@ public class TokenCreator
currentType = currentType.getComponentType();
}
if (m != 0)
{
boolean assignable = Token.class.isAssignableFrom(currentType);
if (assignable && toWrite != null && toWrite.isToken())
current[poss.next()] = toWrite.getToken();
else if (assignable && toWrite != null && toWrite.getSearch() instanceof BracketSearch && toWrite.getArray().length == 1)
current[poss.next()] = toWrite.getArray()[0].getToken();
else
current[poss.next()] = toWrite;
}
int i = poss.next();
boolean assignable = Token.class.isAssignableFrom(currentType);
if (assignable && toWrite != null && toWrite.isToken())
current[i] = toWrite.getToken();
else if (assignable && toWrite != null && toWrite.getSearch() instanceof BracketSearch && toWrite.getArray().length == 1)
current[i] = toWrite.getArray()[0].getToken();
else
current[i] = toWrite;
f = build.getClass().getDeclaredField(to);
f.setAccessible(true);
f.set(build, total);
if (i == total.length - 1)
total = eliminateNulls(total, currentType);
setFieldValue(build, build.getClass().getDeclaredField(to), total);
}
}
private static Object[] eliminateNulls(Object[] obj, Class<?> currentType)
{
int size = 0;
for (Object o : obj)
if (o != null)
size ++;
Object[] ob = (Object[])Array.newInstance(currentType, size);
int pos = 0;
for (Object o : obj)
if (o != null)
ob[pos++] = o;
return ob;
}
private static void setFieldValue(Token to, Field field, Object newValue)
{
try
{
field.setAccessible(true);
Field modifiersField = Field.class.getDeclaredField("modifiers");
modifiersField.setAccessible(true);
modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL);
field.set(to, newValue);
}
catch(Exception e) {}
}
static void fillContextInfos(TokenBuilder builder, Result[] brutResults, Result previousIgnore, Result nextIgnore, int index, int tokenStart)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment