Parser.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  tag: Peter Soetens Mon May 10 19:10:37 CEST 2004 Parser.cxx
3 
4  Parser.cxx - description
5  -------------------
6  begin : Mon May 10 2004
7  copyright : (C) 2004 Peter Soetens
8  email : peter.soetens@mech.kuleuven.ac.be
9 
10  ***************************************************************************
11  * This library is free software; you can redistribute it and/or *
12  * modify it under the terms of the GNU General Public *
13  * License as published by the Free Software Foundation; *
14  * version 2 of the License. *
15  * *
16  * As a special exception, you may use this file as part of a free *
17  * software library without restriction. Specifically, if other files *
18  * instantiate templates or use macros or inline functions from this *
19  * file, or you compile this file and link it with other files to *
20  * produce an executable, this file does not by itself cause the *
21  * resulting executable to be covered by the GNU General Public *
22  * License. This exception does not however invalidate any other *
23  * reasons why the executable file might be covered by the GNU General *
24  * Public License. *
25  * *
26  * This library is distributed in the hope that it will be useful, *
27  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
28  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
29  * General Public License for more details. *
30  * *
31  * You should have received a copy of the GNU General Public *
32  * License along with this library; if not, write to the Free Software *
33  * Foundation, Inc., 59 Temple Place, *
34  * Suite 330, Boston, MA 02111-1307 USA *
35  * *
36  ***************************************************************************/
37 
38 #include "parser-debug.hpp"
39 #include "parser-types.hpp"
40 #include "parse_exception.hpp"
41 #include "Parser.hpp"
42 #include "ProgramGraphParser.hpp"
43 #include "StateGraphParser.hpp"
44 #include "ConditionParser.hpp"
45 #include "ExpressionParser.hpp"
46 #include "ValueChangeParser.hpp"
47 #include "ProgramService.hpp"
48 #include "StateMachineService.hpp"
49 #include "../internal/DataSourceCommand.hpp"
50 #include "ConditionInterface.hpp"
51 #include "CommandComposite.hpp"
52 #include "../internal/GlobalEngine.hpp"
53 #include "ScriptParser.hpp"
54 
55 #include <iostream>
56 #include <fstream>
57 
58 using namespace boost;
59 
60 namespace RTT
61 {
62  using namespace detail;
63 
64  Parser::Parser(ExecutionEngine* caller) : mcaller(caller) {
65 
66  if (mcaller == 0) {
67  log(Debug) << "WARNING: Parser does not know which TaskContext is executing (calling) the parsed code. Using Global Engine. Please specify the caller explicitly in order to avoid any asynchronous operation problems." <<endlog();
69  }
70  }
71 
72  void Parser::runScript(std::string const& code, TaskContext* mowner, ScriptingService* service, std::string const& filename ) {
73  our_buffer_t script(code + "\n"); // work around mandatory trailing newline/eos for statements.
74  our_pos_iter_t parsebegin( script.begin(), script.end(), filename );
75  our_pos_iter_t parseend( script.end(), script.end(), filename );
76 
77  // will store all in mowner, functions are loaded in service, caller is the one calling us.
78  ScriptParser gram( parsebegin, mowner, mcaller );
79 
80  try {
81  gram.parse( parsebegin, parseend );
82  }
83  catch( const parse_exception& exc )
84  {
86  exc.copy(), parsebegin.get_position().file,
87  parsebegin.get_position().line, parsebegin.get_position().column );
88  }
89  }
90 
91  Parser::ParsedFunctions Parser::parseFunction( const std::string& text, TaskContext* c, const std::string& filename)
92  {
93  our_buffer_t function(text);
94  our_pos_iter_t parsebegin( function.begin(), function.end(), filename );
95  our_pos_iter_t parseend( function.end(), function.end(), filename );
96  // The internal parser.
97  CommonParser cp;
98  ProgramGraphParser gram( parsebegin, c, c->engine(), cp );
99  ParsedFunctions ret = gram.parseFunction( parsebegin, parseend );
100  return ret;
101  }
102 
103  Parser::ParsedPrograms Parser::parseProgram( const std::string& text, TaskContext* c, const std::string& filename)
104  {
105  our_buffer_t program(text);
106  our_pos_iter_t parsebegin( program.begin(), program.end(), filename );
107  our_pos_iter_t parseend( program.end(),program.end(),filename );
108 
109  // The internal parser.
110  CommonParser cp;
111  ProgramGraphParser gram( parsebegin, c, c->engine(), cp );
112  ParsedPrograms ret = gram.parse( parsebegin, parseend );
113 
114  return ret;
115  }
116 
117  Parser::ParsedStateMachines Parser::parseStateMachine( const std::string& text, TaskContext* c, const std::string& filename)
118  {
119  // This code is copied from parseProgram()
120 
121  our_buffer_t program(text);
122  our_pos_iter_t parsebegin( program.begin(), program.end(), filename );
123  our_pos_iter_t parseend( program.end(),program.end(),filename );
124 
125  // The internal parser.
126  CommonParser cp;
127  StateGraphParser gram( parsebegin, c, c->engine(), &cp );
129  try {
130  ret = gram.parse( parsebegin, parseend );
131  }
132  catch( const parse_exception& exc )
133  {
134  throw file_parse_exception(
135  exc.copy(), parsebegin.get_position().file,
136  parsebegin.get_position().line, parsebegin.get_position().column );
137  }
138  return ret;
139  }
140 
142  TaskContext* tc )
143  {
144  our_buffer_t scopy(s);
145  our_pos_iter_t parsebegin( scopy.begin(), scopy.end(), "teststring" );
146  our_pos_iter_t parseend( scopy.end(), scopy.end(), "teststring" );
147 
148  CommonParser cp;
149  ConditionParser parser( tc, mcaller ? mcaller : tc->engine(), cp );
150  bool skipref=true;
151  try
152  {
153  parse( parsebegin, parseend, parser.parser(), SKIP_PARSER );
154  }
155  catch( const parse_exception& )
156  {
157  throw;
158  }
159  catch( const parser_error<std::string, iter_t>& e )
160  {
161  throw parse_exception_syntactic_error( e.descriptor );
162  }
163  ConditionInterface* ret = parser.getParseResult();
164  parser.reset();
165  if ( ret == 0 )
166  throw parse_exception_parser_fail("Parser did not find a condition in text.");
167  return ret;
168  }
169 
171  TaskContext* tc )
172  {
173  std::string s( _s );
174 
175  our_pos_iter_t parsebegin( s.begin(), s.end(), "teststring" );
176  our_pos_iter_t parseend( s.end(), s.end(), "teststring" );
177 
178  CommonParser cp;
179  ExpressionParser parser( tc, mcaller, cp );
180  bool skipref=true;
181  try
182  {
183  parse( parsebegin, parseend, parser.parser(), SKIP_PARSER );
184  }
185  catch( const parse_exception& )
186  {
187  throw;
188  }
189  catch( const parser_error<std::string, iter_t>& e )
190  {
191  throw parse_exception_syntactic_error( e.descriptor );
192  }
193  if ( parser.hasResult() ) {
194  DataSourceBase::shared_ptr ret = parser.getResult();
195  parser.dropResult();
196  return ret;
197  }
198  throw parse_exception_parser_fail("Parser did not find a valid expression in text.");
199  }
200 
202  TaskContext* tc )
203  {
204  return parseExpression( _s, tc );
205  }
206 
208  TaskContext* tc )
209  {
210  std::string s( _s );
211 
212  our_pos_iter_t parsebegin( s.begin(), s.end(), "teststring" );
213  our_pos_iter_t parseend( s.end(), s.end(), "teststring" );
214 
215  CommonParser cp;
216  ValueChangeParser parser( tc, cp, tc->provides(), mcaller ? mcaller : tc->engine() );
217  bool skipref=true;
218  try
219  {
220  parse( parsebegin, parseend, parser.parser(), SKIP_PARSER );
221  }
222  catch( const parse_exception& )
223  {
224  throw;
225  }
226  catch( const parser_error<std::string, iter_t>& e )
227  {
228  // this only happens if input is really wrong.
229  throw parse_exception_syntactic_error( e.descriptor );
230  }
231 
232  ActionInterface* ac = 0;
233  std::vector<ActionInterface*> acv = parser.assignCommands();
234  // and not forget to reset()..
235  if ( acv.empty() && parser.lastDefinedValue() ) {
236  return parser.lastDefinedValue()->getDataSource();
237  }
238  if ( acv.size() == 1 ) {
239  if (acv.front() ) { // front will be null if its an alias.
240  ac = acv.front();
241  ac->readArguments();
242  ac->execute();
243  delete ac;
244  }
245  return parser.lastDefinedValue()->getDataSource();
246  }
247  else if (acv.size() > 1) {
248  ac = new CommandComposite(acv);
249  }
250 
251  if ( ac ) {
253  //parser.reset(); don't do this, we want to keep it.
254  return ret;
255  }
257  }
258 }
ParsedFunctions parseFunction(const std::string &s, TaskContext *, const std::string &filename="stream")
Reads out the string, parses it, and returns a new FunctionGraph.
Definition: Parser.cpp:91
Service::shared_ptr provides()
#define SKIP_PARSER
This interface represents the concept of a condition which can be evaluated and return true or false...
virtual void readArguments()=0
base::DataSourceBase::shared_ptr parseValueStatement(const std::string &s, TaskContext *)
Parses a whole value manipulation/creation statement. Requires the set/var/const etc prefixes...
Definition: Parser.cpp:207
ExecutionEngine * mcaller
Definition: Parser.hpp:67
A Parser for Orocos Program Scripts.
virtual parse_exception * copy() const =0
void runScript(std::string const &code, TaskContext *mowner, ScriptingService *service, std::string const &filename)
Definition: Parser.cpp:72
This class contains some very common parser definitions.
ConditionInterface * parseCondition(const std::string &s, TaskContext *)
Parses the string as a condition, and returns a new ConditionInterface. Will throw parse_exception on...
Definition: Parser.cpp:141
position_iterator< our_iterator_t > our_pos_iter_t
ParsedStateMachines parseStateMachine(const std::string &s, TaskContext *, const std::string &filename="stream")
Reads out the string, parses it, and returns a new ParsedStateMachine.
Definition: Parser.cpp:117
static RTT_API ExecutionEngine * Instance()
virtual bool execute()=0
std::vector< ProgramInterfacePtr > ParsedPrograms
Definition: Parser.hpp:101
Based on the software pattern &#39;command&#39;, this interface allows execution of action objects...
base::DataSourceBase::shared_ptr getResult()
std::vector< ProgramInterfacePtr > ParsedFunctions
Definition: Parser.hpp:89
base::DataSourceBase::shared_ptr parseExpression(const std::string &s, TaskContext *)
Parses the expression in s.
Definition: Parser.cpp:170
std::vector< ProgramInterfacePtr > parse(iter_t &begin, iter_t end)
Tries to parse programs, returns the generated programs on success.
std::vector< ProgramInterfacePtr > parseFunction(iter_t &begin, iter_t end)
ParsedPrograms parseProgram(const std::string &s, TaskContext *, const std::string &filename="stream")
Reads out the string, parses it, and returns a new ProgramGraph.
Definition: Parser.cpp:103
std::vector< ParsedStateMachinePtr > ParsedStateMachines
Definition: Parser.hpp:113
boost::intrusive_ptr< DataSourceBase > shared_ptr
Contains TaskContext, Activity, OperationCaller, Operation, Property, InputPort, OutputPort, Attribute.
Definition: Activity.cpp:53
static Logger & log()
Definition: Logger.hpp:350
Based on the software pattern &#39;composite&#39;, this class RTT_SCRIPTING_API allows composing command obje...
const ExecutionEngine * engine() const
Definition: TaskCore.hpp:306
static Logger::LogFunction endlog()
Definition: Logger.hpp:362
std::string our_buffer_t
std::vector< ParsedStateMachinePtr > parse(iter_t &begin, iter_t end)
base::DataSourceBase::shared_ptr parseValueChange(const std::string &s, TaskContext *)
Parses a change of a value in s.
Definition: Parser.cpp:201


rtt
Author(s): RTT Developers
autogenerated on Tue Jun 25 2019 19:33:26