///////////////////////////////////////////////////////////////////////////////
//                                                         
// Main.cc
// -------
// Dragon main module
//                                               
// Design and Implementation by Bjoern Lemke               
//     
// (C)opyright 2007-2010 by Bjoern Lemke
//
// 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; either version 2, or (at your option)
// any later version.
// 
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; see the file COPYING.  If not, write to
// the Free Software Foundation, 59 Temple Place - Suite 330,
// Boston, MA 02111-1307, USA.
//
// IMPLEMENTATION MODULE
//
// Class: main
// 
// Description: main module for dragon 
//
///////////////////////////////////////////////////////////////////////////////


// BASE INCLUDES
#include <lfc/Exception.h>
#include <lfc/Chain.h>
#include <lfc/GetOpt.h>

// DRAGON INCLUDES
#include "Dragon.h"

#define VERSION "1.3.8"

int main(int argc, char** argv)
{

    Chain parserName;
    bool dumpIt=false;
    bool javaMode=false;
    bool dynamicTable=false;
    Chain parserMode("lalr");

    GetOpt opt(argc, argv, (char*)"vhdjyp:m:");
    
    int i = opt.firstOpt();
    
    while ( i > 0 )
    {
	switch (i) 
	{
	case 'v':
	    cout << "Dragon Version " << VERSION << endl;
	    exit (0);
	case 'h':
	    cout << argv[0] << " [ -v ] [ -h ] [ -d ] [ -j ] [ -y ] [ -m <lr1|lalr> ] -p < grammar name >  " << endl;
	    exit(0);
	case 'p':
	    parserName = Chain(opt.getArg());
	    break;
	case 'm':
	    parserMode = Chain(opt.getArg());
	    break;
	case 'j':
	    javaMode=true;
	    break;
	case 'd':
	    dumpIt=true;
	    break;
	case 'y':
	    dynamicTable= true;
	    break;
	}
	
	i = opt.nextOpt();
	
    }
    if ( parserName.length() == 0 )
    {
	cerr << "No grammar specified" << endl;
	exit(1);
    }

    Dragon::ParserMode mode=Dragon::LR1;

    if ( parserMode == Chain("lalr"))
    {
	mode=Dragon::LALR;
    }
    else if ( parserMode == Chain("lr1"))
    {
	mode=Dragon::LR1;
    }
    else
    {
	cerr << "Invalid parser analysis mode, must be <lr1> or <lalr>" << endl;
	exit(1);	
    }
   
    try {
	
	Dragon parserBuilder(parserName, javaMode, mode, dynamicTable);
	parserBuilder.generate(dumpIt);
    }
    catch (Exception e)
    {
	Chain msg;
	e.pop(msg);
	cerr << msg << endl;
	exit(1);
    }

    exit(0);

}

