/* "CodeWorker":	a scripting language for parsing and generating text.

Copyright (C) 1996-1997, 1999-2004 Cédric Lemaire

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library 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
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

To contact the author: codeworker@free.fr
*/

#ifdef WIN32
#pragma warning(disable : 4786)
#endif

#include "ScpStream.h"
#include "DtaScript.h"
#include "DtaPatternScript.h"
#include "DtaBNFScript.h"
#include "DtaTranslateScript.h"

#include "CGCompiler.h"

namespace CodeWorker {
	CGCompiledCommonScript::~CGCompiledCommonScript() {
		delete _pScript;
	}

	void CGCompiledCommonScript::execute(DtaScriptVariable* pContext) {
		_pScript->execute(*pContext);
	}
	void CGCompiledCommonScript::execute(CppParsingTree_var pContext) {
		execute(pContext._pInternalNode);
	}


	CGCompiledTemplateScript::~CGCompiledTemplateScript() {
		delete _pScript;
	}

	void CGCompiledTemplateScript::generate(DtaScriptVariable* pContext, const std::string& sOutputFile) {
		_pScript->generate(sOutputFile.c_str(), *pContext);
	}
	void CGCompiledTemplateScript::generate(CppParsingTree_var pContext, const std::string& sOutputFile) { generate(pContext._pInternalNode, sOutputFile); }

	void CGCompiledTemplateScript::generateString(DtaScriptVariable* pContext, std::string& sOutputText) {
		_pScript->generateString(sOutputText, *pContext);
	}
	void CGCompiledTemplateScript::generateString(CppParsingTree_var pContext, std::string& sOutputText) { generateString(pContext._pInternalNode, sOutputText); }

	void CGCompiledTemplateScript::expand(DtaScriptVariable* pContext, const std::string& sOutputFile) {
		_pScript->expand(sOutputFile.c_str(), *pContext);
	}
	void CGCompiledTemplateScript::expand(CppParsingTree_var pContext, const std::string& sOutputFile) { expand(pContext._pInternalNode, sOutputFile); }


	CGCompiledBNFScript::~CGCompiledBNFScript() {
		delete _pScript;
	}

	void CGCompiledBNFScript::parse(DtaScriptVariable* pContext, const std::string& sParsedFile) {
		_pScript->generate(sParsedFile.c_str(), *pContext);
	}
	void CGCompiledBNFScript::parse(CppParsingTree_var pContext, const std::string& sParsedFile) { parse(pContext._pInternalNode, sParsedFile); }

	void CGCompiledBNFScript::parseString(DtaScriptVariable* pContext, const std::string& sText) {
		std::string sParsedText = sText;
		_pScript->generateString(sParsedText, *pContext);
	}
	void CGCompiledBNFScript::parseString(CppParsingTree_var pContext, const std::string& sText) { parseString(pContext._pInternalNode, sText); }


	CGCompiledTranslationScript::~CGCompiledTranslationScript() {
		delete _pScript;
	}

	void CGCompiledTranslationScript::translate(DtaScriptVariable* pContext, const std::string& sParsedFile, const std::string& sOutputFile) {
		_pScript->translate(sParsedFile, sOutputFile, *pContext);
	}
	void CGCompiledTranslationScript::translate(CppParsingTree_var pContext, const std::string& sParsedFile, const std::string& sOutputFile) { translate(pContext._pInternalNode, sParsedFile, sOutputFile); }

	void CGCompiledTranslationScript::translateString(DtaScriptVariable* pContext, const std::string& sInputText, std::string& sOutputText) {
		_pScript->translateString(sInputText, sOutputText, *pContext);
	}
	void CGCompiledTranslationScript::translateString(CppParsingTree_var pContext, const std::string& sInputText, std::string& sOutputText) { translateString(pContext._pInternalNode, sInputText, sOutputText); }


	CGCompiledCommonScript* CGCompiler::compileCommonScript(const std::string& sScriptFile) {
		std::auto_ptr<DtaScript> pScript(new DtaScript);
		pScript->parseFile(sScriptFile.c_str());
		return new CGCompiledCommonScript(pScript.release());
	}

	CGCompiledCommonScript* CGCompiler::compileCommonStringScript(const std::string& sStringScript) {
		std::auto_ptr<DtaScript> pScript(new DtaScript);
		ScpStream theCommand(sStringScript);
		pScript->parseStream(theCommand);
		return new CGCompiledCommonScript(pScript.release());
	}

	CGCompiledTemplateScript* CGCompiler::compileTemplateScript(const std::string& sScriptFile) {
		std::auto_ptr<DtaPatternScript> pScript(new DtaPatternScript);
		pScript->parseFile(sScriptFile.c_str());
		return new CGCompiledTemplateScript(pScript.release());
	}

	CGCompiledBNFScript* CGCompiler::compileBNFScript(const std::string& sScriptFile) {
		std::auto_ptr<DtaBNFScript> pScript(new DtaBNFScript);
		pScript->parseFile(sScriptFile.c_str());
		return new CGCompiledBNFScript(pScript.release());
	}

	CGCompiledTranslationScript* CGCompiler::compileTranslationScript(const std::string& sScriptFile) {
		std::auto_ptr<DtaTranslateScript> pScript(new DtaTranslateScript);
		pScript->parseFile(sScriptFile.c_str());
		return new CGCompiledTranslationScript(pScript.release());
	}
}
