mirror of
https://github.com/apple/swift-nio-imap
synced 2026-11-27 05:04:23 +00:00
Internal parser functions throw `ParseFailure` (24 bytes, returned in
registers), so a failed speculative match no longer calls
swift_allocError. Public API keeps untyped throws and the same error
types; the entry points convert back with `unwrapped`.
- Array-form `parseOneOf` takes `ParseAlternative`s, which avoid
reabstraction thunks; a typed-throws closure literal as a
`[SubParser]` element crashes SILGen (6.3, 6.4).
- `try? parseSpaces()` becomes an explicit `catch .mismatch`; no
behavior change, since `parseSpaces` only reports incomplete on empty
input. Its doc comment is corrected to match.
- CommandParser: framing runs outside the incomplete catch; it throws
only `ParserError`.
- Untyped public API called by the grammar (`MailboxPath.init`,
`ModifiedUTF7.decode`) is wrapped: `throw ParseFailure(error)`.
- SynchronizingLiteralParser keeps untyped throws.
Reviewer focus: `ParseFailure` (`init(_:)`, `unwrapped`), the
combinators' catch clauses (`.mismatch`, `.mismatch, .badCommand`),
the entry points in CommandParser and ResponseParser (`catch
.incomplete`, `unwrapped`), `ParseAlternative`.
Hand edits, whole files taken from this commit:
- ParserErrors.swift: `ParseFailure`; typed `decodeUTF8`; doc
comments.
- ParseInput+Combinators.swift: typed `SubParser`, `ParseAlternative`,
catch clauses, 2-/3-way `parseOneOf` on `composite`; otherwise the
same conversion as the grammar.
- ParseInput.swift: `composite` takes `SubParser`; doc comments;
otherwise the same conversion as the grammar.
- CommandParser.swift, ResponseParser.swift: entry points; otherwise
the same conversion as the grammar.
- ParseInputCombinatorTests.swift: `ParseFailure` tests,
`ParseAlternative` call sites; otherwise the same conversion.
- ParseInputHarness.swift: `parseErrorThrown`.
- ResponseText+Tests.swift: `parseErrorThrown` and `#expect(error is
ParserError)` instead of `#expect(throws: ParserError.self)`.
Hand edits inside the script: `HAND` (selectingKeyword doc comment,
the two untyped-API wrappers); `OWN_FRAME`: `parseSearchKey` builds
its 35 alternatives in a nested `alternatives()`, off the recursive
frame; `ParseAlternative` naming and layout (`NAMED_FILES`, `RENAME`,
`ONE_PER_LINE`); `BRACE_BELOW`; `EXPLAIN_DO`; the emitted comments.
The rest is the output of the script below. To reproduce, on the
parent commit (swift-format from Swift 6.4.4):
F=(Sources/NIOIMAPCore/Parser/Grammar/*.swift
Tests/NIOIMAPCoreTests/ParseFixture.swift
Tests/NIOIMAPCoreTests/Grammar/CommandType/{CommandType,TaggedCommand}+Tests.swift
Tests/NIOIMAPCoreTests/Parser/Grammar/GrammarParser+Primitives+Tests.swift
Tests/NIOIMAPCoreTests/Parser/ParseInputTests.swift)
python3 convert_typed_throws.py "${F[@]}" && swift format -i "${F[@]}"
then check out the hand-edit files from this commit.
convert_typed_throws.py:
#!/usr/bin/env python3
# Converts the parser's internals from untyped `throws` to `throws(ParseFailure)`.
#
# Usage: python3 convert_typed_throws.py FILE... (in place), then `swift format -i FILE...`
import re
import sys
TYPED = 'throws(ParseFailure)'
# Public error type -> the ParseFailure case that carries it.
CASES = {
'ParserError': 'mismatch',
'BadCommand': 'badCommand',
'NoForwardProgress': 'noForwardProgress',
'TooMuchRecursion': 'tooMuchRecursion',
'ExceededLiteralSizeLimitError': 'literalSizeExceeded',
'ExceededMaximumMessageAttributesError': 'messageAttributesExceeded',
'ExceededMaximumBodySizeError': 'bodySizeExceeded',
}
TYPES = '|'.join(CASES)
# `ParseAlternative` names: `pN` by default; named after the alternative's suffix in these files
# (`parseSearchKey_older` -> `older`), with these exceptions.
NAMED_FILES = ['+Body.swift', '+Commands.swift', '+Response.swift', '+Search.swift']
RENAME = {
'parseBodyEncoding_quotePrintable': 'quotedPrintable',
'parseBodyEncoding_nil': 'nilEncoding',
'parseSuffix_metadataLongEntries': 'longEntries',
'parseSuffix_metadataMaxSize': 'maxSize',
'parseSuffix_metadataTooMany': 'tooMany',
'parseSuffix_metadataNoPrivate': 'noPrivate',
'parseKnownResponseTextCode': 'known',
}
KEYWORDS = {'nil', 'extension'}
# Alternatives (by the first one) whose array is laid out one per line rather than left to swift-format.
ONE_PER_LINE = {'parseSortCriterion_displayFrom', 'parseSearchReturnData_min', 'parseSearchKey_older'}
# Productions whose alternatives are built in a nested function, off the recursive frame.
OWN_FRAME = {'parseSearchKey'}
# Declarations left to swift-format, which puts `{` on its own line; all others too long for one line
# put each parameter on its own line.
BRACE_BELOW = {'parseCompoundObjectID'}
# Functions in which an annotated `do` gets a comment saying why.
EXPLAIN_DO = {'parseTaggedCommand', 'parseTaggedExtensionComplex_helper'}
LINE_LENGTH = 120
# Hand-written changes: (file, old, new), each matching exactly once.
HAND = [
('GrammarParser.swift', '''\
/// - Throws: `IncompleteMessage` when every byte that has arrived is part of the keyword, since
/// more could still follow; ``ParserError`` when the first byte cannot start one.
''', '''\
/// - Throws: ``ParseFailure/incomplete`` when every byte that has arrived is part of the keyword,
/// since more could still follow; ``ParseFailure/mismatch(_:)`` when the first byte cannot start
/// one.
'''),
('GrammarParser.swift', '''\
/// - Important: `select` should throw ``ParserError`` for an unrecognised keyword — not
/// `IncompleteMessage`, which would make the caller wait for bytes that cannot help. Callers
/// that fall back to another production (`parseFetchAttribute`, say) catch exactly that.
''', '''\
/// Pass an annotated closure: `{ keyword, buffer throws(ParseFailure) in switch keyword { … } }`.
///
/// - Important: `select` should throw ``ParseFailure/mismatch(_:)`` for an unrecognised keyword —
/// not ``ParseFailure/incomplete``, which would make the caller wait for bytes that cannot help.
/// The type can't enforce this. Callers that fall back to another production
/// (`parseFetchAttribute`, say) catch exactly `.mismatch`.
'''),
# Untyped public API called from the grammar: classify its error.
('GrammarParser.swift', '''\
return try ModifiedUTF7.decode(value)
''', '''\
do {
return try ModifiedUTF7.decode(value)
} catch {
throw ParseFailure(error)
}
'''),
('GrammarParser+Mailbox.swift', '''\
return MailboxInfo(
attributes: flags,
path: try .init(name: mailbox, pathSeparator: character),
''', '''\
// `MailboxPath.init(name:pathSeparator:)` is untyped public API (throws `InvalidPathSeparatorError`).
let path: MailboxPath
do {
path = try MailboxPath(name: mailbox, pathSeparator: character)
} catch {
throw ParseFailure(error)
}
return MailboxInfo(
attributes: flags,
path: path,
'''),
]
def closing(src, k):
"""Index just past the bracket matching the one before `k`."""
depth = 1
while depth:
c = src[k]
if c == '"': # skip string literals, which may hold brackets
k += 1
while src[k] != '"':
k += 2 if src[k] == '\\' else 1
elif c in '([{':
depth += 1
elif c in ')]}':
depth -= 1
k += 1
return k
def code_lines(src, fn):
"""Applies `fn` to every line that is not a comment."""
return ''.join(l if l.lstrip().startswith('//') else fn(l) for l in src.splitlines(keepends=True))
def throw_sites(src):
# throw ParserError(hint: L) -> throw .mismatch(L); other labels (dynamicHint:, expectedDigits:) stay.
src = re.sub(r'throw ParserError\((hint:\s*)?', 'throw .mismatch(', src)
src = src.replace('throw IncompleteMessage()', 'throw .incomplete')
# throw X(…) -> throw .case(X(…)) for the payload-carrying cases.
out, i = [], 0
for m in re.finditer(rf'throw ({TYPES})\(', src):
end = closing(src, m.end())
out += [src[i:m.start()], f'throw .{CASES[m.group(1)]}(', src[m.start() + len('throw '):end], ')']
i = end
return ''.join(out) + src[i:]
def catch_clauses(src):
# `catch is A, is B` / `catch let e as A` -> `catch .a, .b` / `catch .a(let e)`; adjacent clauses that
# differ only in type merge: `} catch is A {\n X } catch is B {\n X }` -> `} catch .a, .b {\n X }`.
src = re.sub(rf'catch let (\w+) as ({TYPES})\b', lambda m: f'catch .{CASES[m.group(2)]}(let {m.group(1)})', src)
src = re.sub(rf'catch ((?:is (?:{TYPES})(?:,\s*)?)+)',
lambda m: 'catch ' + ', '.join('.' + CASES[t] for t in re.findall(rf'is ({TYPES})', m.group(1))),
src)
src = re.sub(r'catch (\.\w+(?:, \.\w+)*) \{(\n[^\n]*\n\s*)\} catch (\.\w+) \{\2\}', r'catch \1, \3 {\2}', src)
# Rethrowing what a clause bound re-wraps it: `catch .x(let e) { throw e }` -> `throw .x(e)`.
return re.sub(r'(catch \.(\w+)\(let (\w+)\) \{\s*throw )\3\b', r'\1.\2(\3)', src)
def closures(src):
# Two-parameter closure literals: `{ a, b -> T in` / `{ (a, b) in` gain the annotation.
src = re.sub(r'(\{\s*\(?\w+,\s*\w+\)?)(\s*->[^\n{]*?)?(\s+in\b)',
lambda m: f'{m.group(1)} {TYPED}{m.group(2) or ""}{m.group(3)}', src)
# Closures with typed parameters: `{ (buffer: [UInt8]) -> String in`.
src = re.sub(r'(\{\s*\((?:\w+: [^()]+)\))(\s*->[^\n{]*?\s+in\b)', rf'\1 {TYPED}\2', src)
# `{ try f(buffer: &$0, tracker: $1) }` -> `{ b, t throws(ParseFailure) in try f(buffer: &b, tracker: t) }`
return re.sub(r'\{(\s*)(try [^{}\n]*\$0[^{}\n]*?)(\s*)\}',
lambda m: f'{{ b, t {TYPED} in{m.group(1)}{m.group(2).replace("$0", "b").replace("$1", "t")}{m.group(3)}}}',
src)
def return_type(src, name):
"""The declared result type of `func name(…)` in `src`."""
m = re.search(rf'func {name}\(', src)
end = closing(src, m.end())
return re.match(r'\s*throws[^-{]*->\s*([^{]+?)\s*\{', src[end:]).group(1)
def alternative_name(fn, numbered, i):
if numbered:
return f'p{i}'
name = RENAME.get(fn) or fn.split('_')[-1]
return f'_{name}' if name in KEYWORDS or name[0].isdigit() else name
def array_literal(names, one_per_line):
return '[\n' + ''.join(f' {n},\n' for n in names) + ']' if one_per_line else f'[{", ".join(names)}]'
def rewrite_array_one_of(src, path):
# The array form of parseOneOf takes `ParseAlternative`s, one `let` each (a typed-throws closure
# literal as an array element crashes SILGen):
# return try buffer.parseOneOf([{ try parseX_a(buffer: &$0, tracker: $1) }, self.parseB], tracker: tracker)
# ->
# let p1 = ParseAlternative<T> { b, t throws(ParseFailure) in
# try parseX_a(buffer: &b, tracker: t)
# }
# let b = ParseAlternative<T> { b, t throws(ParseFailure) in
# try self.parseB(buffer: &b, tracker: t)
# }
# return try buffer.parseOneOf([b, p1], tracker: tracker)
numbered = not any(path.endswith(f) for f in NAMED_FILES)
out, i = [], 0
for m in re.finditer(r'(\w+)\.parseOneOf\(\s*\[', src):
line = src.rfind('\n', 0, m.start()) + 1
indent = re.match(r'\s*', src[line:]).group()
close = closing(src, m.end())
tail = re.match(r'\s*,\s*tracker: (\w+)\s*\)', src[close:])
calls = re.findall(r'\{ try (\w+)\(buffer: &\$0, tracker: \$1\) \}', src[m.end():close])
methods = re.findall(r'self\.(\w+),', src[m.end():close])
assert len(calls) + len(methods) == src[m.end():close].count('\n') - 1, src[m.end():close]
t = return_type(src, (calls + methods)[0])
own_frame = calls[0].split('_')[0] in OWN_FRAME
width = LINE_LENGTH - len(indent) - 4 * own_frame
def let(name, call):
short = f'let {name} = ParseAlternative<{t}> {{ b, t {TYPED} in try {call}(buffer: &b, tracker: t) }}\n'
long = f'let {name} = ParseAlternative<{t}> {{ b, t {TYPED} in\n try {call}(buffer: &b, tracker: t)\n}}\n'
return short if len(short) - 1 <= width else long
lets, names = '', []
for k, fn in enumerate(calls, 1):
names.append(alternative_name(fn, numbered, k))
lets += let(names[-1], fn)
assert len(methods) <= 1, methods # the one site lists it first: `[self.parseFlagList, { … }]`
for fn in methods: # `self.parseFlagList` -> `flagList`, first in the array as in the literal
names.insert(0, fn[len('parse')].lower() + fn[len('parse') + 1:])
lets += let(names[0], 'self.' + fn)
# More than one statement now, so an implicit return becomes explicit.
head = re.sub(r'^try ', 'return try ', src[line + len(indent):m.start()])
call = f'{head}{m.group(1)}.parseOneOf'
if own_frame:
body = lets + 'return ' + array_literal(names, True)
stmt = (f'// Built in its own frame: the {len(names)} closure values would otherwise sit in every recursion'
" level's\n// frame (debug builds keep each `let`), and `(` nests this production up to the tracker"
' limit.\n'
f'func alternatives() -> [ParseAlternative<{t}>] {{\n'
+ re.sub(r'^(?=.)', ' ', body, flags=re.M) + '\n}\n'
f'{call}(alternatives(), tracker: {tail.group(1)})')
else:
stmt = f'{lets}{call}({array_literal(names, calls[0] in ONE_PER_LINE)}, tracker: {tail.group(1)})'
out += [src[i:line], re.sub(r'^(?=.)', indent, stmt, flags=re.M)]
i = close + tail.end()
return ''.join(out) + src[i:]
def optional_spaces(src):
return re.sub(r'(\n(\s*))try\? (\w+)\.parseSpaces\(\)',
r'\1// Spaces are optional; incomplete still propagates.\1do { try \3.parseSpaces() } catch .mismatch {}',
src)
def in_closure(src, pos):
"""Whether the innermost closure or function body around `pos` is a closure."""
stack, k = [], 0
while k < pos:
c = src[k]
if src.startswith('//', k):
k = src.index('\n', k)
continue
if c == '"':
k += 1
while src[k] != '"':
k += 2 if src[k] == '\\' else 1
elif c == '{':
stack.append(k)
elif c == '}':
stack.pop()
k += 1
for o in reversed(stack):
if re.match(rf'[^{{}}]*?{re.escape(TYPED)}[^{{}}]*?\s+in\b', src[o + 1:]):
return True
if re.search(rf'\bfunc\b[^{{}}]*$', src[:o]):
return False
return False
def annotate_do(src):
# `do` inside a closure doesn't infer typed throws, so a `catch .mismatch` there needs `do throws(…)`.
out, i = [], 0
for m in re.finditer(r'\n(\s*)do \{', src):
catch = re.match(r'\s*catch (\S)', src[closing(src, m.end()):])
if not (catch and catch.group(1) == '.' and in_closure(src, m.start())):
continue
func = re.findall(r'\n func (\w+)\(', src[:m.start()])[-1] # the enclosing member
why = f"\n{m.group(1)}// `do` inside a closure doesn't infer typed throws; annotate it." if func in EXPLAIN_DO else ''
out += [src[i:m.start()], why, f'\n{m.group(1)}do {TYPED} {{']
i = m.end()
return ''.join(out) + src[i:]
def wrap_declarations(src):
# A declaration too long for one line puts each parameter on its own line.
def wrap(m):
indent, head, params, tail = m.groups()
if len(m.group(0)) <= LINE_LENGTH or re.search(r'func (\w+)', head).group(1) in BRACE_BELOW:
return m.group(0)
parts, depth, start = [], 0, 0
for k, c in enumerate(params):
depth += c in '([<'
depth -= c in ')]>'
if c == ',' and depth == 0:
parts.append(params[start:k].strip())
start = k + 1
parts.append(params[start:].strip())
return f'{indent}{head}(\n' + ''.join(f'{indent} {p},\n' for p in parts)[:-2] + f'\n{indent}) {tail} {{'
return re.sub(rf'^( *)((?:\w+ )*func \w+)\((.*)\) ({re.escape(TYPED)}.*) \{{$', wrap, src, flags=re.M)
def convert_test(src):
# Tests calling typed internals: `errorThrown` -> `parseErrorThrown`, which unwraps the ParseFailure.
# `errorThrown { … } != nil` doesn't look at the type and stays.
src = re.sub(r'\berrorThrown\b(?![^\n]*\} != nil)', 'parseErrorThrown', src)
# Only closures passed to the cursor's combinators need typed throws; fixtures take untyped ones.
src = throw_sites(src)
return re.sub(r'(\.composite\(tracker: [^\n]*?\) )(\{[^{}\n]* in)(?= \}$|$)',
lambda m: m.group(1) + closures(m.group(2)), src, flags=re.M)
for path in sys.argv[1:]:
src = open(path).read()
if '/Tests/' in '/' + path:
open(path, 'w').write(convert_test(src))
continue
for file, old, new in HAND:
if path.endswith('/' + file):
assert src.count(old) == 1, (path, old)
src = src.replace(old, new)
src = code_lines(src, lambda l: l if 'rethrows' in l else re.sub(r'\bthrows\b(?!\()', TYPED, l))
src = re.sub(r'((?<!/)//(?!/).*?)`?IncompleteMessage`?', r'\1`.incomplete`', src)
src = rewrite_array_one_of(src, path)
src = throw_sites(src)
src = catch_clauses(src)
src = closures(src)
src = optional_spaces(src)
src = annotate_do(src)
src = wrap_declarations(src)
open(path, 'w').write(src)
749 lines
36 KiB
Swift
749 lines
36 KiB
Swift
//===----------------------------------------------------------------------===//
|
|
//
|
|
// This source file is part of the SwiftNIO open source project
|
|
//
|
|
// Copyright (c) 2020 Apple Inc. and the SwiftNIO project authors
|
|
// Licensed under Apache License v2.0
|
|
//
|
|
// See LICENSE.txt for license information
|
|
// See CONTRIBUTORS.txt for the list of SwiftNIO project authors
|
|
//
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#if canImport(Darwin)
|
|
import Darwin
|
|
#elseif canImport(Glibc)
|
|
import Glibc
|
|
#elseif canImport(Musl)
|
|
import Musl
|
|
#elseif os(Windows)
|
|
import ucrt
|
|
#elseif canImport(Android)
|
|
import Android
|
|
#endif
|
|
|
|
// Compiler constraints not obvious from the code (the others are at their sites: `RegionEntry`,
|
|
// `_span(_:)`, `ParseInput.head`):
|
|
//
|
|
// - The lifetime checker is a SIL pass. `swiftc -typecheck` accepts code a real build rejects, so
|
|
// verify lifetime changes with a full build.
|
|
// - Spell it `@_lifetime`. `@lifetime` without the underscore only warns. Annotations name
|
|
// parameters, never properties.
|
|
//
|
|
// Unsafe seams. In the byte, encoder and parser code — `NIOIMAPCore`'s `Bytes/`, `Parser/` and
|
|
// `EncodeBuffer*.swift`, and `NIOIMAP`'s `Bytes/` — memory safety and `Sendable` are compiler-checked
|
|
// except at these sixteen. Not counted: reading through a pointer, or binding it to `UInt8`, inside the
|
|
// `withUnsafe…` closure that produced it. Outside that scope, `Base64/Chromium.swift` and
|
|
// `MailboxName.swift` use pointers too; both predate this list.
|
|
//
|
|
// 1. Read: `_span(_:)`, below, and the region table it reads, a `withUnsafeTemporaryAllocation` the
|
|
// gather fills (ParserInputBuffer.swift). A `Span` may be rebuilt from a table entry. Entries are written
|
|
// only below the table's capacity and read only below the count filled.
|
|
// 2. Read: `StableRegionParserInputBuffer.withStableRegions(into:)` via `RegionSink`
|
|
// (ParserInputBuffer.swift). The conformer's region pointers outlive their accessor and its owners
|
|
// keep them alive. `RegionSink` retains each owner until the gather ends (`Unmanaged`) and its init
|
|
// is `@_lifetime(immortal)`; it is `~Copyable`, and only the gather creates one. `IMAPInputBuffer`
|
|
// retains only regions with `IMAPByteStorage.hasStableAddress` and copies the rest.
|
|
// 3. Write: `OutputSegment: @unchecked Sendable` (OutputSegment.swift). Handed-out ranges and the bytes
|
|
// still being written are disjoint; a segment never reallocates. An `IMAPInputBuffer` tail is written
|
|
// only while that buffer is its sole owner, and its `Regions` view reads only up to `used`.
|
|
// 4. Write: `IMAPOutputBuffer.OpenTail` (IMAPOutputBuffer.swift). Writes go through its cached raw
|
|
// pointer into the open segment, not through the segment. `Storage.tail` holds that segment for as
|
|
// long as `open` describes it.
|
|
// 5. Write: `@exclusivity(unchecked)` on `IMAPOutputBuffer.Storage`'s properties (IMAPOutputBuffer.swift).
|
|
// No runtime exclusivity check. `Storage` is private, `IMAPOutputBuffer` is not `Sendable`, and every
|
|
// write first checks that the box is uniquely referenced.
|
|
// 6. Write: `IMAPBuffer.makeDispatchData()` donating a pointer to `bytesNoCopy`
|
|
// (IMAPBufferList+DispatchData.swift). Contained by `IMAPByteStorage.hasStableAddress`: other
|
|
// regions copy.
|
|
// 7. Write: `IMAPBufferList.makeDispatchData()` copying a run of regions into a `malloc` it hands to
|
|
// `bytesNoCopy` (IMAPBufferList+DispatchData.swift). Exact-size, filled before the
|
|
// hand-off, and not touched after it.
|
|
// 8. Write: `EncodeBuffer.writeDecimal(_:minimumDigits:)` formats into a `withUnsafeTemporaryAllocation`
|
|
// (EncodeBuffer.swift). `maximumDecimalLength` holds every digit of a number up to
|
|
// `maximumDecimalBitWidth` bits plus a sign; padding stops at the start; only the written suffix is read.
|
|
// 9. Write: `EncodeBuffer.writeQuotedIMAPString(_:)` escapes into a `withUnsafeTemporaryAllocation`
|
|
// (EncodeBuffer+WriteLiteral.swift). Its `2 * count + 2` bytes fit every byte escaped, plus both quotes.
|
|
// 10. Write: `EncodeBuffer.appendBytes(_:)` copies into the `staging` `InlineArray` with `copyMemory`
|
|
// (EncodeBuffer.swift). It copies only when `stagedCount + count` fits `stagingCapacity`, flushing
|
|
// first if needed; `flushStaging()` reads only the `stagedCount` bytes written.
|
|
// 11. Public: `IMAPBuffer.withUnsafeBytesAndOwner(_:)` (IMAPBuffer.swift) lets its pointer escape the
|
|
// closure. It hands out only bytes with a fixed address, and they stay valid while the caller holds
|
|
// `owner`; correct use outside the closure is the caller's job.
|
|
// 12. Public: `StableAddressByteRegion` (ByteRegion.swift) is a promise nothing checks; seams 2, 6 and 11
|
|
// rest on it, through `IMAPByteStorage.hasStableAddress`. The library conforms the stdlib byte arrays
|
|
// and `DispatchDataRegion`, not `DispatchData.Region`, which does not keep its bytes alive; `NIOIMAP`
|
|
// conforms `ByteBuffer`.
|
|
// 13. Sendable: `DispatchDataRegion: @unchecked Sendable` (ParserInputBuffer+Foundation.swift), what
|
|
// `IMAPInputBuffer.append(contentsOf:)` keeps. Its pointer is into the `DispatchData` it holds, which
|
|
// is immutable and keeps its regions alive.
|
|
// 14. Sendable: `GrammarParser: @unchecked Sendable` (GrammarParser.swift), older than this list. Its
|
|
// `parsedStringCache` closure is not `@Sendable`.
|
|
// 15. Sendable: `ThrowSite: @unchecked Sendable` (ParserErrors.swift). It points at a `#fileID` literal,
|
|
// which is immutable and lives as long as the process.
|
|
// 16. Sendable: `LiteralHint: @unchecked Sendable` (ParserErrors.swift). It points at `StaticString`
|
|
// literals, accepted only with a pointer representation.
|
|
|
|
/// One contiguous run of input bytes, plus whoever can keep those bytes alive.
|
|
///
|
|
/// The gather (a later phase) fills a flat table of these and hands it to a ``ParseInput``. A table of
|
|
/// `Span<UInt8>` is not expressible — no container may hold a non-escapable element — which is why the
|
|
/// bytes are a pointer here and become a `Span` only inside the cursor.
|
|
///
|
|
/// `owner` holds a +1 that ``RegionSink/append(_:owner:offset:)`` took and the gather releases when it
|
|
/// ends, so a conformer that boxes inside `withStableRegions` cannot hand over a dead box. Reading an
|
|
/// `Unmanaged` emits no reference counting; one more retain happens per extraction, when the owner is
|
|
/// handed to an ``IMAPBuffer``.
|
|
///
|
|
/// `nil` is always a correct answer for `owner`: it means an extraction copies rather than pinning the
|
|
/// input, which is the right default for a caller whose region pointers this package cannot vouch for.
|
|
internal struct RegionEntry {
|
|
/// The bytes of this region. Valid for as long as the gather that produced the table is running.
|
|
internal var bytes: UnsafeBufferPointer<UInt8>
|
|
|
|
/// Who owns `bytes`, if anybody can vouch for them, so that an extraction can retain instead of copy.
|
|
internal var owner: Unmanaged<IMAPByteStorage>?
|
|
|
|
/// Where `bytes` begins inside the owner's storage.
|
|
///
|
|
/// - Important: This is *not* redundant, and leaving it out silently returns the wrong bytes. An
|
|
/// owner box spans a whole network read while a region may be a *slice* of that read — a staging
|
|
/// type slices its head region rather than re-boxing it after a partial consume — so the offset of
|
|
/// the region inside its owner is not generally zero.
|
|
internal var ownerOffset: Int
|
|
|
|
internal init(
|
|
bytes: UnsafeBufferPointer<UInt8>,
|
|
owner: Unmanaged<IMAPByteStorage>? = nil,
|
|
ownerOffset: Int = 0
|
|
) {
|
|
self.bytes = bytes
|
|
self.owner = owner
|
|
self.ownerOffset = ownerOffset
|
|
}
|
|
}
|
|
|
|
/// The flat region table a ``ParseInput`` reads from. One allocation per gather, never per byte.
|
|
internal typealias ChunkTable = UnsafeBufferPointer<RegionEntry>
|
|
|
|
/// Rebuilds a `Span` from a region table entry.
|
|
///
|
|
/// **This is the one unsafe seam in the parser cursor**, and it is deliberately ten lines long so that
|
|
/// it can be audited in ten seconds. What it asserts is that the bytes a table entry points at outlive
|
|
/// the cursor: the gather holds every region's accessor scope open — or, for a stable-region conformer,
|
|
/// vouches for the pointers explicitly — for as long as the cursor exists.
|
|
///
|
|
/// Two spellings that do not work, recorded so they are not retried:
|
|
///
|
|
/// - It must be a *free function*. `@_unsafeNonescapableResult` on a `mutating` method does not work.
|
|
/// - It must be `@_lifetime(immortal)`. `@_lifetime(borrow x)` fails the SIL lifetime checker here,
|
|
/// because the reconstructed span is stored into `self`.
|
|
@_unsafeNonescapableResult
|
|
@_lifetime(immortal)
|
|
@inline(__always)
|
|
internal func _span(_ bytes: UnsafeBufferPointer<UInt8>) -> Span<UInt8> {
|
|
Span<UInt8>(_unsafeElements: bytes)
|
|
}
|
|
|
|
/// A borrowing cursor over one or more runs of input bytes.
|
|
///
|
|
/// This is what the parser reads through, and what ``ParserInputBuffer/withParseInput(limit:_:)``
|
|
/// hands to its closure. It is a plain 56-byte `BitwiseCopyable` value, so saving and
|
|
/// restoring it for backtracking — which every `parseOneOf` and `parseOptional` does — is a `memcpy`
|
|
/// with no reference counting.
|
|
///
|
|
/// It is `~Escapable` because it caches the current region as a `Span`, and that is load-bearing rather
|
|
/// than incidental: it makes it structurally impossible for the parser to keep input bytes past the
|
|
/// call. Nobody can drop `~Escapable` in a later refactor without first deleting the cached span.
|
|
/// Bytes the parser wants to keep are copied out with `extractBytes(length:)`, or referred to through
|
|
/// an owner with `extractBuffer(upTo:)` — both `internal`, like the rest of the reading surface; see
|
|
/// below.
|
|
///
|
|
/// ## This is not the type to hand the parser
|
|
///
|
|
/// Callers hand the parser an ``IMAPInputBuffer``. ``CommandParser/parseCommandStream(buffer:)`` and
|
|
/// ``ResponseParser/parseResponseStream(buffer:)`` open their own cursor over it, through the gather in
|
|
/// ``ParserInputBuffer``, which is where every `ParseInput` is built.
|
|
///
|
|
/// ## The invariant
|
|
///
|
|
/// `head` is empty **if and only if** the whole input is consumed. The initialisers establish that and
|
|
/// `advance(by:)` restores it, so every primitive may read the head's length as "bytes available right
|
|
/// now" and no primitive needs to know that regions exist. Do not reintroduce a per-primitive
|
|
/// "normalize first" precondition: it is an invariant to remember at a dozen sites, guarding a failure
|
|
/// mode that is silent — a multi-region extraction that stops after its first region.
|
|
///
|
|
/// ## The limit
|
|
///
|
|
/// `limit` caps how many bytes may ever be consumed through this cursor. It replaces clamping the input
|
|
/// down to a slice before parsing, so bytes past it are invisible to every primitive rather than merely
|
|
/// discouraged.
|
|
internal struct ParseInput: ~Escapable {
|
|
/// Every region, in order. Empty for a single-span cursor.
|
|
private var table: ChunkTable
|
|
|
|
/// The current region, already advanced past what has been consumed of it.
|
|
///
|
|
/// Storing this is what forces `~Escapable`, and what makes the common case — a byte from the region
|
|
/// we are already in — a subscript rather than a table lookup.
|
|
private var head: Span<UInt8>
|
|
|
|
/// Which entry of `table` `head` came from.
|
|
///
|
|
/// `Int32` rather than `Int` so that ``terminatingNewline`` fits in the same word and the cursor stays
|
|
/// compact. A table of more than two billion regions is not a thing that happens;
|
|
/// the gather bounds it far below that.
|
|
private var regionIndex: Int32
|
|
|
|
/// The line ending of the most recent ``matchNewline()``, if nothing has been consumed after it.
|
|
///
|
|
/// That is, the line ending that ends what has been parsed so far. One in the middle, for example
|
|
/// a literal header inside an `ENVELOPE`, is cleared by ``advance(by:)``.
|
|
///
|
|
/// It lives on the cursor because the caller that needs it — a response parser deciding whether a
|
|
/// bare CR was a split CRLF — is several grammar frames above the primitive that knows the answer,
|
|
/// and because ``composite(tracker:_:)``'s whole-value restore then rewinds it for free along with
|
|
/// the read position.
|
|
internal private(set) var terminatingNewline: Newline?
|
|
|
|
/// How many bytes have been consumed since this cursor was built.
|
|
///
|
|
/// - Important: Relative to *this cursor*, not to the buffer it was gathered from. A staging type
|
|
/// that trims by this number must present its regions starting at its own read head; the
|
|
/// conformance requirement in the gather says so, and it is asserted there.
|
|
internal private(set) var consumed: Int
|
|
|
|
/// The most bytes this cursor may ever consume.
|
|
internal private(set) var limit: Int
|
|
|
|
/// Multi-region entry point. `owner` ties the cursor's lifetime to a borrow of whatever holds the
|
|
/// regions — the gather's own storage — so the table cannot outlive the bytes it describes.
|
|
@_lifetime(borrow owner)
|
|
internal init<Owner: ~Copyable & ~Escapable>(
|
|
table: ChunkTable,
|
|
owner: borrowing Owner,
|
|
limit: Int = .max
|
|
) {
|
|
self.table = table
|
|
self.head = table.isEmpty ? _span(UnsafeBufferPointer(start: nil, count: 0)) : _span(table[0].bytes)
|
|
self.regionIndex = 0
|
|
self.terminatingNewline = nil
|
|
self.consumed = 0
|
|
self.limit = limit
|
|
self.normalizeHead()
|
|
}
|
|
|
|
/// Single contiguous region. No table, no owner — so an extraction always copies, which is the right
|
|
/// answer when there is nobody to keep the bytes alive.
|
|
@_lifetime(copy bytes)
|
|
internal init(_ bytes: Span<UInt8>, limit: Int = .max) {
|
|
self.table = ChunkTable(start: nil, count: 0)
|
|
self.head = bytes
|
|
self.regionIndex = 0
|
|
self.terminatingNewline = nil
|
|
self.consumed = 0
|
|
self.limit = limit
|
|
}
|
|
}
|
|
|
|
// MARK: - The head invariant
|
|
|
|
// `@_lifetime(self: copy self)` on mutating methods with parameters, here, in `ParseInput+Combinators.swift`
|
|
// and in `ParserInputBuffer.swift`, is what Swift 6.3+ infers. Swift 6.2 infers it only for methods without
|
|
// parameters. Remove once 6.4 is the minimum: https://github.com/apple/swift-nio-imap/issues/875
|
|
|
|
extension ParseInput {
|
|
/// Moves to the next region. Returns `false` when there is none.
|
|
private mutating func loadNext() -> Bool {
|
|
let next = Int(regionIndex) + 1
|
|
guard next < table.count else { return false }
|
|
regionIndex = Int32(next)
|
|
head = _span(table[next].bytes)
|
|
return true
|
|
}
|
|
|
|
/// Restores the invariant: `head` is empty only when the input is exhausted.
|
|
///
|
|
/// Loops rather than loading one region, because a gather is allowed to hand over empty regions — the
|
|
/// recursive one skips them, the one-pass one is not obliged to.
|
|
@discardableResult
|
|
private mutating func normalizeHead() -> Bool {
|
|
while head.isEmpty {
|
|
guard loadNext() else { return false }
|
|
}
|
|
return true
|
|
}
|
|
|
|
/// Bytes available in the current region without crossing a region boundary or the limit.
|
|
///
|
|
/// This, not `head.count`, is what every primitive measures against: bytes past the limit are
|
|
/// invisible, and the cheapest way to hold that everywhere is to have one accessor say so.
|
|
private var headAvailable: Int {
|
|
Swift.min(head.count, limit - consumed)
|
|
}
|
|
|
|
/// Consumes `n` bytes, or everything visible if `n` exceeds that, and restores the invariant.
|
|
///
|
|
/// Note the loop shape: the take is computed, applied, and *only then* is the head refilled, so
|
|
/// landing exactly on a region boundary refills before returning. Advancing an exhausted cursor, or
|
|
/// advancing by zero, is a no-op that still leaves the invariant true.
|
|
@_lifetime(self: copy self)
|
|
internal mutating func advance(by n: Int) {
|
|
precondition(n >= 0, "cannot advance by \(n)")
|
|
#if DEBUG
|
|
// Checked against what was visible *before* the call, so this also catches skipped bytes, which
|
|
// nothing in release can. `remaining` walks the table, hence debug only.
|
|
let consumedBefore = consumed
|
|
let visibleBefore = remaining
|
|
defer {
|
|
assert(consumed - consumedBefore == Swift.min(n, visibleBefore), "advance consumed the wrong count")
|
|
assert(!head.isEmpty || remaining == 0, "cursor invariant broken: head empty, bytes remain")
|
|
}
|
|
#endif
|
|
var wanted = n
|
|
while true {
|
|
let take = Swift.min(wanted, headAvailable)
|
|
head = head.extracting(droppingFirst: take)
|
|
consumed += take
|
|
if take > 0 {
|
|
terminatingNewline = nil
|
|
}
|
|
wanted -= take
|
|
guard normalizeHead() else { return } // exhausted; `wanted` may still be positive
|
|
if wanted == 0 || headAvailable == 0 { return }
|
|
}
|
|
}
|
|
|
|
/// Advances by `n`, which must be positive, and reports whether anything was consumed.
|
|
///
|
|
/// For loops whose termination rests on `advance` moving: they stop instead of spinning should it
|
|
/// ever not, and debug builds trap at the bug.
|
|
@_lifetime(self: copy self)
|
|
private mutating func advanceProgressing(by n: Int) -> Bool {
|
|
let consumedBefore = consumed
|
|
advance(by: n)
|
|
guard consumed > consumedBefore else {
|
|
assertionFailure("advance(by: \(n)) consumed nothing")
|
|
return false
|
|
}
|
|
return true
|
|
}
|
|
|
|
/// Throws ``ParseFailure/noForwardProgress(_:)`` unless something was consumed since `mark`, a value
|
|
/// of ``consumed``.
|
|
///
|
|
/// For hand-written loops in the grammar: an iteration that consumed nothing would repeat forever.
|
|
internal func requireProgress(since mark: Int) throws(ParseFailure) {
|
|
guard consumed > mark else { throw .noForwardProgress(NoForwardProgress()) }
|
|
}
|
|
|
|
/// Narrows the limit to the next `count` bytes.
|
|
///
|
|
/// This is how a caller that has to *look* at bytes before it knows how many of them may be parsed
|
|
/// clamps the cursor afterwards — the command parser frames synchronizing literals over everything
|
|
/// that has arrived, and only then knows how much of it is valid to parse. The alternative is
|
|
/// gathering the same regions twice, once per pass.
|
|
///
|
|
/// - Parameter count: How many more bytes may be consumed. The limit only ever narrows, so passing
|
|
/// more than is already allowed leaves it alone.
|
|
@_lifetime(self: copy self)
|
|
internal mutating func narrowLimit(toNext count: Int) {
|
|
precondition(count >= 0, "cannot narrow the limit to \(count) bytes")
|
|
limit = Swift.min(limit, consumed + count)
|
|
}
|
|
}
|
|
|
|
// MARK: - Reading without consuming
|
|
|
|
extension ParseInput {
|
|
/// Whether any byte is available.
|
|
internal var isEmpty: Bool {
|
|
headAvailable == 0
|
|
}
|
|
|
|
/// How many bytes are visible, across every remaining region and inside the limit.
|
|
///
|
|
/// O(regions remaining). Prefer ``isEmpty`` or a `peek` when that is the actual question.
|
|
internal var remaining: Int {
|
|
var total = head.count
|
|
var index = Int(regionIndex) + 1
|
|
while index < table.count {
|
|
total += table[index].bytes.count
|
|
index += 1
|
|
}
|
|
return Swift.min(total, limit - consumed)
|
|
}
|
|
|
|
/// The byte `k` bytes ahead of the read position, or `nil` if it is past the end or the limit.
|
|
///
|
|
/// Because the cursor is a `BitwiseCopyable` value, a cross-region look-ahead walks a *local copy*
|
|
/// rather than needing indexed access to the table.
|
|
internal func peek(at k: Int = 0) -> UInt8? {
|
|
precondition(k >= 0, "cannot peek at \(k)")
|
|
guard k < limit - consumed else { return nil }
|
|
if k < head.count { return head[k] } // fast path: no copy
|
|
var walker = copy self
|
|
var offset = k
|
|
while true {
|
|
offset -= walker.head.count
|
|
guard walker.loadNext() else { return nil }
|
|
if offset < walker.head.count { return walker.head[offset] }
|
|
}
|
|
}
|
|
|
|
/// The next two bytes, or `nil` if fewer than two are visible.
|
|
internal func peek2() -> (UInt8, UInt8)? {
|
|
if 2 <= headAvailable { return (head[0], head[1]) } // fast path: one region, no copy
|
|
guard let first = peek(at: 0), let second = peek(at: 1) else { return nil }
|
|
return (first, second)
|
|
}
|
|
|
|
/// Scoped access to the current contiguous run — for `memchr` and other bulk operations.
|
|
///
|
|
/// Scoped rather than a property: a `Span` cannot be returned out of a borrow of the cursor, and a
|
|
/// chunk *iterator* is not expressible at all, which is why cross-region walks live in here rather
|
|
/// than in the grammar.
|
|
///
|
|
/// By the invariant the span handed to `body` is empty only when the input is exhausted. It is
|
|
/// clamped to the limit, so a bulk operation cannot read past it.
|
|
internal func withCurrentChunk<R>(_ body: (Span<UInt8>) throws -> R) rethrows -> R {
|
|
try body(head.extracting(first: headAvailable))
|
|
}
|
|
}
|
|
|
|
// MARK: - Bulk scans
|
|
|
|
extension ParseInput {
|
|
/// The distance to the first occurrence of `byte`, or `nil` if it is not among the visible bytes.
|
|
///
|
|
/// Non-consuming. `memchr` per region: a bounds-checked byte-at-a-time loop over the same bytes
|
|
/// is much slower, so this and ``scanWhile(_:)`` are the only two places that scan, and
|
|
/// everything that needs to find something goes through one of them.
|
|
internal func scanUntil(byte: UInt8) -> Int? {
|
|
var scanned = 0
|
|
var walker = copy self
|
|
while true {
|
|
let available = walker.headAvailable
|
|
guard available > 0 else { return nil }
|
|
let hit = walker.withCurrentChunk { chunk in
|
|
chunk.withUnsafeBufferPointer { buffer -> Int? in
|
|
guard let base = buffer.baseAddress,
|
|
let found = memchr(base, Int32(byte), buffer.count)
|
|
else { return nil }
|
|
return UnsafeRawPointer(found) - UnsafeRawPointer(base)
|
|
}
|
|
}
|
|
if let hit { return scanned + hit }
|
|
scanned += available
|
|
guard walker.advanceProgressing(by: available) else { return nil }
|
|
}
|
|
}
|
|
|
|
/// How many leading bytes satisfy `predicate`, or `nil` if *every* visible byte does.
|
|
///
|
|
/// Non-consuming. `nil` means "the run might continue in bytes that have not arrived yet", which is
|
|
/// what both character-class parsers turn into ``ParseFailure/incomplete``. Callers therefore never have to
|
|
/// compare a count against the input length to work out which case they are in.
|
|
///
|
|
/// The pointer is hoisted once per region: this is the primitive behind every atom and every number,
|
|
/// so a naive loop here would be the parser's slowest path rather than a detail.
|
|
internal func scanWhile(_ predicate: (UInt8) -> Bool) -> Int? {
|
|
var scanned = 0
|
|
var walker = copy self
|
|
while true {
|
|
let available = walker.headAvailable
|
|
guard available > 0 else { return nil }
|
|
let stop = walker.withCurrentChunk { chunk in
|
|
chunk.withUnsafeBufferPointer { buffer -> Int? in
|
|
for index in 0..<buffer.count {
|
|
if !predicate(buffer[index]) { return index }
|
|
}
|
|
return nil
|
|
}
|
|
}
|
|
if let stop { return scanned + stop }
|
|
scanned += available
|
|
guard walker.advanceProgressing(by: available) else { return nil }
|
|
}
|
|
}
|
|
}
|
|
|
|
// MARK: - Consuming primitives
|
|
|
|
extension ParseInput {
|
|
/// Consumes and returns one byte.
|
|
internal mutating func readByte() throws(ParseFailure) -> UInt8 {
|
|
guard let byte = peek(at: 0) else { throw .incomplete }
|
|
advance(by: 1)
|
|
return byte
|
|
}
|
|
|
|
/// Consumes and copies the next `length` bytes.
|
|
///
|
|
/// Copying is the right answer for everything the parsed model keeps as a small field: those are
|
|
/// bounded by ``IMAPDefaults/lineLengthLimit`` and almost always far smaller, so owning them
|
|
/// outright beats pinning the whole input. Raw message payloads go through
|
|
/// ``extractBuffer(upTo:)`` instead.
|
|
@_lifetime(self: copy self)
|
|
internal mutating func extractBytes(length: Int) throws(ParseFailure) -> [UInt8] {
|
|
precondition(length >= 0, "cannot extract \(length) bytes")
|
|
guard length > 0 else { return [] }
|
|
// Only walk the table when the answer isn't already in the current region.
|
|
guard headAvailable >= length || remaining >= length else { throw .incomplete }
|
|
var bytes = [UInt8]()
|
|
bytes.reserveCapacity(length)
|
|
var wanted = length
|
|
while wanted > 0 {
|
|
let take = Swift.min(wanted, headAvailable)
|
|
// `remaining >= length` above promises the bytes, and by the invariant a cursor that is not
|
|
// exhausted has some in `head`. Were the invariant ever broken this would spin, not end.
|
|
guard take > 0 else {
|
|
assertionFailure("cursor invariant broken: bytes remain but none are in the head")
|
|
throw .noForwardProgress(NoForwardProgress())
|
|
}
|
|
withCurrentChunk { chunk in
|
|
chunk.extracting(first: take).withUnsafeBufferPointer { bytes.append(contentsOf: $0) }
|
|
}
|
|
advance(by: take)
|
|
wanted -= take
|
|
}
|
|
return bytes
|
|
}
|
|
|
|
/// Consumes `length` bytes without copying them.
|
|
///
|
|
/// Throws ``ParseFailure/incomplete`` if fewer are visible, and consumes nothing then.
|
|
@_lifetime(self: copy self)
|
|
internal mutating func skipBytes(length: Int) throws(ParseFailure) {
|
|
precondition(length >= 0, "cannot skip \(length) bytes")
|
|
guard headAvailable >= length || remaining >= length else { throw .incomplete }
|
|
let before = consumed
|
|
advance(by: length)
|
|
// `remaining >= length` above promises the bytes; falling short means the cursor invariant broke.
|
|
guard consumed - before == length else {
|
|
assertionFailure("cursor invariant broken: skipped \(consumed - before) of \(length) bytes")
|
|
throw .noForwardProgress(NoForwardProgress())
|
|
}
|
|
}
|
|
|
|
/// Consumes raw message bytes — the streaming-literal path.
|
|
///
|
|
/// Returns what is available, never more than `maxLength`, and never crossing a region boundary: a
|
|
/// short return costs the three streaming drivers one extra trip through their state machine and
|
|
/// nothing else, and it is what keeps an ``IMAPBuffer`` to one owner and one range.
|
|
///
|
|
/// A run is handed out as a reference to the input's storage when the current region has an owner
|
|
/// and ``IMAPBuffer/retainsLiteral(byteCount:pinnedByteCount:)`` — at least 512 bytes, and at least a
|
|
/// third of what the owner keeps alive — so a multi-megabyte literal reaches the caller without
|
|
/// being copied, and a small one does not pin a large receive buffer. That decision lives here and
|
|
/// nowhere else, so no call site has to know which representation it got.
|
|
///
|
|
/// - Important: The emptiness check is on the *input*, not on how many bytes are about to be taken.
|
|
/// `maxLength == 0` with bytes available returns an empty result rather than throwing, which is
|
|
/// what `APPEND … {0+}` needs: the command parser has no zero-remaining special case and relies on
|
|
/// getting an empty payload back so that it can move on to the end of the literal.
|
|
@_lifetime(self: copy self)
|
|
internal mutating func extractBuffer(upTo maxLength: Int) throws(ParseFailure) -> IMAPBuffer {
|
|
precondition(maxLength >= 0, "cannot extract up to \(maxLength) bytes")
|
|
let available = headAvailable
|
|
guard available > 0 else { throw .incomplete }
|
|
let take = Swift.min(maxLength, available)
|
|
if take >= IMAPBuffer.minimumRetainedLiteralByteCount, Int(regionIndex) < table.count {
|
|
let entry = table[Int(regionIndex)]
|
|
if let owner = entry.owner,
|
|
IMAPBuffer.retainsLiteral(byteCount: take, pinnedByteCount: owner.takeUnretainedValue().pinnedByteCount)
|
|
{
|
|
// The offset within the OWNER, not within the region: `ownerOffset` is where the region
|
|
// starts inside the owner's storage, and `bytes.count - head.count` is how far into the
|
|
// region we already are. Dropping either term returns the wrong bytes.
|
|
let offset = entry.ownerOffset + (entry.bytes.count - head.count)
|
|
advance(by: take)
|
|
return IMAPBuffer(owner.takeUnretainedValue(), range: offset..<(offset + take))
|
|
}
|
|
}
|
|
return IMAPBuffer(try extractBytes(length: take))
|
|
}
|
|
}
|
|
|
|
// MARK: - Matching
|
|
|
|
extension ParseInput {
|
|
/// How ``matchFixed(_:caseSensitivity:)`` compares letters.
|
|
enum CaseSensitivity {
|
|
/// `SELECT` matches only `SELECT`.
|
|
case caseSensitive
|
|
/// `SELECT` also matches `select` and `Select`: what IMAP keywords need.
|
|
case caseInsensitive
|
|
}
|
|
|
|
/// Consumes `needle` if it is next, and consumes nothing otherwise.
|
|
///
|
|
/// - Parameters:
|
|
/// - needle: The bytes to match. Must be ASCII.
|
|
/// - caseSensitivity: How to compare letters. ASCII case folding is a mask, so the insensitive
|
|
/// compare costs nothing extra.
|
|
/// - Throws: ``ParseFailure/incomplete`` when everything that has arrived matches a prefix of `needle`,
|
|
/// so more bytes could still complete it; ``ParseFailure/mismatch(_:)`` when a byte disagrees, which no
|
|
/// continuation can fix.
|
|
@_lifetime(self: copy self)
|
|
internal mutating func matchFixed(
|
|
_ needle: String,
|
|
caseSensitivity: CaseSensitivity = .caseInsensitive
|
|
) throws(ParseFailure) {
|
|
assert(needle.utf8.allSatisfy { $0 & 0b1000_0000 == 0 }, "needle needs to be ASCII but \(needle) isn't")
|
|
let caseSensitive = caseSensitivity == .caseSensitive
|
|
let mask: UInt8 = caseSensitive ? 0xFF : 0xDF
|
|
let count = needle.utf8.count
|
|
// Fast path: the whole needle is in the current region, so compare through two hoisted pointers.
|
|
// This runs on every failed alternative of every `parseOneOf`, which is most of the parser's work.
|
|
if count <= headAvailable {
|
|
let matches = withCurrentChunk { chunk in
|
|
chunk.withUnsafeBufferPointer { buffer in
|
|
Self.bytesMatch(buffer, needle: needle, mask: mask)
|
|
}
|
|
}
|
|
guard matches else {
|
|
throw .mismatch(caseSensitive ? "Fixed string mismatch" : "Fixed string mismatch (ci)")
|
|
}
|
|
advance(by: count)
|
|
return
|
|
}
|
|
var offset = 0
|
|
for expected in needle.utf8 {
|
|
guard let byte = peek(at: offset) else { throw .incomplete }
|
|
guard byte & mask == expected & mask else {
|
|
throw .mismatch(caseSensitive ? "Fixed string mismatch" : "Fixed string mismatch (ci)")
|
|
}
|
|
offset += 1
|
|
}
|
|
advance(by: offset)
|
|
}
|
|
|
|
/// Whether `buffer` starts with `needle`, comparing under `mask`.
|
|
///
|
|
/// `static` so that the closure below captures the needle rather than the cursor: handing a
|
|
/// `self`-capturing closure to something that also borrows `self` is what makes the compiler take a
|
|
/// copy, and on the encode side that cost an allocation per call before anyone noticed.
|
|
///
|
|
/// `utf8Span` is what the package's macOS 26 floor buys here: the needle's bytes come out as a
|
|
/// `Span` with no optional to unwrap and no fallback path for a string that turns out not to be
|
|
/// contiguous, which is what `withContiguousStorageIfAvailable` needs and what makes the same
|
|
/// comparison in the encoder longer than this one.
|
|
private static func bytesMatch(_ buffer: UnsafeBufferPointer<UInt8>, needle: String, mask: UInt8) -> Bool {
|
|
let expected = needle.utf8Span.span
|
|
for index in 0..<expected.count where buffer[index] & mask != expected[index] & mask {
|
|
return false
|
|
}
|
|
return true
|
|
}
|
|
|
|
/// Consumes a line ending, and records which kind it was in ``terminatingNewline``.
|
|
///
|
|
/// Some servers send a stray space before the line ending, so spaces are skipped first, up to
|
|
/// ``maximumStraySpaces``. Spaces within the cap with nothing after them report
|
|
/// ``ParseFailure/incomplete``. Nothing is consumed unless a line ending is found, so a caller does not
|
|
/// need to wrap this in ``composite(tracker:_:)`` to keep its position on a failure.
|
|
@discardableResult
|
|
internal mutating func matchNewline() throws(ParseFailure) -> Newline {
|
|
let (newline, length) = try newlineAhead()
|
|
advance(by: length)
|
|
terminatingNewline = newline
|
|
return newline
|
|
}
|
|
|
|
/// Most stray spaces tolerated before a line ending.
|
|
///
|
|
/// RFC 3501 allows none. Lenient towards peers that pad lines, but bounded. Spaces elsewhere are
|
|
/// bounded only by the buffer limit.
|
|
internal static let maximumStraySpaces = 40
|
|
|
|
/// The line ending at the read position, and how many bytes it takes up including skipped spaces.
|
|
private func newlineAhead() throws(ParseFailure) -> (Newline, Int) {
|
|
// One linear pass over the spaces: `peek(at:)` walks from the head each call, so peeking at a
|
|
// growing offset would be quadratic in the spaces times the regions.
|
|
// The scan stops one past the cap, so a hostile run is not read in full. A space at `offset`
|
|
// after that means the cap was exceeded, and falls through to the error below.
|
|
var scanned = 0
|
|
let spaces = scanWhile { byte in
|
|
scanned += 1
|
|
return byte == UInt8(ascii: " ") && scanned <= Self.maximumStraySpaces
|
|
}
|
|
// Only spaces so far, within the cap: more spaces, or the line ending, may still arrive.
|
|
guard let offset = spaces else { throw .incomplete }
|
|
|
|
guard let first = peek(at: offset) else { throw .incomplete }
|
|
guard let second = peek(at: offset + 1) else {
|
|
// One byte, and no more may ever arrive as far as this cursor is concerned.
|
|
switch first {
|
|
case UInt8(ascii: "\n"): return (.lf, offset + 1)
|
|
case UInt8(ascii: "\r"): return (.cr, offset + 1)
|
|
default: throw .mismatch("Expected newline")
|
|
}
|
|
}
|
|
switch (first, second) {
|
|
case (UInt8(ascii: "\r"), UInt8(ascii: "\n")): return (.crlf, offset + 2)
|
|
case (UInt8(ascii: "\n"), _): return (.lf, offset + 1)
|
|
case (UInt8(ascii: "\r"), _): return (.cr, offset + 1)
|
|
default: throw .mismatch("Expected newline")
|
|
}
|
|
}
|
|
|
|
/// Consumes a leading `LF`, if there is one. Reports whether it did.
|
|
///
|
|
/// This is how a CRLF split across two reads is stitched back together: the CR ended one parse and
|
|
/// the LF is skipped at the start of the next.
|
|
@discardableResult
|
|
internal mutating func skipLF() -> Bool {
|
|
guard peek(at: 0) == UInt8(ascii: "\n") else { return false }
|
|
advance(by: 1)
|
|
return true
|
|
}
|
|
}
|
|
|
|
extension ParseInput {
|
|
/// A line ending kind.
|
|
internal enum Newline: Sendable, Hashable {
|
|
case crlf
|
|
case cr
|
|
case lf
|
|
}
|
|
}
|
|
|
|
/// `ParseInput` is deliberately **not** `Sendable`, and could not honestly be.
|
|
///
|
|
/// It holds pointers into somebody else's bytes that are valid only for the gather that produced it, and
|
|
/// being non-escapable is what enforces that. What crosses a thread is what the parser *extracted* — an
|
|
/// `[UInt8]` or an ``IMAPBuffer``, both of which own or retain their bytes.
|
|
@available(*, unavailable)
|
|
extension ParseInput: Sendable {}
|
|
|
|
// MARK: - Backtracking
|
|
|
|
extension ParseInput {
|
|
/// Runs `body`, and restores the read position if it throws.
|
|
///
|
|
/// This is the parser's backtracking primitive, and it is why the cursor is a small
|
|
/// `BitwiseCopyable` value: the save is a `memcpy` and the restore is another, with no reference
|
|
/// counting, and it runs on every alternative of every `parseOneOf`.
|
|
///
|
|
/// `let save = copy self` completes before `body(&self, …)` is called, so there is no overlapping
|
|
/// access to `self` even though both mention it.
|
|
@_lifetime(self: copy self)
|
|
internal mutating func composite<T>(
|
|
tracker: StackTracker,
|
|
_ body: SubParser<T>
|
|
) throws(ParseFailure) -> T {
|
|
var tracker = tracker
|
|
try tracker.newStackFrame()
|
|
let save = copy self
|
|
do {
|
|
return try body(&self, tracker)
|
|
} catch {
|
|
self = save
|
|
throw error
|
|
}
|
|
}
|
|
}
|