Fix this PegJS grammer to extract filenames and from markdown code blocks
{
const DEBUG = true; // Enable for debugging test failures
function normalizePath(pathStr) {
const normalized = pathStr.trim().replace(/\/g, '/');
if (DEBUG) console.log([NormalizePath] Input: "${pathStr}", Output: "${normalized}");
return normalized;
}
function logMatch(ruleName, success, text) {
if (!DEBUG) return;
const loc = location();
const status = success ? "MATCH" : "FAIL";
const snippet = text.replace(/\n/g, '\n').substring(0, 60);
console.log([PEG ${status}] ${ruleName} @ L${loc.start.line} T:"${snippet}");
}
}
// Document: Sequence of segments
Document = segments:Segment* EOF {
if (DEBUG) console.log("[Document] EOF reached.");
return segments.filter(s => s && s.format === "Markdown Block");
}
// Segment: FileBlock or IgnorableContent
Segment = FileBlock / IgnorableContent
// FileBlock: Path line, optional blank lines, code block
FileBlock =
&(PathIndicatorLine)
indicator:PathIndicatorLine
blanks:BlankLine*
block:CodeBlock
{
logMatch("FileBlock", true, Path: ${indicator.path});
return { path: indicator.path, content: block, format: "Markdown Block" };
}
// IgnorableContent: Consumes non-FileBlock content
IgnorableContent =
!(PathIndicatorLine BlankLine* CodeBlock)
consumed:( CodeBlock / IgnorableLine )
{ return null; }
// Line Types
PathIndicatorLine = content:(StandalonePathLine / PathInHeading / PathInParagraph) nl:(Newline / EOF) { return content; }
BlankLine = Indent nl:Newline { return null; }
// IgnorableLine: Consumes a single line
IgnorableLine =
$( (!Newline .)* ) Newline
{
logMatch("IgnorableLine", true, text());
return null;
}
// Path Indicator Content Rules
StandalonePathLine = Indent path:FilePath Indent { return { path: normalizePath(path) }; }
PathInHeading = HASH+ Indent (PathMarker ":"? Indent)? path:FilePath (""?)? Indent { return { path: normalizePath(path) }; } PathInParagraph = !CodeFenceStart Indent path:(QuotedFilePath / UnquotedFilePath) suffix:( (""?)? Indent TextAfterPath? Indent (":" Indent)? )
{ return { path: normalizePath(path) }; }
QuotedFilePath = "" path:FilePath "" { return path; }
UnquotedFilePath = path:FilePath { return path; }
// Core Components
FilePath = path:(NameSegment(PathSeparatorNameSegment)∗FileExtension?)r( [a-zA-Z0-9_@.-]+ ) / ".."
PathSeparator = "/" / "\"
FileExtension = "." $( ("bash"i/"cjs"i/"config"i/"cs"i/"css"i/"dockerfile"i/"env"i/"gitignore"i/"go"i/"html"i/"java"i/"js"i/"json"i/"jsx"i/"less"i/"md"i/"mjs"i/"npmrc"i/"py"i/"scss"i/"sh"i/"sql"i/"toml"i/"ts"i/"tsx"i/"txt"i/"xml"i/"yaml"i/"yml"i/"zsh"i) )
// CodeBlock: Captures fenced code block content
CodeBlock =
fenceLineStart:(Indent CodeFenceStart LangTag? optionalWhitespace Newline)
contentLinesText:((CodeContentLine)*)
fenceLineEnd:(Indent CodeFenceEnd optionalWhitespace (Newline / EOF))
{
const fullBlockText = fenceLineStart + contentLinesText + fenceLineEnd;
logMatch("CodeBlock", true, fullBlockText);
return contentLinesText.replace(/\r?\n/, '');
}
// CodeContentLine: Captures a single line within a code block
CodeContentLine =
!(Indent CodeFenceEnd)
line:$((!Newline .)* Newline)
{ return line; }
CodeFenceStart = "" CodeFenceEnd = ""
LangTag = $( [a-zA-Z0-9_-]+ )
optionalWhitespace = [ \t]*
// Helpers
TextAfterPath = $( (!("\n") .)* )
PathMarker = ("File"i / "Path"i / "Updating"i / "Creating"i)
Indent = [ \t]*
Newline = "\n" / "\r\n"
EOF = !.
HASH = "#"