Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 4f020d5

Browse files
Merge pull request #507 from peteraldous/position-equality
2 parents f1e3836 + 11e08c6 commit 4f020d5

File tree

2 files changed

+64
-0
lines changed

2 files changed

+64
-0
lines changed

‎shared/src/main/scala/scala/util/parsing/input/Position.scala

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,4 +60,16 @@ trait Position {
6060
this.line < that.line ||
6161
this.line == that.line && this.column < that.column
6262
}
63+
64+
/** Compare this position to another, checking for equality.
65+
*
66+
* @param `that` a `Position` to compare to this `Position`
67+
* @return true if the line numbers and column numbers are equal.
68+
*/
69+
override def equals(other: Any) = {
70+
other match {
71+
case that: Position => this.line == that.line && this.column == that.column
72+
case _ => false
73+
}
74+
}
6375
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package scala.util.parsing.combinator
2+
3+
import java.io.StringReader
4+
5+
import scala.util.parsing.combinator.Parsers
6+
import scala.util.parsing.input.StreamReader
7+
8+
import org.junit.Test
9+
import org.junit.Assert.{ assertEquals, fail }
10+
11+
class LongestMatchTest {
12+
class TestParsers extends Parsers {
13+
type Elem = Char
14+
15+
def ab: Parser[String] = 'a' ~ 'b' ^^^ "ab"
16+
def a: Parser[String] = 'a' ^^^ "a"
17+
def ab_alt: Parser[String] = 'a' ~ 'b' ^^^ "alt"
18+
}
19+
20+
@Test
21+
def longestMatchFirst: Unit = {
22+
val tParsers = new TestParsers
23+
val reader = StreamReader(new StringReader("ab"))
24+
val p = tParsers.ab ||| tParsers.a
25+
p(reader) match {
26+
case tParsers.Success(result, _) => assertEquals("ab", result)
27+
case _ => fail()
28+
}
29+
}
30+
31+
@Test
32+
def longestMatchSecond: Unit = {
33+
val tParsers = new TestParsers
34+
val reader = StreamReader(new StringReader("ab"))
35+
val p = tParsers.a ||| tParsers.ab
36+
p(reader) match {
37+
case tParsers.Success(result, _) => assertEquals("ab", result)
38+
case _ => fail()
39+
}
40+
}
41+
42+
@Test
43+
def tieGoesToFirst: Unit = {
44+
val tParsers = new TestParsers
45+
val reader = StreamReader(new StringReader("ab"))
46+
val p = tParsers.ab ||| tParsers.ab_alt
47+
p(reader) match {
48+
case tParsers.Success(result, _) => assertEquals("ab", result)
49+
case _ => fail()
50+
}
51+
}
52+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /