@@ -91,17 +91,10 @@ struct ExpressionParser<Visitor: InstructionVisitor> {
9191 }
9292 }
9393
94- @discardableResult
95- mutating func parse( visitor: inout Visitor , wat: inout Wat ) throws -> Int {
96- var numberOfInstructions = 0
97- while true {
98- guard try instruction ( visitor: & visitor, wat: & wat) else {
99- numberOfInstructions += 1
100- break
101- }
94+ mutating func parse( visitor: inout Visitor , wat: inout Wat ) throws {
95+ while try instruction ( visitor: & visitor, wat: & wat) {
10296 // Parse more instructions
10397 }
104- return numberOfInstructions
10598 }
10699
107100 mutating func parseElemExprList( visitor: inout Visitor , wat: inout Wat ) throws {
@@ -170,7 +163,7 @@ struct ExpressionParser<Visitor: InstructionVisitor> {
170163
171164 /// Parse "(instr)" or "instr" and visit the instruction.
172165 /// - Returns: `true` if an instruction was parsed. Otherwise, `false`.
173- private mutating func instruction( visitor: inout Visitor , wat: inout Wat ) throws -> Bool {
166+ mutating func instruction( visitor: inout Visitor , wat: inout Wat ) throws -> Bool {
174167 if try nonFoldedInstruction ( visitor: & visitor, wat: & wat) {
175168 return true
176169 }
@@ -206,9 +199,7 @@ struct ExpressionParser<Visitor: InstructionVisitor> {
206199 }
207200 try parser. expect ( . leftParen)
208201 let keyword = try parser. expectKeyword ( )
209- guard let visit = try parseTextInstruction ( keyword: keyword, wat: & wat) else {
210- return false
211- }
202+ let visit = try parseTextInstruction ( keyword: keyword, wat: & wat)
212203 let suspense : Suspense
213204 switch keyword {
214205 case " if " :
@@ -265,7 +256,7 @@ struct ExpressionParser<Visitor: InstructionVisitor> {
265256 }
266257
267258 /// Parse a single instruction without consuming the surrounding parentheses and instruction keyword.
268- private mutating func parseTextInstruction( keyword: String , wat: inout Wat ) throws -> ( ( inout Visitor ) throws -> Visitor . Output ) ? {
259+ private mutating func parseTextInstruction( keyword: String , wat: inout Wat ) throws -> ( ( inout Visitor ) throws -> Visitor . Output ) {
269260 switch keyword {
270261 case " select " :
271262 // Special handling for "select", which have two variants 1. with type, 2. without type
@@ -293,7 +284,10 @@ struct ExpressionParser<Visitor: InstructionVisitor> {
293284 }
294285 default :
295286 // Other instructions are parsed by auto-generated code.
296- return try WAT . parseTextInstruction ( keyword: keyword, expressionParser: & self , wat: & wat)
287+ guard let visit = try WAT . parseTextInstruction ( keyword: keyword, expressionParser: & self , wat: & wat) else {
288+ throw WatParserError ( " unknown instruction \( keyword) " , location: parser. lexer. location ( ) )
289+ }
290+ return visit
297291 }
298292 }
299293
@@ -302,12 +296,8 @@ struct ExpressionParser<Visitor: InstructionVisitor> {
302296 guard let keyword = try parser. peekKeyword ( ) else {
303297 return false
304298 }
305- let originalParser = parser
306299 try parser. consume ( )
307- guard let visit = try parseTextInstruction ( keyword: keyword, wat: & wat) else {
308- parser = originalParser
309- return false
310- }
300+ let visit = try parseTextInstruction ( keyword: keyword, wat: & wat)
311301 _ = try visit ( & visitor)
312302 return true
313303 }
@@ -455,7 +445,7 @@ extension ExpressionParser {
455445 } else {
456446 tableIndex = 0
457447 }
458- let typeUse = try withWatParser { try $0. typeUse ( mayHaveName: true ) }
448+ let typeUse = try withWatParser { try $0. typeUse ( mayHaveName: false ) }
459449 let ( _, typeIndex) = try wat. types. resolve ( use: typeUse)
460450 return ( UInt32 ( typeIndex) , tableIndex)
461451 }
0 commit comments