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

Rebuilt

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