Skip to content

Prevent OffsetPosition.lineContents from grabbing a newline #77

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
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Fixes OffsetPosition.lineContents so that it doesn't grab a newline a…
…t the end of the line. Includes tests.
  • Loading branch information
toddobryan-amazon authored and gourlaysama committed Feb 10, 2016
commit 3eef4564503a366ae6701b034126c3720ed921bb
10 changes: 8 additions & 2 deletions src/main/scala/scala/util/parsing/input/OffsetPosition.scala
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,14 @@ case class OffsetPosition(source: CharSequence, offset: Int) extends Position {
*
* @return the line at `offset` (not including a newline)
*/
def lineContents: String =
source.subSequence(index(line - 1), index(line)).toString
def lineContents: String = {
val endIndex = if (source.charAt(index(line) - 1) == '\n') {
index(line) - 1
} else {
index(line)
}
source.subSequence(index(line - 1), endIndex).toString
}

/** Returns a string representation of the `Position`, of the form `line.column`. */
override def toString = line+"."+column
Expand Down
57 changes: 57 additions & 0 deletions src/test/scala/scala/util/parsing/combinator/gh56.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package scala.util.parsing.combinator

import org.junit.Assert.{assertEquals, assertTrue}
import org.junit.Test

import scala.util.parsing.combinator.syntactical.StandardTokenParsers

/**
* Test for issue 56: https://github.com/scala/scala-parser-combinators/issues/56
*
* Makes sure that lineContents (and thus longString) in the Position trait doesn't
* include a newline
*/
class gh56 {
private object grammar extends StandardTokenParsers with PackratParsers {
lazy val term = (numericLit | stringLit | ident)+
}

@Test
def test1: Unit = {
import grammar._

val expr =
"""/* an unclosed comment
|of multiple lines
|just to check longString/lineContents
""".stripMargin

val fail =
"""[1.1] failure: identifier expected
|
|/* an unclosed comment
|^""".stripMargin

val parseResult = phrase(term)(new lexical.Scanner(expr))
assertTrue(parseResult.isInstanceOf[Failure])
assertEquals(fail, parseResult.toString)
}


@Test
def test2: Unit = {
import grammar._

val expr = "/* an unclosed comment without newline"

val fail =
"""[1.1] failure: identifier expected
|
|/* an unclosed comment without newline
|^""".stripMargin

val parseResult = phrase(term)(new lexical.Scanner(expr))
assertTrue(parseResult.isInstanceOf[Failure])
assertEquals(fail, parseResult.toString)
}
}