3j: the carry and the combinations read back #42

Merged
lilleman merged 4 commits from carry-read-back into main 2026-09-03 08:10:21 +02:00
11 changed files with 74 additions and 59 deletions
Showing only changes of commit 4d0ad3728c - Show all commits
+3 -2
View File
@@ -136,8 +136,9 @@ literal-text fallback — a typo that reparses as prose is the silent loss §2 r
A node no section spells where it stands — an unknown type, or a known one whose spelling belongs A node no section spells where it stands — an unknown type, or a known one whose spelling belongs
to the other position — rides as its raw JSON and restores to a deep-equal node. A carry may hold to the other position — rides as its raw JSON and restores to a deep-equal node. A carry may hold
a node the emitter spells natively: it restores verbatim, and the next emit spells it canonically a node the emitter spells natively: it restores unreinterpreted, and the next emit spells it
(AGENTS.md §2). Block and inline positions canonicalize differently, each fitting where it sits: canonically (AGENTS.md §2). Block and inline positions canonicalize differently, each fitting
where it sits:
- **Block position**: a fenced code block with info string `adf`, body = the node's JSON — - **Block position**: a fenced code block with info string `adf`, body = the node's JSON —
two-space indent, object keys sorted. two-space indent, object keys sorted.
+2 -2
View File
@@ -2,13 +2,13 @@ import { largestNesting } from './nesting.ts'
export type JsonValue = JsonValue[] | boolean | null | number | string | { [key: string]: JsonValue } export type JsonValue = JsonValue[] | boolean | null | number | string | { [key: string]: JsonValue }
export function isJsonValue(value: unknown): value is JsonValue { export function isJsonValue(value: unknown, levels: number = largestNesting): value is JsonValue {
const pending: { depth: number; item: unknown }[] = [{ depth: 0, item: value }] const pending: { depth: number; item: unknown }[] = [{ depth: 0, item: value }]
while (pending.length > 0) { while (pending.length > 0) {
const entry = pending.pop() const entry = pending.pop()
if (entry === undefined) continue if (entry === undefined) continue
const { depth, item } = entry const { depth, item } = entry
if (depth > largestNesting) return false if (depth > levels) return false
if (item === null || typeof item === 'boolean' || typeof item === 'string') continue if (item === null || typeof item === 'boolean' || typeof item === 'string') continue
if (typeof item === 'number') { if (typeof item === 'number') {
if (!Number.isFinite(item)) return false if (!Number.isFinite(item)) return false
+13
View File
@@ -77,6 +77,15 @@ export function readInlineDirective(text: string, index: number): Read<Directive
return readNestedDirective(text, index, 1) return readNestedDirective(text, index, 1)
} }
export function readSoleStringAttribute(span: DirectiveSpan, key: string): Read<string> {
if (span.content !== undefined) return { fault: unsupportedNodeShape(`${span.name} takes no content`) }
const spelled = span.attributes.get(key)
if (spelled === undefined || span.attributes.size !== 1) return { fault: unsupportedNodeShape(`${span.name} holds one ${key} attribute alone`) }
const spelling = spellStringAttribute(spelled.decoded)
if (spelling !== spelled.spelling) return { fault: unsupportedNodeShape(`${span.name} spells its ${key} attribute as ${key}=${spelling}`) }
return { value: spelled.decoded }
}
// Both directions answer alike: an inline directive never spans lines, so no content slot holds a line ending. // Both directions answer alike: an inline directive never spans lines, so no content slot holds a line ending.
export function slotLineEndingFault(type: string, text: string): ConvertFault | undefined { export function slotLineEndingFault(type: string, text: string): ConvertFault | undefined {
if (!/[\n\r]/.test(text)) return undefined if (!/[\n\r]/.test(text)) return undefined
@@ -116,6 +125,10 @@ export function unknownDirectiveFault(name: string): ConvertFault {
return { code: 'unknown-directive-name', message: `the directive name ${name} reads back to no node` } return { code: 'unknown-directive-name', message: `the directive name ${name} reads back to no node` }
} }
export function unsupportedNodeShape(message: string): ConvertFault {
return { code: 'unsupported-node-shape', message }
}
function keyOrder(left: string, right: string): number { function keyOrder(left: string, right: string): number {
if (left < right) return -1 if (left < right) return -1
return left > right ? 1 : 0 return left > right ? 1 : 0
+7 -1
View File
@@ -167,11 +167,17 @@ test('breaks a mark run at the node it carries', () => {
) )
}) })
test('refuses a carried node nested deeper than the emitter carries', () => { test('refuses a carried node nested deeper than the levels its position leaves', () => {
let node: AdfNode = { type: 'blockCard' } let node: AdfNode = { type: 'blockCard' }
for (let depth = 0; depth < 600; depth += 1) node = { content: [node], type: 'blockCard' } for (let depth = 0; depth < 600; depth += 1) node = { content: [node], type: 'blockCard' }
assert.equal(code(adfToMarkdown(document(node))), 'unsupported-nesting-depth') assert.equal(code(adfToMarkdown(document(node))), 'unsupported-nesting-depth')
assert.equal(code(adfToMarkdown(document(paragraph(node)))), 'unsupported-nesting-depth') assert.equal(code(adfToMarkdown(document(paragraph(node)))), 'unsupported-nesting-depth')
let shallow: AdfNode = { type: 'blockCard' }
for (let depth = 0; depth < 200; depth += 1) shallow = { content: [shallow], type: 'blockCard' }
assert.ok(adfToMarkdown(document(shallow)).ok)
let quoted: AdfNode = shallow
for (let depth = 0; depth < 150; depth += 1) quoted = { content: [quoted], type: 'blockquote' }
assert.equal(code(adfToMarkdown(document(quoted))), 'unsupported-nesting-depth')
}) })
test('refuses a node whose content model the canonical form cannot emit', () => { test('refuses a node whose content model the canonical form cannot emit', () => {
+5 -5
View File
@@ -73,7 +73,7 @@ function interruptsParagraph(node: AdfNode): boolean {
function emitBlock(node: AdfNode, path: ConvertErrorPath, depth: number): Result<EmittedBlock> { function emitBlock(node: AdfNode, path: ConvertErrorPath, depth: number): Result<EmittedBlock> {
const directive = blockDirective(node.type) const directive = blockDirective(node.type)
if (directive === undefined) return commonMarkLine(carriedBlock(node, path)) if (directive === undefined) return commonMarkLine(carriedBlock(node, path, depth))
const readable = readableBlock(node, path, depth) const readable = readableBlock(node, path, depth)
if (readable !== undefined) return readable if (readable !== undefined) return readable
return emitDirectiveBlock(node, directive, path, depth) return emitDirectiveBlock(node, directive, path, depth)
@@ -114,9 +114,9 @@ function emitDirectiveBlock(node: AdfNode, directive: BlockDirective, path: Conv
if (node.text !== undefined) return failure('unsupported-node-shape', `a ${node.type} carries no text`, path) if (node.text !== undefined) return failure('unsupported-node-shape', `a ${node.type} carries no text`, path)
const content = node.content ?? [] const content = node.content ?? []
if (directive.contentModel === 'none' && content.length > 0) return failure('unsupported-node-shape', `a ${node.type} holds no content`, path) if (directive.contentModel === 'none' && content.length > 0) return failure('unsupported-node-shape', `a ${node.type} holds no content`, path)
if (directive.contentModel === 'code') return emitCodeDirective(node, directive, path) if (directive.contentModel === 'code') return emitCodeDirective(node, directive, path, depth)
const header = spellDirectiveHeader(node, directive) const header = spellDirectiveHeader(node, directive)
if (header === undefined) return commonMarkLine(carriedBlock(node, path)) if (header === undefined) return commonMarkLine(carriedBlock(node, path, depth))
if (directive.contentModel === 'none' || (directive.contentModel === 'inline' && content.length === 0)) { if (directive.contentModel === 'none' || (directive.contentModel === 'inline' && content.length === 0)) {
return success({ fenceColons: 2, spelling: 'directive', text: `::${header}` }) return success({ fenceColons: 2, spelling: 'directive', text: `::${header}` })
} }
@@ -154,10 +154,10 @@ function emitCodeBlock(node: AdfNode, path: ConvertErrorPath): Result<EmittedBlo
return success(commonMarkText(fencedCodeBlock(slot.kind === 'fence' ? slot.info : '', text.value))) return success(commonMarkText(fencedCodeBlock(slot.kind === 'fence' ? slot.info : '', text.value)))
} }
function emitCodeDirective(node: AdfNode, directive: BlockDirective, path: ConvertErrorPath): Result<EmittedBlock> { function emitCodeDirective(node: AdfNode, directive: BlockDirective, path: ConvertErrorPath, depth: number): Result<EmittedBlock> {
const slot = languageSlot(node.attrs?.['language']) const slot = languageSlot(node.attrs?.['language'])
const header = spellDirectiveHeader(node, directive, slot.kind === 'attribute' ? [] : ['language']) const header = spellDirectiveHeader(node, directive, slot.kind === 'attribute' ? [] : ['language'])
if (header === undefined) return commonMarkLine(carriedBlock(node, path)) if (header === undefined) return commonMarkLine(carriedBlock(node, path, depth))
const text = codeBlockText(node, path) const text = codeBlockText(node, path)
if (!text.ok) return text if (!text.ok) return text
const info = slot.kind === 'fence' ? slot.info : '' const info = slot.kind === 'fence' ? slot.info : ''
+19 -25
View File
@@ -1,5 +1,4 @@
import type { AdfNode } from '../adf/document.ts' import type { AdfNode } from '../adf/document.ts'
import type { ConvertFault } from '../result.ts'
import type { DirectiveSpan, Read } from './directive-syntax.ts' import type { DirectiveSpan, Read } from './directive-syntax.ts'
import type { JsonSpelling } from '../canonical-json.ts' import type { JsonSpelling } from '../canonical-json.ts'
import { failure, success, type ConvertErrorPath, type Result } from '../result.ts' import { failure, success, type ConvertErrorPath, type Result } from '../result.ts'
@@ -7,58 +6,57 @@ import { isAdfNode } from '../adf/document.ts'
import { isJsonValue } from '../json-value.ts' import { isJsonValue } from '../json-value.ts'
import { fencedCodeBlock } from './backtick-runs.ts' import { fencedCodeBlock } from './backtick-runs.ts'
import { largestNesting } from '../nesting.ts' import { largestNesting } from '../nesting.ts'
import { malformedDirective, spellAttributes, spellStringAttribute } from './directive-syntax.ts' import { malformedDirective, readSoleStringAttribute, spellAttributes, spellStringAttribute, unsupportedNodeShape } from './directive-syntax.ts'
import { serializeCanonicalJson } from '../canonical-json.ts' import { serializeCanonicalJson } from '../canonical-json.ts'
export const carryName = 'adf' export const carryName = 'adf'
const jsonAttribute = 'json' const jsonAttribute = 'json'
export function carriedBlock(node: AdfNode, path: ConvertErrorPath): Result<string> { export function carriedBlock(node: AdfNode, path: ConvertErrorPath, depth: number): Result<string> {
const json = carriedJson(node, 'two-space', path) const json = carriedJson(node, 'two-space', path, largestNesting - depth)
if (!json.ok) return json if (!json.ok) return json
return success(fencedCodeBlock(carryName, json.value)) return success(fencedCodeBlock(carryName, json.value))
} }
export function carriedInline(node: AdfNode, path: ConvertErrorPath): Result<string> { export function carriedInline(node: AdfNode, path: ConvertErrorPath): Result<string> {
const json = carriedJson(node, 'compact', path) const json = carriedJson(node, 'compact', path, largestNesting)
if (!json.ok) return json if (!json.ok) return json
return success(`:${carryName}${spellAttributes([[jsonAttribute, spellStringAttribute(json.value)]])}`) return success(`:${carryName}${spellAttributes([[jsonAttribute, spellStringAttribute(json.value)]])}`)
} }
export function readCarriedBlock(body: string): Read<AdfNode> { export function readCarriedBlock(body: string, depth: number): Read<AdfNode> {
return readCarriedJson(body, 'two-space') return readCarriedJson(body, 'two-space', largestNesting - depth)
} }
export function readCarriedInline(span: DirectiveSpan): Read<AdfNode> | undefined { export function readCarriedInline(span: DirectiveSpan): Read<AdfNode> | undefined {
if (span.name !== carryName) return undefined if (span.name !== carryName) return undefined
if (span.content !== undefined) return { fault: unsupported(`${carryName} takes no content`) } const spelled = readSoleStringAttribute(span, jsonAttribute)
const spelled = span.attributes.get(jsonAttribute) if (spelled.fault !== undefined) return spelled
if (spelled === undefined || span.attributes.size !== 1) return { fault: unsupported(`${carryName} holds one ${jsonAttribute} attribute alone`) } return readCarriedJson(spelled.value, 'compact', largestNesting)
const spelling = spellStringAttribute(spelled.decoded)
if (spelling !== spelled.spelling) return { fault: unsupported(`${carryName} spells its ${jsonAttribute} attribute as ${jsonAttribute}=${spelling}`) }
return readCarriedJson(spelled.decoded, 'compact')
} }
function carriedJson(node: AdfNode, spelling: JsonSpelling, path: ConvertErrorPath): Result<string> { function carriedJson(node: AdfNode, spelling: JsonSpelling, path: ConvertErrorPath, levels: number): Result<string> {
if (!isJsonValue(node)) { if (!isJsonValue(node, levels)) {
return failure('unsupported-nesting-depth', `a carried node's JSON nests deeper than the ${largestNesting} levels the emitter carries`, path) return failure('unsupported-nesting-depth', `a carried node's JSON nests the document deeper than the ${largestNesting} levels the emitter carries`, path)
} }
return success(serializeCanonicalJson(node, spelling)) return success(serializeCanonicalJson(node, spelling))
} }
function readCarriedJson(raw: string, spelling: JsonSpelling): Read<AdfNode> { function readCarriedJson(raw: string, spelling: JsonSpelling, levels: number): Read<AdfNode> {
const parsed = parseJsonText(raw) const parsed = parseJsonText(raw)
if (parsed === undefined) return { fault: malformedDirective('the opaque carry holds invalid JSON') } if (parsed === undefined) return { fault: malformedDirective('the opaque carry holds invalid JSON') }
const { value } = parsed const { value } = parsed
if (!isJsonValue(value)) { if (!isJsonValue(value, levels)) {
return { fault: { code: 'unsupported-nesting-depth', message: `a carried node's JSON nests deeper than the ${largestNesting} levels the parser carries` } } // Unbounded, the same walk parts the two causes one `false` holds (AGENTS.md §8).
if (!isJsonValue(value, Number.POSITIVE_INFINITY)) return { fault: unsupportedNodeShape('the opaque carry holds a number JSON cannot spell') }
return { fault: { code: 'unsupported-nesting-depth', message: `a carried node's JSON nests the input deeper than the ${largestNesting} levels the parser carries` } }
} }
if (serializeCanonicalJson(value, spelling) !== raw) { if (serializeCanonicalJson(value, spelling) !== raw) {
const shape = spelling === 'compact' ? 'compact, keys sorted' : 'two-space indent, keys sorted' const shape = spelling === 'compact' ? 'compact, keys sorted' : 'two-space indent, keys sorted'
return { fault: unsupported(`the opaque carry spells its node's JSON canonically: ${shape}`) } return { fault: unsupportedNodeShape(`the opaque carry spells its node's JSON canonically: ${shape}`) }
} }
if (!isAdfNode(value)) return { fault: unsupported("the opaque carry holds one ADF node's JSON") } if (!isAdfNode(value)) return { fault: unsupportedNodeShape("the opaque carry holds one ADF node's JSON") }
return { value } return { value }
} }
@@ -70,7 +68,3 @@ function parseJsonText(raw: string): { value: unknown } | undefined {
return undefined return undefined
} }
} }
function unsupported(message: string): ConvertFault {
return { code: 'unsupported-node-shape', message }
}
+2 -2
View File
@@ -173,7 +173,7 @@ function directivePiece(scan: Scan, span: DirectiveSpan): Result<Piece> {
const text = readTextDirective(span) const text = readTextDirective(span)
if (text?.fault !== undefined) return faulted(text.fault, scan.path) if (text?.fault !== undefined) return faulted(text.fault, scan.path)
if (text !== undefined) return success({ kind: 'nodes', nodes: [{ text: text.value, type: 'text' }] }) if (text !== undefined) return success({ kind: 'nodes', nodes: [{ text: text.value, type: 'text' }] })
const slot = slotNodes(scan, span.content) const slot = slotContent(scan, span.content)
if (!slot.ok) return slot if (!slot.ok) return slot
const mark = readDirectiveMark(span.name, span.attributes, scan.path) const mark = readDirectiveMark(span.name, span.attributes, scan.path)
if (mark !== undefined) { if (mark !== undefined) {
@@ -189,7 +189,7 @@ function directivePiece(scan: Scan, span: DirectiveSpan): Result<Piece> {
return success({ kind: 'nodes', nodes: [node.value] }) return success({ kind: 'nodes', nodes: [node.value] })
} }
function slotNodes(scan: Scan, content: string | undefined): Result<SlotContent | undefined> { function slotContent(scan: Scan, content: string | undefined): Result<SlotContent | undefined> {
if (content === undefined) return success(undefined) if (content === undefined) return success(undefined)
const parsed = parseInline(content, scan.definitions, scan.path, false) const parsed = parseInline(content, scan.definitions, scan.path, false)
if (!parsed.ok) return parsed if (!parsed.ok) return parsed
+13 -3
View File
@@ -312,9 +312,19 @@ test('names the shape the inline carry reads alone', () => {
assert.equal(content(markdownToAdf(':adf{json="null"}\n')), 'unsupported-node-shape: adf spells its json attribute as json=null') assert.equal(content(markdownToAdf(':adf{json="null"}\n')), 'unsupported-node-shape: adf spells its json attribute as json=null')
}) })
test('holds a carried JSON value to the nesting the parser carries', () => { test('holds a carried JSON value to the nesting its position leaves', () => {
const deep = `:adf{json="${'['.repeat(largestNesting + 2)}${']'.repeat(largestNesting + 2)}"}\n` const nested = (levels: number): string => `${'['.repeat(levels)}${']'.repeat(levels)}`
assert.equal(content(markdownToAdf(deep)), `unsupported-nesting-depth: a carried node's JSON nests deeper than the ${largestNesting} levels the parser carries`) const fence = (prefix: string, levels: number): string => `${prefix}\`\`\`adf\n${prefix}${nested(levels)}\n${prefix}\`\`\`\n`
const deeper = `unsupported-nesting-depth: a carried node's JSON nests the input deeper than the ${largestNesting} levels the parser carries`
assert.equal(content(markdownToAdf(`:adf{json="${nested(largestNesting + 2)}"}\n`)), deeper)
assert.equal(code(markdownToAdf(fence('', largestNesting + 1))), 'unsupported-node-shape')
assert.equal(content(markdownToAdf(fence('> ', largestNesting + 1))), deeper)
})
test('names the number no JSON spelling carries in an opaque carry', () => {
const named = 'unsupported-node-shape: the opaque carry holds a number JSON cannot spell'
assert.equal(content(markdownToAdf(':adf{json="{\\"attrs\\":{\\"width\\":1e999},\\"type\\":\\"blockCard\\"}"}\n')), named)
assert.equal(content(markdownToAdf('```adf\n1e999\n```\n')), named)
}) })
test('names the mark spelling no opaque carry sits inside', () => { test('names the mark spelling no opaque carry sits inside', () => {
+3 -3
View File
@@ -36,7 +36,7 @@ function blockNode(block: Block, definitions: LinkDefinitions, path: ConvertErro
case 'bulletList': case 'bulletList':
return listNode({ type: 'bulletList' }, block.items, definitions, path, depth) return listNode({ type: 'bulletList' }, block.items, definitions, path, depth)
case 'code': case 'code':
return codeBlockNode(block.language, block.text, path) return codeBlockNode(block.language, block.text, path, depth)
case 'directive': case 'directive':
return directiveNode(block, definitions, path, depth) return directiveNode(block, definitions, path, depth)
case 'fault': case 'fault':
@@ -134,9 +134,9 @@ function listNode(node: AdfNode, items: readonly Block[][], definitions: LinkDef
return success({ ...node, content }) return success({ ...node, content })
} }
function codeBlockNode(language: string, text: string, path: ConvertErrorPath): Result<AdfNode> { function codeBlockNode(language: string, text: string, path: ConvertErrorPath, depth: number): Result<AdfNode> {
if (language === carryName) { if (language === carryName) {
const carried = readCarriedBlock(text) const carried = readCarriedBlock(text, depth)
if (carried.fault !== undefined) return faulted(carried.fault, path) if (carried.fault !== undefined) return faulted(carried.fault, path)
return success(carried.value) return success(carried.value)
} }
+5 -13
View File
@@ -1,6 +1,5 @@
import type { ConvertFault } from '../result.ts'
import type { DirectiveSpan, Read } from './directive-syntax.ts' import type { DirectiveSpan, Read } from './directive-syntax.ts'
import { spellAttributes, spellLeafDirective, spellStringAttribute } from './directive-syntax.ts' import { readSoleStringAttribute, spellAttributes, spellLeafDirective, spellStringAttribute, unsupportedNodeShape } from './directive-syntax.ts'
const name = 'text' const name = 'text'
const whitespaceRun = /^(?:[ \t]+|\n+)$/ const whitespaceRun = /^(?:[ \t]+|\n+)$/
@@ -13,15 +12,8 @@ export function spellTextDirective(text: string): string {
export function readTextDirective(span: DirectiveSpan): Read<string> | undefined { export function readTextDirective(span: DirectiveSpan): Read<string> | undefined {
if (span.name !== name) return undefined if (span.name !== name) return undefined
if (span.content !== undefined) return { fault: unsupported(`${name} takes no content`) } const spelled = readSoleStringAttribute(span, name)
const spelled = span.attributes.get(name) if (spelled.fault !== undefined) return spelled
if (spelled === undefined || span.attributes.size !== 1) return { fault: unsupported(`${name} holds one ${name} attribute alone`) } if (!whitespaceRun.test(spelled.value)) return { fault: unsupportedNodeShape(`${name} spells one run of spaces and tabs, or one run of newlines`) }
const spelling = spellStringAttribute(spelled.decoded) return spelled
if (spelling !== spelled.spelling) return { fault: unsupported(`${name} spells its ${name} attribute as ${name}=${spelling}`) }
if (!whitespaceRun.test(spelled.decoded)) return { fault: unsupported(`${name} spells one run of spaces and tabs, or one run of newlines`) }
return { value: spelled.decoded }
}
function unsupported(message: string): ConvertFault {
return { code: 'unsupported-node-shape', message }
} }
+2 -3
View File
@@ -29,9 +29,8 @@ The numbering is the order the work was planned in, not the order it ships.
- [x] **2e5 — Combined documents and the collision property.** - [x] **2e5 — Combined documents and the collision property.**
- [x] **2f — The attributes CommonMark cannot hold.** - [x] **2f — The attributes CommonMark cannot hold.**
- [ ] **3 — `markdownToAdf` (`0.1.0`).** Each sub-item lands the fixtures its own code reads, and - [ ] **3 — `markdownToAdf` (`0.1.0`).** Each sub-item lands the fixtures its own code reads, and
the runner grows a parse half as they do: `parsingDirectories` beside `emittingDirectories`, a the runner grows a parse half as they do: readers for `corpus/normalization/` (setext,
round-trip directory joining it only once every fixture in it reads back to its document, indented code, loose lists, `*`/`+`
and readers for `corpus/normalization/` (setext, indented code, loose lists, `*`/`+`
bullets, entity references, soft wraps — one-way, the markdown not canonical) and bullets, entity references, soft wraps — one-way, the markdown not canonical) and
`corpus/errors/` (a markdown input per named error, the code in a `.error` beside it) with `corpus/errors/` (a markdown input per named error, the code in a `.error` beside it) with
the first fixture each. `commonmark-subset/` cannot be the first to green — `::paragraph` the first fixture each. `commonmark-subset/` cannot be the first to green — `::paragraph`