Closed lwgray closed 1 week ago
Level: Master
Implement comprehensive error detection and reporting for indentation-based block structure, focusing on consistency and clarity of error messages.
Add indentation validation:
class IndentationTracker: def __init__(self): self.indent_stack = [0] self.indent_size = 4 # Standard indent size self.current_line = 1 self.tab_seen = False self.space_seen = False def validate_indent(self, indent_str, line_no): """Validate indentation string for consistency""" if '\t' in indent_str: if self.space_seen: raise IndentationError( "Mixed tabs and spaces", line_no ) self.tab_seen = True if ' ' in indent_str: if self.tab_seen: raise IndentationError( "Mixed tabs and spaces", line_no ) self.space_seen = True
Add error context collection:
class ErrorContext: def __init__(self): self.source_lines = [] self.error_line = 0 self.error_col = 0 self.error_type = None def collect_context(self, line_no, window=2): """Collect source lines around error""" start = max(0, line_no - window) end = line_no + window return self.source_lines[start:end]
Add enhanced error reporting:
def format_error(self, message, line_no, col_no=None): context = self.error_context.collect_context(line_no) return f""" Error at line {line_no}{f", column {col_no}" if col_no else ''} {message}
Context: {self._format_context(context, line_no)} """
## Test Cases ### Test Case 1: Mixed Indentation ```python task "Mixed" x = 1 y = 2 # Tab instead of spaces
Should detect and report mixed indentation.
task "Inconsistent" x = 1 y = 2 # Wrong indentation level
Should detect and report inconsistent level.
task "Under" if x > 0 y = 1 # Missing indentation
Should detect and report missing indentation.
task "Over" x = 1 y = 2 # Extra indentation
Should detect and report extra indentation.
task "Complex" if x > 0 for item in items process(item) # Wrong indent level
Should detect and report inconsistent nesting.
Error at line 3, column 1 Inconsistent indentation: expected 4 spaces, found 2 Context: 1 | task "Example" 2 | x = 1 > 3 | y = 2 4 | z = 3 5 | Expected: y = 2 Found: y = 2
Block Level Error Detection
Level: Master
Description
Implement comprehensive error detection and reporting for indentation-based block structure, focusing on consistency and clarity of error messages.
Technical Details
Changes Needed in parser.py
Add indentation validation:
Add error context collection:
Add enhanced error reporting:
Context: {self._format_context(context, line_no)} """
Should detect and report mixed indentation.
Test Case 2: Inconsistent Indentation
Should detect and report inconsistent level.
Test Case 3: Under-Indentation
Should detect and report missing indentation.
Test Case 4: Over-Indentation
Should detect and report extra indentation.
Test Case 5: Complex Nesting
Should detect and report inconsistent nesting.
Completion Criteria
Dependencies
Documentation Updates Needed
Testing Instructions
Error Message Format Example