From c6cf9110cb739bd1ee43ba49325ece5f9e296e04 Mon Sep 17 00:00:00 2001 From: Peter Aldous Date: Wed, 22 Feb 2023 15:37:25 -0700 Subject: [PATCH 1/2] added equality comparison for Position objects --- .../src/main/scala/scala/util/parsing/input/Position.scala | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/shared/src/main/scala/scala/util/parsing/input/Position.scala b/shared/src/main/scala/scala/util/parsing/input/Position.scala index cb18dc52..688ca63b 100644 --- a/shared/src/main/scala/scala/util/parsing/input/Position.scala +++ b/shared/src/main/scala/scala/util/parsing/input/Position.scala @@ -60,4 +60,11 @@ trait Position { this.line < that.line || this.line == that.line && this.column < that.column } + + /** Compare this position to another, checking for equality. + * + * @param `that` a `Position` to compare to this `Position` + * @return true if the line numbers and column numbers are equal. + */ + def ==(that: Position) = this.line == that.line && this.column == that.column } From 11e08c6037d454b81fc6941437d6bc8edf825b5c Mon Sep 17 00:00:00 2001 From: Peter Aldous Date: Mon, 27 Feb 2023 10:58:51 -0700 Subject: [PATCH 2/2] added a test and changed from == to equals --- .../scala/util/parsing/input/Position.scala | 7 ++- .../parsing/combinator/LongestMatchTest.scala | 52 +++++++++++++++++++ 2 files changed, 58 insertions(+), 1 deletion(-) create mode 100644 shared/src/test/scala/scala/util/parsing/combinator/LongestMatchTest.scala diff --git a/shared/src/main/scala/scala/util/parsing/input/Position.scala b/shared/src/main/scala/scala/util/parsing/input/Position.scala index 688ca63b..e2326829 100644 --- a/shared/src/main/scala/scala/util/parsing/input/Position.scala +++ b/shared/src/main/scala/scala/util/parsing/input/Position.scala @@ -66,5 +66,10 @@ trait Position { * @param `that` a `Position` to compare to this `Position` * @return true if the line numbers and column numbers are equal. */ - def ==(that: Position) = this.line == that.line && this.column == that.column + override def equals(other: Any) = { + other match { + case that: Position => this.line == that.line && this.column == that.column + case _ => false + } + } } diff --git a/shared/src/test/scala/scala/util/parsing/combinator/LongestMatchTest.scala b/shared/src/test/scala/scala/util/parsing/combinator/LongestMatchTest.scala new file mode 100644 index 00000000..f9dca807 --- /dev/null +++ b/shared/src/test/scala/scala/util/parsing/combinator/LongestMatchTest.scala @@ -0,0 +1,52 @@ +package scala.util.parsing.combinator + +import java.io.StringReader + +import scala.util.parsing.combinator.Parsers +import scala.util.parsing.input.StreamReader + +import org.junit.Test +import org.junit.Assert.{ assertEquals, fail } + +class LongestMatchTest { + class TestParsers extends Parsers { + type Elem = Char + + def ab: Parser[String] = 'a' ~ 'b' ^^^ "ab" + def a: Parser[String] = 'a' ^^^ "a" + def ab_alt: Parser[String] = 'a' ~ 'b' ^^^ "alt" + } + + @Test + def longestMatchFirst: Unit = { + val tParsers = new TestParsers + val reader = StreamReader(new StringReader("ab")) + val p = tParsers.ab ||| tParsers.a + p(reader) match { + case tParsers.Success(result, _) => assertEquals("ab", result) + case _ => fail() + } + } + + @Test + def longestMatchSecond: Unit = { + val tParsers = new TestParsers + val reader = StreamReader(new StringReader("ab")) + val p = tParsers.a ||| tParsers.ab + p(reader) match { + case tParsers.Success(result, _) => assertEquals("ab", result) + case _ => fail() + } + } + + @Test + def tieGoesToFirst: Unit = { + val tParsers = new TestParsers + val reader = StreamReader(new StringReader("ab")) + val p = tParsers.ab ||| tParsers.ab_alt + p(reader) match { + case tParsers.Success(result, _) => assertEquals("ab", result) + case _ => fail() + } + } +}