Skip to content

Commit c3010a0

Browse files
authored
Merge pull request #33 from samtkit/question-mark-crash
Fix crash on ?Type
2 parents 45a255c + a60b375 commit c3010a0

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

parser/src/main/kotlin/tools/samt/parser/Parser.kt

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -493,11 +493,21 @@ class Parser private constructor(
493493
check<OpenBraceToken>() -> parseObjectNode()
494494

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

497507
else -> {
498508
diagnostic.fatal {
499509
message("Expected an expression")
500-
highlight(locationFromStart(start), highlightBeginningOnly = true)
510+
highlight(current!!.location, highlightBeginningOnly = true)
501511
}
502512
}
503513
}

parser/src/test/kotlin/tools/samt/parser/ParserTest.kt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -542,6 +542,17 @@ class ParserTest {
542542
val exception = parseWithFatalError(source)
543543
assertEquals("Expected an expression", exception.message)
544544
}
545+
546+
@Test
547+
fun `unexpected question mark`() {
548+
val source = """
549+
package a
550+
551+
typealias A = ?String
552+
"""
553+
val (_, diagnostics) = parseWithRecoverableError(source)
554+
assertEquals("Nullability is indicated after a type", diagnostics.messages.single().message)
555+
}
545556
}
546557

547558
@Nested

0 commit comments

Comments
 (0)