3
0
mirror of https://github.com/larvit/adf-codec.git synced 2026-11-17 21:55:44 +01:00

Answer architecture round 1: 2 🔴, 5 🟡; readableBlock alone fills and reads the spelling memo, the escaper's steps hand on what they claim rather than write a shared buffer, engine-limit names only the throws an engine returns, and the docs agree on it

This commit is contained in:
2026-10-13 17:32:36 +02:00
parent ecd695a6d1
commit 025ccc0cf8
10 changed files with 110 additions and 83 deletions
+12 -17
View File
@@ -108,39 +108,34 @@ function interruptsParagraph(node: AdfNode): boolean {
function emitBlock(node: AdfNode, path: ConvertErrorPath, depth: number, writing: Writing): Result<EmittedBlock> {
const model = blockNodeModel(node.type)
if (model === undefined) return carriedFence(carriedBlock(node, path, depth))
const readable = readableBlock(node, path, depth, writing)
const readable = readableBlock(node, path, depth, writing, false)
if (readable !== undefined) return readable
return emitDirectiveBlock(node, model, path, depth, () => walkBlocks(nodeContent(node), path, depth + 1, writing))
}
export function commonMarkSpelling(node: AdfNode, path: ConvertErrorPath, depth: number, writing: Writing & { memo: SpellingMemo }): Result<null> | undefined {
const readable = readableBlock(node, path, depth, writing)
if (readable === undefined) {
writing.memo.set(node, { block: undefined, depth })
return undefined
}
const readable = readableBlock(node, path, depth, writing, true)
if (readable === undefined) return undefined
if (!readable.ok) return readable
const fits = withinLongest(readable.value.text)
if (!fits.ok) return fits
writing.memo.set(node, { block: readable.value, depth })
return readable.value.spelling === 'directive' ? undefined : success(null)
}
function readableBlock(node: AdfNode, path: ConvertErrorPath, depth: number, writing: Writing): Result<EmittedBlock> | undefined {
const kept = takeKeptSpelling(writing.memo, node)
// Only an asked node's spelling is kept, and reading it drops it: the walk above spells the node into its own text, so no level of a subtree holds that text twice.
function readableBlock(node: AdfNode, path: ConvertErrorPath, depth: number, writing: Writing, asked: boolean): Result<EmittedBlock> | undefined {
const { memo } = writing
const kept = memo?.get(node)
memo?.delete(node)
if (kept !== undefined) {
if (kept.block === undefined) return undefined
// An entry holds the spelling at its fill depth. A read at or above it rebases headroom; a read below re-spells, because a hit skips the walk's depth guards.
if (depth <= kept.depth) return success({ ...kept.block, headroom: kept.block.headroom + kept.depth - depth })
}
return spellReadableBlock(node, path, depth, writing)
}
// docs/decisions.md §The spelling memo
function takeKeptSpelling(memo: SpellingMemo | undefined, node: AdfNode): KeptSpelling | undefined {
const kept = memo?.get(node)
memo?.delete(node)
return kept
const spelled = spellReadableBlock(node, path, depth, writing)
if (asked && spelled === undefined) memo?.set(node, { block: undefined, depth })
if (asked && spelled?.ok === true) memo?.set(node, { block: spelled.value, depth })
return spelled
}
function spellReadableBlock(node: AdfNode, path: ConvertErrorPath, depth: number, writing: Writing): Result<EmittedBlock> | undefined {
+36 -36
View File
@@ -11,7 +11,7 @@ import { opensLinkDefinition } from '../commonmark/link-reference-definitions.ts
export type DelimiterRole = 'close' | 'open'
// A segment's kind: `backslash` text, `bracketed` text inside `[…]` (link text, alt, a directive's content), `bracketed-link-target` syntax: a link target inside `[…]`, `none` syntax written as is.
export type InlineEscaping = 'backslash' | 'bracketed' | 'bracketed-link-target' | 'none'
export type InlineEscaping = (typeof escapingCodes)[number]
export type NodeRange = { first: number; last: number }
@@ -32,9 +32,14 @@ type EmittedDelimiter = { closes: boolean; offset: number; pair: number; width:
type EmittedRun = { canClose: boolean; canOpen: boolean; character: string; delimiters: EmittedDelimiter[]; length: number; start: number }
// One byte a character, never an array element or Set entry: a line may run to hundreds of millions of characters.
type Escapings = (index: number) => InlineEscaping | undefined
// Where a segment's first character landed in the output.
type PlacedSegment = { segment: InlineSegment; start: number }
// The run's escapable characters, or its characters before `end`.
type RunEscapes = { end: number; escapable: false } | { escapable: true }
type EscapeWalk = {
readonly claims: Claims
readonly container: LineContainer
@@ -44,7 +49,7 @@ type EscapeWalk = {
readonly scan: string
}
const escapingCodes: readonly InlineEscaping[] = ['backslash', 'bracketed', 'bracketed-link-target', 'none']
const escapingCodes: readonly ['backslash', 'bracketed', 'bracketed-link-target', 'none'] = ['backslash', 'bracketed', 'bracketed-link-target', 'none']
const followsLinkText = /[([]/
@@ -85,6 +90,7 @@ function resolveEmphasis(segments: readonly InlineSegment[]): InlineSegment[] {
function escape(segments: readonly InlineSegment[], container: LineContainer, claims: Claims): AssembledLine {
const scan = segments.map((segment) => segment.text).join('')
// One byte a character, never an array element or Set entry: a line may run to hundreds of millions of characters.
const codes = new Uint8Array(scan.length)
let start = 0
for (const segment of segments) {
@@ -96,16 +102,15 @@ function escape(segments: readonly InlineSegment[], container: LineContainer, cl
return code === undefined ? undefined : escapingCodes[code]
}
const escaped = escapeClosedRuns(scan, escapings, escapeClaims(scan, escapings, container, claims))
const { output, placements } = backslashed(segments, scan, escaped)
const { output, placed } = backslashed(segments, scan, escaped)
if (container === 'paragraph' && opensLinkDefinition(output)) {
if (segments[0]?.nodes !== undefined) return { line: output, openingLinkAsDirective: true, unspellableRuns: [] }
return { line: `\\${output}`, unspellableRuns: unspellableRuns(segments, output, placements) }
return { line: `\\${output}`, unspellableRuns: unspellableRuns(placed, output) }
}
return { line: output, unspellableRuns: unspellableRuns(segments, output, placements) }
return { line: output, unspellableRuns: unspellableRuns(placed, output) }
}
// The output, and where each segment's first character landed in it.
function backslashed(segments: readonly InlineSegment[], scan: string, escaped: Uint8Array): { output: string; placements: number[] } {
function backslashed(segments: readonly InlineSegment[], scan: string, escaped: Uint8Array): { output: string; placed: PlacedSegment[] } {
// Joined a few thousand pieces at a time: an array element per escape would cost more than the character it escapes.
const chunks: string[] = []
let pieces: string[] = []
@@ -119,7 +124,7 @@ function backslashed(segments: readonly InlineSegment[], scan: string, escaped:
}
pieces.push(scan.slice(from))
chunks.push(pieces.join(''))
const placements: number[] = []
const placed: PlacedSegment[] = []
let escapes = 0
let next = escaped.indexOf(1)
let start = 0
@@ -128,10 +133,10 @@ function backslashed(segments: readonly InlineSegment[], scan: string, escaped:
escapes += 1
next = escaped.indexOf(1, next + 1)
}
placements.push(start + escapes)
placed.push({ segment, start: start + escapes })
start += segment.text.length
}
return { output: chunks.join(''), placements }
return { output: chunks.join(''), placed }
}
function escapeClaims(scan: string, escapings: Escapings, container: LineContainer, claims: Claims): Uint8Array {
@@ -150,7 +155,9 @@ function escapeClaims(scan: string, escapings: Escapings, container: LineContain
while (index > line.start + line.text.length) line = scanLine(scan, line.start + line.text.length + 1)
if (runCharacters.includes(scan.charAt(index))) {
const end = index + runLength(scan, index)
runEscapes(walk, index, end, line, escaped)
const run = runEscapes(walk, index, end, line)
if (!run.escapable) escaped.fill(1, index, run.end)
else for (let at = index; at < end; at += 1) if (isEscapable(escapings(at))) escaped[at] = 1
index = end
continue
}
@@ -164,18 +171,13 @@ function escapeClaims(scan: string, escapings: Escapings, container: LineContain
}
// A text character beside one of the emitter's delimiters would lengthen that delimiter's run, so every text character in such a run escapes.
function runEscapes(walk: EscapeWalk, start: number, end: number, line: ScanLine, escaped: Uint8Array): void {
function runEscapes(walk: EscapeWalk, start: number, end: number, line: ScanLine): RunEscapes {
const { escapings, scan } = walk
if (runHolds(escapings, start, end, isSyntax) && runHolds(escapings, start, end, isEscapable)) {
for (let index = start; index < end; index += 1) if (isEscapable(escapings(index))) escaped[index] = 1
return
}
for (let index = start; index < end; index += 1) {
// A backtick run escapes whole: a bare rest would be a shorter backtick string, which can close a span an earlier run opens.
const claimed = (index > start && scan.charAt(index) === '`') || claimsRunRest(walk, index, end, line)
if (!claimed) break
escaped[index] = 1
}
if (runHolds(escapings, start, end, isSyntax) && runHolds(escapings, start, end, isEscapable)) return { escapable: true }
let index = start
// A backtick run escapes whole: a bare rest would be a shorter backtick string, which can close a span an earlier run opens.
while (index < end && ((index > start && scan.charAt(index) === '`') || claimsRunRest(walk, index, end, line))) index += 1
return { end: index, escapable: false }
}
function runHolds(escapings: Escapings, start: number, end: number, kind: (escaping: InlineEscaping | undefined) => boolean): boolean {
@@ -225,8 +227,8 @@ function claimsCharacter({ container, escapings, headingCloser, linkClose, scan
}
// CommonMark reads no escape inside a code span, so a backtick string left beside an escape still closes a span that an earlier bare run of its length opens.
// The walk runs backwards and escapes only the run it just read, so every index it reads still holds the claims alone.
function escapeClosedRuns(scan: string, escapings: Escapings, escaped: Uint8Array): Uint8Array {
function escapeClosedRuns(scan: string, escapings: Escapings, claimed: Uint8Array): Uint8Array {
const escaped = claimed.slice()
// The lengths of the backtick strings escapes leave: a bare run of one of these lengths would open a span one of them closes, so it is escaped too.
const formed = new Set<number>()
let end = scan.length - 1
@@ -239,11 +241,11 @@ function escapeClosedRuns(scan: string, escapings: Escapings, escaped: Uint8Arra
while (scan.charAt(start - 1) === '`') start -= 1
let segmentEnd = end
for (let index = end; index > start; index -= 1) {
if (escaped[index] !== 1) continue
if (claimed[index] !== 1) continue
formed.add(segmentEnd - index + 1)
segmentEnd = index - 1
}
if (segmentEnd !== end || escaped[start] === 1) formed.add(segmentEnd - start + 1)
if (segmentEnd !== end || claimed[start] === 1) formed.add(segmentEnd - start + 1)
else if (escapings(start) !== 'none' && formed.has(end - start + 1)) {
escaped.fill(1, start, end + 1)
// Each escaped backtick then stands alone, a string of length one.
@@ -255,18 +257,17 @@ function escapeClosedRuns(scan: string, escapings: Escapings, escaped: Uint8Arra
}
// One emphasis run, the innermost, or every highlight run the line cannot spell.
function unspellableRuns(segments: readonly InlineSegment[], output: string, placements: readonly number[]): MarkRun[] {
const { nodes, runs } = emittedRuns(segments, placements, output)
function unspellableRuns(placed: readonly PlacedSegment[], output: string): MarkRun[] {
const { nodes, runs } = emittedRuns(placed, output)
const pair = misflanked(runs) ?? unpaired(runs)
const run = pair === undefined ? undefined : nodes[pair]
return run === undefined ? unreadHighlights(segments, output, placements) : [run]
return run === undefined ? unreadHighlights(placed, output) : [run]
}
// No `==` in text can open or close, and highlights never nest, so a pair reads back where each delimiter flanks.
function unreadHighlights(segments: readonly InlineSegment[], output: string, placements: readonly number[]): MarkRun[] {
function unreadHighlights(placed: readonly PlacedSegment[], output: string): MarkRun[] {
const unread: MarkRun[] = []
for (const [index, segment] of segments.entries()) {
const start = placements[index] ?? 0
for (const { segment, start } of placed) {
if (segment.highlight === undefined) continue
const flanking = highlightFlanking(output, start)
const flanks = segment.highlight === 'open' ? flanking.opens : flanking.closes
@@ -305,12 +306,11 @@ function delimiterAt(run: EmittedRun, closes: boolean, offset: number, width: nu
return run.delimiters.find((delimiter) => delimiter.closes === closes && delimiter.offset === offset && delimiter.width === width)
}
function emittedRuns(segments: readonly InlineSegment[], placements: readonly number[], output: string): { nodes: MarkRun[]; runs: EmittedRun[] } {
function emittedRuns(placed: readonly PlacedSegment[], output: string): { nodes: MarkRun[]; runs: EmittedRun[] } {
const runs: EmittedRun[] = []
const nodes: MarkRun[] = []
const open: number[] = []
for (const [index, segment] of segments.entries()) {
const start = placements[index] ?? 0
for (const { segment, start } of placed) {
if (segment.emphasis === undefined) continue
const closes = segment.emphasis === 'close'
const pair = closes ? (open.pop() ?? nodes.length) : nodes.length
+3
View File
@@ -36,6 +36,9 @@ test('refuses a string past the bound as too long on every engine, and any other
}
assert.equal(code('Invalid array length'), 'engine-limit')
assert.equal(code('Array buffer allocation failed'), 'engine-limit')
assert.equal(code('Map maximum size exceeded'), 'engine-limit')
assert.equal(code('Set maximum size exceeded'), 'engine-limit')
assert.equal(code('invalid array length'), 'engine-limit')
assert.deepEqual(
withinEngineLimits(() => success('x')),
{ ok: true, value: 'x' },
+9 -2
View File
@@ -3,9 +3,16 @@ import { failure, success, type Result } from '../result.ts'
// V8's longest string (docs/decisions.md §Markdown past V8's longest string is refused on every engine).
export const longestMarkdown = 2 ** 29 - 24
// Measured on Firefox 155, Node 24 and Bun 1.4. SpiderMonkey's overflow and V8's string overflow both mean a string past longestMarkdown; the others name an engine's own limit or its memory running short.
// Measured on Firefox 155, Node 24 and Bun 1.4, and SpiderMonkey's array length from its js.msg. SpiderMonkey's overflow and V8's string overflow both mean a string past longestMarkdown; the others name an engine's own limit or its memory running short.
const pastLongestString = ['InternalError: allocation size overflow', 'RangeError: Invalid string length']
const pastEngineLimit = ['RangeError: Array buffer allocation failed', 'RangeError: Invalid array length', 'RangeError: Out of memory']
const pastEngineLimit = [
'RangeError: Array buffer allocation failed',
'RangeError: Invalid array length',
'RangeError: Map maximum size exceeded',
'RangeError: Out of memory',
'RangeError: Set maximum size exceeded',
'RangeError: invalid array length',
]
export function tooLong<T>(): Result<T> {
return failure('unsupported-node-shape', `the markdown for this document, or for a directive's plain CommonMark, would be longer than ${longestMarkdown} characters, the longest this library writes`, [])