Skip to content

Commit de080ab

Browse files
WAT: Fix literal parsing with underscores
1 parent fc0e985 commit de080ab

File tree

2 files changed

+10
-5
lines changed

2 files changed

+10
-5
lines changed

Sources/WAT/Lexer.swift

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -480,18 +480,25 @@ extension Lexer.Cursor {
480480
/// - Returns: The parsed string without underscores
481481
mutating func parseUnderscoredChars(continueParsing: (Unicode.Scalar) -> Bool) throws -> String {
482482
var value = String.UnicodeScalarView()
483-
var lastChar: Unicode.Scalar?
483+
var lastParsedChar: Unicode.Scalar?
484484
while let char = try peek() {
485-
lastChar = char
486485
if char == "_" {
486+
guard let lastChar = lastParsedChar else {
487+
throw createError("Invalid hex number, leading underscore")
488+
}
489+
guard lastChar != "_" else {
490+
throw createError("Invalid hex number, consecutive underscores")
491+
}
492+
lastParsedChar = char
487493
_ = try next()
488494
continue
489495
}
490496
guard continueParsing(char) else { break }
497+
lastParsedChar = char
491498
value.append(char)
492499
_ = try next()
493500
}
494-
if lastChar == "_" {
501+
if lastParsedChar == "_" {
495502
throw createError("Invalid hex number, trailing underscore")
496503
}
497504
return String(value)

Tests/WATTests/EncoderTests.swift

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,9 +107,7 @@ class EncoderTests: XCTestCase {
107107

108108
var stats = CompatibilityTestStats()
109109
let excluded: [String] = [
110-
"float_literals.wast",
111110
"imports.wast",
112-
"int_literals.wast",
113111
"start.wast",
114112
"token.wast",
115113
"utf8-invalid-encoding.wast"

0 commit comments

Comments
 (0)