ValueParser.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  tag: Peter Soetens Mon May 10 19:10:37 CEST 2004 ValueParser.cxx
3 
4  ValueParser.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 "parse_exception.hpp"
40 #include "ValueParser.hpp"
41 #include "../Attribute.hpp"
42 
43 #include "../TaskContext.hpp"
44 #include "../Service.hpp"
45 #include "../types/GlobalsRepository.hpp"
46 
47 #include <boost/bind.hpp>
48 #include <boost/lexical_cast.hpp>
49 
50 #include <iostream>
51 using namespace std;
52 
53 namespace RTT
54 {
55  using boost::bind;
56  using namespace detail;
57 
58  ValueParser::ValueParser( TaskContext* tc, CommonParser& cp)
59  : commonparser(cp), peerparser(tc,cp), propparser(cp)
60  {
61  BOOST_SPIRIT_DEBUG_RULE( constant );
62  BOOST_SPIRIT_DEBUG_RULE( const_float );
63  BOOST_SPIRIT_DEBUG_RULE( const_double );
64  BOOST_SPIRIT_DEBUG_RULE( const_int );
65  BOOST_SPIRIT_DEBUG_RULE( const_hex );
66  BOOST_SPIRIT_DEBUG_RULE( const_uint );
67  BOOST_SPIRIT_DEBUG_RULE( const_llong );
68  BOOST_SPIRIT_DEBUG_RULE( const_ullong );
69  BOOST_SPIRIT_DEBUG_RULE( const_char );
70  BOOST_SPIRIT_DEBUG_RULE( const_bool );
71  BOOST_SPIRIT_DEBUG_RULE( const_string );
72  BOOST_SPIRIT_DEBUG_RULE( named_constant );
73 
74  // note the order is important: commonparser.identifier throws a
75  // useful "cannot use x as identifier" error if it fails, so we
76  // must first show all non-identifier rules.
77  constant =
79  | const_double
80  | const_hex
81  | const_ullong
82  | const_llong
83  | const_uint
84  | const_int
85  | const_bool
86  | const_char
87  | const_string
89 
90  const_float =
91  strict_real_p [
92  boost::bind( &ValueParser::seenfloatconstant, this, _1 ) ] >> ch_p('f');
93 
94  const_double =
95  strict_real_p [
96  boost::bind( &ValueParser::seendoubleconstant, this, _1 ) ];
97 
98  const_hex = (str_p("0x") | str_p("0X")) >>
99  hex_p [
100  boost::bind( &ValueParser::seenhexconstant, this, _1 ) ];
101 
102  const_ullong =
103  uint_parser<unsigned long long>() [
104  boost::bind( &ValueParser::seenullongconstant, this, _1 ) ] >> str_p("ull");
105 
106  const_llong =
107  uint_parser<long long>() [
108  boost::bind( &ValueParser::seenllongconstant, this, _1 ) ] >> str_p("ll");
109 
110  const_uint =
111  uint_parser<unsigned int>() [
112  boost::bind( &ValueParser::seenuintconstant, this, _1 ) ] >> ch_p('u');
113 
114  const_int =
115  int_parser<int>() [
116  boost::bind( &ValueParser::seenintconstant, this, _1 ) ];
117 
118  const_bool =
119  ( keyword_p( "true" ) | keyword_p("false") )[
120  boost::bind( &ValueParser::seenboolconstant, this, _1, _2 ) ];
121 
122  const_char = (ch_p('\'') >> ch_p('\\') >> ch_p('0') >> ch_p('\''))[boost::bind( &ValueParser::seennull,this)] |
123  confix_p( "'", (c_escape_ch_p[ boost::bind( &ValueParser::seencharconstant, this, _1 ) ]) , "'" );
124 
125  const_string = lexeme_d[confix_p(
126  ch_p( '"' ), *c_escape_ch_p[ boost::bind( &ValueParser::push_str_char, this, _1 ) ], '"' )[ boost::bind( &ValueParser::seenstring, this ) ]];
127 
129  ( keyword_p("done")[boost::bind( &ValueParser::seennamedconstant, this, _1, _2 ) ]
130  |
131  ( peerparser.locator()[boost::bind( &ValueParser::seenpeer, this) ]
132  >> propparser.locator()
133  >> commonparser.identifier[boost::bind( &ValueParser::seennamedconstant, this, _1, _2 ) ]) )
134  ;
135  }
136 
138  // inform propparser of new peer :
139  //std::cerr << "ValueParser: seenpeer : "<< peerparser.taskObject()->getName()
140  // <<" has props :" << (peerparser.taskObject()->properties() != 0) << std::endl;
141  propparser.setPropertyBag( peerparser.taskObject()->properties() );
142  }
143 
145  {
146  std::string value( begin, end );
147  assert( value == "true" || value == "false" );
148  if ( value == "true" )
149  ret =
150  new ConstantDataSource<bool>( true );
151  else
152  ret =
153  new ConstantDataSource<bool>( false );
154  }
155 
157  {
158  std::string name( begin, end );
160  peerparser.reset();
161  //std::cerr << "ValueParser: seenvar : "<< name
162  // <<" is bag : " << (propparser.bag() != 0) << " is prop: "<< (propparser.property() != 0) << std::endl;
163  // in case our task is a taskcontext:
164  if ( task && propparser.bag() && propparser.property() ) {
165  // nested property case :
166  if ( ! propparser.bag()->find( name ) ) {
167  //std::cerr << "In "<<peer->getName() <<" : " << name << " not present"<<std::endl;
168  throw parse_exception_semantic_error("Property " + name + " not present in PropertyBag "+propparser.property()->getName()+" in "+ task->getName()+".");
169  }
170  ret = propparser.bag()->find( name )->getDataSource();
171  propparser.reset();
172  return;
173  }
174 
175  // non-nested property or attribute case :
176  if ( task && task->hasAttribute( name ) ) {
177  ret = task->getValue(name)->getDataSource();
178  return;
179  }
180  if ( task && task->hasProperty( name ) ) {
181  ret = task->properties()->find(name)->getDataSource();
182  return;
183  }
184 
185  // Global Attribute case:
186  if ( GlobalsRepository::Instance()->hasAttribute( name ) ) {
187  ret = GlobalsRepository::Instance()->getValue(name)->getDataSource();
188  return;
189  }
190 
191  // Global Property case:
192  if ( GlobalsRepository::Instance()->hasProperty( name ) ) {
193  ret = GlobalsRepository::Instance()->properties()->find(name)->getDataSource();
194  return;
195  }
196 
197  throw_(begin, "Value " + name + " not defined in "+ task->getName()+".");
198  }
199 
201  {
202  ret = new ConstantDataSource<char>( '\0' );
203  }
204 
206  {
207  ret = new ConstantDataSource<char>( *c );
208  }
209 
210  void ValueParser::seenhexconstant( unsigned int i )
211  {
213  }
214 
216  {
217  ret = new ConstantDataSource<int>( i );
218  }
219 
220  void ValueParser::seenuintconstant( unsigned int i ) // RobWork uint -> unsigned int
221  {
222  ret = new ConstantDataSource<unsigned int>( i ); // RobWork uint -> unsigned int
223  }
224 
225  void ValueParser::seenllongconstant( long long i )
226  {
228  }
229 
230  void ValueParser::seenullongconstant( unsigned long long i )
231  {
233  }
234 
236  {
237  ret = new ConstantDataSource<float>( float(i) );
238  }
239 
241  {
242  ret = new ConstantDataSource<double>( i );
243  }
244 
246  {
247  clear();
248  }
249 
251  {
252  propparser.reset();
253  }
254 
256  {
257  return constant;
258  }
259 
261  {
262  mcurstring += c;
263  }
264 
266  {
267  // due to our config parse rule, the '"' terminating a
268  // string will be in mcurstring, and we don't want it, so we
269  // remove it..
270  mcurstring.erase( mcurstring.end() - 1 );
272  //deleter.reset( ret );
273  mcurstring.clear();
274  }
275 }
#define keyword_p(word)
void seennamedconstant(iter_t begin, iter_t end)
void seenllongconstant(long long c)
base::DataSourceBase::shared_ptr ret
Definition: ValueParser.hpp:78
void seenfloatconstant(double c)
void seenboolconstant(iter_t begin, iter_t end)
void seenullongconstant(unsigned long long c)
void seenuintconstant(unsigned int c)
Definition: mystd.hpp:163
base::PropertyBase * property() const
This class contains some very common parser definitions.
boost::shared_ptr< Service > shared_ptr
Definition: Service.hpp:101
void seendoubleconstant(double c)
base::PropertyBase * find(const std::string &name) const
void setPropertyBag(PropertyBag *pb)
rule< scanner_t > rule_t
PropertyBag * bag() const
void seenhexconstant(unsigned int c)
void seencharconstant(iter_t c)
const std::string & getName() const
Contains TaskContext, Activity, OperationCaller, Operation, Property, InputPort, OutputPort, Attribute.
Definition: Activity.cpp:53
our_pos_iter_t iter_t
virtual DataSourceBase::shared_ptr getDataSource() const =0


rtt
Author(s): RTT Developers
autogenerated on Fri Oct 25 2019 03:59:45