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 ...@@ -163,11 +163,11 @@ public class ClassGenerator
builder.append(", "); 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(", ");
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(", "); builder.append(", ");
} }
} }
......
...@@ -190,12 +190,12 @@ public class Compiler ...@@ -190,12 +190,12 @@ public class Compiler
{ {
ArraySearchToken ats = (ArraySearchToken)token; ArraySearchToken ats = (ArraySearchToken)token;
int min, max; int min, max;
if (ats.minSize.length != 0) if (ats.minSize != null)
min = Integer.valueOf(Token.getStringFromTokens(ats.minSize)); min = Integer.valueOf(Token.getStringFromTokens(ats.minSize));
else else
min = 1; min = 1;
if (ats.maxSize.length != 0) if (ats.maxSize != null)
max = Integer.valueOf(Token.getStringFromTokens(ats.maxSize)); max = Integer.valueOf(Token.getStringFromTokens(ats.maxSize));
else else
max = Integer.MAX_VALUE; max = Integer.MAX_VALUE;
......
...@@ -2,6 +2,7 @@ package parser; ...@@ -2,6 +2,7 @@ package parser;
import java.lang.reflect.Array; import java.lang.reflect.Array;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.Iterator; import java.util.Iterator;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
...@@ -20,7 +21,7 @@ public class TokenCreator ...@@ -20,7 +21,7 @@ public class TokenCreator
{ {
if (ss[i].getSearchName() != null) if (ss[i].getSearchName() != null)
write(build, ss[i].getSearchName(), pos, max, null); write(build, ss[i].getSearchName(), pos, max, null);
if (ss[i] instanceof ArraySearch) /*if (ss[i] instanceof ArraySearch)
{ {
pos.add(i); pos.add(i);
max.add(0); max.add(0);
...@@ -30,10 +31,10 @@ public class TokenCreator ...@@ -30,10 +31,10 @@ public class TokenCreator
max.removeLast(); max.removeLast();
pos.removeLast(); pos.removeLast();
} }*/
} }
} }
if (result.isToken()) if (result.isToken())
{ {
if (result.getSearch().getSearchName() != null) if (result.getSearch().getSearchName() != null)
...@@ -66,7 +67,7 @@ public class TokenCreator ...@@ -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()) if (pos.isEmpty())
{ {
...@@ -74,17 +75,17 @@ public class TokenCreator ...@@ -74,17 +75,17 @@ public class TokenCreator
f.setAccessible(true); f.setAccessible(true);
boolean assignable = Token.class.isAssignableFrom(f.getType()); boolean assignable = Token.class.isAssignableFrom(f.getType());
if (assignable && toWrite != null && toWrite.isToken()) 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) 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 else
f.set(build, toWrite); setFieldValue(build, f, toWrite);
} }
else else
{ {
Object[] current, total; Object[] current, total;
Class<?> currentType = build.getClass().getDeclaredField(to).getType().getComponentType();
Field f = build.getClass().getDeclaredField(to); Field f = build.getClass().getDeclaredField(to);
Class<?> currentType = f.getType().getComponentType();
f.setAccessible(true); f.setAccessible(true);
current = total = (Object[])f.get(build); current = total = (Object[])f.get(build);
...@@ -112,21 +113,51 @@ public class TokenCreator ...@@ -112,21 +113,51 @@ public class TokenCreator
currentType = currentType.getComponentType(); currentType = currentType.getComponentType();
} }
if (m != 0) int i = poss.next();
{ boolean assignable = Token.class.isAssignableFrom(currentType);
boolean assignable = Token.class.isAssignableFrom(currentType); if (assignable && toWrite != null && toWrite.isToken())
if (assignable && toWrite != null && toWrite.isToken()) current[i] = toWrite.getToken();
current[poss.next()] = toWrite.getToken(); else if (assignable && toWrite != null && toWrite.getSearch() instanceof BracketSearch && toWrite.getArray().length == 1)
else if (assignable && toWrite != null && toWrite.getSearch() instanceof BracketSearch && toWrite.getArray().length == 1) current[i] = toWrite.getArray()[0].getToken();
current[poss.next()] = toWrite.getArray()[0].getToken(); else
else current[i] = toWrite;
current[poss.next()] = toWrite;
}
f = build.getClass().getDeclaredField(to); if (i == total.length - 1)
f.setAccessible(true); total = eliminateNulls(total, currentType);
f.set(build, total);
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) 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