Commit 8ed104bd authored by Milhomme Allan's avatar Milhomme Allan
Browse files

Rebuilt

parent 254965d3
package parser;
import java.util.LinkedList;
import java.util.List;
/**
* An array search is composed of a left search, a core and a right search
* Left and right search are array for convenience
* The core is the center part of the array
* The left search is not used at the start of the search
* The right search is not used at the start of the search
*
* Example : core right
* left core right
* left core
*/
public class ArraySearch extends MultipleSearch
{
private final Search[] leftSearches, rightSearches;
private final int minSize, maxSize;
public ArraySearch(Search... s)
{
this(null, false, false, s);
}
public ArraySearch(String name, Search... s)
{
this(name, false, false, s);
}
public ArraySearch(int min, int max, Search... s)
{
this(null, min, max, s);
}
public ArraySearch(boolean left, boolean right, Search... s)
{
this(null, left, right, s);
}
public ArraySearch(boolean left, boolean right, int min, int max, Search... s)
{
this(null, left, right, min, max, s);
}
public ArraySearch(String name, int min, int max, Search... searches)
{
this(name, min, max, new Search[0], searches, new Search[0]);
}
public ArraySearch(String name, boolean left, boolean right, Search... searches)
{
this(name, left, right, 1, Integer.MAX_VALUE, searches);
}
public ArraySearch(String name, boolean left, boolean right, int min, int max, Search... searches)
{
this(name, min, max, left ? new Search[] {searches[0]} : new Search[0], getCoreSearches(left, right, searches), right ? new Search[] {searches[searches.length - 1]} : new Search[0]);
}
private ArraySearch(String name, int min, int max, Search[] lS, Search[] coreSearch, Search[] rS)
{
super(name, coreSearch);
this.leftSearches = lS;
this.rightSearches = rS;
this.minSize = min;
this.maxSize = max;
}
/**
* Returns the core searches from all searches (including left and right)
* @param left True if a left search is used
* @param right True if a right search is used
* @param searches The array of search
* @return The extracted core searches
*/
private static Search[] getCoreSearches(boolean left, boolean right, Search... searches)
{
if (!left && !right)
return searches;
Search[] coreSearch = new Search[searches.length - (left ? 1 : 0) - (right ? 1 : 0)];
for (int i = 0; i < coreSearch.length; i++)
coreSearch[i] = searches[i + (left ? 1 : 0)];
return coreSearch;
}
/**
* Execute the search.
* Try to read as much parts as possible.
* If the next search fails, try with less parts
* Read the first core, then use {@link #readRest(int, Result[], Search[], Result[])}
*/
@Override
public boolean executeSearch(boolean hasCurrent, Token current, int position, Result[] results, Search[] searches)
{
Result[] t = new Result[this.getSearches().length];
if (Parser.executeSearches(hasCurrent, current, 0, t, this.getSearches()))
return this.readRest(position, results, searches, t);
return false;
}
/**
* Reads as much (right, core, left) as possible
*/
private boolean readRest(int position, Result[] results, Search[] searches, Result[] firstCoreResult)
{
Parser.submark();
LinkedList<Result> lineResultList = new LinkedList<>();
Result[] leftResult, coreResult, rightResult,
lastLeftResult = new Result[0], lastCoreResult = firstCoreResult;
while (Parser.executeSearches(false, null, 0, rightResult = new Result[this.rightSearches.length], this.rightSearches)
&& Parser.executeSearches(false, null, 0, leftResult = new Result[this.leftSearches.length], this.leftSearches)
&& Parser.executeSearches(false, null, 0, coreResult = new Result[this.getSearches().length], this.getSearches()))
{
Parser.submark();
lineResultList.addLast(getResultFrom(merge(lastLeftResult, lastCoreResult, rightResult)));
lastLeftResult = leftResult;
lastCoreResult = coreResult;
}
Parser.subreset();
lineResultList.addLast(getResultFrom(merge(lastLeftResult, lastCoreResult)));
if (lineResultList.size() < this.minSize)
return false;
while (!Parser.executeSearches(false, null, position + 1, results, searches) || lineResultList.size() > this.maxSize)
{
if (!Parser.isSubmarked())
return false;
Parser.subreset();
lineResultList.removeLast();
}
results[position] = new Result(this, lineResultList.toArray(new Result[lineResultList.size()]));
return true;
}
public static Result getResultFrom(Result[] resultsIn)
{
Search[] searches = new Search[resultsIn.length];
for (int i=0;i<resultsIn.length;i++)
searches[i] = resultsIn[i].getSearch();
return new Result(new BracketSearch(searches), resultsIn);
}
public static Result[] merge(Result[]... results)
{
int sum = 0;
for (int i=0;i<results.length;i++)
sum += results[i].length;
Result[] lineResult = new Result[sum];
int i = 0;
for (Result[] rr : results)
for (Result r : rr)
if (r != null)
lineResult[i++] = r;
return lineResult;
}
public Search[] getLeftSearches()
{
return this.leftSearches;
}
public Search[] getRightSearches()
{
return this.rightSearches;
}
@Override
MultipleSearch addIgnore(Ignore ignore)
{
/*return new ArraySearch(this.getSearchName(), this.minSize, this.maxSize,
TokenBuilder.addIgnoresToArray(ignore, this.leftSearches),
TokenBuilder.addIgnoresToArray(ignore, this.getSearches()),
TokenBuilder.addIgnoresToArray(ignore, this.rightSearches)
);*/
List<Search> leftWithIgnored = TokenBuilder.addIgnores(ignore, this.leftSearches),
coreWithIgnored = TokenBuilder.addIgnores(ignore, this.getSearches()),
rightWithIgnored = TokenBuilder.addIgnores(ignore, this.rightSearches);
if (leftWithIgnored.size() > 0)
leftWithIgnored.add(0, ignore.getRealSearch());
leftWithIgnored.add(ignore.getRealSearch());
if (rightWithIgnored.size() > 0)
rightWithIgnored.add(0, ignore.getRealSearch());
return new ArraySearch(this.getSearchName(), this.minSize, this.maxSize,
leftWithIgnored.toArray(new Search[leftWithIgnored.size()]),
coreWithIgnored.toArray(new Search[coreWithIgnored.size()]),
rightWithIgnored.toArray(new Search[rightWithIgnored.size()])
);
//coreWithIgnored, coreWithIgnored.length == 0 ? new Search[0] : ignore.getRealSearch());
}
@Override
void setDeclarationBuilder(Builder builder)
{
super.setDeclarationBuilder(builder);
for(Search s : this.getLeftSearches())
s.setDeclarationBuilder(builder);
for(Search s : this.getRightSearches())
s.setDeclarationBuilder(builder);
}
@Override
public final String toString(int tabs)
{
String tab = "", s;
for (int i=0;i<tabs;i++)
tab+= "\t";
s = tab + this.getClass().getSimpleName();
s += " "+this.getSearchName()+" : ";
s += "\n"+tab+"LeftSearches :";
for (Search ss : this.leftSearches)
s += "\n"+ss.toString(tabs+1);
s += "\n"+tab+"CoreSearches :";
for (Search ss : this.getSearches())
s += "\n"+ss.toString(tabs+1);
s += "\n"+tab+"RightSearches :";
for (Search ss : this.rightSearches)
s += "\n"+ss.toString(tabs+1);
return s;
}
}
package parser;
import java.util.LinkedList;
import java.util.List;
/**
* An array search is composed of a left search, a core and a right search
* Left and right search are array for convenience
* The core is the center part of the array
* The left search is not used at the start of the search
* The right search is not used at the start of the search
*
* Example : core right
* left core right
* left core
*/
public class ArraySearch extends MultipleSearch
{
private final Search[] leftSearches, rightSearches;
private final int minSize, maxSize;
public ArraySearch(Search... s)
{
this(null, false, false, s);
}
public ArraySearch(String name, Search... s)
{
this(name, false, false, s);
}
public ArraySearch(int min, int max, Search... s)
{
this(null, min, max, s);
}
public ArraySearch(boolean left, boolean right, Search... s)
{
this(null, left, right, s);
}
public ArraySearch(boolean left, boolean right, int min, int max, Search... s)
{
this(null, left, right, min, max, s);
}
public ArraySearch(String name, int min, int max, Search... searches)
{
this(name, min, max, new Search[0], searches, new Search[0]);
}
public ArraySearch(String name, boolean left, boolean right, Search... searches)
{
this(name, left, right, 1, Integer.MAX_VALUE, searches);
}
public ArraySearch(String name, boolean left, boolean right, int min, int max, Search... searches)
{
this(name, min, max, left ? new Search[] {searches[0]} : new Search[0], getCoreSearches(left, right, searches), right ? new Search[] {searches[searches.length - 1]} : new Search[0]);
}
private ArraySearch(String name, int min, int max, Search[] lS, Search[] coreSearch, Search[] rS)
{
super(name, coreSearch);
this.leftSearches = lS;
this.rightSearches = rS;
this.minSize = min;
this.maxSize = max;
}
/**
* Returns the core searches from all searches (including left and right)
* @param left True if a left search is used
* @param right True if a right search is used
* @param searches The array of search
* @return The extracted core searches
*/
private static Search[] getCoreSearches(boolean left, boolean right, Search... searches)
{
if (!left && !right)
return searches;
Search[] coreSearch = new Search[searches.length - (left ? 1 : 0) - (right ? 1 : 0)];
for (int i = 0; i < coreSearch.length; i++)
coreSearch[i] = searches[i + (left ? 1 : 0)];
return coreSearch;
}
/**
* Execute the search.
* Try to read as much parts as possible.
* If the next search fails, try with less parts
* Read the first core, then use {@link #readRest(int, Result[], Search[], Result[])}
*/
@Override
public boolean executeSearch(boolean hasCurrent, Token current, int position, Result[] results, Search[] searches)
{
Result[] t = new Result[this.getSearches().length];
if (Parser.executeSearches(hasCurrent, current, 0, t, this.getSearches()))
return this.readRest(position, results, searches, t);
return false;
}
/**
* Reads as much (right, core, left) as possible
*/
private boolean readRest(int position, Result[] results, Search[] searches, Result[] firstCoreResult)
{
Parser.submark();
LinkedList<Result> lineResultList = new LinkedList<>();
Result[] leftResult, coreResult, rightResult,
lastLeftResult = new Result[0], lastCoreResult = firstCoreResult;
while (Parser.executeSearches(false, null, 0, rightResult = new Result[this.rightSearches.length], this.rightSearches)
&& Parser.executeSearches(false, null, 0, leftResult = new Result[this.leftSearches.length], this.leftSearches)
&& Parser.executeSearches(false, null, 0, coreResult = new Result[this.getSearches().length], this.getSearches()))
{
Parser.submark();
lineResultList.addLast(getResultFrom(merge(lastLeftResult, lastCoreResult, rightResult)));
lastLeftResult = leftResult;
lastCoreResult = coreResult;
}
Parser.subreset();
lineResultList.addLast(getResultFrom(merge(lastLeftResult, lastCoreResult)));
if (lineResultList.size() < this.minSize)
return false;
while (!Parser.executeSearches(false, null, position + 1, results, searches) || lineResultList.size() > this.maxSize)
{
if (!Parser.isSubmarked())
return false;
Parser.subreset();
lineResultList.removeLast();
}
results[position] = new Result(this, lineResultList.toArray(new Result[lineResultList.size()]));
return true;
}
public static Result getResultFrom(Result[] resultsIn)
{
Search[] searches = new Search[resultsIn.length];
for (int i=0;i<resultsIn.length;i++)
searches[i] = resultsIn[i].getSearch();
return new Result(new BracketSearch(searches), resultsIn);
}
public static Result[] merge(Result[]... results)
{
int sum = 0;
for (int i=0;i<results.length;i++)
sum += results[i].length;
Result[] lineResult = new Result[sum];
int i = 0;
for (Result[] rr : results)
for (Result r : rr)
if (r != null)
lineResult[i++] = r;
return lineResult;
}
public Search[] getLeftSearches()
{
return this.leftSearches;
}
public Search[] getRightSearches()
{
return this.rightSearches;
}
@Override
MultipleSearch addIgnore(Ignore ignore)
{
/*return new ArraySearch(this.getSearchName(), this.minSize, this.maxSize,
TokenBuilder.addIgnoresToArray(ignore, this.leftSearches),
TokenBuilder.addIgnoresToArray(ignore, this.getSearches()),
TokenBuilder.addIgnoresToArray(ignore, this.rightSearches)
);*/
List<Search> leftWithIgnored = TokenBuilder.addIgnores(ignore, this.leftSearches),
coreWithIgnored = TokenBuilder.addIgnores(ignore, this.getSearches()),
rightWithIgnored = TokenBuilder.addIgnores(ignore, this.rightSearches);
if (leftWithIgnored.size() > 0)
leftWithIgnored.add(0, ignore.getRealSearch());
leftWithIgnored.add(ignore.getRealSearch());
if (rightWithIgnored.size() > 0)
rightWithIgnored.add(0, ignore.getRealSearch());
return new ArraySearch(this.getSearchName(), this.minSize, this.maxSize,
leftWithIgnored.toArray(new Search[leftWithIgnored.size()]),
coreWithIgnored.toArray(new Search[coreWithIgnored.size()]),
rightWithIgnored.toArray(new Search[rightWithIgnored.size()])
);
//coreWithIgnored, coreWithIgnored.length == 0 ? new Search[0] : ignore.getRealSearch());
}
@Override
void setDeclarationBuilder(Builder builder)
{
super.setDeclarationBuilder(builder);
for(Search s : this.getLeftSearches())
s.setDeclarationBuilder(builder);
for(Search s : this.getRightSearches())
s.setDeclarationBuilder(builder);
}
@Override
public final String toString(int tabs)
{
String tab = "", s;
for (int i=0;i<tabs;i++)
tab+= "\t";
s = tab + this.getClass().getSimpleName();
s += " "+this.getSearchName()+" : ";
s += "\n"+tab+"LeftSearches :";
for (Search ss : this.leftSearches)
s += "\n"+ss.toString(tabs+1);
s += "\n"+tab+"CoreSearches :";
for (Search ss : this.getSearches())
s += "\n"+ss.toString(tabs+1);
s += "\n"+tab+"RightSearches :";
for (Search ss : this.rightSearches)
s += "\n"+ss.toString(tabs+1);
return s;
}
}
package parser;
/**
* Represents multiple searches
*/
public class BracketSearch extends MultipleSearch
{
public BracketSearch(String searchName, Search... searches)
{
super(searchName, searches);
}
public BracketSearch(Search... searches)
{
this((String)null, searches);
}
@Override
public boolean executeSearch(boolean hasCurrent, Token current, int position, Result[] results, Search[] searches)
{
Result[] t = new Result[this.getSearches().length];
if (Parser.executeSearches(hasCurrent, current, 0, t, this.getSearches()) && Parser.executeSearches(false, null, position + 1, results, searches))
{
results[position] = new Result(this, t);
return true;
}
return false;
}
@Override
MultipleSearch addIgnore(Ignore ignore)
{
return new BracketSearch(this.getSearchName(), TokenBuilder.addIgnoresToArray(ignore, this.getSearches()));
}
}
package parser;
/**
* Represents multiple searches
*/
public class BracketSearch extends MultipleSearch
{
public BracketSearch(String searchName, Search... searches)
{
super(searchName, searches);
}
public BracketSearch(Search... searches)
{
this((String)null, searches);
}
@Override
public boolean executeSearch(boolean hasCurrent, Token current, int position, Result[] results, Search[] searches)
{
Result[] t = new Result[this.getSearches().length];
if (Parser.executeSearches(hasCurrent, current, 0, t, this.getSearches()) && Parser.executeSearches(false, null, position + 1, results, searches))
{
results[position] = new Result(this, t);
return true;
}
return false;
}
@Override
MultipleSearch addIgnore(Ignore ignore)
{
return new BracketSearch(this.getSearchName(), TokenBuilder.addIgnoresToArray(ignore, this.getSearches()));
}
}
package parser;
/**
* A builder represents a line and contains searches
*/
public abstract class Builder
{
private final int line;
private final boolean isTransient;
public Builder(boolean isTransient, int line)
{
this.isTransient = isTransient;
this.line = line;
}
public int getLine()
{
return this.line;
}
public boolean isTransient()
{
return this.isTransient;
}
public abstract Search[] getSearches();
/**
* @return true if the provided token correspond to this builder
*/