diff --git a/Sources/WAT/Lexer.swift b/Sources/WAT/Lexer.swift index e914b47a..15ebbf38 100644 --- a/Sources/WAT/Lexer.swift +++ b/Sources/WAT/Lexer.swift @@ -306,7 +306,10 @@ struct Lexer { private mutating func lexBlockComment() throws -> TokenKind { var level = 1 while true { - switch try cursor.next() { + guard let char = try cursor.next() else { + throw cursor.unexpectedEof() + } + switch char { case "(": if try cursor.peek() == ";" { // Nested comment block diff --git a/Tests/WATTests/LexerTests.swift b/Tests/WATTests/LexerTests.swift index 623a963d..ac987110 100644 --- a/Tests/WATTests/LexerTests.swift +++ b/Tests/WATTests/LexerTests.swift @@ -25,6 +25,7 @@ class LexerTests: XCTestCase { } func testLexComment() { + XCTAssertEqual(try collectToken("(; foo ;)"), [.blockComment]) try XCTAssertEqual( collectToken( """ @@ -40,6 +41,12 @@ class LexerTests: XCTestCase { } + func testLexBrokenComment() { + XCTAssertThrowsError(try collectToken("(;)")) + XCTAssertThrowsError(try collectToken("(; foo )")) + XCTAssertThrowsError(try collectToken(";)")) + } + func testLexIdAndString() throws { try XCTAssertEqual(collectToken("$foo"), [.id]) try XCTAssertEqual(collectToken("\"foo\""), [.string(Array("foo".utf8))])