📄 parserscriptingaccess.cpp
字号:
/*************************************************************************** tag: Peter Soetens Mon Jun 26 13:25:57 CEST 2006 ParserScriptingAccess.cxx ParserScriptingAccess.cxx - description ------------------- begin : Mon June 26 2006 copyright : (C) 2006 Peter Soetens email : peter.soetens@fmtc.be *************************************************************************** * This library 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 of the License. * * * * As a special exception, you may use this file as part of a free * * software library without restriction. Specifically, if other files * * instantiate templates or use macros or inline functions from this * * file, or you compile this file and link it with other files to * * produce an executable, this file does not by itself cause the * * resulting executable to be covered by the GNU General Public * * License. This exception does not however invalidate any other * * reasons why the executable file might be covered by the GNU General * * Public License. * * * * 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 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 * * * ***************************************************************************/ #include "ParserScriptingAccess.hpp"#include "StatementProcessor.hpp"#include "Parser.hpp"#include "Logger.hpp"#include "TaskContext.hpp"#include "Method.hpp"#include "Command.hpp"#include "ProgramProcessor.hpp"#include "StateMachineProcessor.hpp"#include <sstream>#include <fstream>namespace RTT{ using namespace std; ParserScriptingAccess::ParserScriptingAccess( TaskContext* parent ) : ScriptingAccess(parent), sproc(0) { OperationInterface* obj = parent->getObject("scripting"); obj = this->createTaskObject( obj ); parent->addObject( obj ); } bool ParserScriptingAccess::doExecute(const std::string& code) { return this->execute(code) >= 0; } bool ParserScriptingAccess::doLoadPrograms( std::string filename ) { return this->loadPrograms(filename, false); } bool ParserScriptingAccess::doLoadProgramText( std::string code ) { return this->loadPrograms(code, "string", false); } bool ParserScriptingAccess::doUnloadProgram( std::string name ) { return this->unloadProgram(name, false); } bool ParserScriptingAccess::doLoadStateMachines( std::string filename ) { return this->loadStateMachines(filename, false); } bool ParserScriptingAccess::doLoadStateMachineText( std::string code ) { return this->loadStateMachines(code, "string", false); } bool ParserScriptingAccess::doUnloadStateMachine( std::string name ) { return this->unloadStateMachine(name, false); } OperationInterface* ParserScriptingAccess::createTaskObject(OperationInterface* obj) { if ( !obj ) obj = new TaskObject("scripting","Access to the Scripting interface. \Use this object in order to load or query programs or state machines."); obj->methods()->addMethod( method( "execute", &ParserScriptingAccess::execute, this), "Execute a line of code.", "Code", "A single statement."); // Methods for loading programs obj->methods()->addMethod( method( "loadPrograms", &ParserScriptingAccess::doLoadPrograms, this), "Load a program from a given file.", "Filename", "The filename of the script." ); obj->methods()->addMethod( method( "loadProgramText", &ParserScriptingAccess::doLoadProgramText, this), "Load a program from a string.", "Code", "A string containing one or more program scripts." ); obj->methods()->addMethod( method( "unloadProgram", &ParserScriptingAccess::doUnloadProgram, this), "Remove a loaded program.", "Name", "The name of the loaded Program" ); // Query Methods for programs obj->methods()->addMethod( method( "getProgramStatus", &ParserScriptingAccess::getProgramStatus, this), "Get the status of a program?", "Name", "The Name of the loaded Program" ); obj->methods()->addMethod( method( "getProgramLine", &ParserScriptingAccess::getProgramLine, this), "Get the current line of execution of a program?", "Name", "The Name of the loaded Program" ); // Methods for loading state machines obj->methods()->addMethod( method( "loadStateMachines", &ParserScriptingAccess::doLoadStateMachines, this), "Load a state machine from a given file.", "Filename", "The filename of the script." ); obj->methods()->addMethod( method( "loadStateMachineText", &ParserScriptingAccess::doLoadStateMachineText, this), "Load a state machine from a string.", "Code", "A string containing one or more state machine scripts." ); obj->methods()->addMethod( method( "unloadStateMachine", &ParserScriptingAccess::doUnloadStateMachine, this), "Remove a loaded state machine.", "Name", "The name of the loaded State Machine" ); // Query Methods for state machines obj->methods()->addMethod( method( "getStateMachineStatus", &ParserScriptingAccess::getStateMachineStatus, this), "Get the status of a state machine?", "Name", "The Name of the loaded State Machine" ); obj->methods()->addMethod( method( "getStateMachineLine", &ParserScriptingAccess::getStateMachineLine, this), "Get the current line of execution of a state machine?", "Name", "The Name of the loaded State Machine" ); return obj; } ParserScriptingAccess::~ParserScriptingAccess() { delete sproc; } int ParserScriptingAccess::execute(const std::string& code ){ if (sproc == 0) sproc = new StatementProcessor(mparent); return sproc->execute( code ); } DispatchInterface::shared_ptr ParserScriptingAccess::getCommand( int ticket ){ if (sproc) return sproc->getCommand(ticket); return DispatchInterface::shared_ptr(); } ParserScriptingAccess::Functions ParserScriptingAccess::loadFunctions( std::string file, bool do_throw/* = false*/ ) { std::ifstream inputfile(file.c_str()); if ( !inputfile ) { Logger::In in("ParserScriptingAccess::loadFunctions"); Logger::log() << Logger::Error << "Script "+file+" does not exist." << Logger::endl; return Functions(); } std::string text; inputfile.unsetf( std::ios_base::skipws ); std::istream_iterator<char> streambegin( inputfile ); std::istream_iterator<char> streamend; std::copy( streambegin, streamend, std::back_inserter( text ) ); return this->loadFunctions( text, file, do_throw ); } ParserScriptingAccess::Functions ParserScriptingAccess::loadFunctions( string code, string filename, bool mrethrow ) { Logger::In in("ParserScriptingAccess::loadFunctions"); Parser p; Functions exec; Functions ret; try { Logger::log() << Logger::Info << "Parsing file "<<filename << Logger::endl; ret = p.parseFunction(code, mparent, filename); } catch( const file_parse_exception& exc ) {#ifndef ORO_EMBEDDED Logger::log() << Logger::Error << filename<<" :"<< exc.what() << Logger::endl; if ( mrethrow ) throw;#endif return Functions(); } if ( ret.empty() ) { Logger::log() << Logger::Debug << "No Functions executed from "<< filename << Logger::endl;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -