0

My first version of this question was rich with misunderstandings. My answer below suits my needs. But I kept at it to understand what could be done with with<>. What I get is that it intended to inject context into a parser. Then the parser is called from with_directive::parse (in x3's with.hpp) In the following code, that is just what happens.

#include <boost/spirit/home/x3.hpp>

using namespace boost::spirit::x3;
struct eol_parser_cnt : parser<eol_parser_cnt>
{
    struct context {
        int line = 0;
        std::string::iterator iter_pos;
    };
    template <typename Iterator, typename Context, typename Attribute>
    bool parse(Iterator& first, Iterator const& last
        , Context const& context, unused_type, Attribute& attr) const
    {
        //std::cout << context.line;
        auto& ctx = context;

        return boost::spirit::x3::parse(first, last, lit(' ') | (lit("//") >> *(char_ - eol) >> eol));
    }
};
const auto& our_skipper = eol_parser_cnt{};

eol_parser_cnt::context lines;
auto with_skipper = with<eol_parser_cnt::context>(lines)[our_skipper];

int main()
{
    std::string str("12 word");
    auto first = str.begin();
    phrase_parse(first, str.end(), int_ >> *char_("a-z"), with_skipper);
}

Putting a break point in eol_parser_cnt::parse and I see it working. The debugger consistently shows the context is there and that it is the structure of eol_parser_cnt::context. I can change the value of line in the debugger and the next hit shows that value, it is a real object. But, try to uncomment the line std::cout << context.line; and the compiler complains that is it an unused_type. So, I just don't get it.

test.cpp(15,30): error C2039: 'line': is not a member of 'boost::spirit::x3::context<ID,T,Context>'
        with
        [
            ID=eol_parser_cnt::context,
            T=eol_parser_cnt::context,
            Context=boost::spirit::x3::unused_type
        ]
F:\cpp\boost_1_76_0\boost\spirit\home\x3\support\context.hpp(18): message : see declaration of 'boost::spirit::x3::context<ID,T,Context>'
        with
        [
            ID=eol_parser_cnt::context,
            T=eol_parser_cnt::context,
            Context=boost::spirit::x3::unused_type
        ]
F:\cpp\boost_1_76_0\boost\spirit\home\x3\directive\with.hpp(62): message : see reference to function template instantiation 'bool eol_parser_cnt::parse<Iterator,boost::spirit::x3::context<ID,T,Context>,Attribute>(Iterator &,const Iterator &,const boost::spirit::x3::context<ID,T,Context> &,boost::spirit::x3::unused_type,Attribute &) const' being compiled
        with
        [
            Iterator=std::_String_iterator<std::_String_val<std::_Simple_types<char>>>,
            ID=eol_parser_cnt::context,
            T=eol_parser_cnt::context,
            Context=boost::spirit::x3::unused_type,
            Attribute=const boost::spirit::x3::unused_type
        ]
lakeweb
  • 1,859
  • 2
  • 16
  • 21
  • `x3::get(context)` returns a skipper, not a context. Skippers always called with `unused` context, so there is no way to get the parser context in a skipper. Is you parser producing an AST? Maybe you can reorganize your parser to parse by steps in a loop where you will skip and count newlines yourself?` – Nikita Kniazev Dec 16 '21 at 23:17
  • Hi, @Nikita Kniazev, Thank you. I figured out what that means and did see that skippers will not provide context. No to the AST. It is a language I made up for a printer driver, lots of unrelated stuff. I have come up with instantiating the skipper like `eol_parser_cnt::context ctx;` `eol_parser_cnt eol_count(ctx);` and that keeps the context in local scope. I'm also looking at hacking into the `x3` namespace and creating an `x3::skip_over` that would bypass the `x3::detail::skip_over`. I am learning a lot more about MPL! That's the good part of this exercise. – lakeweb Dec 17 '21 at 00:41

1 Answers1

0

Well, it took a while but I was going to understand this. I re-read C++ Template Metaprogramming seriously this time. Then looking at the x3 code I finally understood that with<> is just another parser wrapper. It is interesting to debug an optimized build and see just how much code disappears. VS shows all the disappeared stuff on the stack as inlined and what was 9 layers of parser calls becomes 2 into the likes of with_error_handling::on_error. Everything from my call to parse_rhs_main (in rule.hpp, line 232), is gone.

So because with<> is just another parser wrapper and if the skipper is wrapped, the context is only avaliable to the skipper. But no big deal as the skipper is in the context of the main parser. The difference is that in the skipper object a get<skipper_eol_cnt>(context) returns a reference to the skipper_eol_cnt::context{}. Whereas in the main parser we have to us get<skipper_tag>(context) and this returns the with<> parser object. val is a member of that parser, the reference to the skipper_eol_cnt::context{}. So get<skipper_tag>(context).val retrieves the context we are looking for.

So here it is, using with<> applied to a skipper.

#include<iostream>
#include <iomanip>
#include <vector>
//#define BOOST_SPIRIT_X3_DEBUG
#include <boost/spirit/home/x3.hpp>
#include <boost/fusion/adapted.hpp>

using namespace boost::spirit::x3;

struct skipper_eol_cnt : parser<skipper_eol_cnt>
{
    struct context {
        int line = 1;
        std::string::iterator iter_pos;
    };
    template <typename Iterator, typename Context, typename Attribute>
    bool parse(Iterator& first, Iterator const& last
        , Context const& context, unused_type, Attribute& attr) const
    {
        const char* start_cmt = "/*";
        const char* end_cmt = "*/";
        if (first == last)
            return false;
        bool matched = false;
        //here we are getting from the 'with<>' wrapper
        auto& ctx = get<skipper_eol_cnt>(context);
        //skip: space | '//comment'
        boost::spirit::x3::parse(first, last, lit(' ') | (lit("//") >> *(char_ - eol)));//eol counted below
        //skip: '/*comment*/'
        if (detail::string_parse(start_cmt, first, last, unused, case_compare<Iterator>())) {
            for (; first != last; ++first) {
                if (detail::string_parse(end_cmt, first, last, unused, case_compare<Iterator>()))
                    break;
                if (*first == '\n')
                    ++ctx.line, ctx.iter_pos = first;
            }
        }
        Iterator iter = first;
        for (; iter != last && (*iter == '\r' || *iter == '\n'); ++iter) {
            matched = true;
            if (*iter == '\n')  // LF
                ++ctx.line, ctx.iter_pos = iter;
        }
        //{static int pos = 0; if (pos < ctx.line) { pos = ctx.line; std::cout << pos << std::endl; }}
        if (matched) first = iter;
        return matched;
    }
};
auto const& skip_eol_cnt = skipper_eol_cnt{};

struct with_error_handling {
    template<typename It, typename Ctx>
    error_handler_result on_error(It f, It l, expectation_failure<It> const& ef, Ctx const& ctx) const {
        It erit = f + std::distance(f, ef.where());
        //here we are getting the wrapped skipper so need the 'with<>.val'
        const auto& sctx = get<skipper_tag>(ctx).val;

        It bit = erit;
        for (; *bit != '\n'/* && bit != f*/; --bit)
            ;
        It eit = erit;
        for (; *eit != '\n' && eit != l; ++eit)
            ;
        int str_pos = erit - bit - 1;
        std::ostringstream oss;
        oss << "Expecting " << ef.which() << "\n at line: " << sctx.line
            << "\n\t" << std::string(bit + 1, eit)
            << "\n\t" << std::setw(str_pos) << std::setfill('-') << "" << "^";

        get<with_error_handling>(ctx).push_back(oss.str());;
        return error_handler_result::fail;
    }
};

//attr sections
struct section_type {
    std::string name;
    int line;
    std::string::iterator iter_pos;
};
BOOST_FUSION_ADAPT_STRUCT(section_type, name)
using sections_type = std::vector<section_type>;

struct parser_find_sections : parser<parser_find_sections> {
    template<typename Iterator, typename Context, typename RContext, typename Attribute>
    bool parse(Iterator& first, Iterator const& last, Context const& context, RContext const& rcontext, Attribute& section) const {
        const auto& sssctx = get<skipper_eol_cnt>(context); //now here this doesn't work, unused_type, but
        const auto& sctx = get<skipper_tag>(context).val;
        auto to_line = [&sctx, first](auto& ctx) {
            _attr(ctx).line = sctx.line;
            //_attr(ctx).iter_pos = first; // this one will get at 'section color(x,x)'
            _attr(ctx).iter_pos = _where(ctx).begin(); // this one is '(x,x)'
        };
        static_assert(BOOST_VERSION / 100 % 1000 >= 77);
        ////NOTE!!! if you have a boost version of less than 1.77, x3::seek will fail here
        ////quick fix, copy from: https://github.com/boostorg/spirit/blob/boost-1.78.0/include/boost/spirit/home/x3/directive/seek.hpp
        ////and paste to your boost file...
        return phrase_parse(first, last, *(seek["section"] >> (*alpha)[to_line]), get<skipper_tag>(context), section);
    }
};

auto const parse_section = rule<with_error_handling, std::pair<int, int>>("the_sec_parser") = [] {
    return '(' > int_ > ',' > int_ > ')';
}();

template<typename T>
std::ostream& operator << (std::ostream& os, std::pair<T, T>& t) {
    return os << t.first << ',' << t.second;
}

//errors
std::vector<std::string> errors;
auto with_errors = with<with_error_handling>(errors)[parse_section];

auto test_section = [](auto& content, auto& section) {
    //attr
    std::pair<int, int> attr;
    skipper_eol_cnt::context ctx{ section.line, section.iter_pos };
    auto with_skip_cnt = with< skipper_eol_cnt>(ctx)[skip_eol_cnt];
    auto first(section.iter_pos);
    return std::tuple(phrase_parse(first, content.end(), with_errors, with_skip_cnt, attr), attr);
};

int main() {
    std::string str(R"(//line 1
section red(5, 6)
section green( 7, 8) //line 3
    section blue(9, 10) //no error
     /*comment
            bunch of lines of stuff....
        */   section white(11, a 12) //error on line 7
section black(    13,14)
)");
    //get the list of sections
    auto with_skip_cnt = with<skipper_eol_cnt>(skipper_eol_cnt::context{})[skip_eol_cnt];
    sections_type secs;
    auto first(str.begin());
    phrase_parse(first, str.end(), parser_find_sections(), with_skip_cnt, secs);
    for (auto& item : secs)
        std::cout << item.name << "\t at line: " << item.line << std::endl;

    //section 'blue', at 2, has no error
    auto [r, attr] = test_section(str, secs.at(2));
    if (r)
        std::cout << "\nthe " << secs.at(2).name << " hase vals: " << attr << "\n\n";

    //section 'white', at 3, has an error
    test_section(str, secs.at(3));

    if (errors.size())
        std::cout << errors.front() << std::endl;

    return 0;
}
lakeweb
  • 1,859
  • 2
  • 16
  • 21