#!/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'))

# Force regeneration of parser with debug info
import shutil
if os.path.exists('src/alloy/parsetab.py'):
    shutil.move('src/alloy/parsetab.py', 'src/alloy/parsetab.py.bak')

# Create parser with debug output
from alloy.lexer import AlloyLexer
from alloy.parser import AlloyParser

def debug_parser_conflicts():
    print("Creating parser with debug output...")
    
    # Force debug mode in PLY
    lexer = AlloyLexer()
    parser = AlloyParser(lexer)
    
    # Check if parser.out was created
    if os.path.exists('parser.out'):
        print("\nParser conflicts found in parser.out:")
        with open('parser.out', 'r') as f:
            content = f.read()
            if 'conflict' in content.lower() or 'reduce' in content.lower():
                print(content)
            else:
                print("No obvious conflicts found")
    else:
        print("No parser.out file generated")
    
    # Now test the problematic case
    problem_code = '''
tool test takes order returns result {
    tax_amount = order + 0.08
    total = order + tax_amount
    result = {
        subtotal: order,
        tax: tax_amount,
        total: total
    }
}
'''
    
    print(f"\nTesting problematic code:")
    print(repr(problem_code))
    
    try:
        ast = parser.parser.parse(problem_code, lexer=lexer.lexer, debug=True)
        print(f"Result: {ast}")
    except Exception as e:
        print(f"Error: {e}")
        import traceback
        traceback.print_exc()

if __name__ == "__main__":
    debug_parser_conflicts()