#!/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, lexer

def debug_parser_detailed():
    # Test just the tool definition first
    tool_only = '''
tool process_order takes order returns order_result {
    tax_amount = order + 0.08
}
'''
    
    print("=== Testing tool definition only ===")
    print(repr(tool_only))
    
    try:
        ast = parser.parse(tool_only)
        print("AST:", ast)
    except Exception as e:
        print(f"Parser error: {e}")
        
    # Test just an object assignment
    object_only = '''
order_result = {
    subtotal: order,
    tax: tax_amount,
    total: total
}
'''
    
    print("\n=== Testing object assignment only ===")
    print(repr(object_only))
    
    try:
        ast = parser.parse(object_only)
        print("AST:", ast)
    except Exception as e:
        print(f"Parser error: {e}")
        
    # Test the lexer output to see what tokens are generated
    print("\n=== Testing lexer tokens ===")
    test_lexer = lexer.get_global_lexer()
    test_lexer.input(tool_only)
    
    tokens = []
    while True:
        tok = test_lexer.token()
        if not tok:
            break
        tokens.append((tok.type, tok.value, tok.lineno, tok.lexpos))
    
    print("Tokens generated:")
    for i, (typ, val, line, pos) in enumerate(tokens):
        print(f"  {i:2d}: {typ:12s} '{val}' (line {line}, pos {pos})")

if __name__ == "__main__":
    debug_parser_detailed()