Re: X3 employee example with semantic actions

Larry Evans <[email protected]> Wed, 30 May 2018 12:00:49 -0500
Newsgroups gmane.comp.parsers.spirit.general
Message-ID <[email protected]>
On 05/30/2018 10:16 AM, Maarten Verhage wrote:
> Dear people,
> 
> 
> 
> What would help me in understanding Spirit better is when I can clearly see
> how the parsed attributes are stored into the data structure. So, I’m aiming
> for semantic actions to do the work.
> 
> 
> 
> I was dealing with the employee example code in Spirit 2. But I realized
> that the type of an compound attribute can grow pretty complex. For example
> if I want the quotes themselves stored as well. See:
> 
> 
> 
> 
> 
> quoted_string = lexeme[char_('"') >> +(char_ - '"') >> char_('"')]
> 
>    [phoenix::bind( &assign_quoted_str, phoenix::ref(qi::_val), ::_1 ) ];
> 
> 
> 
> 
> 
> And the function:
> 
> 
> 
> void assign_quoted_str(
> 
>    std::string& val,
> 
>    const boost::fusion::vector<
> 
>    boost::fusion::vector< char, std::vector<char>, char >>& fvc )
> 
> {
> 
>    val = boost::fusion::at_c<0>( boost::fusion::at_c<0>( fvc ) );
> 
> 
> 
>    const std::vector<char>& vc =
> 
>      boost::fusion::at_c<1>( boost::fusion::at_c<0>( fvc ) );
> 
>    val.append( vc.begin(), vc.end() );
> 
> 
> 
>    val += boost::fusion::at_c<2>( boost::fusion::at_c<0>( fvc ) );
> 
> }
> 
> 
> 
> So, I thought how nice it would have been to get a pair of iterators into
> the “parsed” result once Spirit determined the rule to match. But I couldn’t
> found this facility in Spirit 2. But Spirit X3 has the Context object to be
> able to call the _where member function to get this right? And then just
> copy it to a memory area that will become my new file. For the file contents
> I do not want to modify this would be a pretty efficient way right?
> 
> 
> 
> The obstacle I’m facing right now (and hope a list member here would be able
> to help me on) is this: As I do want manually store stuff using semantic
> actions how do I need to modify the Spirit X3 employee example code to 1)
> Prevent spirit to automatically store it. And 2) How do I need to provide
> the empty employee object to this client::parser namespace area as this is
> not a class anymore.

Maarten, why couldn't you define:

   std::ostream&
operator<<
   ( std::ostream& sout
   , ast_from_parse_t const& ast
   )
   {
     //then print the ast to sout here using whatever
     //method.
   }

That way you wouldn't have to wrestle with semantic actions at all,
unless I'm missing something (happens occasionally).

-regards,
Larry


------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
Spirit-general mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/spirit-general