diff --git a/Sources/NIOIMAPCore/Parser/Grammar/GrammarParser.swift b/Sources/NIOIMAPCore/Parser/Grammar/GrammarParser.swift index b70e3edc1..bdb668620 100644 --- a/Sources/NIOIMAPCore/Parser/Grammar/GrammarParser.swift +++ b/Sources/NIOIMAPCore/Parser/Grammar/GrammarParser.swift @@ -771,18 +771,28 @@ extension GrammarParser { } func parseIDParamsList_empty(buffer: inout ParseBuffer, tracker: StackTracker) throws -> OrderedDictionary { - try PL.parseFixedString("()", buffer: &buffer, tracker: tracker) + try PL.parseFixedString("(", buffer: &buffer, tracker: tracker) + _ = try PL.parseOptional(buffer: &buffer, tracker: tracker) { buffer, tracker in + try PL.parseSpaces(buffer: &buffer, tracker: tracker) + } + try PL.parseFixedString(")", buffer: &buffer, tracker: tracker) return [:] } func parseIDParamsList_some(buffer: inout ParseBuffer, tracker: StackTracker) throws -> OrderedDictionary { try PL.parseFixedString("(", buffer: &buffer, tracker: tracker) + _ = try PL.parseOptional(buffer: &buffer, tracker: tracker) { buffer, tracker in + try PL.parseSpaces(buffer: &buffer, tracker: tracker) + } let (key, value) = try parseIDParamsList_element(buffer: &buffer, tracker: tracker) var dic: OrderedDictionary = [key: value] try PL.parseZeroOrMore(buffer: &buffer, into: &dic, tracker: tracker) { (buffer, tracker) -> (String, String?) in try PL.parseSpaces(buffer: &buffer, tracker: tracker) return try parseIDParamsList_element(buffer: &buffer, tracker: tracker) } + _ = try PL.parseOptional(buffer: &buffer, tracker: tracker) { buffer, tracker in + try PL.parseSpaces(buffer: &buffer, tracker: tracker) + } try PL.parseFixedString(")", buffer: &buffer, tracker: tracker) return dic } diff --git a/Tests/NIOIMAPCoreTests/Parser/IMAPParserTests.swift b/Tests/NIOIMAPCoreTests/Parser/IMAPParserTests.swift index d0fca291e..bd555dc4d 100644 --- a/Tests/NIOIMAPCoreTests/Parser/IMAPParserTests.swift +++ b/Tests/NIOIMAPCoreTests/Parser/IMAPParserTests.swift @@ -1414,6 +1414,8 @@ extension ParserUnitTests { testFunction: GrammarParser().parseIDParamsList, validInputs: [ ("NIL", " ", [:], #line), + ("()", " ", [:], #line), + ("( )", " ", [:], #line), (#"("key1" "value1")"#, "", ["key1": "value1"], #line), ( #"("key1" "value1" "key2" "value2" "key3" "value3")"#, @@ -1427,6 +1429,25 @@ extension ParserUnitTests { ["key1": "£", "flag": "🏴󠁧󠁢󠁥󠁮󠁧󠁿"], #line ), + ( + #"("a" "1" "b" "2")"#, + "", + ["a": "1", "b": "2"], + #line + ), + // Extra spaces + ( + #"( "a" "1" "b" "2" )"#, + "", + ["a": "1", "b": "2"], + #line + ), + ( + #"("a" "1" "b" "2")"#, + "", + ["a": "1", "b": "2"], + #line + ), ], parserErrorInputs: [], incompleteMessageInputs: []