Fix this PegJS grammer to extract filenames and from markdown code blocks { const DEBUG…
- User
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:( [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 = "#"
Assistant
Here's an improved version of the PEG.js grammar for extracting filenames 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; 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 = 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; } FilePath = path:$( NameSegment ( PathSeparator NameSegment )* ( "." [a-zA-Z0-9_]+ )? ) { return path; } NameSegment = $( [a-zA-Z0-9_@.-]+ ) / ".." PathSeparator = "/" / "\\" 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)*) 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 = "#"Key improvements and changes:
- Removed the commented-out
FileExtensionrule - Simplified the
FileBlockrule to remove the unnecessary lookahead - Kept the core logic for extracting filenames from various markdown contexts
- Maintained the debug logging and path normalization functions
This grammar should now more robustly extract filenames from markdown code blocks, supporting various formats like:
- Standalone path lines
- Paths in headings
- Paths in paragraphs
- Quoted and unquoted paths
The parser will return an array of objects with
pathandcontentproperties for each matched markdown code block with a preceding path indicator.- Removed the commented-out
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Identical first prompt
gemini-2.0-flash-001Compare side by side →
- gpt-4o-2024-11-20Compare side by side →
claude-3-5-sonnet-20241022Compare side by side →
claude-3-7-sonnet-20250219Compare side by side →
claude-3-7-sonnet-20250219Compare side by side →
qwen-max-2025-01-25Compare side by side →
- gpt-4.1-2025-04-14Compare side by side →
- grok-3-preview-02-24Compare side by side →
- grok-3-preview-02-24Compare side by side →
- chatgpt-4o-latest-20250326Compare side by side →
- o3-2025-04-16Compare side by side →