Skip to content

Commit e714e82

Browse files
WAT: Fix lexing of block comments
Close #164
1 parent d282a70 commit e714e82

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

Sources/WAT/Lexer.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,10 @@ struct Lexer {
306306
private mutating func lexBlockComment() throws -> TokenKind {
307307
var level = 1
308308
while true {
309-
switch try cursor.next() {
309+
guard let char = try cursor.next() else {
310+
throw cursor.unexpectedEof()
311+
}
312+
switch char {
310313
case "(":
311314
if try cursor.peek() == ";" {
312315
// Nested comment block

Tests/WATTests/LexerTests.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ class LexerTests: XCTestCase {
2525
}
2626

2727
func testLexComment() {
28+
XCTAssertEqual(try collectToken("(; foo ;)"), [.blockComment])
2829
try XCTAssertEqual(
2930
collectToken(
3031
"""
@@ -40,6 +41,12 @@ class LexerTests: XCTestCase {
4041

4142
}
4243

44+
func testLexBrokenComment() {
45+
XCTAssertThrowsError(try collectToken("(;)"))
46+
XCTAssertThrowsError(try collectToken("(; foo )"))
47+
XCTAssertThrowsError(try collectToken(";)"))
48+
}
49+
4350
func testLexIdAndString() throws {
4451
try XCTAssertEqual(collectToken("$foo"), [.id])
4552
try XCTAssertEqual(collectToken("\"foo\""), [.string(Array("foo".utf8))])

0 commit comments

Comments
 (0)