setting attribute value from on_error handler

classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|

setting attribute value from on_error handler

Kent Hauser
Is there a context variable (or other method) to set the parse attribute in an `on_error` handler. This would be for the case where the handler returns `error_handler_result::accept`.

Thanks.



------------------------------------------------------------------------------

_______________________________________________
Spirit-general mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/spirit-general
Reply | Threaded
Open this post in threaded view
|

Re: setting attribute value from on_error handler

Kent Hauser
This question is for X3. Sorry for confusion. 


On April 6, 2016 at 12:11:45 AM, Kent Hauser ([hidden email]) wrote:

Is there a context variable (or other method) to set the parse attribute in an `on_error` handler. This would be for the case where the handler returns `error_handler_result::accept`.

Thanks.



------------------------------------------------------------------------------

_______________________________________________
Spirit-general mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/spirit-general
Reply | Threaded
Open this post in threaded view
|

Re: setting attribute value from on_error handler

Mario Lang
In reply to this post by Kent Hauser
Kent Hauser <[hidden email]> writes:

> Is there a context variable (or other method) to set the parse
> attribute in an `on_error` handler.

Have you tried _val(context)?

--
CYa,
  ⡍⠁⠗⠊⠕

------------------------------------------------------------------------------
_______________________________________________
Spirit-general mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/spirit-general
Reply | Threaded
Open this post in threaded view
|

Re: setting attribute value from on_error handler

Kent Hauser
That’s what I tried, but with no luck. I can print my “ast” in the `on_error` handler, assign it `x3::_val(context) = ast`, but
the `ast` from the previous iteration of the `parse_phrase` loop is what I see in the application code.

For what it’s worth, I also update `first` with a resync parser & that works great.

Thanks.


On April 6, 2016 at 3:26:08 AM, Mario Lang ([hidden email]) wrote:

Kent Hauser <[hidden email]> writes:

> Is there a context variable (or other method) to set the parse
> attribute in an `on_error` handler.

Have you tried _val(context)?

--
CYa,
⡍⠁⠗⠊⠕

------------------------------------------------------------------------------
_______________________________________________
Spirit-general mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/spirit-general

------------------------------------------------------------------------------

_______________________________________________
Spirit-general mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/spirit-general
Reply | Threaded
Open this post in threaded view
|

Re: setting attribute value from on_error handler

Joel de Guzman
On 07/04/2016 5:57 AM, Kent Hauser wrote:
> That’s what I tried, but with no luck. I can print my “ast” in the `on_error` handler,
> assign it `x3::_val(context) = ast`, but
> the `ast` from the previous iteration of the `parse_phrase` loop is what I see in the
> application code.
>
> For what it’s worth, I also update `first` with a resync parser & that works great.

Please (minimal) post some code for us to see what's going on.

Regards,
--
Joel de Guzman
http://www.ciere.com
http://boost-spirit.com
http://www.cycfi.com/


------------------------------------------------------------------------------
_______________________________________________
Spirit-general mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/spirit-general
Reply | Threaded
Open this post in threaded view
|

Re: setting attribute value from on_error handler

Kent Hauser
In reply to this post by Kent Hauser
Here is an example of what I’m seeing. 

I’m setting an “on_error” handler on the `top` rule. It is only entered when I use the `expectation parser` in the `element` rule.

My naive expectation of `on_error` is that it’s called for the rule it’s attached to. This I expected `x3::_val` to reference the attribute for `top`. I also expected it to be entered for all parse failures, not just `expectation failures`. 

The program output is as follows:

./on_error
expect: 1 2 -1 4
output: 1 2 2 4

Thanks.

=========================================
#include <iostream>
#include <boost/spirit/home/x3.hpp>
#include <boost/spirit/home/x3/support/ast/variant.hpp>

namespace x3 = boost::spirit::x3;
using namespace x3;

using var_t = int;
// using var_t = x3::variant<int>;

rule<struct elemnent, var_t> const element_p = "element_p";
rule<struct top, var_t> const top_p = "top_p";

auto const element_p_def = int_ > ',';
auto const top_p_def = element_p;
auto const resync = *(char_ - ',') >> ',';

BOOST_SPIRIT_DEFINE(element_p, top_p)

struct top {
    template <typename Iterator, typename Exception, typename Context>
    auto on_error(Iterator& first , Iterator const& last
                , Exception const& x, Context const& context)
    {
        x3::_val(context) = -1;
        parse(first, last, resync);
        return error_handler_result::accept;
    }
};

int main() {
    std::string input = "1, 2, 3 x, 4, ";


    auto first = input.begin();
    auto last  = input.end();
    var_t  v {};

    std::cout << "expect: 1 2 -1 4 " << std::endl;
    std::cout << "output: ";

    while (first != last) {
        auto before = first;
        if (phrase_parse(first, last, top_p, space, v)) {
            // std::cout << boost::get<int>(v) << " ";
            std::cout << v << " ";
        } else {
            std::cout << "\nParse failed: remaining input = \"" << std::string(first, last) << "\"";
            break;
        }
        if (first == before) {
            std::cout << "\nNo input consumed!";
            break;
        }
    }
    std::cout << std::endl;
}
=========================================

On April 6, 2016 at 7:35:44 PM, Joel de Guzman ([hidden email]) wrote:

On 07/04/2016 5:57 AM, Kent Hauser wrote:
> That’s what I tried, but with no luck. I can print my “ast” in the `on_error` handler,
> assign it `x3::_val(context) = ast`, but
> the `ast` from the previous iteration of the `parse_phrase` loop is what I see in the
> application code.
>
> For what it’s worth, I also update `first` with a resync parser & that works great.

Please (minimal) post some code for us to see what's going on.

Regards,
--
Joel de Guzman
http://www.ciere.com
http://boost-spirit.com
http://www.cycfi.com/


------------------------------------------------------------------------------
_______________________________________________
Spirit-general mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/spirit-general

------------------------------------------------------------------------------

_______________________________________________
Spirit-general mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/spirit-general
Reply | Threaded
Open this post in threaded view
|

Re: setting attribute value from on_error handler

Kent Hauser
Could I get some feedback on my example? I guess I’m unclear how the `on_error` works because what I’m seeing is certainly a surprise to me!

Thanks.


------------------------------------------------------------------------------
Find and fix application performance issues faster with Applications Manager
Applications Manager provides deep performance insights into multiple tiers of
your business applications. It resolves application problems quickly and
reduces your MTTR. Get your free trial! http://pubads.g.doubleclick.net/
gampad/clk?id=1444514301&iu=/ca-pub-7940484522588532
_______________________________________________
Spirit-general mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/spirit-general
Reply | Threaded
Open this post in threaded view
|

Re: setting attribute value from on_error handler

Joel de Guzman
On 10/04/2016 12:25 AM, Kent Hauser wrote:
> Could I get some feedback on my example? I guess I’m unclear how the `on_error` works
> because what I’m seeing is certainly a surprise to me!
>

Pardon. I ran out of time. I'll try to squeeze some more time to look into
the code, but I am hoping that someone else will beat me to it ;-)

Regards,
--
Joel de Guzman
http://www.ciere.com
http://boost-spirit.com
http://www.cycfi.com/


------------------------------------------------------------------------------
Find and fix application performance issues faster with Applications Manager
Applications Manager provides deep performance insights into multiple tiers of
your business applications. It resolves application problems quickly and
reduces your MTTR. Get your free trial!
https://ad.doubleclick.net/ddm/clk/302982198;130105516;z
_______________________________________________
Spirit-general mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/spirit-general