// file: $isip/class/pr/ProductionRule/prdrl_05.cc
// version: $Id: prdrl_05.cc 10355 2006-01-09 19:38:18Z wholland $

// ISIP include files
//
#include "ProductionRule.h"

// get rule - return a string representation of the entire rule
//
String ProductionRule::getRule() {
  String temp(L"");

  temp.concat(rule_name_d);
  temp.concat(L" -> ");
  
  if (rule_d.gotoFirst()) {
    temp.concat(convertToString(*(rule_d.getCurr())));
    while (rule_d.gotoNext()) {
      temp.concat(convertToString(*(rule_d.getCurr())));
    }
  }

  return temp;
}

// convert a single item to a string representation
//
String ProductionRule::convertToString(Triple<ProductionRuleTokenType,
				       String, Float>& item_a) {
  if (item_a.first().getType()==ProductionRuleTokenType::KLEENE_STAR) {
    return L"*";
  }
  else if (item_a.first().getType()==ProductionRuleTokenType::KLEENE_PLUS) {
    return L"+";
  }
  else if (item_a.first().getType()==ProductionRuleTokenType::ALTERNATION) {
    return L"|";
  }
  else if (item_a.first().getType()==ProductionRuleTokenType::OPEN_PAREN) {
    return L"(";
  }
  else if (item_a.first().getType()==ProductionRuleTokenType::CLOSE_PAREN) {
    return L")";
  }
  else if (item_a.first().getType()==ProductionRuleTokenType::TERMINAL) {
    String temp=item_a.second();
    temp.toLower();
    return temp;
  }
  else if (item_a.first().getType()==ProductionRuleTokenType::NON_TERMINAL) {
    String temp=item_a.second();
    temp.toUpper();
    return temp;
  }
  else if (item_a.first().getType()==ProductionRuleTokenType::OPEN_BRACE) {
    return L"[";
  }
  else if (item_a.first().getType()==ProductionRuleTokenType::CLOSE_BRACE) {
    return L"]";
  }
  else if (item_a.first().getType()==ProductionRuleTokenType::CONCATENATION) {
    return L" ";
  }
  else if (item_a.first().getType()==ProductionRuleTokenType::EPSILON) {
    return L"(epsilon)";
  }

  return L"";
}

// get rule name
//
String ProductionRule::getRuleName() {
  return rule_name_d;
}

// set rule name
//
boolean ProductionRule::setRuleName(const String& rule_name_a) {
  return rule_name_d.assign(rule_name_a);
}
