Commit 7bd79b0f authored by Custom2043's avatar Custom2043
Browse files

Update token creator for lists

parent fd06002d
Dec : {Modifier} Header Body
Header :
Result Declarator [Throws]
Method Declarator :
Identifier ( [FormalParameterList] ) [Dims]
ReceiverParameter :
{Annotation} UnannType [Identifier .] this
#Dims = {{Annotation} "[]"}
#VariableDeclaratorId = Identifier [Dims]
#VariableModifier = Annotation|"final"
#Parametre = [{VariableModifier}] UnannType VariableDeclaratorId|([{Annotation}] "..." Identifier)
#ParameterList = {Parametre ","|}
#Header = Identifier "("
MethodDeclarator : [{MethodModifier}] Header
MethodModifier = "public"|"protected"|"private"|"abstract"|"static"|"final"|"synchronized"|"native"|"strictfp"|"super"|Annotation
PrimitiveType = "int"|"float"|"double"|"short"|"byte"|"boolean"|"float"
Identifier = 'a-z|A-Z|$|_' {'a-z|A-Z|0-9|$|_'}
ignore = {' |\t|\r|\n'}
Dims = {"[" "]"}
#TypeName = {Identifier "."|}
#Annotation = "@" TypeName ["(" ["*"] ")"]
Type = "void"|(PrimitiveType|Identifier Dims)
MethodDeclarator = [{MethodModifier}] Type Identifier "("
\ No newline at end of file
File added
Bug si
#a = ...
[a] [b]
#b = ...
\ No newline at end of file
T = "a" quote = "\""
singleQuote = "\'"
backslash = "\\"
#NumberChar = "0" (base:"b" {value:'0-1'})|(base:"x" {value:'0-9|A-F'})|(base:"d" {value:digit})
#SingleChar = (backslash backslash:("b"|"r"|"t"|"n"|backslash|quote|singleQuote)) | anyChar
CharacterToken = singleQuote [notToken:notToken] {|orToken character1:(NumberChar|SingleChar) ["-" character2:(NumberChar|SingleChar)]} singleQuote
StringToken = (quote {charTokens:(CharacterToken | SingleChar)} quote) | singleChar:CharacterToken
#ignoreKeyword = "_ignore"
letter = 'a-z|A-Z'
digit = '0-9'
exprOpen = "("
exprClose = ")"
optionOpen = "["
optionClose = "]"
arrayOpen = "{"
arrayClose = "}"
notToken = "!"
orToken = "|"
transientToken = "#"
equals = "="
nonGeneratedName = "_"
Jump = "\r\n"|"\r"|"\n"
Space = " "|"\t"
Identifier = firstLetter:letter [{idName:(letter|digit)}]
ignore = {Space|Jump}
SearchName = [list:transientToken] [notGenerated:nonGeneratedName] id:Identifier ":"
LineHeader = [trans:transientToken] id:Identifier equals
TokenSearchToken = ([trans:transientToken [included:nonGeneratedName]] id:Identifier)|string:StringToken
ArraySearchToken = [{minSize:digit} "-"] arrayOpen [orToken left:SearchToken] {searches:SearchToken} [right:SearchToken orToken] arrayClose ["-" {maxSize:digit}]
OptionalSearchToken = optionOpen {searches:SearchToken} optionClose
BracketSearchToken = exprOpen {searches:SearchToken} exprClose
OrSearchToken = 2-{searches:#SearchToken orToken|}
#SearchToken = ([searchName:SearchName] searchToken:(TokenSearchToken|ArraySearchToken|OptionalSearchToken|BracketSearchToken)) | searchToken:OrSearchToken
Line = header:LineHeader [firstIgnore:ignoreKeyword] {searchTokens:SearchToken} [lastIgnore:ignoreKeyword]
anyChar = '*'
MetaParser 2:
Gérer les listes
Génération optimale
Mieux gérer les ignore
Ajouter des notes sur les tokens ? ("Type:integer":Expression)
Catégorie :
category Expression :
ObjectInstanciation = ...
Truc = ...
Tout déclaré dans une category est transient
Expression = Object | Truc | ...
Liste : #SearchName : search
Doit etre dans une array
Une liste est un tableau ne contenant que les éléments non null
Si une liste est vide, tableau taille 0
Si une liste est dans un tableau qui n'est jamais visité, elle est nulle car n'existe pas
...@@ -249,7 +249,7 @@ public class ClassGenerator ...@@ -249,7 +249,7 @@ public class ClassGenerator
builder.append("\""); builder.append("\"");
builder.append(name.id.getName()); builder.append(name.id.getName());
builder.append("\", "); builder.append("\", ");
builder.append(name.list); builder.append(name.isList());
builder.append(", "); builder.append(", ");
} }
} }
......
...@@ -301,19 +301,4 @@ class SearchNameMemory ...@@ -301,19 +301,4 @@ class SearchNameMemory
t[i++] = new SearchToken(p.search); t[i++] = new SearchToken(p.search);
return t; return t;
} }
} }
\ No newline at end of file
class JavaSourceFromFile extends SimpleJavaFileObject
{
public JavaSourceFromFile(File file)
{
super(file.toURI(), Kind.SOURCE);
}
@Override
public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException
{
return new String(Files.readAllBytes(Paths.get(this.toUri())));
}
}
...@@ -28,7 +28,7 @@ public class OptionalSearch extends MultipleSearch ...@@ -28,7 +28,7 @@ public class OptionalSearch extends MultipleSearch
return true; return true;
} }
Parser.subreset(); Parser.subreset();
results[position] = new Result(this, (Result[])null); results[position] = new Result(this);
return Parser.executeSearches(hasCurrent, current, position + 1, results, searches); return Parser.executeSearches(hasCurrent, current, position + 1, results, searches);
} }
@Override @Override
......
...@@ -17,6 +17,7 @@ public class OrSearch extends MultipleSearch ...@@ -17,6 +17,7 @@ public class OrSearch extends MultipleSearch
if (Parser.executeSearches(hasCurrent, current, 0, t, new Search[]{s}) && Parser.executeSearches(false, null, position + 1, results, searches)) if (Parser.executeSearches(hasCurrent, current, 0, t, new Search[]{s}) && Parser.executeSearches(false, null, position + 1, results, searches))
{ {
results[position] = t[0]; results[position] = t[0];
results[position].orSearch = this;
return true; return true;
} }
Parser.subreset(); Parser.subreset();
......
...@@ -12,6 +12,16 @@ public class Result ...@@ -12,6 +12,16 @@ public class Result
private final Token token; private final Token token;
private final Search search; private final Search search;
private final boolean isToken; private final boolean isToken;
public OrSearch orSearch; // Use to remember the or search of a token
public Result(Search s)
{
this.array = null;
this.token = null;
this.search = s;
this.isToken = false;
}
public Result(Search s, Result[] at) public Result(Search s, Result[] at)
{ {
this.array = at; this.array = at;
...@@ -38,6 +48,11 @@ public class Result ...@@ -38,6 +48,11 @@ public class Result
return this.token; return this.token;
} }
public boolean empty()
{
return token == null && array == null;
}
@Override @Override
public String toString() public String toString()
{ {
......
...@@ -47,7 +47,7 @@ public abstract class Token ...@@ -47,7 +47,7 @@ public abstract class Token
/********** Do not modify or remove **********/ /********** Do not modify or remove **********/
private int start, length, line, column, index, containerPosition; private int start, length, line, column, index, containerPosition, tokenPosition;
/** /**
* Only make sense in an array * Only make sense in an array
*/ */
...@@ -69,12 +69,13 @@ public abstract class Token ...@@ -69,12 +69,13 @@ public abstract class Token
this.init(); this.init();
} }
final void setContextInfos(int in, Result p, Result n, int containerPosition) final void setContextInfos(int in, Result p, Result n, int containerPosition, int position)
{ {
this.index = in; this.index = in;
this.previousIgnore = p; this.previousIgnore = p;
this.nextIgnore = n; this.nextIgnore = n;
this.containerPosition = containerPosition; this.containerPosition = containerPosition;
this.tokenPosition = position;
this.initContext(); this.initContext();
} }
...@@ -93,6 +94,10 @@ public abstract class Token ...@@ -93,6 +94,10 @@ public abstract class Token
{ {
return this.nextIgnore; return this.nextIgnore;
} }
public int getPositionInTotalTokens()
{
return this.tokenPosition;
}
public int getInnerStart() public int getInnerStart()
{ {
return this.start - this.containerPosition; return this.start - this.containerPosition;
......
...@@ -10,7 +10,7 @@ public class TokenCreator ...@@ -10,7 +10,7 @@ public class TokenCreator
{ {
static void fill(List<Token> totalList, Result result, Token build, LinkedList<Integer> pos, LinkedList<Integer> max) throws Exception static void fill(List<Token> totalList, Result result, Token build, LinkedList<Integer> pos, LinkedList<Integer> max) throws Exception
{ {
if (result.getSearch() instanceof OptionalSearch && result.getArray() == null) /*if (result.getSearch() instanceof OptionalSearch && result.empty())
{ {
if (result.getSearch().getSearchName() != null) if (result.getSearch().getSearchName() != null)
write(build, result.getSearch(), pos, max, result); write(build, result.getSearch(), pos, max, result);
...@@ -20,7 +20,7 @@ public class TokenCreator ...@@ -20,7 +20,7 @@ public class TokenCreator
{ {
if (ss[i].getSearchName() != null) if (ss[i].getSearchName() != null)
write(build, ss[i], pos, max, null); write(build, ss[i], 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,9 +30,9 @@ public class TokenCreator ...@@ -30,9 +30,9 @@ public class TokenCreator
max.removeLast(); max.removeLast();
pos.removeLast(); pos.removeLast();
}*/ }
} }
} }*/
if (result.isToken()) if (result.isToken())
{ {
...@@ -43,12 +43,11 @@ public class TokenCreator ...@@ -43,12 +43,11 @@ public class TokenCreator
} }
else else
{ {
if (result.getSearch().getSearchName() != null) if (!result.empty())
if (result.getArray() != null) {
for (int i=0;i<result.getArray().length;i++) if (result.getSearch().getSearchName() != null)
write(build, result.getSearch(), pos, max, result.getArray()[i]); write(build, result.getSearch(), pos, max, result);
if (result.getArray() != null)
for (int i=0;i<result.getArray().length;i++) for (int i=0;i<result.getArray().length;i++)
{ {
if (result.getSearch() instanceof ArraySearch) if (result.getSearch() instanceof ArraySearch)
...@@ -63,6 +62,43 @@ public class TokenCreator ...@@ -63,6 +62,43 @@ public class TokenCreator
pos.removeLast(); pos.removeLast();
} }
} }
if (result.getSearch() instanceof ArraySearch)
arraysToLists(build, ((ArraySearch)result.getSearch()).getSearches().length, (ArraySearch)result.getSearch(), pos, max);
}
}
}
// max cannot be empty here
private static void arraysToLists(Token build, int lastArraySize, MultipleSearch search, LinkedList<Integer> pos, LinkedList<Integer> max) throws Exception
{
for (Search s : search.getSearches())
{
if (s.getSearchName() != null)// && s.isList())
{
if (max.size() == 0)
{
Field f = build.getClass().getDeclaredField(s.getSearchName());
Object[] o = (Object[])f.get(build);
if (o != null)
{
Class<?> currentType = f.getType().getComponentType();
setFieldValue(build, f, eliminateNulls(o, currentType));
}
else
{
setFieldValue(build, f, Array.newInstance(f.getType().getComponentType(), lastArraySize));
}
}
else
{
simplify(getFieldDatas(build, s.getSearchName(), pos, max), build);
}
}
if (s instanceof MultipleSearch && !(s instanceof ArraySearch))
arraysToLists(build, lastArraySize, (MultipleSearch)s, pos, max);
} }
} }
...@@ -71,7 +107,6 @@ public class TokenCreator ...@@ -71,7 +107,6 @@ public class TokenCreator
if (pos.isEmpty()) if (pos.isEmpty())
{ {
Field f = build.getClass().getDeclaredField(s.getSearchName()); Field f = build.getClass().getDeclaredField(s.getSearchName());
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())
setFieldValue(build, f, toWrite.getToken()); setFieldValue(build, f, toWrite.getToken());
...@@ -79,64 +114,73 @@ public class TokenCreator ...@@ -79,64 +114,73 @@ public class TokenCreator
setFieldValue(build, f, toWrite.getArray()[0].getToken()); setFieldValue(build, f, toWrite.getArray()[0].getToken());
else else
setFieldValue(build, f, toWrite); setFieldValue(build, f, toWrite);
return;
} }
else
{ FieldData datas = getFieldDatas(build, s.getSearchName(), pos, max);
Object[] current, total;
Field f = build.getClass().getDeclaredField(s.getSearchName());
Class<?> currentType = f.getType().getComponentType();
f.setAccessible(true);
current = total = (Object[])f.get(build);
Iterator<Integer> maxs = max.iterator(), poss = pos.iterator();
int m = maxs.next(), p;
if (current == null)
{
current = total = (Object[])Array.newInstance(currentType, m);
if (currentType.getComponentType() != null)
currentType = currentType.getComponentType();
}
for (;maxs.hasNext();) boolean assignable = Token.class.isAssignableFrom(datas.currentType);
{ if (assignable && toWrite != null && toWrite.isToken())
m = maxs.next(); datas.current[datas.nextPos] = toWrite.getToken();
p = poss.next(); else if (assignable && toWrite != null && toWrite.getSearch() instanceof BracketSearch && toWrite.getArray().length == 1)
datas.current[datas.nextPos] = toWrite.getArray()[0].getToken();
else
datas.current[datas.nextPos] = toWrite;
//if (s.isList() && i == total.length - 1)
//total = eliminateNulls(total, currentType);
// Uncomment to behave like a stack
setFieldValue(build, datas.field, datas.total);
}
private static FieldData getFieldDatas(Token build, String searchName, LinkedList<Integer> pos, LinkedList<Integer> max) throws Exception
{
Object[] current, total;
Field f = build.getClass().getDeclaredField(searchName);
Class<?> currentType = f.getType().getComponentType();
f.setAccessible(true);
current = total = (Object[])f.get(build);
if (current[p] == null) Iterator<Integer> maxs = max.iterator(), poss = pos.iterator();
current = (Object[])(current[p] = Array.newInstance(currentType, m));
else
current = (Object[])current[p];
if (currentType.getComponentType() != null) int m = maxs.next(), p;
currentType = currentType.getComponentType(); if (current == null)
} {
current = total = (Object[])Array.newInstance(currentType, m);
if (currentType.getComponentType() != null)
currentType = currentType.getComponentType();
}
for (;maxs.hasNext();)
{
m = maxs.next();
p = poss.next();
int i = poss.next(); if (current[p] == null)
boolean assignable = Token.class.isAssignableFrom(currentType); current = (Object[])(current[p] = Array.newInstance(currentType, m));
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 else
current[i] = toWrite; current = (Object[])current[p];
if (s.isList() && i == total.length - 1) if (currentType.getComponentType() != null)
total = eliminateNulls(total, currentType); currentType = currentType.getComponentType();
// Uncomment to behave like a stack
setFieldValue(build, build.getClass().getDeclaredField(s.getSearchName()), total);
} }
return new FieldData(f, current, total, currentType, poss.next());
} }
private static Object[] eliminateNulls(Object[] obj, Class<?> currentType) private static Object[] eliminateNulls(Object[] obj, Class<?> currentType)
{ {
//System.out.println("current Type : "+currentType);
if (obj == null)
return (Object[])Array.newInstance(currentType, 0);
int size = 0; int size = 0;
for (Object o : obj) for (Object o : obj)
if (o != null) if (o != null)
size ++; size ++;
Object[] ob = (Object[])Array.newInstance(currentType, size); Object[] ob = (Object[])Array.newInstance(currentType, size);
int pos = 0; int pos = 0;
for (Object o : obj) for (Object o : obj)
if (o != null) if (o != null)
...@@ -145,23 +189,21 @@ public class TokenCreator ...@@ -145,23 +189,21 @@ public class TokenCreator
return ob; return ob;
} }
private static void setFieldValue(Token to, Field field, Object newValue) private static void simplify(FieldData datas, Token build) throws Exception
{ {
try datas.current[datas.nextPos] = eliminateNulls((Object[])datas.current[datas.nextPos], datas.current.getClass().getComponentType().getComponentType());
{ setFieldValue(build, datas.field, datas.total);
field.setAccessible(true); }
/*Field modifiersField = Field.class.getDeclaredField("modifiers"); private static void setFieldValue(Token to, Field field, Object newValue) throws Exception
modifiersField.setAccessible(true); {
modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL);*/ field.setAccessible(true);
field.set(to, newValue);
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)
{ {
int pos = 0;
for (int i=0;i<brutResults.length;i++) for (int i=0;i<brutResults.length;i++)
{ {
if (builder.getIgnore() == null || brutResults[i].getSearch() != builder.getIgnore().getRealSearch()) // Not ignore if (builder.getIgnore() == null || brutResults[i].getSearch() != builder.getIgnore().getRealSearch()) // Not ignore
...@@ -173,7 +215,7 @@ public class TokenCreator ...@@ -173,7 +215,7 @@ public class TokenCreator
if (brutResults[i].isToken()) if (brutResults[i].isToken())
{ {
brutResults[i].getToken().setContextInfos(index, pi, ni, tokenStart); brutResults[i].getToken().setContextInfos(index, pi, ni, tokenStart, pos++);
} }
else // Array else // Array
{ {
...@@ -193,3 +235,19 @@ public class TokenCreator ...@@ -193,3 +235,19 @@ public class TokenCreator
} }
} }
} }
class FieldData
{
final Field field;
final Object[] current, total;
final Class<?> currentType;
final int nextPos;
FieldData(Field f, Object[] o, Object[] t, Class<?> c, int n)
{
field = f;
current = o;
total = t;
currentType = c;
nextPos = n;
}
}
...@@ -10,8 +10,12 @@ public class SearchName extends Token ...@@ -10,8 +10,12 @@ public class SearchName extends Token
public Result searchResults[];