lwgray / think

MIT License
0 stars 1 forks source link

Block Level Error Detection #26

Closed lwgray closed 1 week ago

lwgray commented 1 week ago

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

  1. 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
  2. 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]
  3. 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.

Test Case 2: Inconsistent Indentation

task "Inconsistent"
    x = 1
      y = 2  # Wrong indentation level

Should detect and report inconsistent level.

Test Case 3: Under-Indentation

task "Under"
    if x > 0
    y = 1  # Missing indentation

Should detect and report missing indentation.

Test Case 4: Over-Indentation

task "Over"
    x = 1
        y = 2  # Extra indentation

Should detect and report extra indentation.

Test Case 5: Complex Nesting

task "Complex"
    if x > 0
        for item in items
          process(item)  # Wrong indent level

Should detect and report inconsistent nesting.

Completion Criteria

  1. [ ] Detects mixed tabs and spaces
  2. [ ] Detects inconsistent indentation levels
  3. [ ] Detects missing required indentation
  4. [ ] Detects excessive indentation
  5. [ ] Provides clear error messages
  6. [ ] Shows relevant source context
  7. [ ] Reports correct line numbers
  8. [ ] Reports correct column numbers
  9. [ ] All test cases pass
  10. [ ] Error messages are user-friendly

Dependencies

Documentation Updates Needed

Testing Instructions

  1. Run provided test cases
  2. Verify error messages
  3. Check context display
  4. Validate line numbers
  5. Test complex scenarios
  6. Verify user-friendliness

Error Message Format Example

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