Skip to content

Commit 845d5ab

Browse files
committed
Merge pull request #77 from gourlaysama/issues/56-offsetposition-linecontent2
Prevent OffsetPosition.lineContents from grabbing a newline
2 parents ee2708e + 51d69b4 commit 845d5ab

File tree

3 files changed

+71
-2
lines changed

3 files changed

+71
-2
lines changed

.travis.yml

+6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
language: scala
22

3+
before_install:
4+
- cat /etc/hosts # optionally check the content *before*
5+
- sudo hostname "$(hostname | cut -c1-63)"
6+
- sed -e "s/^\\(127\\.0\\.0\\.1.*\\)/\\1 $(hostname | cut -c1-63)/" /etc/hosts | sudo tee /etc/hosts
7+
- cat /etc/hosts # optionally check the content *after*
8+
39
env:
410
global:
511
- PUBLISH_JDK=openjdk6

src/main/scala/scala/util/parsing/input/OffsetPosition.scala

+8-2
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,14 @@ case class OffsetPosition(source: CharSequence, offset: Int) extends Position {
6262
*
6363
* @return the line at `offset` (not including a newline)
6464
*/
65-
def lineContents: String =
66-
source.subSequence(index(line - 1), index(line)).toString
65+
def lineContents: String = {
66+
val endIndex = if (source.charAt(index(line) - 1) == '\n') {
67+
index(line) - 1
68+
} else {
69+
index(line)
70+
}
71+
source.subSequence(index(line - 1), endIndex).toString
72+
}
6773

6874
/** Returns a string representation of the `Position`, of the form `line.column`. */
6975
override def toString = line+"."+column
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package scala.util.parsing.combinator
2+
3+
import org.junit.Assert.{assertEquals, assertTrue}
4+
import org.junit.Test
5+
6+
import scala.util.parsing.combinator.syntactical.StandardTokenParsers
7+
8+
/**
9+
* Test for issue 56: https://github.com/scala/scala-parser-combinators/issues/56
10+
*
11+
* Makes sure that lineContents (and thus longString) in the Position trait doesn't
12+
* include a newline
13+
*/
14+
class gh56 {
15+
private object grammar extends StandardTokenParsers with PackratParsers {
16+
lazy val term = (numericLit | stringLit | ident)+
17+
}
18+
19+
@Test
20+
def test1: Unit = {
21+
import grammar._
22+
23+
val expr =
24+
"""/* an unclosed comment
25+
|of multiple lines
26+
|just to check longString/lineContents
27+
""".stripMargin
28+
29+
val fail =
30+
"""[1.1] failure: identifier expected
31+
|
32+
|/* an unclosed comment
33+
|^""".stripMargin
34+
35+
val parseResult = phrase(term)(new lexical.Scanner(expr))
36+
assertTrue(parseResult.isInstanceOf[Failure])
37+
assertEquals(fail, parseResult.toString)
38+
}
39+
40+
41+
@Test
42+
def test2: Unit = {
43+
import grammar._
44+
45+
val expr = "/* an unclosed comment without newline"
46+
47+
val fail =
48+
"""[1.1] failure: identifier expected
49+
|
50+
|/* an unclosed comment without newline
51+
|^""".stripMargin
52+
53+
val parseResult = phrase(term)(new lexical.Scanner(expr))
54+
assertTrue(parseResult.isInstanceOf[Failure])
55+
assertEquals(fail, parseResult.toString)
56+
}
57+
}

0 commit comments

Comments
 (0)