/*
 * This file is part of OpenModelica.
 *
 * Copyright (c) 1998-2026, Open Source Modelica Consortium (OSMC),
 * c/o Linköpings universitet, Department of Computer and Information Science,
 * SE-58183 Linköping, Sweden.
 *
 * All rights reserved.
 *
 * THIS PROGRAM IS PROVIDED UNDER THE TERMS OF AGPL VERSION 3 LICENSE OR
 * THIS OSMC PUBLIC LICENSE (OSMC-PL) VERSION 1.8.
 * ANY USE, REPRODUCTION OR DISTRIBUTION OF THIS PROGRAM CONSTITUTES
 * RECIPIENT'S ACCEPTANCE OF THE OSMC PUBLIC LICENSE OR THE GNU AGPL
 * VERSION 3, ACCORDING TO RECIPIENTS CHOICE.
 *
 * The OpenModelica software and the OSMC (Open Source Modelica Consortium)
 * Public License (OSMC-PL) are obtained from OSMC, either from the above
 * address, from the URLs:
 * http://www.openmodelica.org or
 * https://github.com/OpenModelica/ or
 * http://www.ida.liu.se/projects/OpenModelica,
 * and in the OpenModelica distribution.
 *
 * GNU AGPL version 3 is obtained from:
 * https://www.gnu.org/licenses/licenses.html#GPL
 *
 * This program is distributed WITHOUT ANY WARRANTY; without
 * even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE, EXCEPT AS EXPRESSLY SET FORTH
 * IN THE BY RECIPIENT SELECTED SUBSIDIARY LICENSE CONDITIONS OF OSMC-PL.
 *
 * See the full OSMC Public License conditions for more details.
 *
 */


/* Programming Environments Laboratory
*******************************************************************************
  File        : "FlatModelicaXML.cpp"
  Author      : Adrian Pop 2005-05-26
  Description : Antlr Parser caller and XML serializer for Flat Modelica
-------------------------------------------------------------------------------
  Source:
  Revision:
  Author:  Adrian Pop
  Date:    2004-05-26
*******************************************************************************
*/


//**  HEADERS ************************************************************

// standard include files
#include <iostream>
#include <sstream>
#include <cstdlib>
#include <fstream>

// ModelicaParser related include file
// the following header files are automatically generated by the antlr parser (Do not modify them)
#include "Flat_Modelica_Lexer.hpp"
#include "Flat_Modelica_Parser.hpp"
#include "Flat_Modelica_Tree_Parser.hpp"
#include "Flat_Modelica_ParserTokenTypes.hpp"
#include "Flat_Modelica_Tree_ParserTokenTypes.hpp"
#include "parse_tree_dumper.hpp"

// antlr related include files
#include "antlr/AST.hpp"
#include "antlr/CommonAST.hpp"
#include "antlr/ASTFactory.hpp"
#include "MyAST.h"


using namespace std;

//-----------------------------------------------------------------------------
// func         : main function for the modelica optimizer
// author       : Adrian Pop 2003-10-25
// organization : Link�ping University Sweden, Programming Environment Laboratory
//-----------------------------------------------------------------------------
// the function accept as argument a flattened modelica file ad will generate as output the corresponding C files
// for the Modelica Simulator
int main( int argc, char* argv[] )
{
  ifstream file;
  //ofstream wfile;

  // check if the modelica file is present in the argument list
    if (argc < 2)
  {
      cerr << "Incorrect number of arguments\n";
      return 1;
    }

  //open the file passed as an argument
    file.open(argv[1]);

  //if the file cannot be opened
    if (!file)
  {
      cerr << "Could not open file: " << argv[1] << "\n";
    getchar();
      return 2;
    }

    try
  {
    antlr::ASTFactory my_factory("MyAST", MyAST::factory);
    flat_modelica_lexer lexer(file);
    lexer.setFilename(argv[1]);
    flat_modelica_parser parser(lexer);
    parser.initializeASTFactory(my_factory);
    parser.setASTFactory(&my_factory);
    parser.stored_definition();
    //wfile.open("output.txt");
    //wfile << parser.getAST()->toStringList() << std::endl;
    antlr::RefAST ast = parser.getAST();
    //parse_tree_dumper dumper(std::cout);
    std::cout << std::flush;
    if (ast)
    {
        //dumper.dump(ast);
        flat_modelica_tree_parser walker;
      walker.initializeASTFactory(my_factory);
      walker.setASTFactory(&my_factory);
      std::string xmlFile(argv[1]);
      /*
      http://www.ida.liu.se/~adrpo/modelica/xml/modelicaxml-v2.dtd
      c:\\dev\\src\\modelicaxml\\modelicaxml-v2.dtd
      */
      // set the doctype
      std::string docType(
        (argc > 2)?argv[2]:"http://www.ida.liu.se/~adrpo/modelica/xml/modelicaxml-v2.dtd");
      xmlFile += ".xml";
      walker.stored_definition(
        RefMyAST(ast),
        xmlFile,
        argv[1],
        docType);
    }
    else
    {
      //wfile << std::endl << "Parse error: <NULL> AST\n";
      std::cerr << std::endl << "Parse error: <NULL> AST\n";
    }
  }
    catch(antlr::ANTLRException& e)
  {
    std::cerr << "Parser/Lexer/Walker Exception: " << e.toString() << std::endl;
    file.close();
    std::cerr << "ERROR! File:" << argv[1] << std::endl;
    getchar();
    return EXIT_FAILURE;
  }
    catch(std::exception& e)
  {
    std::cerr << "Exception: " << e.what() << std::endl;
    file.close();
    std::cerr << "ERROR! File:" << argv[1] << std::endl;
    getchar();
    return EXIT_FAILURE;
  }

    file.close();
  std::cout << "SUCCESS! File:" << argv[1] << std::endl;
  //wfile << std::endl << "SUCCESS! File:" << argv[1] << std::endl;
  //wfile.close();
    return EXIT_SUCCESS;
}
