Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 15 additions & 15 deletions src/main/java/grammar_objects/DynamicToken.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,32 +39,32 @@ public String getValue() {
*/
@Override
public boolean equals(Object obj) {
if(!(obj instanceof DynamicToken)) { return false; }

return grammaticallyEquals((DynamicToken)obj);
return grammaticallyEquals(obj);
}

/**
* Grammatical equality, based on grammatical qualities, not value
*/
public boolean grammaticallyEquals(DynamicToken otherToken) {
boolean grammaticallyEqual = super.equals(otherToken);
if(!grammaticallyEqual) { return false; }

return true;
public boolean grammaticallyEquals(Object other) {
return super.equals(other);
}

/**
* Exact equality, including DnamicToken value, useful for semantic evaluation
* Exact equality, including DynamicToken value, useful for semantic evaluation
*/
public boolean exactlyEquals(DynamicToken otherIdentifier) {
if(!grammaticallyEquals(otherIdentifier)) { return false; }
public boolean exactlyEquals(Object other) {
if(!(other instanceof DynamicToken)) { return false; }

if(value != null) {
if(!value.equals(otherIdentifier.getValue())) { return false; }
}
DynamicToken otherToken = (DynamicToken)other;

return true;
if(!grammaticallyEquals(otherToken)) { return false; }

if(value == null) {
return otherToken.getValue() == null;
}
else {
return value.equals(otherToken.getValue());
}
}

@Override
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/grammar_objects/LexicalElement.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public String getName() {

@Override
public boolean equals(Object obj) {
if (!this.getClass().isInstance(obj)) { return false; }
if (!(obj instanceof LexicalElement)) { return false; }

LexicalElement otherElement = (LexicalElement) obj;

Expand Down
5 changes: 5 additions & 0 deletions src/main/java/grammar_objects/NonTerminal.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,9 @@ public String toString() {
return "<" + this.getName() + ">";
}

@Override
public boolean equals(Object obj) {
if (!(obj instanceof NonTerminal)) { return false; }
return super.equals(obj);
}
}
15 changes: 10 additions & 5 deletions src/main/java/grammar_objects/Token.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,16 +64,21 @@ public String getPositionString() {
}
}

@Override
public String toString() {
return "\"" + getName() + "\"";
}

@Override
public boolean equals(Object obj) {
if (!(obj instanceof Token)) { return false; }
return super.equals(obj);
}

private enum PositionType {
LineAndColumn,
ColumnOnly,
Symbol
}

@Override
public String toString() {
return "\"" + getName() + "\"";
}

}
32 changes: 16 additions & 16 deletions src/main/java/lexical_analysis/GeneralLexicalAnalyser.java
Original file line number Diff line number Diff line change
Expand Up @@ -244,17 +244,17 @@ public Token[] analyse(String sentence) {
return tokenHolder.tokenList.toArray(new Token[tokenHolder.tokenList.size()]);
}

private void tokeniseSection(String regex, TokenisationHolder tokenHolder) {
if(matchWeaklyReservedWord(regex, tokenHolder)){
private void tokeniseSection(String section, TokenisationHolder tokenHolder) {
if(matchWeaklyReservedWord(section, tokenHolder)){
return;
}

while(true) {
BookendDetails startBookend = findStartingBookend(regex);
BookendDetails startBookend = findStartingBookend(section);

//Due to assumption that two non-bookended dynamic tokens cannot be adjacent. If this changes, change this to tokenise without using bookends
if(startBookend == null) {
Token newToken = tokenise(regex, tokenHolder);
Token newToken = tokenise(section, tokenHolder);

if(newToken == null) {
throw new LexicalError(
Expand All @@ -270,25 +270,25 @@ Check all Regex special characters (e.g ".", "\\") are escaped correctly, and re
}

tokenHolder.tokenList.add(newToken);
updateDisplayPosition(tokenHolder, regex, StringType.Dynamic);
updateDisplayPosition(tokenHolder, section, StringType.Dynamic);

return;
}

if(startBookend.position != BookendPosition.Start) {
tokenHolder.tokenList.add(tokeniseWithoutBookends(regex.substring(0, startBookend.startIndex)));
regex = regex.substring(startBookend.startIndex);
tokenHolder.tokenList.add(tokeniseWithoutBookends(section.substring(0, startBookend.startIndex)));
section = section.substring(startBookend.startIndex);
}

String afterStart = regex.substring(startBookend.length);
String afterStart = section.substring(startBookend.length);
BookendDetails endBookend = findFirstEndBookend(startBookend.regex, afterStart);

if(endBookend == null) {
Token token = tokenise(regex, tokenHolder);
Token token = tokenise(section, tokenHolder);

if(token != null) {
tokenHolder.tokenList.add(token);
updateDisplayPosition(tokenHolder, regex, StringType.Dynamic);
updateDisplayPosition(tokenHolder, section, StringType.Dynamic);
return;
}

Expand All @@ -301,7 +301,7 @@ Check all Regex special characters (e.g ".", "\\") are escaped correctly, and re

if(endBookend.position == BookendPosition.End) {
tokenHolder.tokenList.add(tokenise(
regex.substring(0, startBookend.length + endBookend.startIndex + endBookend.length),
section.substring(0, startBookend.length + endBookend.startIndex + endBookend.length),
startBookend.regex,
endBookend.regex,
tokenHolder
Expand All @@ -315,13 +315,13 @@ Check all Regex special characters (e.g ".", "\\") are escaped correctly, and re
if(edgeIndex == null) {
//Tokenise all as one token
tokenHolder.tokenList.add(tokenise(
regex,
section,
startBookend.regex,
endBookend.regex,
tokenHolder
));

updateDisplayPosition(tokenHolder, regex, StringType.Dynamic);
updateDisplayPosition(tokenHolder, section, StringType.Dynamic);
return;
}

Expand All @@ -331,10 +331,10 @@ Check all Regex special characters (e.g ".", "\\") are escaped correctly, and re

BookendDetails finalMatch = findFirstEndBookend(
startBookend.regex,
regex.substring(endOfStartIndex + edgeIndex)
section.substring(endOfStartIndex + edgeIndex)
); //TODO: Alter to use the same endBookend, not just any potential one, only useful for length

String stringToTokenise = regex.substring(0, endOfStartIndex + edgeIndex + finalMatch.length);
String stringToTokenise = section.substring(0, endOfStartIndex + edgeIndex + finalMatch.length);

tokenHolder.tokenList.add(tokenise(
stringToTokenise,
Expand All @@ -345,7 +345,7 @@ Check all Regex special characters (e.g ".", "\\") are escaped correctly, and re

updateDisplayPosition(tokenHolder, stringToTokenise, StringType.Dynamic);

regex = regex.substring(endOfStartIndex + edgeIndex + finalMatch.length);
section = section.substring(endOfStartIndex + edgeIndex + finalMatch.length);
}
}

Expand Down
13 changes: 11 additions & 2 deletions src/test/java/grammar_objects/IdentifierTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,20 @@ public void identifierEquality() {
}

@Test
public void similarTokenEquality() {
public void similarTokenEquality_grammatically() {
Identifier ident = new Identifier("Identifier", "x");
Token token = new Token("Identifier");

assertNotEquals(ident, token);
assertTrue(ident.equals(token));
assertTrue(ident.grammaticallyEquals(token));
}

@Test
public void similarTokenEquality_exact() {
Identifier ident = new Identifier("Identifier", "x");
Token token = new Token("Identifier");

assertFalse(ident.exactlyEquals(token));
}

@Test
Expand Down
Loading