// file: $isip/class/asr/JSGFParser/jp_02.cc
// version: $Id: jp_02.cc 8929 2002-12-21 01:13:43Z alphonso $
//

// isip include files
//
#include "JSGFParser.h"
#include <Console.h>

// method: diagnose
//
// arguments:
//  Integral::DEBUG level: (input) debug level for diagnostics
//
// return: a boolean value indicating status
//
boolean JSGFParser::diagnose(Integral::DEBUG level_a) {

  //---------------------------------------------------------------------------
  //
  // 0. preliminaries
  //
  //---------------------------------------------------------------------------

  // output the class name
  //
  if (level_a > Integral::NONE) {
    SysString output(L"diagnosing class ");
    output.concat(CLASS_NAME);
    output.concat(L": ");
    Console::put(output);
    Console::increaseIndention();
  }
  
  //--------------------------------------------------------------------------
  //
  // 1. required public methods
  //
  //--------------------------------------------------------------------------

  // set indentation
  //
  if (level_a > Integral::NONE) {
    Console::put(L"testing required public methods...\n");
    Console::increaseIndention();
  }

  // test instantiation
  //
  JSGFParser jp;
  Console::put(L"instantiation passed\n");
  
  // reset indentation
  //
  if (level_a > Integral::NONE) {
    Console::decreaseIndention();
  }
  
  //---------------------------------------------------------------------------
  //
  // 2. class-specified public methods:
  //
  //---------------------------------------------------------------------------
  
  // set indentation
  //
  if (level_a > Integral::NONE) {
    Console::put(L"testing class-specific public methods...\n");
    Console::increaseIndention();
  }

  // read file
  // 
  Sof sof;
  String tmp_filename(L"www.sof");
  if(!sof.open(tmp_filename, File::READ_ONLY)) {
    return Error::handle(tmp_filename, L"open", Error::TEST,
			 __FILE__, __LINE__);
  }
  if(!sof.find(L"JSGF", 0)) {
    return Error::handle(L"JSGF", L"no matching tag", Error::TEST,
			 __FILE__, __LINE__);
  }
    
  String line;
  sof.gets(line, Sof::BUFFER_SIZE);
  sof.gets(line, Sof::BUFFER_SIZE);
  sof.gets(line, Sof::BUFFER_SIZE);
  
  while (!line.eq(L"};", true)) {     
    if(!line.eq(L"", true)) {
      jp.setExpression(line);
    }
    sof.gets(line, Sof::BUFFER_SIZE);
  }

  // close the file
  //  
  sof.close();
    
  String start(L"S");
  String term(L"T");
    
  // test parseJSGF method
  //
  if(!jp.parseJSGF(start, term)) {
    return Error::handle(name(), L"parseExpression", Error::TEST,
                         __FILE__, __LINE__);
  }
    
  // test getSymbolList method
  //
  Vector<String> symbol_list;
  symbol_list = jp.getSymbolList();

  // test getGraph method
  //
  DiGraph<String> graph;
  graph = jp.getGraph();

  // open file in write mode
  //
  Integral::makeTemp(tmp_filename);
  
  if(!sof.open(tmp_filename, File::WRITE_ONLY)) {
    return Error::handle(tmp_filename, L"open", Error::TEST, __FILE__, __LINE__);
  }

  // write the symbol list
  //
  symbol_list.write(sof, 0);

  // write the graph
  //
  graph.write(sof, 0);

  // close the file
  //  
  sof.close();

  // reset indentation
  //
  if (level_a > Integral::NONE) {
    Console::decreaseIndention();
  }
  
  //---------------------------------------------------------------------------
  //
  // 4. print completion message
  //
  //---------------------------------------------------------------------------

  // reset indentation
  //
  if (level_a > Integral::NONE) {
    Console::decreaseIndention();
  }
  
  if (level_a > Integral::NONE) {
    SysString output(L"diagnostics passed for class ");
    output.concat(name());
    output.concat(L"\n");
    Console::put(output);
  }
    
  // exit gracefully
  //
  return true;
}
