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
builder.append("\"");
builder.append(name.id.getName());
builder.append("\", ");
builder.append(name.list);
builder.append(name.isList());
builder.append(", ");
}
}
......
......@@ -301,19 +301,4 @@ class SearchNameMemory
t[i++] = new SearchToken(p.search);
return t;
}
}
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())));
}
}
}
\ No newline at end of file
......@@ -28,7 +28,7 @@ public class OptionalSearch extends MultipleSearch
return true;
}
Parser.subreset();
results[position] = new Result(this, (Result[])null);
results[position] = new Result(this);
return Parser.executeSearches(hasCurrent, current, position + 1, results, searches);
}
@Override
......
......@@ -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))
{
results[position] = t[0];
results[position].orSearch = this;
return true;
}
Parser.subreset();
......
......@@ -12,6 +12,16 @@ public class Result
private final Token token;
private final Search search;
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)
{
this.array = at;
......@@ -38,6 +48,11 @@ public class Result
return this.token;
}
public boolean empty()
{
return token == null && array == null;
}
@Override
public String toString()
{
......
......@@ -47,7 +47,7 @@ public abstract class Token
/********** 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
*/
......@@ -69,12 +69,13 @@ public abstract class Token
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.previousIgnore = p;
this.nextIgnore = n;
this.containerPosition = containerPosition;
this.tokenPosition = position;
this.initContext();
}
......@@ -93,6 +94,10 @@ public abstract class Token
{
return this.nextIgnore;
}
public int getPositionInTotalTokens()
{
return this.tokenPosition;
}
public int getInnerStart()
{
return this.start - this.containerPosition;
......
......@@ -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
{
if (result.getSearch() instanceof OptionalSearch && result.getArray() == null)
/*if (result.getSearch() instanceof OptionalSearch && result.empty())
{
if (result.getSearch().getSearchName() != null)
write(build, result.getSearch(), pos, max, result);
......@@ -20,7 +20,7 @@ public class TokenCreator
{
if (ss[i].getSearchName() != null)
write(build, ss[i], pos, max, null);
/*if (ss[i] instanceof ArraySearch)
if (ss[i] instanceof ArraySearch)
{
pos.add(i);
max.add(0);
......@@ -30,9 +30,9 @@ public class TokenCreator
max.removeLast();
pos.removeLast();
}*/
}
}
}
}*/
if (result.isToken())
{
......@@ -43,12 +43,11 @@ public class TokenCreator
}
else
{
if (result.getSearch().getSearchName() != null)
if (result.getArray() != null)
for (int i=0;i<result.getArray().length;i++)
write(build, result.getSearch(), pos, max, result.getArray()[i]);
if (result.getArray() != null)
if (!result.empty())
{
if (result.getSearch().getSearchName() != null)
write(build, result.getSearch(), pos, max, result);
for (int i=0;i<result.getArray().length;i++)
{
if (result.getSearch() instanceof ArraySearch)
......@@ -63,6 +62,43 @@ public class TokenCreator
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
if (pos.isEmpty())
{
Field f = build.getClass().getDeclaredField(s.getSearchName());
f.setAccessible(true);
boolean assignable = Token.class.isAssignableFrom(f.getType());
if (assignable && toWrite != null && toWrite.isToken())
setFieldValue(build, f, toWrite.getToken());
......@@ -79,64 +114,73 @@ public class TokenCreator
setFieldValue(build, f, toWrite.getArray()[0].getToken());
else
setFieldValue(build, f, toWrite);
return;
}
else
{
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();
}
FieldData datas = getFieldDatas(build, s.getSearchName(), pos, max);
for (;maxs.hasNext();)
{
m = maxs.next();
p = poss.next();
boolean assignable = Token.class.isAssignableFrom(datas.currentType);
if (assignable && toWrite != null && toWrite.isToken())
datas.current[datas.nextPos] = toWrite.getToken();
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)
current = (Object[])(current[p] = Array.newInstance(currentType, m));
else
current = (Object[])current[p];
Iterator<Integer> maxs = max.iterator(), poss = pos.iterator();
if (currentType.getComponentType() != null)
currentType = currentType.getComponentType();
}
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();)
{
m = maxs.next();
p = poss.next();
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();
if (current[p] == null)
current = (Object[])(current[p] = Array.newInstance(currentType, m));
else
current[i] = toWrite;
if (s.isList() && i == total.length - 1)
total = eliminateNulls(total, currentType);
// Uncomment to behave like a stack
setFieldValue(build, build.getClass().getDeclaredField(s.getSearchName()), total);
current = (Object[])current[p];
if (currentType.getComponentType() != null)
currentType = currentType.getComponentType();
}
return new FieldData(f, current, total, currentType, poss.next());
}
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;
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)
......@@ -145,23 +189,21 @@ public class TokenCreator
return ob;
}
private static void setFieldValue(Token to, Field field, Object newValue)
{
try
{
field.setAccessible(true);
private static void simplify(FieldData datas, Token build) throws Exception
{
datas.current[datas.nextPos] = eliminateNulls((Object[])datas.current[datas.nextPos], datas.current.getClass().getComponentType().getComponentType());
setFieldValue(build, datas.field, datas.total);
}
/*Field modifiersField = Field.class.getDeclaredField("modifiers");
modifiersField.setAccessible(true);
modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL);*/
field.set(to, newValue);
}
catch(Exception e) {}
private static void setFieldValue(Token to, Field field, Object newValue) throws Exception
{
field.setAccessible(true);
field.set(to, newValue);
}
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++)
{
if (builder.getIgnore() == null || brutResults[i].getSearch() != builder.getIgnore().getRealSearch()) // Not ignore
......@@ -173,7 +215,7 @@ public class TokenCreator
if (brutResults[i].isToken())
{
brutResults[i].getToken().setContextInfos(index, pi, ni, tokenStart);
brutResults[i].getToken().setContextInfos(index, pi, ni, tokenStart, pos++);
}
else // Array
{
......@@ -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
public Result searchResults[];
public Identifier id;
public ParserString list;
public ParserString notGenerated;
public static final Search builderSearches[] = {
new OptionalSearch(
new TokenSearch("list", false, ParserString.transientToken)
),
new OptionalSearch(
new TokenSearch("notGenerated", false, ParserString.nonGeneratedName)
),
......@@ -21,5 +25,8 @@ public class SearchName extends Token
/********** Do not modify or remove **********/
public boolean list = false;
public boolean isList()
{
return list != null;
}
}
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