{"version":3,"file":"error-formatter-3DUz7fOl.mjs","names":["lines: string[]","match: RegExpExecArray | null","errors: Array<{ file: string; line: number; col: number; type: string; message: string }>"],"sources":["../src/error-formatter.ts"],"sourcesContent":["// src/error-formatter.ts\n// Error formatting and runtime diagnostics.\n\nimport pc from 'picocolors';\nimport fs from 'node:fs';\n\nconst SIGNAL_EXPLANATIONS: Record<string, { name: string; desc: string; hint: string }> = {\n  SIGSEGV: {\n    name: 'Segmentation Fault',\n    desc: 'The process accessed memory outside of its valid address space.',\n    hint: 'Check array bounds, null pointers, and deep recursion stack usage.',\n  },\n  SIGFPE: {\n    name: 'Floating Point Exception',\n    desc: 'An invalid arithmetic operation occurred.',\n    hint: 'Check division/modulo by zero and integer overflow conditions.',\n  },\n  SIGABRT: {\n    name: 'Abort',\n    desc: 'The process terminated itself via abort().',\n    hint: 'Check failed assertions and runtime sanity checks.',\n  },\n  SIGKILL: {\n    name: 'Killed',\n    desc: 'The process was terminated by the operating system.',\n    hint: 'This often indicates memory pressure or external termination.',\n  },\n  SIGBUS: {\n    name: 'Bus Error',\n    desc: 'The process made an invalid memory alignment or bus access.',\n    hint: 'Check low-level pointer operations and memory layout assumptions.',\n  },\n  SIGILL: {\n    name: 'Illegal Instruction',\n    desc: 'The process executed an invalid CPU instruction.',\n    hint: 'Check compiler flags, architecture mismatches, or memory corruption.',\n  },\n};\n\nfunction appendSignalDiagnosis(lines: string[], signal: string): void {\n  const info = SIGNAL_EXPLANATIONS[signal];\n  if (!info) return;\n\n  lines.push(pc.red(`[Signal] ${signal} (${info.name})`));\n  lines.push(pc.dim(`  ${info.desc}`));\n  lines.push(pc.yellow(`  Hint: ${info.hint}`));\n  lines.push('');\n}\n\nexport function formatRuntimeError(stderr: string, exitCode?: number): string {\n  const lines: string[] = [];\n  const detectedSignals = new Set<string>();\n  const stderrLower = stderr.toLowerCase();\n\n  for (const signal of Object.keys(SIGNAL_EXPLANATIONS)) {\n    if (stderr.includes(signal) || stderrLower.includes(signal.toLowerCase())) {\n      detectedSignals.add(signal);\n      appendSignalDiagnosis(lines, signal);\n    }\n  }\n\n  const signalFromExitCode = typeof exitCode === 'number' ? getSignalFromExitCode(exitCode) : null;\n  if (signalFromExitCode && !detectedSignals.has(signalFromExitCode)) {\n    appendSignalDiagnosis(lines, signalFromExitCode);\n  }\n\n  if (stderr.includes('out_of_range') || stderr.includes('vector::_M_range_check')) {\n    lines.push(pc.red('Container out-of-range access detected.'));\n    lines.push(pc.dim('  The process accessed a vector/array index outside valid bounds.'));\n    lines.push(pc.yellow('  Hint: verify all index calculations and boundary checks.'));\n    lines.push('');\n  }\n\n  if (stderr.includes('bad_alloc')) {\n    lines.push(pc.red('Memory allocation failed.'));\n    lines.push(pc.dim('  The process requested more memory than available.'));\n    lines.push(pc.yellow('  Hint: review allocation size and possible unbounded growth.'));\n    lines.push('');\n  }\n\n  if (stderr.includes('stack smashing') || stderr.includes('buffer overflow')) {\n    lines.push(pc.red('Stack/buffer overflow detected.'));\n    lines.push(pc.dim('  A write likely exceeded the allocated buffer boundary.'));\n    lines.push(pc.yellow('  Hint: validate fixed-size buffer writes and local array bounds.'));\n    lines.push('');\n  }\n\n  if (lines.length === 0 && stderr.trim()) {\n    lines.push(pc.red('Error output:'));\n    lines.push(pc.dim(stderr.trim()));\n  }\n\n  if (lines.length === 0 && typeof exitCode === 'number') {\n    lines.push(pc.red(`Process exited with code ${exitCode}.`));\n  }\n\n  return lines.join('\\n');\n}\n\nexport function formatCompilerError(stderr: string, sourceFile?: string): string {\n  const lines: string[] = [];\n\n  const errorPattern = /^(.+?):(\\d+):(\\d+):\\s*(error|warning):\\s*(.+)$/gm;\n  let match: RegExpExecArray | null;\n  const errors: Array<{ file: string; line: number; col: number; type: string; message: string }> = [];\n\n  while ((match = errorPattern.exec(stderr)) !== null) {\n    errors.push({\n      file: match[1],\n      line: Number.parseInt(match[2], 10),\n      col: Number.parseInt(match[3], 10),\n      type: match[4],\n      message: match[5],\n    });\n  }\n\n  if (errors.length === 0) {\n    return stderr;\n  }\n\n  for (const error of errors) {\n    const tag = error.type === 'error' ? '[ERROR]' : '[WARN]';\n    const color = error.type === 'error' ? pc.red : pc.yellow;\n\n    lines.push(color(`${tag} ${error.file}:${error.line}:${error.col}`));\n    lines.push(`  ${error.message}`);\n\n    const targetFile = sourceFile || error.file;\n    if (fs.existsSync(targetFile)) {\n      try {\n        const content = fs.readFileSync(targetFile, 'utf-8');\n        const sourceLines = content.split('\\n');\n        const errorLine = sourceLines[error.line - 1];\n\n        if (errorLine) {\n          lines.push('');\n          lines.push(pc.dim(`  ${error.line} | `) + errorLine);\n          const pointer = ' '.repeat(Math.max(0, error.col - 1)) + pc.red('^');\n          lines.push(pc.dim('    | ') + pointer);\n        }\n      } catch {\n        // Ignore source preview failures.\n      }\n    }\n    lines.push('');\n  }\n\n  return lines.join('\\n');\n}\n\nexport function getSignalFromExitCode(exitCode: number): string | null {\n  if (exitCode > 128) {\n    const signalNum = exitCode - 128;\n    const signalMap: Record<number, string> = {\n      4: 'SIGILL',\n      6: 'SIGABRT',\n      7: 'SIGBUS',\n      8: 'SIGFPE',\n      9: 'SIGKILL',\n      11: 'SIGSEGV',\n    };\n    return signalMap[signalNum] || null;\n  }\n  return null;\n}\n"],"mappings":";;;;AAmGA,SAAgB,oBAAoB,QAAgB,YAA6B;CAC/E,MAAMA,QAAkB,EAAE;CAE1B,MAAM,eAAe;CACrB,IAAIC;CACJ,MAAMC,SAA4F,EAAE;AAEpG,SAAQ,QAAQ,aAAa,KAAK,OAAO,MAAM,KAC7C,QAAO,KAAK;EACV,MAAM,MAAM;EACZ,MAAM,OAAO,SAAS,MAAM,IAAI,GAAG;EACnC,KAAK,OAAO,SAAS,MAAM,IAAI,GAAG;EAClC,MAAM,MAAM;EACZ,SAAS,MAAM;EAChB,CAAC;AAGJ,KAAI,OAAO,WAAW,EACpB,QAAO;AAGT,MAAK,MAAM,SAAS,QAAQ;EAC1B,MAAM,MAAM,MAAM,SAAS,UAAU,YAAY;EACjD,MA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