#!/usr/bin/env python3
"""Debug PLY parser generation"""

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

# Create minimal parser to test
class MinimalParser:
    def __init__(self):
        self.lexer = AlloyLexer()
        self.tokens = self.lexer.tokens
        self.parser = None
        
    precedence = (
        ('right', 'IDENTIFIER'),  # Force shift on IDENTIFIER
    )
    
    def p_program(self, p):
        '''program : statement'''
        p[0] = ('program', p[1])
    
    def p_statement(self, p):
        '''statement : IDENTIFIER EQ alloy_expr'''
        p[0] = ('assign', p[1], p[3])
    
    def p_alloy_expr(self, p):
        '''alloy_expr : ALLOY IDENTIFIER IDENTIFIER
                      | ALLOY IDENTIFIER'''
        if len(p) == 4:
            p[0] = ('alloy', p[2], p[3])
        else:
            p[0] = ('alloy', p[2])
    
    def p_error(self, p):
        if p:
            print(f"Syntax error at '{p.value}'")
        else:
            print("Syntax error at EOF")
    
    def build(self):
        self.parser = yacc.yacc(module=self, debug=True)
        return self.parser
    
    def parse(self, text):
        if not self.parser:
            self.build()
        return self.parser.parse(text, lexer=self.lexer.lexer)

# Test
parser = MinimalParser()
test_cases = [
    'x = alloy Review',
    'x = alloy Review code',
]

for test in test_cases:
    print(f"\nTesting: {test}")
    result = parser.parse(test)
    print(f"Result: {result}")