/*******************************************************************************
 * Copyright (c) 2015-2016 ONERA.
 * Ce logiciel est la propriété de l’ONERA (the French Aerospace Lab).
 * Tous droits réservés.
 *
 * Ce programme et les éléments qui l'accompagnent sont mis à disposition
 * aux conditions définies par le contrat de licence logiciel CeCILL-C soumise
 * au droit français et respectant les principes de diffusion des logiciels libres.
 * Vous pouvez utiliser, modifier et/ou redistribuer ce programme
 * sous les conditions de la licence CeCILL-C (http://www.cecill.info).

 * This software is property of ONERA (the French Aerospace Lab).
 * All rights reserved.
 *
 * This program and the accompanying materials are made available under
 * the terms of the CeCILL-C license under French law and
 * abiding by the rules of distribution of free software.
 * You can  use, modify and/ or redistribute the software under the terms of
 * the CeCILL-C license (http://www.cecill.info).
 *
 * Contributeurs/contributors:
 *      Florent Teichteil-Königsbuch (ONERA - Centre de Toulouse) - initial API and implementation
 *
 *******************************************************************************/

#ifndef PARSING_DRIVER_IMPL_HH
#define PARSING_DRIVER_IMPL_HH

#include <sstream>
#include <clocale>
#include <stdio.h>

namespace epoch {
	template <class TParser>
	void ParsingDriverBase::error(const typename TParser::location_type& loc, const std::string& msg) {
		std::ostringstream oss;
		oss << "ERROR: " << loc << ": " << msg << std::endl;
		m_rReporter.Append(oss.str());
	}
	
	
	template <class Tdriver>
	int ParsingDriverBase::ParseFile(Tdriver& driver) {
		FILE* parsedFile = fopen(m_ParsedRessource.c_str(), "r");
		
		if (!parsedFile) {
			m_rReporter.Append(std::string("Cannot open file ") + m_ParsedRessource);
			return -1;
		}
		
		setlocale(LC_ALL, "C");

		yyscan_t scanner;
		yylex_init(&scanner);
		yyset_debug(m_TraceScanning, scanner);
		yyrestart(parsedFile, scanner);
		typename Tdriver::Parser parser(driver, scanner);
		parser.set_debug_level(m_TraceParsing);
		int res = parser.parse();
		yylex_destroy(scanner);
		fclose(parsedFile);
		setlocale(LC_ALL, "");
		return res;
	}

	template <class Tdriver>
	int ParsingDriverBase::ParseString(Tdriver& driver) {
		std::string stringToParse = m_ParsedRessource;
		stringToParse += "\0\0";

		yyscan_t scanner;
		yylex_init(&scanner);
		yyset_debug(m_TraceScanning, scanner);
		YY_BUFFER_STATE parsedString = yy_scan_string(stringToParse.c_str(), scanner);
		
		if (!parsedString) {
			m_rReporter.Append(std::string("Cannot open string ") + m_ParsedRessource);
			return -1;
		}
		
		setlocale(LC_ALL, "C");

		typename Tdriver::Parser parser(driver, scanner);
		parser.set_debug_level(m_TraceParsing);
		int res = parser.parse();
		yylex_destroy(scanner);
		setlocale(LC_ALL, "");
		return res;
	}
	
	template <class Trule>
	bool ParsingDriverBase::DeleteRuleHelper(boost::any& rule) {
		Trule** prule = boost::any_cast<Trule*>(&rule);
		if (prule != NULL) {
			delete *prule;
			return true;
		} else
			return false;
	}
} // namespace epoch

#endif // PARSING_DRIVER_IMPL_HH
