Orocos Real-Time Toolkit  2.5.0
ProgramGraphParser.hpp
00001 /***************************************************************************
00002   tag: Peter Soetens  Mon Jan 19 14:11:26 CET 2004  ProgramGraphParser.hpp
00003 
00004                         ProgramGraphParser.hpp -  description
00005                            -------------------
00006     begin                : Mon January 19 2004
00007     copyright            : (C) 2004 Peter Soetens
00008     email                : peter.soetens@mech.kuleuven.ac.be
00009 
00010  ***************************************************************************
00011  *   This library is free software; you can redistribute it and/or         *
00012  *   modify it under the terms of the GNU General Public                   *
00013  *   License as published by the Free Software Foundation;                 *
00014  *   version 2 of the License.                                             *
00015  *                                                                         *
00016  *   As a special exception, you may use this file as part of a free       *
00017  *   software library without restriction.  Specifically, if other files   *
00018  *   instantiate templates or use macros or inline functions from this     *
00019  *   file, or you compile this file and link it with other files to        *
00020  *   produce an executable, this file does not by itself cause the         *
00021  *   resulting executable to be covered by the GNU General Public          *
00022  *   License.  This exception does not however invalidate any other        *
00023  *   reasons why the executable file might be covered by the GNU General   *
00024  *   Public License.                                                       *
00025  *                                                                         *
00026  *   This library is distributed in the hope that it will be useful,       *
00027  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00028  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU     *
00029  *   Lesser General Public License for more details.                       *
00030  *                                                                         *
00031  *   You should have received a copy of the GNU General Public             *
00032  *   License along with this library; if not, write to the Free Software   *
00033  *   Foundation, Inc., 59 Temple Place,                                    *
00034  *   Suite 330, Boston, MA  02111-1307  USA                                *
00035  *                                                                         *
00036  ***************************************************************************/
00037 
00038 #ifndef EXECUTION_PROGRAM_PARSER_HPP
00039 #define EXECUTION_PROGRAM_PARSER_HPP
00040 
00041 #include "parser-types.hpp"
00042 
00043 #include "CommonParser.hpp"
00044 #include "PeerParser.hpp"
00045 #include "ConditionParser.hpp"
00046 #include "ExpressionParser.hpp"
00047 #include "../TaskContext.hpp"
00048 #include "FunctionGraphBuilder.hpp"
00049 #include "ValueChangeParser.hpp"
00050 
00051 #include <map>
00052 #include <vector>
00053 #include <string>
00054 
00055 
00056 #include "ProgramService.hpp"
00057 //namespace RTT {
00058 //    class ProgramService;
00059 //}
00060 
00061 namespace RTT { namespace scripting
00062 {
00072   class ProgramGraphParser
00073   {
00074       typedef FunctionGraphBuilder::CommandNode CommandNode;
00075       typedef FunctionGraphBuilder::ConditionEdge ConditionEdge;
00076       typedef FunctionGraphBuilder::Graph  Graph;
00077 
00082       TaskContext* rootc;
00083 
00087       Service::shared_ptr context;
00088 
00092       TaskContext* fcontext;
00093 
00097       TaskContext* peer;
00098 
00099       our_pos_iter_t& mpositer;
00100 
00101       // The current function we are parsing.
00102       FunctionGraphPtr mfunc;
00103 
00104       // The function we will call next
00105       FunctionGraphPtr mcallfunc;
00106 
00107       // A map of all functions
00108       typedef std::map<std::string, FunctionGraphPtr> funcmap;
00109       funcmap mfuncs;
00110 
00111       // The return type of the current function.
00112       std::string rettype;
00113 
00114       // The implicit termination condition we just got for the
00115       // current command from the CommandParser..
00116       ConditionInterface* implcond;
00117       std::vector<ConditionInterface*> implcond_v;
00118       std::vector<base::DataSourceBase::shared_ptr> callfnargs;
00119 
00120       // last seen condition
00121       ConditionInterface* mcondition;
00122       // try-block condition
00123       ConditionInterface* try_cond;
00124 
00125       void seencondition();
00126 
00127       void seencallfunclabel( iter_t begin, iter_t end );
00128 
00129       void seencontinue( );
00130 
00131       void skip_eol();
00132       void noskip_eol();
00133       void seenyield();
00134       void seenstatement();
00135       void seentrystatement();
00136       void startcatchpart();
00137       void seencatchpart();
00138 
00139       void seenvaluechange();
00140 
00141       void functiondef( iter_t begin, iter_t end );
00142       void exportdef(  );
00143       void globaldef(  );
00144       void seenfunctionarg();
00145       void seenfunctionend();
00146 
00147       void seenfuncidentifier( iter_t begin, iter_t end);
00148       void seencallfuncstatement();
00149       void seencallfuncargs();
00150 
00151       void seenreturnstatement();
00152       void seenreturnlabel();
00153       void seenreturnvalue();
00154       void seenreturntype( iter_t begin, iter_t end );
00155 
00156       void seenifstatement();
00157       void endifblock();
00158       void endifstatement();
00159 
00160       void seenwhilestatement();
00161       void endwhilestatement();
00162 
00163       void seenbreakstatement();
00164 
00165       void seenforstatement();
00166       void seenforinit();
00167       void seenforinit_expr();
00168       void seenforincr();
00169       void seenemptyforincr();
00170       void endforstatement();
00171 
00172       void startofprogram();
00173       void programdef( iter_t begin, iter_t end );
00174       void seenprogramend();
00175       void programtext(iter_t, iter_t);
00176 
00177       void setStack(Service::shared_ptr st);
00179       void clearParseState();
00180       void setup();
00181       void setup2();
00182       void cleanup(bool remove_service);
00183 
00184       rule_t newline, terminationclause, jumpdestination, terminationpart, andpart,
00185           dostatement, trystatement, statement, line, content, program,
00186           production, valuechange, returnstatement, function, functions, arguments, funcstatement,
00187           continuepart, returnpart, callpart, ifstatement, ifblock, whilestatement, breakstatement,
00188           openbrace, closebrace, opencurly, closecurly, forstatement, semicolon,
00189           condition, catchpart, funcargs, functionarg, emitstatement ;
00190 
00191       CommonParser& commonparser;
00192       ConditionParser conditionparser;
00193       ValueChangeParser valuechangeparser;
00194       ExpressionParser expressionparser;
00195       ArgumentsParser* argsparser;
00196       PeerParser peerparser;
00197 
00198       boost::shared_ptr<FunctionGraphBuilder> program_builder;
00199       std::vector< FunctionGraphPtr > program_list;
00200 
00201       base::ActionInterface* for_init_command;
00202       std::stack<base::ActionInterface*> for_incr_command;
00203       std::string program_text;
00204       bool exportf,globalf;
00205       int ln_offset;
00206   public:
00207       ProgramGraphParser( iter_t& positer, TaskContext* context, ExecutionEngine* caller, CommonParser& cp);
00208       ~ProgramGraphParser();
00209 
00214       std::vector<ProgramInterfacePtr> parse( iter_t& begin, iter_t end );
00215 
00216       std::vector<ProgramInterfacePtr> parseFunction( iter_t& begin, iter_t end );
00217 
00222       void initBodyParser(const std::string& name, Service::shared_ptr stck, int offset);
00223 
00229       rule_t& bodyParser();
00230 
00234       rule_t& statementParser();
00235 
00239       rule_t& programParser();
00240 
00244       ProgramInterfacePtr programParserResult();
00248       rule_t& functionParser();
00249 
00250       ProgramInterfacePtr bodyParserResult();
00251 
00252   };
00253 }}
00254 
00255 #endif