Commit c8a35586 authored by Custom2043's avatar Custom2043
Browse files
parents 6c092c74 e1d83abc
......@@ -3,7 +3,6 @@ gen/**
.classpath
.project
**.jar
**.jardesc
test.txt
......
......@@ -3,10 +3,10 @@ singleQuote = "\'"
backslash = "\\"
anyCharToken = "\*"
#NumberChar = "0" ("b" {value:'0-1'})|("x" {value:'0-9|A-F'})|("d" {value:digit})
#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|anyCharToken|quote|singleQuote)) | anyChar | anyCharToken
CharacterToken = singleQuote [notToken:notToken] {|orToken character1:(SingleChar|NumberChar) ["-" character2:(SingleChar|NumberChar)]} singleQuote
ParserStringToken = (quote {charTokens:(CharacterToken | SingleChar)} quote) | singleChar:CharacterToken
StringToken = (quote {charTokens:(CharacterToken | SingleChar)} quote) | singleChar:CharacterToken
letter = 'a-z|A-Z'
digit = '0-9'
......@@ -29,14 +29,14 @@ ignore = {Space|Jump}
SearchName = id:Identifier ":"
LineHeader = [trans:transientToken] id:Identifier equals
TokenSearchToken = Identifier|StringToken
ArraySearchToken = arrayOpen [orToken left:NamedSearch] {searches:NamedSearch} [right:NamedSearch orToken] arrayClose
OptionalSearchToken = optionOpen {searches:NamedSearch} optionClose
BracketSearchToken = exprOpen {searches:NamedSearch} exprClose
OrSearchToken = {searches:NamedSearch orToken|}
TokenSearchToken = ([trans:transientToken] 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|}
#NamedSearch = {([searchNames:SearchName] searches:(TokenSearchToken|ArraySearchToken|OptionalSearchToken|BracketSearchToken)) | searches:OrSearchToken}
#SearchToken = ([searchNames:SearchName] searches:(TokenSearchToken|ArraySearchToken|OptionalSearchToken|BracketSearchToken)) | searches:OrSearchToken
Line = header:LineHeader {namedSearches:NamedSearch}
Line = header:LineHeader {namedSearches:SearchToken}
anyChar = "*"
......@@ -81,15 +81,15 @@ public class ClassGenerator
classBuilder.append(Generator.getLineJump());
classBuilder.append(Generator.getLineJump());
classBuilder.append("\tpublic Result searchResults[];");
classBuilder.append(new VariableGenerator().generateTokenVariables(l, lines));
classBuilder.append(VariableGenerator.generateTokenVariables(l, lines));
classBuilder.append(Generator.getLineJump());
classBuilder.append("\tpublic static final Search builderSearches[] = {");
classBuilder.append(Generator.getLineJump());
for (SearchToken tc : l.types)
for (SearchToken tc : l.searchTokens)
{
classBuilder.append("\t\t");
generateSearchToken(null, tc, 2, lines, l, classBuilder, lineIndex);
if (tc != l.types[l.types.length - 1])
if (tc != l.searchTokens[l.searchTokens.length - 1])
classBuilder.append(",");
classBuilder.append(Generator.getLineJump());
}
......@@ -149,6 +149,14 @@ public class ClassGenerator
builder.append(a.right != null);
builder.append(", ");
}
if (a.minSize != null || a.maxSize != null)
{
builder.append(a.minSize == null ? 1 : Integer.valueOf(Token.getStringFromTokens(a.minSize)));
builder.append(", ");
builder.append(a.maxSize == null ? "Integer.MAX_VALUE" : Integer.valueOf(Token.getStringFromTokens(a.maxSize)));
builder.append(", ");
}
}
generateMultipleSearch(((MultipleSearchToken)token).getTypes(), null, tabs+1, lines, currentLine, builder, lineIndex);
......@@ -159,7 +167,7 @@ public class ClassGenerator
if (tokenName != null)
generateSearchName(tokenName, builder);
TokenSearchToken tokenSearch = (TokenSearchToken)token;
if (tokenSearch.id != null)
......@@ -167,6 +175,9 @@ public class ClassGenerator
else if (tokenSearch.string != null)
generateStringToken(tokenSearch.string, currentLine.isParserString() ? currentLine.header.id.getName() : null, lineIndex, builder);
if (tokenSearch.trans != null)
builder.append(", true");
builder.append(")");
}
}
......
......@@ -106,9 +106,9 @@ public class Compiler
HashMap<String, SearchNameMemory> paths = new HashMap<>();
for (SearchToken tc : currentLine.types)
for (SearchToken tc : currentLine.searchTokens)
{
if (SearchToken.canBeOptionnal(currentLine.types))
if (SearchToken.canBeOptionnal(currentLine.searchTokens))
report(new Info(InfoModel.emptyToken, currentLine));
compileSearchToken(currentLine, lines, tc, first, paths);
first = false;
......@@ -173,6 +173,9 @@ public class Compiler
else if (first && foundAfterThisLine)
if ((currentLine.header.trans != null) == tokenTransient)
report(new Info(InfoModel.firstTokenUndeclared, token));
if (!tokenTransient && tokenSearch.trans != null)
report(new Info(InfoModel.invalidTransSearch, token));
}
}
......@@ -218,19 +221,20 @@ class SearchPath
public SearchPath(List<Integer> indexes, Token t)
{
index = indexes.toArray(new Integer[indexes.size()]);
search = t;
this.index = indexes.toArray(new Integer[indexes.size()]);
this.search = t;
}
@Override
public boolean equals(Object o)
{
if (o instanceof SearchPath)
{
SearchPath other = (SearchPath)o;
if (index.length == other.index.length)
if (this.index.length == other.index.length)
{
for (int i=0;i<index.length;i++)
if (index[i] != other.index[i])
for (int i=0;i<this.index.length;i++)
if (this.index[i] != other.index[i])
return false;
return true;
}
......@@ -245,18 +249,18 @@ class SearchNameMemory
public SearchNameMemory(int l)
{
level = l;
this.level = l;
}
public void add(int level, SearchPath p)
{
if (level == this.level)
paths.add(p);
this.paths.add(p);
}
public boolean contains(SearchPath p)
{
return paths.contains(p);
return this.paths.contains(p);
}
public int getLevel()
......@@ -266,9 +270,9 @@ class SearchNameMemory
public SearchToken[] getSearchArray()
{
SearchToken[] t = new SearchToken[paths.size()];
SearchToken[] t = new SearchToken[this.paths.size()];
int i = 0;
for (SearchPath p : paths)
for (SearchPath p : this.paths)
t[i++] = new SearchToken(p.search);
return t;
}
......
......@@ -81,7 +81,7 @@ public class Generator
{
if (l.isParserString())
{
ParserStringGenerator.generateParserString(((TokenSearchToken)l.types[0].searchToken).string, l.header.id.getName(), lineIndex);
ParserStringGenerator.generateParserString(((TokenSearchToken)l.searchTokens[0].searchToken).string, l.header.id.getName(), lineIndex);
return;
}
......
......@@ -14,10 +14,10 @@ public class IgnoreGenerator
ignoreBuilder.append(lineIndex);
ignoreBuilder.append(", ");
for (SearchToken t : l.types)
for (SearchToken t : l.searchTokens)
{
ClassGenerator.generateSearchToken(null, t, 6, lines, l, ignoreBuilder, lineIndex);
if (t != l.types[l.types.length - 1])
if (t != l.searchTokens[l.searchTokens.length - 1])
{
ignoreBuilder.append(",");
ignoreBuilder.append(Generator.getLineJump());
......
......@@ -33,7 +33,7 @@ enum InfoModel
nameIgnore(Etat.ERROR, "Can't name an ignore token"),
tokenUndeclared(Etat.ERROR, "This token doesn't exist"),
illegalIdentifier(Etat.ERROR, "This name is not allowed"),
levelError(Etat.ERROR, "Level Error"),
invalidTransSearch(Etat.ERROR, "'#' can only be used with a transient token"),
;
public final Etat etat;
......
......@@ -172,8 +172,6 @@ public class ParserStringGenerator
builder.append(Generator.getLineJump());
builder.append(parserStringBuilder);
builder.append("\t};");
builder.append(Generator.getLineJump());
builder.append(Generator.getLineJump());
return builder.toString();
}
......@@ -189,8 +187,6 @@ public class ParserStringGenerator
builder.append(parserCharacterBuilder);
builder.append("\t};");
builder.append(Generator.getLineJump());
builder.append(Generator.getLineJump());
return builder.toString();
}
......
......@@ -9,24 +9,30 @@ import tokens.*;
public class VariableGenerator
{
private StringBuilder builder = new StringBuilder();
public String generateTokenVariables(Line l, List<Line> lines)
private static final String RESULT = "Result", TOKEN = "Token", PARSERSTRING = "ParserString";
private static StringBuilder builder;
public static String generateTokenVariables(Line l, List<Line> lines)
{
builder = new StringBuilder();
HashMap<String, SearchNameMemory> paths = Compiler.getAssociatedPath(l, lines);
for (String s : paths.keySet())
{
SearchNameMemory list = paths.get(s);
this.generateVariable(getTypeOf(lines, list.getSearchArray()), s, list.getLevel());
String type = getOrType(lines, list.getSearchArray());
generateVariable(type, s, list.getLevel());
}
return this.builder.toString();
return builder.toString();
}
private static String getTypeOf(List<Line> lines, SearchToken... types)
{
if (types.length > 1)
return "Result";
return RESULT;
Token search = types[0].searchToken;
......@@ -36,39 +42,39 @@ public class VariableGenerator
if (tokenSearch.id != null)
return getIDType(tokenSearch.id, lines);
else
return "ParserString";
return PARSERSTRING;
}
else if (search instanceof OrSearchToken)
return getOrType(lines, ((OrSearchToken)search));
else if (search instanceof MultipleSearchToken)
return getOrType(lines, ((OrSearchToken)search).getTypes());
else if (search instanceof BracketSearchToken)
return getTypeOf(lines, ((MultipleSearchToken)search).getTypes());
return "Result";
return RESULT;
}
private static String getOrType(List<Line> lines, OrSearchToken search)
private static String getOrType(List<Line> lines, SearchToken... search)
{
String id = null;
boolean string = false;
for (SearchToken t : search.getTypes())
for (SearchToken t : search)
{
String s = getTypeOf(lines, t);
if (s.equals("Result"))
return "Result";
else if (s.equals("ParserString"))
if (s.equals(RESULT))
return RESULT;
else if (s.equals(PARSERSTRING))
string = true;
else if (!"Token".equals(id))
else if (id == null)
id = s;
else
id = TOKEN;
}
if (string)
if (id != null)
return "Token";
return TOKEN;
else
return "ParserString";
return PARSERSTRING;
return id;
}
public static String getIDType(Identifier i, List<Line> lines)
......@@ -76,26 +82,27 @@ public class VariableGenerator
for (Line l : lines)
if (l.header.id.getName().equals(i.getName()))
if (l.isParserString())
return "ParserString";
return PARSERSTRING;
else
return i.getName();
return "Result";
return null; //Shouldn't happen
}
private void generateVariable(String type, String name, int level)
private static void generateVariable(String type, String name, int level)
{
this.builder.append(Generator.getLineJump());
this.builder.append("\t");
builder.append(Generator.getLineJump());
builder.append("\t");
if (Main.vis.length() > 0)
{
this.builder.append(Main.vis);
this.builder.append(" ");
builder.append(Main.vis);
builder.append(" ");
}
this.builder.append(type);
this.builder.append(" ");
this.builder.append(name);
builder.append(type);
builder.append(" ");
builder.append(name);
for (int i=0;i<level;i++)
this.builder.append("[]");
this.builder.append(";");
builder.append("[]");
builder.append(";");
}
}
......@@ -16,6 +16,7 @@ import java.util.LinkedList;
public class ArraySearch extends MultipleSearch
{
private final Search[] leftSearches, rightSearches;
private final int minSize, maxSize;
public ArraySearch(Search... s)
{
......@@ -27,21 +28,43 @@ public class ArraySearch extends MultipleSearch
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 ? new Search[] {searches[0]} : new Search[0], getCoreSearches(left, right, searches), right ? new Search[] {searches[searches.length - 1]} : new Search[0]);
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, Search[] lS, Search[] coreSearch, Search[] rS)
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;
}
/**
......@@ -105,8 +128,11 @@ public class ArraySearch extends MultipleSearch
Parser.subreset();
lineResultList.addLast(getResultFrom(merge(lastLeftResult, lastCoreResult)));
if (lineResultList.size() < this.minSize)
return false;
while (!Parser.executeSearches(false, null, position + 1, results, searches))
while (!Parser.executeSearches(false, null, position + 1, results, searches) || lineResultList.size() > this.maxSize)
{
if (!Parser.isSubmarked())
return false;
......@@ -155,11 +181,22 @@ public class ArraySearch extends MultipleSearch
@Override
MultipleSearch addIgnore(Ignore ignore)
{
return new ArraySearch(this.getSearchName(),
return new ArraySearch(this.getSearchName(), this.minSize, this.maxSize,
TokenBuilder.addIgnoresToArray(ignore, this.leftSearches),
TokenBuilder.addIgnoresToArray(ignore, this.getSearches()),
TokenBuilder.addIgnoresToArray(ignore, this.rightSearches)
);
}
@Override
void setDeclarationBuilder(Builder builder)
{
super.setDeclarationBuilder(builder);
for(Search s : this.getLeftSearches())
s.setDeclarationBuilder(builder);
for(Search s : this.getRightSearches())
s.setDeclarationBuilder(builder);
}
}
......@@ -45,6 +45,9 @@ public class Ignore extends Builder
@Override
public Search[] getSearches()
{
for (Search s : this.search)
s.setDeclarationBuilder(this);
return this.search;
}
public OptionalSearch getRealSearch()
......
......@@ -28,4 +28,13 @@ public abstract class MultipleSearch extends Search
s += "\n"+ss.toString(tabs+1);
return s;
}
@Override
void setDeclarationBuilder(Builder builder)
{
super.setDeclarationBuilder(builder);
for (Search s : this.getSearches())
s.setDeclarationBuilder(builder);
}
}
......@@ -29,7 +29,7 @@ public class Parser
Token t;
if ((t = readUntil(null)) != null)
if ((t = readUntil(-1, false)) != null)
tokens.add(t);
else
index ++;
......@@ -71,17 +71,22 @@ public class Parser
* Reads until the line defining the given token (inclusive) or until the end if the builder is null</br>
* Tries to build the specified token at each line</br>
* If the token is not correct when the last line is reached, returns to the index at which the fonction was called</br>
* @param tokenBuilder the builder to reach or null to read all lines ; Should not be a transient token as it can't be built by reading lines</br>
* @param tokenBuilder the builder to reach or null to read all lines</br>
* @return the token built or null if the fonction has failed
*/
static Token readUntil(TokenBuilder tokenBuilder)
static Token readUntil(int line, boolean excluded)//TokenBuilder tokenBuilder)
{
//System.out.println("Searching for : "+tokenBuilder+" from index : "+index);
/*int line = tokenBuilder == null ? -1 : tokenBuilder.getLine();
boolean excluded = tokenBuilder == null ? false : tokenBuilder.isTransient();*/
//if (trans)
//line --;
int oldIndex = index;
Token t;
ParserMemory memory = ParserMemory.setLastResult(getTokenStart(), tokenBuilder == null ? -1 : tokenBuilder.getLine());
ParserMemory memory = ParserMemory.setLastResult(getTokenStart(), line);
mark.currentToken = memory.getToken();
if (memory.getToken() != null)
{
......@@ -92,14 +97,14 @@ public class Parser
for (TokenBuilder lineBuilder : Token.tokenBuilder)
if (lineBuilder.getLine() > memory.getLine())
if (tokenBuilder == null || (lineBuilder.getLine() < tokenBuilder.getLine() ||
!tokenBuilder.isTransient() && lineBuilder.getLine() == tokenBuilder.getLine()))
if (line == -1 || (lineBuilder.getLine() < line)
|| !excluded && lineBuilder.getLine() == line)
if ((t = buildToken(lineBuilder, mark.currentToken)) != null)
{
mark.currentToken = t;
ParserMemory.addResult(getTokenStart(), new ParserMemory(mark.currentToken, lineBuilder.getLine()));
}
else if (lineBuilder == tokenBuilder)
else if (lineBuilder.getLine() == line && !excluded)
{
index = oldIndex;
return null;
......@@ -147,6 +152,7 @@ public class Parser
}
catch (Exception e)
{
System.out.println("Error while building "+tokenBuilder.getTokenClass());
e.printStackTrace();
}
}
......
......@@ -14,38 +14,39 @@ public class ParserCharacter
public static final ParserCharacter[] chars = new ParserCharacter[]
{
new ParserCharacter(false, new char[]{'\"'}),
new ParserCharacter(false, new char[]{'#'}),
new ParserCharacter(false, new char[]{'!'}),
new ParserCharacter(false, new char[]{'\''}),
new ParserCharacter(false, new char[]{'\\'}),
new ParserCharacter(false, new char[]{'*'}),
new ParserCharacter(false, new char[]{'|'}),
new ParserCharacter(false, new char[]{'b'}),
new ParserCharacter(false, new char[]{'r'}),
new ParserCharacter(false, new char[]{'t'}),
new ParserCharacter(false, new char[]{'n'}),
new ParserCharacter(false, new char[]{'x'}),
new ParserCharacter(false, new char[]{'o'}),
new ParserCharacter(false, new char[]{'-'}),
new ParserCharacter(false, new char[]{'a', 'A'}, new char[]{'z', 'Z'}),
new ParserCharacter(false, new char[]{'0'}, new char[]{'9'}),
new ParserCharacter(false, new char[]{'('}),
new ParserCharacter(false, new char[]{')'}),
new ParserCharacter(false, new char[]{'['}),
new ParserCharacter(false, new char[]{']'}),
new ParserCharacter(false, new char[]{'{'}),
new ParserCharacter(false, new char[]{'}'}),
new ParserCharacter(false, new char[]{'='}),
new ParserCharacter(false, new char[]{':'}),
new ParserCharacter(false, new char[]{'\r'}),
new ParserCharacter(false, new char[]{'\n'}),
new ParserCharacter(false, new char[]{' '}),
new ParserCharacter(false, new char[]{'\t'}),
new ParserCharacter(false, new char[]{'0'}),
new ParserCharacter(false, new char[]{'\"'}), //0
new ParserCharacter(false, new char[]{'\''}), //1
new ParserCharacter(false, new char[]{'\\'}), //2
new ParserCharacter(false, new char[]{'*'}), //3
new ParserCharacter(false, new char[]{'0'}), //4
new ParserCharacter(false, new char[]{'b'}), //5
new ParserCharacter(false, new char[]{'0'}, new char[]{'1'}), //6
new ParserCharacter(false, new char[]{'x'}), //7
new ParserCharacter(false, new char[]{'0', 'A'}, new char[]{'9', 'F'}), //8
new ParserCharacter(false, new char[]{'d'}), //9
new ParserCharacter(false, new char[]{'r'}), //10
new ParserCharacter(false, new char[]{'t'}), //11
new ParserCharacter(false, new char[]{'n'}), //12
new ParserCharacter(false, new char[]{'-'}), //13
new ParserCharacter(false, new char[]{'a', 'A'}, new char[]{'z', 'Z'}), //14
new ParserCharacter(false, new char[]{'0'}, new char[]{'9'}), //15
new ParserCharacter(false, new char[]{'('}), //16
new ParserCharacter(false, new char[]{')'}), //17
new ParserCharacter(false, new char[]{'['}), //18
new ParserCharacter(false, new char[]{']'}), //19
new ParserCharacter(false, new char[]{'{'}), //20
new ParserCharacter(false, new char[]{'}'}), //21