UNPKG

python2ib

Version:

Convert Python code to IB Pseudocode format

134 lines • 4.79 kB
/** * Custom error classes for python2ib converter */ export class PythonSyntaxError extends Error { line; column; constructor(message, line, column) { super(message); this.line = line; this.column = column; this.name = 'PythonSyntaxError'; } toString() { const location = this.line !== undefined ? ` at line ${this.line}` : ''; return `${this.name}: ${this.message}${location}`; } } export class UnsupportedSyntaxError extends Error { syntaxType; suggestion; constructor(message, syntaxType, suggestion) { super(message); this.syntaxType = syntaxType; this.suggestion = suggestion; this.name = 'UnsupportedSyntaxError'; } toString() { const suggestion = this.suggestion ? `\n\nSuggestion: ${this.suggestion}` : ''; return `${this.name}: ${this.message} (${this.syntaxType})${suggestion}`; } } export class ConversionError extends Error { originalCode; partialResult; constructor(message, originalCode, partialResult) { super(message); this.originalCode = originalCode; this.partialResult = partialResult; this.name = 'ConversionError'; } toString() { let result = `${this.name}: ${this.message}`; if (this.originalCode) { result += `\n\nOriginal code:\n${this.originalCode}`; } if (this.partialResult) { result += `\n\nPartial conversion result:\n${this.partialResult}`; } return result; } } export class ConfigurationError extends Error { configPath; constructor(message, configPath) { super(message); this.configPath = configPath; this.name = 'ConfigurationError'; } toString() { const path = this.configPath ? ` in ${this.configPath}` : ''; return `${this.name}: ${this.message}${path}`; } } /** * Error messages with helpful suggestions */ export const ERROR_MESSAGES = { UNSUPPORTED_CLASS: { message: 'Class definitions are not supported in IB Pseudocode', suggestion: 'Convert classes to functions. Use separate functions instead of methods.' }, UNSUPPORTED_LAMBDA: { message: 'Lambda functions are not supported', suggestion: 'Convert lambda to a regular function definition.' }, UNSUPPORTED_LIST_COMPREHENSION: { message: 'List comprehensions are not supported', suggestion: 'Convert to explicit for loop with append operations.' }, UNSUPPORTED_EXCEPTION_HANDLING: { message: 'Exception handling (try/except) is not supported', suggestion: 'Use conditional statements to check for error conditions.' }, UNSUPPORTED_IMPORT: { message: 'Import statements are not supported', suggestion: 'Include necessary functions directly in your code.' }, UNSUPPORTED_DECORATOR: { message: 'Decorators are not supported', suggestion: 'Remove decorators and use plain function definitions.' }, UNSUPPORTED_ASYNC: { message: 'Async/await syntax is not supported', suggestion: 'Convert to synchronous code using regular functions.' }, UNSUPPORTED_MATCH_CASE: { message: 'Match/case statements are not supported', suggestion: 'Convert to if/elif/else chain.' }, INVALID_PYTHON_SYNTAX: { message: 'Invalid Python syntax detected', suggestion: 'Check your Python code for syntax errors before conversion.' }, EMPTY_INPUT: { message: 'Empty or whitespace-only input provided', suggestion: 'Provide valid Python code to convert.' } }; /** * Helper function to create UnsupportedSyntaxError with predefined messages */ export function createUnsupportedSyntaxError(errorType, syntaxType) { const errorInfo = ERROR_MESSAGES[errorType]; return new UnsupportedSyntaxError(errorInfo.message, syntaxType, errorInfo.suggestion); } /** * Helper function to format error for CLI output */ export function formatErrorForCLI(error) { if (error instanceof UnsupportedSyntaxError) { return `āŒ ${error.toString()}\n\nšŸ’” ${error.suggestion || 'See documentation for alternatives.'}`; } if (error instanceof PythonSyntaxError) { return `āŒ ${error.toString()}\n\nšŸ’” Check your Python syntax and try again.`; } if (error instanceof ConversionError) { return `āŒ ${error.toString()}\n\nšŸ’” Try simplifying your code or report this as a bug.`; } if (error instanceof ConfigurationError) { return `āŒ ${error.toString()}\n\nšŸ’” Check your configuration file format.`; } return `āŒ Unexpected error: ${error.message}`; } //# sourceMappingURL=index.js.map