Skip to content

Commit

Permalink
WAT: Fix lexing of block comments
Browse files Browse the repository at this point in the history
Close #164
  • Loading branch information
kateinoigakukun committed Dec 14, 2024
1 parent d282a70 commit e714e82
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
5 changes: 4 additions & 1 deletion Sources/WAT/Lexer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions Tests/WATTests/LexerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class LexerTests: XCTestCase {
}

func testLexComment() {
XCTAssertEqual(try collectToken("(; foo ;)"), [.blockComment])
try XCTAssertEqual(
collectToken(
"""
Expand All @@ -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))])
Expand Down

0 comments on commit e714e82

Please sign in to comment.