
Hi, I have some trouble to retreive error parsing information using qi::on_error<fail> to work on a std::string. The documentation propose the following code : on_error<fail> ( start , std::cout << val("constituent: expected ") << _4 // what failed? << val(" here: \"") << constructstd::string(_3, _2) // iterators to error-pos, end << val("\"") << std::endl ); * On my First attempt. I have define a class member (std::stringstream m_error) in my grammar class and replace std::cout by phx::ref(m_error). But this code generate a compilation error * For the second attempt, I declare class member m_error as std::string and use this error handler: namespace qi = boost::spirit::qi; namespace phx = boost::phoenix; on_error< qi::fail> ( start , phx::ref(m_error) = ( phx::constructstd::string(phx::val("Error! Expecting ")) // + qi::_4 REMARK1 + phx::constructstd::string( phx::val(" here: \"") ) + phx::constructstd::string(qi::_3, qi::_2) + phx::constructstd::string( phx::val("\"") ) ) ); that's work perfectly. On error my string is correctly filled. But my problem is that I can't get the expected token (_4) (REMARK1), If I uncomment line REMARK1, I get a compilation error, same result using the code phx::constructstd::string(qi::_4). How can a get qi::_4 as string ? Can any one with kindness help me?

2011/4/6 Vincent Agnus
Hi,
I have some trouble to retreive error parsing information using qi::on_error<fail> to work on a std::string.
The documentation propose the following code :
on_error<fail> ( start , std::cout << val("constituent: expected ") << _4 // what failed? << val(" here: \"") << constructstd::string(_3, _2) // iterators to error-pos, end << val("\"") << std::endl );
* On my First attempt. I have define a class member (std::stringstream m_error) in my grammar class and replace std::cout by phx::ref(m_error). But this code generate a compilation error
Seems like a Phoenix bug to me. Here's a workaround: (ostream&)m_error << ...
* For the second attempt, I declare class member m_error as std::string and use this error handler:
namespace qi = boost::spirit::qi; namespace phx = boost::phoenix;
on_error< qi::fail> ( start , phx::ref(m_error) = ( phx::constructstd::string(phx::val("Error! Expecting ")) // + qi::_4 REMARK1 + phx::constructstd::string( phx::val(" here: \"") ) + phx::constructstd::string(qi::_3, qi::_2) + phx::constructstd::string( phx::val("\"") ) ) );
that's work perfectly. On error my string is correctly filled. But my problem is that I can't get the expected token (_4) (REMARK1), If I uncomment line REMARK1, I get a compilation error, same result using the code phx::constructstd::string(qi::_4).
How can a get qi::_4 as string ?
phx::bind(&boost::spirit::info::tag, qi::_4)
Can any one with kindness help me?
HTH

2011/4/6 TONGARI
2011/4/6 Vincent Agnus
How can a get qi::_4 as string ?
phx::bind(&boost::spirit::info::tag, qi::_4)
Well, this may or may not be what you want, though... qi::_4 here stands for a boost::spirit::info object which seems not documented yet. Anyway, it's not a string, but it's outputable (traverse through its value, a tree structure), so its tag member (a utf8_string) is not identical to what will be output, you can see your BOOST_ROOT/boost/spirit/home/support/info.hpp

thanks for our answer ! Your solution work, but it is not exactly I want. Indeed this expression return the typename of a tag (for example literal-char) instead its value ( "A" , "B" , .... ). for example if my start rule is start = qi::char_("a-z") > qi::char_("!"); and I parse the string "a#" then I expect that _4 contain "!" , phx::bind(&boost::spirit::info::tag, qi::_4) return "literal-char" 2 questions : a) I have a look in boost::spirit::info class is the boost::spirit::info::value member is containing the data I want ( ie "!") b) perhaps it exists an alternative like phx::construct<>( phx::val("") << _4 ) thanks again Vincent Agnus Le 06/04/2011 17:07, TONGARI a écrit :
2011/4/6 TONGARI
mailto:tongari95@gmail.com> 2011/4/6 Vincent Agnus
mailto:vincent.agnus@ircad.fr> How can a get qi::_4 as string ?
phx::bind(&boost::spirit::info::tag, qi::_4)
Well, this may or may not be what you want, though...
qi::_4 here stands for a boost::spirit::info object which seems not documented yet. Anyway, it's not a string, but it's outputable (traverse through its value, a tree structure), so its tag member (a utf8_string) is not identical to what will be output, you can see your BOOST_ROOT/boost/spirit/home/support/info.hpp
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users

2011/4/7 Vincent Agnus
thanks for our answer !
Your solution work, but it is not exactly I want. Indeed this expression return the typename of a tag (for example literal-char) instead its value ( "A" , "B" , .... ).
Does my first suggestion solve your first attempt? i.e. (ostream&)m_error << ... for example if my start rule is
start = qi::char_("a-z") > qi::char_("!");
and I parse the string "a#" then I expect that _4 contain "!" , phx::bind(&boost::spirit::info::tag, qi::_4) return "literal-char"
2 questions : a) I have a look in boost::spirit::info class is the boost::spirit::info::value member is containing the data I want ( ie "!")
True here, but it's a more subtle thing than I can explain... Not every case can you get a utf8_string form boost::spirit::info::value. For example, "... > qi::char_('a', 'z')" you can get the value as a utf8_string "a-z"; while "... > qi::char_("a-z")" you can only get a nil object from value. b) perhaps it exists an alternative like
phx::construct<>( phx::val("") << _4 )
I don't know what you mean here.

Le 06/04/2011 20:08, TONGARI a écrit :
Does my first suggestion solve your first attempt? i.e. (ostream&)m_error << ...
I don't have see our answer in our previous mail. I have test it with m_error as std::stringstream and phx::ref( (ostream&)m_error ). And it works fine !!! Thanks a lot for our help Note that there is a compilation error with *std::ostringstream* m_error and phx::ref( m_error )
for example if my start rule is start = qi::char_("a-z") > qi::char_("!");
and I parse the string "a#" then I expect that _4 contain "!" , phx::bind(&boost::spirit::info::tag, qi::_4) return "literal-char"
2 questions : a) I have a look in boost::spirit::info class is the boost::spirit::info::value member is containing the data I want ( ie "!")
True here, but it's a more subtle thing than I can explain... Not every case can you get a utf8_string form boost::spirit::info::value.
For example, "... > qi::char_('a', 'z')" you can get the value as a utf8_string "a-z"; while "... > qi::char_("a-z")" you can only get a nil object from value.
OK
b) perhaps it exists an alternative like phx::construct<>( phx::val("") << _4 )
I don't know what you mean here.
With this kind of proposition I will hope to hack the string construction (m_error in this case is a std::string). a) phx::constructstd::string( phx::val("...") ) is a valid expression b) phx::val("...") << _4 is a valid expression c) phx::constructstd::string( phx::val("...") << _4 ). The evaluation on this expression first compute (exp. b) phx::val("...") << _4 then then apply (a) evaluation. For my application I will use our workarond, thanks again Vincent Agnus
participants (2)
-
TONGARI
-
Vincent Agnus