#!/usr/bin/env python3

import sys
import os

# Add the src directory to the Python path
sys.path.insert(0, os.path.join(os.path.dirname(os.path.abspath(__file__)), 'src'))

from alloy import parser

def test_parser_fix():
    # Test the exact failing case
    failing_code = '''
tool process_order takes order returns order_result {
    tax_amount = order + 0.08
    total = order + tax_amount
    order_result = {
        subtotal: order,
        tax: tax_amount,
        total: total
    }
}
result = process_order(100)
'''
    
    print("=== Testing fixed parser ===")
    print(repr(failing_code))
    
    try:
        ast = parser.parse(failing_code)
        if ast and ast[0] == 'program' and len(ast) > 1:
            statements = ast[1]
            print(f"SUCCESS! Found {len(statements)} statements")
            for i, stmt in enumerate(statements):
                print(f"  Statement {i}: {stmt[0]}")
                if stmt[0] == 'tool':
                    tool_body = stmt[4]
                    if tool_body[0] == 'block':
                        tool_statements = tool_body[1]
                        print(f"    Tool body has {len(tool_statements)} statements:")
                        for j, tool_stmt in enumerate(tool_statements):
                            print(f"      {j}: {tool_stmt[0]}")
        else:
            print("FAILED: No statements found")
            print("AST:", ast)
    except Exception as e:
        print(f"PARSER ERROR: {e}")
        import traceback
        traceback.print_exc()

if __name__ == "__main__":
    test_parser_fix()