Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion packages/parser/src/parser/parse/context.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import { DEFAULT_SETTINGS, type PageRef } from "@wdprlib/ast";
import type { Token } from "../../lexer";
import { blockFallbackRule, blockRules, inlineRules, type ParseContext } from "../rules";
import { splitSpansAtDivBoundaries } from "../rules/inline/span/block-split";
import type { ParserOptions } from "./options";

export function createParseContext(
tokens: Token[],
options: ParserOptions = {},
deferInclude?: (location: PageRef) => boolean,
): ParseContext {
return {
const ctx: ParseContext = {
tokens,
pos: 0,
version: options.version ?? "wikidot",
Expand All @@ -29,4 +30,6 @@ export function createParseContext(
footnoteBlockParsed: false,
},
};
ctx.tokens = splitSpansAtDivBoundaries(ctx);
return ctx;
}
2 changes: 1 addition & 1 deletion packages/parser/src/parser/rules/block/html/body.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ export function consumeHtmlClose(ctx: ParseContext, startPos: number): number {
return consumed;
}

function isHtmlCloseAt(ctx: ParseContext, pos: number): boolean {
export function isHtmlCloseAt(ctx: ParseContext, pos: number): boolean {
if (ctx.tokens[pos]?.type !== "BLOCK_END_OPEN") {
return false;
}
Expand Down
2 changes: 2 additions & 0 deletions packages/parser/src/parser/rules/contracts/scope.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,6 @@ export interface ScopeContext {
* the original primitive semantics while avoiding rollback-unsafe shared state.
*/
readonly footnoteBlockParsed: boolean;
/** Number of enclosing footnote bodies. */
readonly footnoteDepth?: number;
}
36 changes: 36 additions & 0 deletions packages/parser/src/parser/rules/inline/footnote/boundary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,39 @@ export function findFootnoteEnd(ctx: ParseContext, start: number): number {
}
return ctx.tokens.length;
}

/**
* Map each footnote opener to the end of its close in one pass, using the same
* protected regions and nesting as {@link findFootnoteEnd}.
*/
export function matchFootnoteCloses(ctx: ParseContext): Map<number, number> {
const probe = createOpaqueProbe(ctx);
const matches = new Map<number, number>();
const openers: number[] = [];
for (let pos = 0; pos < ctx.tokens.length; pos++) {
if (ctx.tokens[pos]?.type === "EOF") break;
const protectedEnd = protectedInlineRegionEnd(ctx.tokens, pos, ctx.tokens.length);
if (protectedEnd > pos) {
pos = protectedEnd - 1;
continue;
}

probe.pos = pos;
const close = tryConsumeFootnoteClose(probe, pos);
if (close) {
const open = openers.pop();
if (open !== undefined) matches.set(open, pos + close.consumed);
pos += close.consumed - 1;
continue;
}
const open = parseFootnoteOpen(probe);
if (open) {
openers.push(pos);
pos += open.consumed - 1;
continue;
}
const opaqueEnd = opaqueRuleEnd(probe, pos, opaqueRules);
if (opaqueEnd > pos) pos = opaqueEnd - 1;
}
return matches;
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export function parseFootnoteContent(ctx: ParseContext, startPos: number): Footn
inlineEnd: undefined,
tableFormatting: undefined,
blockCloseCondition: undefined,
footnoteDepth: (ctx.scope.footnoteDepth ?? 0) + 1,
},
};
const result = parseBlocksUntil(bodyCtx, () => false);
Expand Down
8 changes: 7 additions & 1 deletion packages/parser/src/parser/rules/inline/footnote/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ import { parseFootnoteContent } from "./content";
import { buildFootnoteChildren } from "./elements";
import { parseFootnoteOpen } from "./open";

/**
* Each nested body is parsed recursively, so deeper footnotes stay as text
* instead of overflowing the call stack.
*/
const MAX_FOOTNOTE_NESTING = 64;

export const footnoteRule: InlineRule = {
name: "footnote",
startTokens: ["BLOCK_OPEN"],
Expand All @@ -22,7 +28,7 @@ export const footnoteRule: InlineRule = {
}

const openResult = parseFootnoteOpen(ctx);
if (!openResult) {
if (!openResult || (ctx.scope.footnoteDepth ?? 0) >= MAX_FOOTNOTE_NESTING) {
return { success: false };
}

Expand Down
Loading
Loading