package osa.attribute.atgrammar.grammar.symbols;

import java.util.*;
/**
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2005</p>
 * <p>Company: </p>
 * @author not attributable
 * @version 1.0
 */

public class NonTerminalSymbol
  extends Symbol
  {
  public NonTerminalSymbol(String symbol_name)
    {
    super(symbol_name);
    symbol_type=Symbol.NON_TERMINAL_SYMBOL;
    is_cloned=false;
    original_reference=this;
    }


  public void tranferDefferedAssignments()
    {
    Attribute temp_attribute;
    for(int i=0;i<attributes_count;i++)
       {
       temp_attribute=getAttributeAt(i);
       if(temp_attribute.getValue()==null)
         {
         Attribute source=temp_attribute.getSourceOfAssignment();
         ArrayList deffered_assignments=temp_attribute.getDeferredAssignments();

         source.removeDeferredAssignment(temp_attribute);

         System.err.println("Source!!! "+source);


         for(int j=0;j<deffered_assignments.size();j++)
            {
            source.addDeferredAssignment((Attribute)deffered_assignments.get(j));
            System.err.println("!!! "+deffered_assignments.get(j)+" "+j);
            }
         }
       }
    }


  public Object clone()
    {
    // creating clone instance
    NonTerminalSymbol cloned=new NonTerminalSymbol(original_reference.getOriginalName());
    // setting corresponding symbol index
    cloned.symbol_index=original_reference.getCounterValue();
    original_reference.incrementCounter();
    // storing reference to original
    if(is_cloned)
      {
      cloned.original_reference=this.original_reference;
      }
    else
      {
      cloned.original_reference=this;
      }
    cloned.is_cloned=true;
    // copying fields

    // cloning attributes
    Attribute temp_attribute;
    int attr_count=original_reference.getAttrubutesCount();
    for(int i=0;i<attr_count;i++)
       {
       temp_attribute=(Attribute)original_reference.attributes_list.get(i);
       cloned.attributes_list.add(temp_attribute.clone());
       }
    return cloned;
    }
  }