#!/usr/bin/env python3
"""Detailed debug of parser issue"""

from src.alloy.parser import AlloyParser
from src.alloy.lexer import AlloyLexer
import ply.yacc as yacc

# Test case that's failing
code = 'review = alloy Review code'
print(f"Parsing: {code}")
print("=" * 60)

# First, let's see what tokens we get
lexer = AlloyLexer()
lexer.lexer.input(code)
tokens = []
for tok in lexer.lexer:
    tokens.append(tok)
    print(f"Token: {tok.type:15} Value: '{tok.value}'")

print("\n" + "=" * 60)
print("Now let's trace the parsing:")
print("=" * 60)

# Create a new parser instance
lexer = AlloyLexer()
parser = AlloyParser(lexer)

# Get the actual PLY parser object
ply_parser = parser.parser

# Check the grammar productions
print("\nGrammar productions containing 'alloy':")
for num, prod in enumerate(ply_parser.productions):
    if prod and 'alloy' in str(prod):
        print(f"{num}: {prod}")

print("\n" + "=" * 60)
print("Attempting to parse...")
print("=" * 60)

# Parse with debug
result = parser.parse(code, debug=False)
print(f"\nResult: {result}")

# Let's also test the working case
print("\n" + "=" * 60)
code2 = 'review = alloy Review'
print(f"Testing simpler case: {code2}")
result2 = parser.parse(code2)
print(f"Result: {result2}")