Skip to content

Commit

Permalink
Merge pull request #33 from samtkit/question-mark-crash
Browse files Browse the repository at this point in the history
Fix crash on ?Type
  • Loading branch information
mjossdev authored May 27, 2023
2 parents 45a255c + a60b375 commit c3010a0
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
12 changes: 11 additions & 1 deletion parser/src/main/kotlin/tools/samt/parser/Parser.kt
Original file line number Diff line number Diff line change
Expand Up @@ -493,11 +493,21 @@ class Parser private constructor(
check<OpenBraceToken>() -> parseObjectNode()

skip<AsteriskToken>() -> WildcardNode(locationFromStart(start))
skip<QuestionMarkToken>() -> {
val literal = parseLiteral()
diagnostic.error {
message("Nullability is indicated after a type")
highlight(locationFromStart(start), highlightBeginningOnly = true)
info("A valid nullable type looks like 'Int?' or 'String(size(1..*))?'")
help("To declare the type nullable move the question mark to the end of the type")
}
literal
}

else -> {
diagnostic.fatal {
message("Expected an expression")
highlight(locationFromStart(start), highlightBeginningOnly = true)
highlight(current!!.location, highlightBeginningOnly = true)
}
}
}
Expand Down
11 changes: 11 additions & 0 deletions parser/src/test/kotlin/tools/samt/parser/ParserTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -542,6 +542,17 @@ class ParserTest {
val exception = parseWithFatalError(source)
assertEquals("Expected an expression", exception.message)
}

@Test
fun `unexpected question mark`() {
val source = """
package a
typealias A = ?String
"""
val (_, diagnostics) = parseWithRecoverableError(source)
assertEquals("Nullability is indicated after a type", diagnostics.messages.single().message)
}
}

@Nested
Expand Down

0 comments on commit c3010a0

Please sign in to comment.