Skip to content

Commit 414af16

Browse files
committedJul 8, 2021
StdLexical now emits one ErrorToken for unclosed comments.
The remaining input is consumed so the ErrorToken is the last token emitted by the scanner.
1 parent e952ff4 commit 414af16

File tree

3 files changed

+22
-7
lines changed

3 files changed

+22
-7
lines changed
 

Diff for: ‎shared/src/main/scala/scala/util/parsing/combinator/lexical/StdLexical.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ class StdLexical extends Lexical with StdTokens {
5858
whitespaceChar
5959
| '/' ~ '*' ~ comment
6060
| '/' ~ '/' ~ rep( chrExcept(EofCh, '\n') )
61-
| '/' ~ '*' ~ failure("unclosed comment")
61+
| '/' ~ '*' ~ rep( elem("", _ => true) ) ~> err("unclosed comment")
6262
)
6363

6464
protected def comment: Parser[Any] = (

Diff for: ‎shared/src/test/scala/scala/util/parsing/combinator/gh56.scala

+5-5
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,12 @@ class gh56 {
2525
"""/* an unclosed comment
2626
|of multiple lines
2727
|just to check longString/lineContents
28-
""".stripMargin
28+
|""".stripMargin
2929

3030
val fail =
31-
"""[1.1] failure: identifier expected
31+
"""[4.1] failure: identifier expected
32+
|
3233
|
33-
|/* an unclosed comment
3434
|^""".stripMargin
3535

3636
val parseResult = phrase(term)(new lexical.Scanner(expr))
@@ -46,10 +46,10 @@ class gh56 {
4646
val expr = "/* an unclosed comment without newline"
4747

4848
val fail =
49-
"""[1.1] failure: identifier expected
49+
"""[1.39] failure: identifier expected
5050
|
5151
|/* an unclosed comment without newline
52-
|^""".stripMargin
52+
| ^""".stripMargin
5353

5454
val parseResult = phrase(term)(new lexical.Scanner(expr))
5555
assertTrue(parseResult.isInstanceOf[Failure])

Diff for: ‎shared/src/test/scala/scala/util/parsing/combinator/lexical/StdLexicalTest.scala

+16-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import org.junit.Assert.assertEquals
66
import scala.util.parsing.input.Reader
77

88
import scala.collection.mutable.ListBuffer
9-
import java.awt.RenderingHints.Key
109

1110
class StdLexicalTest {
1211
private def lex[Lexer <: StdLexical](lexer: Lexer, input: String): List[lexer.Token] = {
@@ -123,4 +122,20 @@ class StdLexicalTest {
123122
lex(Lexer, "/* single */ id1 /* multi \n line */ id2")
124123
)
125124
}
125+
126+
@Test
127+
def parseUnclosedComments: Unit = {
128+
object Lexer extends StdLexical
129+
import Lexer._
130+
131+
assertEquals(
132+
List(Identifier("id"), ErrorToken("unclosed comment")),
133+
lex(Lexer, "id /*")
134+
)
135+
136+
assertEquals(
137+
List(Identifier("id"), ErrorToken("unclosed comment")),
138+
lex(Lexer, "id /* ")
139+
)
140+
}
126141
}

0 commit comments

Comments
 (0)
Please sign in to comment.