mirror of
https://github.com/larvit/adf-codec.git
synced 2026-11-20 15:15:44 +01:00
Answer architecture round 1: 0 🔴, 11 🟡 (0 misread); one line policy, one reduction run, named sweep stages, spelling memo, block text, link options, HTML refusals
This commit is contained in:
@@ -34,8 +34,9 @@ export type PlacedBlock = SpelledBlock & { node: AdfNode }
|
||||
export type PlacedBlocks = { blocks: readonly PlacedBlock[]; headroom: number }
|
||||
// CommonMark starts a new list where the marker changes, so every second list in a run of one kind takes `alternateText`.
|
||||
type SpelledBlock = { spelling: 'commonmark' | 'directive'; text: BlockText } | { alternateText: BlockText; interruptsParagraph: boolean; ordered: boolean; spelling: 'list'; text: BlockText }
|
||||
// Keyed by reference: only a caller building one object per position (the parse, the portable reduction) passes one; a consumer's document may share a node.
|
||||
export type SpellingMemo = Map<AdfNode, KeptSpelling>
|
||||
type Spell = () => Result<EmittedBlock> | undefined
|
||||
// ask: spell the node, keeping the spelling unless refused; take: spell the node, reading and dropping what is kept.
|
||||
export type SpellingMemo = Readonly<Record<'ask' | 'take', (node: AdfNode, depth: number, spell: Spell) => Result<EmittedBlock> | undefined>>
|
||||
type WalkedItem = { node: AdfNode; walk: PlacedBlocks }
|
||||
export type WrittenAs = { flavour: 'lossless' } | { blocks: FlavourBlockWriters; flavour: 'portable' }
|
||||
export type Writing = WrittenAs & { memo: SpellingMemo | undefined }
|
||||
@@ -110,14 +111,14 @@ function blankLineBetween(previous: PlacedBlock, next: PlacedBlock, container: B
|
||||
export 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, 'walked')
|
||||
const readable = readableBlock(node, path, depth, writing)
|
||||
if (readable !== undefined) return readable
|
||||
return emitDirectiveBlock(node, model, path, depth, () => walkBlocks(nodeContent(node), path, depth + 1, writing))
|
||||
}
|
||||
|
||||
// Whether the node takes a CommonMark spelling, rather than its directive.
|
||||
export function commonMarkSpelling(node: AdfNode, path: ConvertErrorPath, depth: number, writing: Writing & { memo: SpellingMemo }): Result<boolean> {
|
||||
const readable = readableBlock(node, path, depth, writing, 'asked')
|
||||
const readable = writing.memo.ask(node, depth, () => spellReadableBlock(node, path, depth, writing))
|
||||
if (readable === undefined) return success(false)
|
||||
if (!readable.ok) return readable
|
||||
const fits = withinLongest(readable.value.text.length, readable.value)
|
||||
@@ -125,20 +126,30 @@ export function commonMarkSpelling(node: AdfNode, path: ConvertErrorPath, depth:
|
||||
return success(readable.value.spelling !== 'directive')
|
||||
}
|
||||
|
||||
// Only an `asked` call keeps a spelling, and any read drops it: the ancestor that reads it spells it into its own text, so no two levels hold that text.
|
||||
function readableBlock(node: AdfNode, path: ConvertErrorPath, depth: number, writing: Writing, caller: 'asked' | 'walked'): Result<EmittedBlock> | undefined {
|
||||
const { memo } = writing
|
||||
const kept = memo?.get(node)
|
||||
memo?.delete(node)
|
||||
if (kept !== undefined) {
|
||||
if (kept.block === undefined) return undefined
|
||||
function readableBlock(node: AdfNode, path: ConvertErrorPath, depth: number, writing: Writing): Result<EmittedBlock> | undefined {
|
||||
const spell = () => spellReadableBlock(node, path, depth, writing)
|
||||
return writing.memo === undefined ? spell() : writing.memo.take(node, depth, spell)
|
||||
}
|
||||
|
||||
// Keyed by reference: only a caller building one object per position (the parse, the portable reduction) makes one; a consumer's document may share a node.
|
||||
export function spellingMemo(): SpellingMemo {
|
||||
const kept = new Map<AdfNode, KeptSpelling>()
|
||||
// Any read drops the entry: the ancestor that reads it spells it into its own text, so no two levels hold that text.
|
||||
const take: SpellingMemo['take'] = (node, depth, spell) => {
|
||||
const entry = kept.get(node)
|
||||
kept.delete(node)
|
||||
if (entry === undefined) return spell()
|
||||
if (entry.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 depth <= entry.depth ? success({ ...entry.block, headroom: entry.block.headroom + entry.depth - depth }) : spell()
|
||||
}
|
||||
const spelled = spellReadableBlock(node, path, depth, writing)
|
||||
if (caller === 'asked' && spelled === undefined) memo?.set(node, { block: undefined, depth })
|
||||
if (caller === 'asked' && spelled?.ok === true) memo?.set(node, { block: spelled.value, depth })
|
||||
return spelled
|
||||
const ask: SpellingMemo['ask'] = (node, depth, spell) => take(node, depth, () => {
|
||||
const spelled = spell()
|
||||
if (spelled === undefined) kept.set(node, { block: undefined, depth })
|
||||
else if (spelled.ok) kept.set(node, { block: spelled.value, depth })
|
||||
return spelled
|
||||
})
|
||||
return { ask, take }
|
||||
}
|
||||
|
||||
function spellReadableBlock(node: AdfNode, path: ConvertErrorPath, depth: number, writing: Writing): Result<EmittedBlock> | undefined {
|
||||
|
||||
@@ -5,7 +5,7 @@ export type BlockText = Readonly<Lines & { first: FirstLine; later: LaterLines;
|
||||
type Lines =
|
||||
| { body: BlockText; head: string; indent: string; kind: 'prefixed' }
|
||||
| { body: BlockText; kind: 'quoted' }
|
||||
| { kind: 'joined'; parts: readonly BlockText[] }
|
||||
| { kind: 'joined'; parts: readonly [BlockText, ...BlockText[]] }
|
||||
| { kind: 'text'; lines: readonly string[] }
|
||||
type Prefix = Lines & { kind: 'prefixed' | 'quoted' }
|
||||
type OpenPrefix = { innermostQuote: number; prefix: Prefix }
|
||||
@@ -17,37 +17,49 @@ type LaterLines = Readonly<{ blank: number; empty: number; written: number }>
|
||||
export const emptyLine = blockText('')
|
||||
|
||||
export function blockText(text: string): BlockText {
|
||||
const [first = '', ...rest] = text.split('\n')
|
||||
const empty = rest.filter((line) => line === '').length
|
||||
const later = { blank: rest.filter((line) => line !== '' && isBlankLine(line)).length, empty, written: rest.length - empty }
|
||||
return { first: { blank: first !== '' && isBlankLine(first), character: first.charAt(0), ruleCharacters: holdsOnlyRuleCharacters(first), text: first }, kind: 'text', later, length: text.length, lines: [first, ...rest] }
|
||||
return measured({ kind: 'text', lines: text.split('\n') })
|
||||
}
|
||||
|
||||
export function joinedText(parts: readonly BlockText[]): BlockText {
|
||||
const [first, ...others] = parts
|
||||
if (first === undefined) return emptyLine
|
||||
let { blank, empty, written } = first.later
|
||||
let { length } = first
|
||||
for (const part of others) {
|
||||
blank += part.later.blank + (part.first.blank ? 1 : 0)
|
||||
empty += part.later.empty + (part.first.text === '' ? 1 : 0)
|
||||
written += part.later.written + (part.first.text === '' ? 0 : 1)
|
||||
length += 1 + part.length
|
||||
}
|
||||
return { first: first.first, kind: 'joined', later: { blank, empty, written }, length, parts }
|
||||
return first === undefined ? emptyLine : measured({ kind: 'joined', parts: [first, ...others] })
|
||||
}
|
||||
|
||||
// head, never blank, leads the first line; indent, spaces alone, leads every later line holding text.
|
||||
export function prefixedText(head: string, indent: string, body: BlockText): BlockText {
|
||||
const length = body.length + head.length + indent.length * body.later.written
|
||||
return { body, first: headedLine(head, body.first), head, indent, kind: 'prefixed', later: body.later, length }
|
||||
return measured({ body, head, indent, kind: 'prefixed' })
|
||||
}
|
||||
|
||||
// Each line takes `> `, an empty one `>`.
|
||||
export function quotedText(body: BlockText): BlockText {
|
||||
return measured({ body, kind: 'quoted' })
|
||||
}
|
||||
|
||||
function measured(lines: Lines): BlockText {
|
||||
if (lines.kind === 'text') {
|
||||
const [first = '', ...rest] = lines.lines
|
||||
const empty = rest.filter((line) => line === '').length
|
||||
const later = { blank: rest.filter((line) => line !== '' && isBlankLine(line)).length, empty, written: rest.length - empty }
|
||||
const length = rest.reduce((sum, line) => sum + 1 + line.length, first.length)
|
||||
return { ...lines, first: { blank: first !== '' && isBlankLine(first), character: first.charAt(0), ruleCharacters: holdsOnlyRuleCharacters(first), text: first }, later, length }
|
||||
}
|
||||
if (lines.kind === 'joined') {
|
||||
const [first, ...others] = lines.parts
|
||||
let { blank, empty, written } = first.later
|
||||
let { length } = first
|
||||
for (const part of others) {
|
||||
blank += part.later.blank + (part.first.blank ? 1 : 0)
|
||||
empty += part.later.empty + (part.first.text === '' ? 1 : 0)
|
||||
written += part.later.written + (part.first.text === '' ? 0 : 1)
|
||||
length += 1 + part.length
|
||||
}
|
||||
return { ...lines, first: first.first, later: { blank, empty, written }, length }
|
||||
}
|
||||
const { body } = lines
|
||||
if (lines.kind === 'prefixed') return { ...lines, first: headedLine(lines.head, body.first), later: body.later, length: body.length + lines.head.length + lines.indent.length * body.later.written }
|
||||
const head = body.first.text === '' ? '>' : '> '
|
||||
const later = { blank: 0, empty: 0, written: body.later.empty + body.later.written }
|
||||
return { body, first: headedLine(head, body.first), kind: 'quoted', later, length: body.length + head.length + 2 * body.later.written + body.later.empty }
|
||||
return { ...lines, first: headedLine(head, body.first), later, length: body.length + head.length + 2 * body.later.written + body.later.empty }
|
||||
}
|
||||
|
||||
// A line holds only rule characters exactly when each of its parts does.
|
||||
|
||||
@@ -30,6 +30,15 @@ type Emission = { carries: [NodeRange, ...NodeRange[]]; segments?: undefined } |
|
||||
|
||||
type Fallback = [NodeRange, ...NodeRange[]] | 'opening-link'
|
||||
|
||||
// How a line is judged and what its fallbacks do: the lossless writer carries nodes, the portable reduction drops marks.
|
||||
export type FallbackAction<Line, LineFallback, Written> = {
|
||||
giveUpEmphasis: (line: Line) => Line
|
||||
judge: (line: Line, grouping: RunGrouping) => Result<LineJudgement<Line, LineFallback, Written>>
|
||||
// A line whose fallbacks rewrite its nodes judges its spanned line again on every pass.
|
||||
spansEachPass: boolean
|
||||
take: (line: Line, fallback: LineFallback) => Result<Line>
|
||||
}
|
||||
|
||||
type InlineContext = {
|
||||
atBlockEnd: boolean
|
||||
bracketed: boolean
|
||||
@@ -44,10 +53,13 @@ type InlineContext = {
|
||||
|
||||
type LineAttempt = { fallback: Fallback; line?: undefined; segments?: undefined } | { fallback?: undefined; line: string; segments: InlineSegment[] }
|
||||
|
||||
// What one grouping writes, or the fallback it needs and the line that fallback's indexes name.
|
||||
export type LineJudgement<Line, LineFallback, Written> = { fallback: LineFallback; kind: 'fallback'; line: Line } | { kind: 'written'; written: Written }
|
||||
|
||||
type LineFallbacks = { carried: Set<number>; flavour: WrittenFlavour; openingLinkAsDirective: boolean }
|
||||
|
||||
// docs/decisions.md §After 32 passes, a line carries or drops every em, strike and strong mark.
|
||||
export const readBackPasses = 32
|
||||
const readBackPasses = 32
|
||||
|
||||
export type PortableLineFallback = { kind: 'opening-link' } | { kind: 'unspellable-run'; runs: [MarkRun, ...MarkRun[]] }
|
||||
|
||||
@@ -93,28 +105,44 @@ export function tryImageLine(alt: string | undefined, url: string, flavour: Writ
|
||||
}
|
||||
|
||||
function emitLine(nodes: readonly AdfNode[], container: LineContainer, path: ConvertErrorPath, flavour: WrittenFlavour): Result<EmittedLine> {
|
||||
const fallbacks = withheldMarkFallbacks(nodes, flavour)
|
||||
// spec/flavour.md, Marks: the spanned line where it reads back, else the parted line with every fallback it needs.
|
||||
const spanned = linePass(nodes, container, path, fallbacks, 'spanned')
|
||||
if (!spanned.ok) return spanned
|
||||
const { fallback, line, segments } = spanned.value
|
||||
if (fallback === undefined) return success({ line, openingLinkAsDirective: false, segments })
|
||||
return partedLine(nodes, container, path, fallbacks)
|
||||
return settledLine(withheldMarkFallbacks(nodes, flavour), {
|
||||
giveUpEmphasis: (fallbacks) => carryEveryEmphasis(fallbacks, nodes),
|
||||
judge: (fallbacks, grouping) => writerJudgement(nodes, container, path, fallbacks, grouping),
|
||||
spansEachPass: false,
|
||||
take: (fallbacks, fallback) => {
|
||||
const taken = takeFallback(fallbacks, fallback, path)
|
||||
return taken.ok ? success(fallbacks) : taken
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
function partedLine(nodes: readonly AdfNode[], container: LineContainer, path: ConvertErrorPath, fallbacks: LineFallbacks): Result<EmittedLine> {
|
||||
// Terminates because takeFallback refuses a pass that took no new fallback.
|
||||
for (let pass = 1; ; pass += 1) {
|
||||
const attempt = linePass(nodes, container, path, fallbacks, 'parted')
|
||||
if (!attempt.ok) return attempt
|
||||
const { fallback, line, segments } = attempt.value
|
||||
if (fallback === undefined) return success({ line, openingLinkAsDirective: fallbacks.openingLinkAsDirective, segments })
|
||||
const taken = takeFallback(fallbacks, fallback, path)
|
||||
// spec/flavour.md, Marks: the spanned line where it reads back, else the parted line, taking one fallback a pass.
|
||||
export function settledLine<Line, LineFallback, Written>(line: Line, action: FallbackAction<Line, LineFallback, Written>): Result<Written> {
|
||||
// Terminates because `take` refuses a pass that changes nothing.
|
||||
for (let current = line, pass = 1; ; pass += 1) {
|
||||
let parting = current
|
||||
if (pass === 1 || action.spansEachPass) {
|
||||
const spanned = action.judge(current, 'spanned')
|
||||
if (!spanned.ok) return spanned
|
||||
if (spanned.value.kind === 'written') return success(spanned.value.written)
|
||||
parting = spanned.value.line
|
||||
}
|
||||
const parted = action.judge(parting, 'parted')
|
||||
if (!parted.ok) return parted
|
||||
if (parted.value.kind === 'written') return success(parted.value.written)
|
||||
const taken = action.take(parted.value.line, parted.value.fallback)
|
||||
if (!taken.ok) return taken
|
||||
if (pass === readBackPasses) carryEveryEmphasis(fallbacks, nodes)
|
||||
current = pass === readBackPasses ? action.giveUpEmphasis(taken.value) : taken.value
|
||||
}
|
||||
}
|
||||
|
||||
function writerJudgement(nodes: readonly AdfNode[], container: LineContainer, path: ConvertErrorPath, fallbacks: LineFallbacks, grouping: RunGrouping): Result<LineJudgement<LineFallbacks, Fallback, EmittedLine>> {
|
||||
const attempt = linePass(nodes, container, path, fallbacks, grouping)
|
||||
if (!attempt.ok) return attempt
|
||||
const { fallback, line, segments } = attempt.value
|
||||
return success(fallback === undefined ? { kind: 'written', written: { line, openingLinkAsDirective: fallbacks.openingLinkAsDirective, segments } } : { fallback, kind: 'fallback', line: fallbacks })
|
||||
}
|
||||
|
||||
export function linePass(nodes: readonly AdfNode[], container: LineContainer, path: ConvertErrorPath, fallbacks: LineFallbacks, grouping: RunGrouping): Result<LineAttempt> {
|
||||
const emission = lineSegments(nodes, container, path, fallbacks, grouping)
|
||||
if (!emission.ok) return emission
|
||||
@@ -122,8 +150,9 @@ export function linePass(nodes: readonly AdfNode[], container: LineContainer, pa
|
||||
return success(segments === undefined ? { fallback: carries } : attemptLine(segments, container, fallbacks.flavour))
|
||||
}
|
||||
|
||||
function carryEveryEmphasis(fallbacks: LineFallbacks, nodes: readonly AdfNode[]): void {
|
||||
function carryEveryEmphasis(fallbacks: LineFallbacks, nodes: readonly AdfNode[]): LineFallbacks {
|
||||
for (const [index, node] of nodes.entries()) if (nodeMarks(node).some(isEmphasis)) fallbacks.carried.add(index)
|
||||
return fallbacks
|
||||
}
|
||||
|
||||
// Seeds the withheld-mark carries once per line, so the run at each mark depth does not rescan every node's marks.
|
||||
@@ -359,7 +388,7 @@ function tryLink(nodes: readonly [RunNode, ...RunNode[]], mark: AdfMark, depth:
|
||||
const href = linkHref(nodeAttrs(mark))
|
||||
if (href === undefined) return carriedRun(range, context)
|
||||
const opening = depth === 0 && range.first === 0 && context.openingLinkAsDirective
|
||||
const commonMark = opening ? undefined : commonMarkLink(nodeAttrs(mark), href, nodes.map(({ node }) => node), nodes[0].outer, context.bracketed, titleEscapes(context.flavour, context.container))
|
||||
const commonMark = opening ? undefined : commonMarkLink(nodeAttrs(mark), href, nodes.map(({ node }) => node), { bracketed: context.bracketed, escapes: titleEscapes(context.flavour, context.container), marksInside: nodes[0].outer })
|
||||
if (commonMark === undefined) return undefined
|
||||
if (commonMark.form === 'autolink') return success({ segments: [syntax(`<${href}>`)] })
|
||||
const inner = emitRun(nodes, depth + 1, range.first, { ...context, bracketed: true })
|
||||
|
||||
@@ -1,3 +1,3 @@
|
||||
// The only file `parse/` imports from `emit/` (docs/decisions.md §The source parts by ADF and format).
|
||||
export { commonMarkSpelling, type SpellingMemo } from './adf-to-markdown.ts'
|
||||
export { commonMarkSpelling, spellingMemo, type SpellingMemo } from './adf-to-markdown.ts'
|
||||
export { openingLinkTakesDirective } from './inline-line.ts'
|
||||
|
||||
@@ -14,35 +14,47 @@ type EmphasisPair = { closer: number; delimiter: string; nodes: MarkRun; opener:
|
||||
// The pairs in closing order, and what the sweep has settled about them.
|
||||
type PairSweep = {
|
||||
readonly fallback: RunFallback
|
||||
// 1 where the pair is settled to fall.
|
||||
readonly fallen: Uint8Array
|
||||
readonly first: number
|
||||
// The bounds of every run whose drop may merge runs: its first node's index and the index past its last.
|
||||
readonly mergingEnds: ReadonlySet<number>
|
||||
readonly output: string
|
||||
// The pair each placed segment delimits, -1 where it delimits none.
|
||||
readonly pairAt: Int32Array
|
||||
readonly pairs: readonly EmphasisPair[]
|
||||
readonly placed: readonly PlacedSegment[]
|
||||
readonly verdicts: Verdict[]
|
||||
}
|
||||
|
||||
// How another pair stands when the judged one falls: `moves` where a carry or a merge may move its delimiters, `unsettled` where it may stand or have fallen.
|
||||
type Standing = 'fallen' | 'moves' | 'stands' | 'unsettled'
|
||||
|
||||
// The runs this pass takes back: the first emphasis run the line misreads and every run that falls with it, else every misflanked highlight run. docs/decisions.md §With the first misread emphasis run, `unspellableRuns` returns only runs that, taken back early, leave the line as the one-run-per-pass reference writes it.
|
||||
// `merges` where a drop may merge runs at the pair's ends, so its delimiters may move; `falls` where it goes back with the first misread pair, itself included unless it merges; `kept` otherwise.
|
||||
type Verdict = 'falls' | 'kept' | 'merges'
|
||||
|
||||
// The runs this pass takes back: the first emphasis run the line misreads and every run that falls with it, else every misflanked highlight run.
|
||||
export function unspellableRuns(placed: readonly PlacedSegment[], reading: InlineReading, output: string, fallback: RunFallback): MarkRun[] {
|
||||
const { pairAt, pairs } = emphasisPairs(placed)
|
||||
const first = pairs.findIndex((pair) => !readAsWritten(reading, placed, pair))
|
||||
const firstPair = pairs[first]
|
||||
if (firstPair === undefined) return misflankedHighlights(placed, output)
|
||||
const sweep: PairSweep = { fallback, fallen: new Uint8Array(pairs.length), first, mergingEnds: mergingEnds(pairs, fallback), output, pairAt, pairs, placed }
|
||||
sweep.fallen[first] = 1
|
||||
const verdicts = pairVerdicts({ fallback, first, output, pairAt, pairs, placed })
|
||||
return pairs.reduce(
|
||||
(runs, pair, index) => {
|
||||
if (index !== first && verdicts[index] === 'falls') runs.push(pair.nodes)
|
||||
return runs
|
||||
},
|
||||
[firstPair.nodes],
|
||||
)
|
||||
}
|
||||
|
||||
// docs/decisions.md §With the first misread emphasis run, `unspellableRuns` returns only runs that, taken back early, leave the line as the one-run-per-pass reference writes it.
|
||||
function pairVerdicts(line: Omit<PairSweep, 'verdicts'>): Verdict[] {
|
||||
const { fallback, first, pairs } = line
|
||||
const ends = mergingEnds(pairs, fallback)
|
||||
const verdicts = pairs.map((pair, index): Verdict => (mergesAt(ends, pair) ? 'merges' : index === first ? 'falls' : 'kept'))
|
||||
const sweep: PairSweep = { ...line, verdicts }
|
||||
// Each verdict reads the verdicts settled before it, so the sweep settles forward from the first pair, then back.
|
||||
for (let judged = first + 1; judged < pairs.length; judged += 1) if (fallsWithFirst(sweep, judged)) sweep.fallen[judged] = 1
|
||||
for (let judged = first - 1; judged >= 0; judged -= 1) if (fallsWithFirst(sweep, judged)) sweep.fallen[judged] = 1
|
||||
const runs = [firstPair.nodes]
|
||||
for (const [index, pair] of pairs.entries()) if (index !== first && sweep.fallen[index] === 1) runs.push(pair.nodes)
|
||||
return runs
|
||||
for (let judged = first + 1; judged < pairs.length; judged += 1) if (fallsWithFirst(sweep, judged)) verdicts[judged] = 'falls'
|
||||
for (let judged = first - 1; judged >= 0; judged -= 1) if (fallsWithFirst(sweep, judged)) verdicts[judged] = 'falls'
|
||||
return verdicts
|
||||
}
|
||||
|
||||
// The pairs in closing order.
|
||||
@@ -97,18 +109,18 @@ function endsAlone(edge: AdfNode | undefined, beside: AdfNode | undefined, mark:
|
||||
return marks.slice(marks.findIndex((held) => identicalMark(held, mark)) + 1).every((held) => !besideTypes.has(held.type))
|
||||
}
|
||||
|
||||
// A pair falls with the first only where that leaves the line as the one-run-per-pass reference writes it: it falls cleanly, and cannot read whichever character it is written with.
|
||||
// A pair falls with the first where it falls cleanly, and cannot read whichever character it is written with.
|
||||
function fallsWithFirst(sweep: PairSweep, judged: number): boolean {
|
||||
if (!fallsCleanly(sweep, judged)) return false
|
||||
const spellings = pairSpellings(sweep, judged, judged)
|
||||
return spellings !== undefined && spellings.every((character) => cannotRead(sweep, judged, character))
|
||||
}
|
||||
|
||||
// A carry splits every pair holding it, so a pair falls cleanly under a carry only where it holds no pair spanning fewer nodes, and under a drop only where no drop may merge runs at its ends.
|
||||
function fallsCleanly({ fallback, mergingEnds, pairs }: PairSweep, judged: number): boolean {
|
||||
// A carry splits every pair holding it, so a pair falls cleanly under a carry only where it holds no pair spanning fewer nodes, and under a drop only where it does not merge.
|
||||
function fallsCleanly({ fallback, pairs, verdicts }: PairSweep, judged: number): boolean {
|
||||
const pair = pairs[judged]
|
||||
if (pair === undefined) return false
|
||||
return fallback.kind === 'drop' ? !mergesAt(mergingEnds, pair) : !holdsPairBefore(pairs, judged, pair)
|
||||
if (pair === undefined || verdicts[judged] === 'merges') return false
|
||||
return fallback.kind === 'drop' || !holdsPairBefore(pairs, judged, pair)
|
||||
}
|
||||
|
||||
function mergesAt(mergingEnds: ReadonlySet<number>, { nodes }: EmphasisPair): boolean {
|
||||
@@ -122,12 +134,12 @@ function cannotRead(sweep: PairSweep, judged: number, character: string): boolea
|
||||
}
|
||||
|
||||
// Whether the judged pair holds a pair not settled to fall that may be written with `character`. Pairs close in order, so the pairs it holds close just before it.
|
||||
function holdsAlike({ fallen, pairs }: PairSweep, judged: number, character: string): boolean {
|
||||
function holdsAlike({ first, pairs, verdicts }: PairSweep, judged: number, character: string): boolean {
|
||||
const pair = pairs[judged]
|
||||
for (let held = judged - 1; pair !== undefined && held >= 0; held -= 1) {
|
||||
const inner = pairs[held]
|
||||
if (inner === undefined || inner.closer < pair.opener) return false
|
||||
if (fallen[held] === 0 && mayBeWrittenWith(inner, character)) return true
|
||||
if (held !== first && verdicts[held] !== 'falls' && mayBeWrittenWith(inner, character)) return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
@@ -205,13 +217,13 @@ function delimiterEdge(placed: readonly PlacedSegment[], at: number, step: -1 |
|
||||
}
|
||||
|
||||
// Derived case by case in docs/decisions.md §With the first misread emphasis run, `unspellableRuns` returns only runs that, taken back early, leave the line as the one-run-per-pass reference writes it.
|
||||
function pairStanding({ fallback, fallen, first, mergingEnds, pairs }: PairSweep, judged: number, other: number): Standing {
|
||||
function pairStanding({ fallback, first, pairs, verdicts }: PairSweep, judged: number, other: number): Standing {
|
||||
const pair = pairs[other]
|
||||
if (other === judged) return 'stands'
|
||||
if (pair === undefined || mergesAt(mergingEnds, pair)) return 'moves'
|
||||
if (pair === undefined || verdicts[other] === 'merges') return 'moves'
|
||||
if (other > judged && judged > first) return 'stands'
|
||||
if (fallback.kind === 'carry') return spanSameNodes(pair, pairs[judged]) ? 'stands' : 'moves'
|
||||
return fallen[other] === 1 ? 'fallen' : 'unsettled'
|
||||
return verdicts[other] === 'kept' ? 'unsettled' : 'fallen'
|
||||
}
|
||||
|
||||
// The characters the other pair may be written with when the judged one falls: em takes `_`, turning `*` beside a word character.
|
||||
|
||||
Reference in New Issue
Block a user