From 70096fa45205caf95e08745771436fc7d3797fc5 Mon Sep 17 00:00:00 2001 From: warburec <77669019+warburec@users.noreply.github.com> Date: Thu, 15 Oct 2026 23:51:37 +0100 Subject: [PATCH 1/3] DynamicToken equality fix/cleanup --- .../java/grammar_objects/DynamicToken.java | 30 +++++++++---------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/src/main/java/grammar_objects/DynamicToken.java b/src/main/java/grammar_objects/DynamicToken.java index 555e903..fcc38de 100644 --- a/src/main/java/grammar_objects/DynamicToken.java +++ b/src/main/java/grammar_objects/DynamicToken.java @@ -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 From 945d40f45caf664720aed6ee71383806635c41f4 Mon Sep 17 00:00:00 2001 From: warburec <77669019+warburec@users.noreply.github.com> Date: Thu, 15 Oct 2026 23:51:47 +0100 Subject: [PATCH 2/3] Unrelated cleanup --- src/main/java/grammar_objects/Token.java | 10 +++--- .../GeneralLexicalAnalyser.java | 32 +++++++++---------- 2 files changed, 21 insertions(+), 21 deletions(-) diff --git a/src/main/java/grammar_objects/Token.java b/src/main/java/grammar_objects/Token.java index 48e8809..f9457b6 100644 --- a/src/main/java/grammar_objects/Token.java +++ b/src/main/java/grammar_objects/Token.java @@ -64,16 +64,16 @@ public String getPositionString() { } } + @Override + public String toString() { + return "\"" + getName() + "\""; + } + private enum PositionType { LineAndColumn, ColumnOnly, Symbol } - @Override - public String toString() { - return "\"" + getName() + "\""; - } - } diff --git a/src/main/java/lexical_analysis/GeneralLexicalAnalyser.java b/src/main/java/lexical_analysis/GeneralLexicalAnalyser.java index d3f6ccb..67feb89 100644 --- a/src/main/java/lexical_analysis/GeneralLexicalAnalyser.java +++ b/src/main/java/lexical_analysis/GeneralLexicalAnalyser.java @@ -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( @@ -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; } @@ -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 @@ -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; } @@ -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, @@ -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); } } From ab34b9a165353d04a7430ecf906462ca25c0b968 Mon Sep 17 00:00:00 2001 From: warburec <77669019+warburec@users.noreply.github.com> Date: Fri, 16 Oct 2026 00:03:24 +0100 Subject: [PATCH 3/3] LexicalElement equality Including small Identifier test edit --- src/main/java/grammar_objects/LexicalElement.java | 2 +- src/main/java/grammar_objects/NonTerminal.java | 5 +++++ src/main/java/grammar_objects/Token.java | 5 +++++ src/test/java/grammar_objects/IdentifierTests.java | 13 +++++++++++-- 4 files changed, 22 insertions(+), 3 deletions(-) diff --git a/src/main/java/grammar_objects/LexicalElement.java b/src/main/java/grammar_objects/LexicalElement.java index 1eb4656..547231d 100644 --- a/src/main/java/grammar_objects/LexicalElement.java +++ b/src/main/java/grammar_objects/LexicalElement.java @@ -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; diff --git a/src/main/java/grammar_objects/NonTerminal.java b/src/main/java/grammar_objects/NonTerminal.java index e75ffaa..a5e0f56 100644 --- a/src/main/java/grammar_objects/NonTerminal.java +++ b/src/main/java/grammar_objects/NonTerminal.java @@ -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); + } } diff --git a/src/main/java/grammar_objects/Token.java b/src/main/java/grammar_objects/Token.java index f9457b6..f1f0ee3 100644 --- a/src/main/java/grammar_objects/Token.java +++ b/src/main/java/grammar_objects/Token.java @@ -69,6 +69,11 @@ 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, diff --git a/src/test/java/grammar_objects/IdentifierTests.java b/src/test/java/grammar_objects/IdentifierTests.java index c91878f..c7f1703 100644 --- a/src/test/java/grammar_objects/IdentifierTests.java +++ b/src/test/java/grammar_objects/IdentifierTests.java @@ -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