Fix this PegJS grammer to extract filenames and from markdown code blocks
{
const DEBUG = true;
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;
// location is a variable, not a function
// const loc = location();
const status = success ? "MATCH" : "FAIL";
const snippet = text.replace(/\n/g, '\n').substring(0, 60);
console.log([PEG ${status}] ${ruleName} T:"${snippet}");
}
}
Document = segments:Segment* EOF {
if (DEBUG) console.log("[Document] EOF reached.");
return segments.filter(s => s && s.format === "Markdown Block");
}
Segment = FileBlock / IgnorableContent
FileBlock =
&(PathIndicatorLine)
indicator:PathIndicatorLine
blanks:BlankLine*
block:CodeBlock
{
logMatch("FileBlock", true, Path: ${indicator.path});
return { path: indicator.path, content: block, format: "Markdown Block" };
}
IgnorableContent =
!(PathIndicatorLine BlankLine* CodeBlock)
consumed:( CodeBlock / IgnorableLine )
{ return null; }
PathIndicatorLine = content:(StandalonePathLine / PathInHeading / PathInParagraph) nl:(Newline / EOF) { return content; }
BlankLine = Indent nl:Newline { return null; }
IgnorableLine =
$( (!Newline .)* ) Newline
{
logMatch("IgnorableLine", true, text());
return null;
}
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; }
// Allow any extension, not just a fixed list
FilePath = path:(NameSegment(PathSeparatorNameSegment)∗("."[a−zA−Z( [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 =
start:CodeFenceLineStart
content:CodeBlockContent
end:CodeFenceLineEnd
{
const fullBlockText = start + content + end;
logMatch("CodeBlock", true, fullBlockText);
return content.replace(/\r?\n$/, '');
}
CodeFenceLineStart = Indent CodeFenceStart LangTag? optionalWhitespace Newline { return text(); }
CodeFenceLineEnd = Indent CodeFenceEnd optionalWhitespace (Newline / EOF) { return text(); }
CodeBlockContent = $((CodeContentLine)*) // capture as string
CodeContentLine =
!(Indent CodeFenceEnd)
line:$((!Newline .)* Newline)
{ return line; }
CodeFenceStart = "" CodeFenceEnd = ""
LangTag = $( [a-zA-Z0-9_-]+ )
optionalWhitespace = [ \t]*
TextAfterPath = $( (!Newline .)* )
PathMarker = ("File" / "file" / "Path" / "path" / "Updating" / "updating" / "Creating" / "creating")
Indent = [ \t]*
Newline = "\n" / "\r\n"
EOF = !.
HASH = "#"