/*
 * --- GSMP-COPYRIGHT-NOTE-BEGIN ---
 * 
 * This copyright note is auto-generated by ./scripts/Create-CopyPatch.
 * Please add additional copyright information _after_ the line containing
 * the GSMP-COPYRIGHT-NOTE-END tag. Otherwise it might get removed by
 * the ./scripts/Create-CopyPatch script. Do not edit this copyright text!
 * 
 * GSMP: dam/utility/spec2cc/spec2cc.cc
 * General Sound Manipulation Program is Copyright (C) 2000 - 2004
 *   Valentin Ziegler and René Rebe
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 2. A copy of the GNU General
 * Public License can be found in the file LICENSE.
 * 
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANT-
 * ABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
 * Public License for more details.
 * 
 * --- GSMP-COPYRIGHT-NOTE-END ---
 */
#include <iostream>
#include <stdio.h>

#include <TokenHack.h>

#include <vector>

using namespace std;

extern "C" {
  extern FILE* yyin;
  extern int line_num;
  extern char* id;

  char* filename = "<stdin>";

  int yylex ();
  //  int yyparse ();

  int yyerror (char* msg)
  {
    cerr << line_num << " : " << msg << endl;
    exit (-1);
  }

  int yywrap ()
  {
    return 1;
  }

#include "ScannerUtils.h"
}

int token;

string namesep = "";

#define n_endl endl << namesep

bool Lex ()
{
  token = yylex ();
  //  cout << token << endl;
  return token != 0;
}

string GetId ()
{
  if (!Lex () || token != ID)
    yyerror ("expected identifier");
  return string (id);
}

string GetBlock ()
{
  if (!Lex () || token != C_BLOCK)
    yyerror ("expected identifier");
  return TextGet ();
}

void Lex (char t)
{
  if (!Lex () || token != ((int) t))
    {
      char msg [32];
      msg[0] = t; 
      sprintf (msg+1, " expected.");
      yyerror (msg); 
    }
}

string prelude = "";
string AutoT;
string NotT;
string QueryT;

struct state
{
  string name;
  bool av;
};

struct query
{
  string name;
};

struct notification
{
  string name;
};


vector <state> States;
int num_states;

vector <notification> Notifs;
int num_notifs;

vector <query> Queries;
int num_queries;

int default_state;

void ReadAutomaton ()
{
  num_states = 0;
  num_notifs = 0;
  num_queries = 0;
  default_state = -1;

  bool av = false;

  state n_state;
  query n_query;
  notification n_not;

  AutoT = GetId ();
  NotT = GetBlock ();
  QueryT = GetBlock ();

  Lex ('{');
  while (token != '}' && Lex ())
    {
      switch (token)
	{
	case '+':
	  av = true;
	  break;
	case '-':
	  av = false;
	  break;
	case '$':
	  if (default_state != -1)
	    yyerror ("cannot have two default states");
	  default_state = num_states;
	  break;
	case ID:
	  n_state.name = id;
	  n_state.av = av; 
	  if (num_states < States.size ())
	    States[num_states] = n_state;
	  else
	    States.push_back (n_state);
	  num_states++;
	  break;
	case '}':
	  break;
	default:
	  yyerror ("illegal Token in state list");
	}
    }
  if (token != '}')
    yyerror ("file ends in state list");

  Lex ('{');
  while (token != '}' && Lex ())
    {
      switch (token)
	{
	case ID:
	  n_not.name = id;
	  if (num_notifs < Notifs.size ())
	    Notifs[num_notifs] = n_not;
	  else
	    Notifs.push_back (n_not);
	  num_notifs++;
	  break;
	case '}':
	  break;
	default:
	  yyerror ("illegal Token in notification list");
	}
    }
  if (token != '}')
    yyerror ("file ends in notification list");

  Lex ('{');
  while (token != '}' && Lex ())
    {
      switch (token)
	{
	case ID:
	  n_query.name = id;
	  if (num_queries < Queries.size ())
	    Queries[num_queries] = n_query;
	  else
	    Queries.push_back (n_query);
	  num_queries++;
	  break;
	case '}':
	  break;
	default:
	  yyerror ("illegal Token in query list");
	}
    }
  if (token != '}')
    yyerror ("file ends in query list");

  if (num_states > 0)
    {
      cout << n_endl;
      cout << "const DAM::State " << AutoT << "_StateList [" << num_states << "] = {" <<n_endl;
      for (int i = 0; i < num_states; i++)
	{
	  cout << "  {\"" << States[i].name << "\", " << States[i].av << "}";
	  if (i < num_states - 1)
	    cout << ",";
	  cout << n_endl;
	}
      cout << "};" << n_endl;
    }

  if (num_notifs > 0)
    {
      cout << n_endl;
      cout << "const DAM::Notification " << AutoT << "_NotificationList [" << num_notifs << "] = {" <<n_endl;
      for (int i = 0; i < num_notifs; i++)
	{
	  cout << "  {\"" << Notifs[i].name << "\"}";
	  if (i < num_notifs - 1)
	    cout << ",";
	  cout << n_endl;
	}
      cout << "};" << n_endl;
    }

  if (num_queries > 0)
    {
      cout << n_endl;
      cout << "const DAM::Query " << AutoT << "_QueryList [" << num_queries << "] = {" <<n_endl;
      for (int i = 0; i < num_queries; i++)
	{
	  cout << "  {\"" << Queries[i].name << "\"}";
	  if (i < num_queries - 1)
	    cout << ",";
	  cout << n_endl;
	}
      cout << "};" << n_endl;
    }


  cout << n_endl;
  cout << "class " << AutoT << "_States" << n_endl;
  cout << "{" << n_endl;
  cout << "public:" << n_endl;
  cout << "  typedef class " << AutoT << " Master;" << n_endl;
  cout << "  typedef class " << NotT << " NotificationData;" << n_endl;
  cout << "  typedef class " << QueryT << " QueryData;" << n_endl << n_endl;
  cout << "  enum state_id {";
  for (int i = 0; i < num_states; i++)
    {
      if (i > 0)
	cout << ", ";
      cout << States[i].name;
    }
  cout << "};" << n_endl;
  cout << "  enum notif_id {";
  for (int i = 0; i < num_notifs; i++)
    {
      if (i > 0)
	cout << ", ";
      cout << Notifs[i].name;
    }
  cout << "};" << n_endl;
  cout << "  enum query_id {";
  for (int i = 0; i < num_queries; i++)
    {
      if (i > 0)
	cout << ", ";
      cout << Queries[i].name;
    }
  cout << "};" << endl << n_endl;

  cout << "  static const state_id start = " << States[default_state].name << ";" << n_endl;
  cout << "  static const int num_states = " << num_states << ";" << n_endl;
  cout << "  static const int num_notifs = " << num_notifs << ";" << n_endl;
  cout << "  static const int num_queries = " << num_queries << ";" << endl << n_endl;

  cout << "  static const DAM::State& FullState (state_id id) {";
  if (num_states > 0)
    cout << "return " << AutoT << "_StateList [id];";
  cout << " }" << n_endl;
  cout << "  static const DAM::Notification& FullNotification (notif_id id) {";
  if (num_notifs > 0)
    cout << "return " << AutoT << "_NotificationList [id];";
  cout << " }" << n_endl;
  cout << "  static const DAM::Query& FullQuery (query_id id) {";
  if (num_queries > 0)
    cout << "return " << AutoT << "_QueryList [id];";
  cout << " }" << n_endl;

  cout << "};" << n_endl;
}

void AppendPrelude ()
{
  GetBlock ();
  cout << TextGet ();
};

int namespaces = 0;

void UpdateNEndl ()
{
  namesep = "";
  for (int i = 0; i < namespaces; i++)
    namesep += "  ";
}

void SetNameSpace ()
{
  GetBlock ();
  cout << "namespace " << TextGet () << n_endl << "{" << n_endl;
  namespaces ++;
  UpdateNEndl ();
}

void EndNameSpace ()
{
  namespaces --;
  UpdateNEndl ();
  cout << n_endl << "} // end namespace" << n_endl;  
}

main ()
{
  cout << "// this is generated code, so do not edit !" << endl;

  while (Lex ())
  {
    switch (token)
      {
      case AUTOMATON:
	ReadAutomaton ();
	break;
      case PRELUDE:
	AppendPrelude ();
	break;
      case NAMESPACE:
	SetNameSpace ();
	break;
      default:
	yyerror ("expected \"automaton\" or \"prelude\" or \"namespace\"");
      }
  }
  while (namespaces > 0)
    EndNameSpace ();
}


