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

Shorten token creator

parent 894a65b5
No preview for this file type
......@@ -10,38 +10,16 @@ public class TokenCreator
{
static void fill(List<Token> totalList, Result result, Token build, LinkedList<Integer> pos, LinkedList<Integer> max) throws Exception
{
if (result.getArray() == null && result.getToken() == null)
if (result.getSearch() instanceof OptionalSearch && result.getArray() == null)
{
if (result.getSearch().getSearchName() != null)
write(build, result.getSearch().getSearchName(), pos, max, result);
if (result.getSearch() instanceof MultipleSearch)
Search[] ss = ((OptionalSearch)result.getSearch()).getSearches();
for (int i=0;i<ss.length;i++)
{
Search[] ss = ((MultipleSearch)result.getSearch()).getSearches();
for (int i=0;i<ss.length;i++)
{
if (result.getSearch() instanceof ArraySearch)
{
pos.add(i);
max.add(ss.length);
}
if (ss[i].getSearchName() != null)
write(build, ss[i].getSearchName(), pos, max, null);
//else if (ss[i] instanceof MultipleSearch)
//fill(totalList, new Result(ss[i], (Token)null), build, pos, max);
else if (ss[i] instanceof MultipleSearch)
for (Search sss : ((MultipleSearch)ss[i]).getSearches())
if (sss.getSearchName() != null)
write(build, sss.getSearchName(), pos, max, null);
if (result.getSearch() instanceof ArraySearch)
{
max.removeLast();
pos.removeLast();
}
}
if (ss[i].getSearchName() != null)
write(build, ss[i].getSearchName(), pos, max, null);
}
}
......@@ -131,17 +109,4 @@ public class TokenCreator
build.getClass().getDeclaredField(to).set(build, total);
}
}
public static Object getNext(Object o, String name, int pos, int size) throws Exception
{
if (name == null)
return null;
if (o instanceof Object[])
return ((Object[])o)[pos] = new Object[size];
Token build = (Token)o;
Object[] oo = new Object[size];
build.getClass().getDeclaredField(name).set(build, oo);
return oo;
}
}
package parser;
import java.lang.reflect.Array;
import java.lang.reflect.Field;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
public class TokenCreator
{
static void fill(List<Token> totalList, Result result, Token build, LinkedList<Integer> pos, LinkedList<Integer> max) throws Exception
{
if (result.getArray() == null && result.getToken() == null) // Probably optionnal search
{
if (result.getSearch().getSearchName() != null)
write(build, result.getSearch().getSearchName(), pos, max, result);
if (result.getSearch() instanceof MultipleSearch) // Optionnal search
{
System.out.println("Null : "+result.getSearch());
Search[] ss = ((MultipleSearch)result.getSearch()).getSearches();
for (int i=0;i<ss.length;i++)
{
/*System.out.println("In : "+ss[i]);
if (ss[i] instanceof ArraySearch)
{
pos.add(0);
max.add(0);
for (Search s : ((ArraySearch)ss[i]).getSearches())
{
if (s instanceof OrSearch)
{
for (Search ssss : ((OrSearch)s).getSearches())
if (ssss.getSearchName() != null)
write(build, ssss.getSearchName(), pos, max, null);
}
else
if (s.getSearchName() != null)
write(build, s.getSearchName(), pos, max, null);
}
max.removeLast();
pos.removeLast();
}*/
if (ss[i] instanceof ArraySearch)
{
pos.add(i);
max.add(ss.length);
}
if (ss[i].getSearchName() != null)
write(build, ss[i].getSearchName(), pos, max, null);
else if (ss[i] instanceof MultipleSearch)
fill(totalList, new Result(ss[i], (Token)null), build, pos, max);
if (ss[i] instanceof MultipleSearch)
{
System.out.println("Search in multiple null : "+ss[i].getClass());
for (Search sss : ((MultipleSearch)ss[i]).getSearches())
if (sss.getSearchName() != null)
{
System.out.println("Write null in : "+sss);
for (int ii : pos)
System.out.println(ii);
for (int ii : max)
System.out.println(ii);
write(build, sss.getSearchName(), pos, max, null);
}
}
if (ss[i] instanceof ArraySearch)
{
max.removeLast();
pos.removeLast();
}
}
}
}
if (result.isToken())
{
if (result.getSearch().getSearchName() != null)
write(build, result.getSearch().getSearchName(), pos, max, result);
totalList.add(result.getToken());
}
else
{
if (result.getSearch().getSearchName() != null)
if (result.getArray() != null)
for (int i=0;i<result.getArray().length;i++)
write(build, result.getSearch().getSearchName(), pos, max, result.getArray()[i]);
if (result.getArray() != null)
for (int i=0;i<result.getArray().length;i++)
{
if (result.getSearch() instanceof ArraySearch)
{
pos.add(i);
max.add(result.getArray().length);
}
fill(totalList, result.getArray()[i], build, pos, max);
if (result.getSearch() instanceof ArraySearch)
{
max.removeLast();
pos.removeLast();
}
}
}
}
private static void write(Token build, String to, List<Integer> pos, List<Integer> max, Result toWrite) throws Exception
{
if (pos.isEmpty())
{
Field f = build.getClass().getDeclaredField(to);
boolean assignable = Token.class.isAssignableFrom(f.getType());
if (assignable && toWrite != null && toWrite.isToken())
f.set(build, toWrite.getToken());
else if (assignable && toWrite != null && toWrite.getSearch() instanceof BracketSearch && toWrite.getArray().length == 1)
f.set(build, toWrite.getArray()[0].getToken());
else
f.set(build, toWrite);
}
else
{
Object[] current, total;
Class<?> currentType = build.getClass().getDeclaredField(to).getType().getComponentType();
current = total = (Object[])build.getClass().getDeclaredField(to).get(build);
Iterator<Integer> maxs = max.iterator(), poss = pos.iterator();
int m = maxs.next();
if (current == null)
{
current = total = (Object[])Array.newInstance(currentType, m);
if (currentType.getComponentType() != null)
currentType = currentType.getComponentType();
}
for (;maxs.hasNext();)
{
m = maxs.next();
int p = poss.next();
if (current[p] == null)
current = (Object[])(current[p] = Array.newInstance(currentType, m));
else
current = (Object[])current[p];
if (currentType.getComponentType() != null)
currentType = currentType.getComponentType();
}
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// if (current.length > 0)
current[poss.next()] = toWrite;
build.getClass().getDeclaredField(to).set(build, total);
}
}
}
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