Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit d473236

Browse files
committedJul 5, 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 fcb6afa commit d473236

File tree

3 files changed

+22
-6
lines changed

3 files changed

+22
-6
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
Original file line numberDiff line numberDiff line change
@@ -123,4 +123,20 @@ class StdLexicalTest {
123123
lex(Lexer, "/* single */ id1 /* multi \n line */ id2")
124124
)
125125
}
126+
127+
@Test
128+
def parseUnclosedComments: Unit = {
129+
object Lexer extends StdLexical
130+
import Lexer._
131+
132+
assertEquals(
133+
List(Identifier("id"), ErrorToken("unclosed comment")),
134+
lex(Lexer, "id /*")
135+
)
136+
137+
assertEquals(
138+
List(Identifier("id"), ErrorToken("unclosed comment")),
139+
lex(Lexer, "id /* ")
140+
)
141+
}
126142
}

0 commit comments

Comments
 (0)
Please sign in to comment.