Skip to content

Fix Parsers.Parser.||| (by adding equality comparison for Position objects) #507

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 5, 2023
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
12 changes: 12 additions & 0 deletions shared/src/main/scala/scala/util/parsing/input/Position.scala
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,16 @@ 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.
*/
override def equals(other: Any) = {
other match {
case that: Position => this.line == that.line && this.column == that.column
case _ => false
}
}
}
Original file line number Diff line number Diff line change
@@ -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()
}
}
}