package osa.attribute.atgrammar.grammar;

import osa.attribute.atgrammar.grammar.symbols.*;

/**
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: </p>
 * <p>Company: </p>
 * @author not attributable
 * @version
 */

public class DerivationString
  extends SymbolsString
  {
  private Symbol current_symbol=null;
  private int current_index=-1;

  private Grammar grammar_reference;

  public DerivationString(Grammar grammar_reference)
    {
    this.grammar_reference=grammar_reference;
    appendSymbol(grammar_reference.getInitialSymbol());
    moveToNextSymbol();
    }

  public void executeSubstitution(GrammarProduction production)
    {
    NonTerminalSymbol symbol_to_substitute;
    symbol_to_substitute=(NonTerminalSymbol)current_symbol;

    SymbolsString substitution_string;
    substitution_string=production.executeProduction(symbol_to_substitute);

    removeSymbolAt(current_index);

    insertStringAt(current_index,substitution_string);
    current_symbol=getSymbolAt(current_index);
    }

  public boolean moveToNextSymbol()
    {
    current_index++;
    if(length<current_index)
      {
      return false;
      }
    current_symbol=this.getSymbolAt(current_index);
    return true;
    }

  public Symbol getCurrentSymbol()
    {
    return current_symbol;
    }

  }