Skip to content

Instantly share code, notes, and snippets.

@ghuntley
Last active April 7, 2025 13:44
Show Gist options
  • Save ghuntley/4e772dc2f616b9128c2c548cf5cf396e to your computer and use it in GitHub Desktop.
Save ghuntley/4e772dc2f616b9128c2c548cf5cf396e to your computer and use it in GitHub Desktop.

Parser Resolution Plan for Interface Support

After analyzing the failing tests and parser errors, I've identified several issues with interface method declarations and type handling. This document outlines a detailed plan to fix the parser to properly support interfaces.

Issues Identified

  1. Method Declaration Syntax: Parser fails to handle both dot notation (Person.greet()) and colon notation (Person:greet()).

  2. Interface Method Signatures: Fails to parse method signatures like greet() tea; in interface definitions.

  3. Type Annotations: Cannot parse type annotations in variable declarations like sus g Greeter = p;.

Failing Tests

The following tests are currently failing:

failures:
    test_interface_generic
    test_interface_multiple
    test_interface_simple

All these failures stem from parser limitations in handling interface syntax.

Proposed Parser Fixes

1. Fix Method Declaration Parser

// in parse_method_declaration function
pub fn parse_method_declaration(&mut self) -> Result<Box<dyn Statement>, Error> {
    // Store the 'slay' token
    let token = self.current_token.token_literal();
    
    // Parse receiver type (struct name)
    if !self.expect_peek_identifier() {
        return Err(Error::from_str(&format!("Expected identifier after 'slay', got {:?}", self.peek_token)));
    }
    
    let receiver_type = ast::Identifier {
        token: self.current_token.token_literal(),
        value: self.current_token.token_literal(),
    };
    
    // Support both colon and dot notation
    if !(self.expect_peek(&Token::Colon) || self.expect_peek(&Token::Dot)) {
        return Err(Error::from_str(&format!("Expected ':' or '.' after receiver type, got {:?}", self.peek_token)));
    }
    
    // Rest of method parsing...

2. Fix Interface Method Signature Parser

// in parse_collab_statement function
fn parse_collab_statement(&mut self, token: String, name: ast::Identifier, type_parameters: Vec<ast::Identifier>) -> Result<Box<dyn Statement>, Error> {
    // ... existing code ...
    
    // Parse method signature
    if let Token::Identifier(method_name) = &self.current_token {
        let method_id = ast::Identifier {
            token: self.current_token.token_literal(),
            value: method_name.clone(),
        };
        
        // Handle parameter list - make LParen optional for parameterless methods
        let params = if self.peek_token == Token::LParen {
            self.next_token()?; // consume LParen
            self.parse_parameters()?
        } else {
            Vec::new() // No parameters
        };
        
        // Handle return type
        let return_type = if let Token::Identifier(_) = &self.current_token {
            Some(/* parse return type */)
        } else {
            None
        };
        
        // ... rest of method signature parsing
    }

3. Fix Type Annotation Support

// in parse_let_statement
fn parse_let_statement(&mut self) -> Result<Box<dyn Statement>, Error> {
    // ... existing code ...
    
    // After parsing variable name, check for type annotation
    if let Token::Identifier(_) = &self.peek_token {
        self.next_token()?; // consume type name
        
        // Now expect '=' for the value
        if !self.expect_peek(&Token::Assign) {
            return Err(Error::from_str(&format!("Expected '=' after type annotation, got {:?}", self.peek_token)));
        }
    }
    
    // ... rest of variable parsing
}

Implementation Strategy

  1. Create isolated test cases for each feature to test incrementally
  2. Implement fixes one at a time, starting with method declarations
  3. Add extensive error reporting to help diagnose future issues
  4. Add parser unit tests for each fix to ensure correctness
  5. Update grammar documentation to clearly specify both supported syntaxes

Testing Plan

  1. Create minimal test cases for each syntax feature:

    • Method declaration (both dot and colon notation)
    • Interface method signatures
    • Type annotations in variable declarations
  2. Expand to integration tests with full interfaces once individual features work

  3. Ensure backward compatibility with existing code

Once these parser changes are implemented, the interface tests should pass without requiring test modifications.

Documentation Updates

After implementation, the following documentation should be updated:

  1. specs/grammar.md - Document supported interface syntax
  2. specs/types.md - Clarify interface method signatures
  3. Add examples showing correct interface usage
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment