skipper does not work

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

skipper does not work

Jens Kallup
Hello,

I wonder me, why the skipper don't work to parse comment lines:

&& comment
** comment
// comment
/* block */

What is wrong in the code bellow?:
-------------
#define BOOST_SPIRIT_DEBUG

#include <boost/config/warning_disable.hpp>

#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/phoenix.hpp>

#include <string>
#include <iostream>

namespace client
{
     namespace qi = boost::spirit::qi;
     namespace ascii = boost::spirit::ascii;

     template <typename Iterator>
     struct dbase_skipper : public qi::grammar<Iterator>
     {
         dbase_skipper() : dbase_skipper::base_type(my_skip, "dBase")
         {
             using qi::ascii::char_;
             using qi::ascii::space;
             using qi::eol;
             using qi::eoi;

             my_skip =
             *(char_("[ \\t\\n\\r]"))  |
             (("**") >> *(char_ - eol) >> (eol | eoi | char_("\\n"))) |
             (("&&") >> *(char_ - eol) >> (eol | eoi | char_("\\n"))) |
             (("//") >> *(char_ - eol) >> (eol | eoi | char_("\\n"))) |
             ("/*" >> *(char_ - "*/") >> "*/")
             ;
         }
         qi::rule<Iterator> my_skip;
     };

     template <typename Iterator, typename Skipper =
dbase_skipper<Iterator>>
     struct dbase_grammar : public qi::grammar<Iterator, Skipper>
     {
         qi::rule<Iterator, Skipper> start, run_app;
         qi::rule<Iterator, Skipper> block;
         qi::rule<Iterator, Skipper> statement;

         dbase_grammar() : dbase_grammar::base_type(start)
         {
             start = run_app.alias();

             run_app =
                 symbol.alias()
                 ;

             symbol =
                 (symbol_raw.alias())
                 ;

             symbol_raw =
                 +(qi::alpha | qi::char_( "_" ))
                 ;

             BOOST_SPIRIT_DEBUG_NODE(start);
             BOOST_SPIRIT_DEBUG_NODE(symbol);
             BOOST_SPIRIT_DEBUG_NODE(symbol_raw);
         }

         qi::rule<Iterator, Skipper> symbol, symbol_raw;
     };
}

bool parseText(std::string str, int mode)
{
     typedef std::string::const_iterator iterator_t;

     typedef client::dbase_grammar <iterator_t> grammar;
     typedef client::dbase_skipper <iterator_t> skipper;

     grammar pg;
     skipper skp;

     iterator_t iter = str.begin();
     iterator_t end  = str.end();

     bool r = phrase_parse(iter, end, pg, skp);
     if (r == true) {
         std::cout << "Parsing SUCCESS.\n";
         return true;
     }

     if (iter != end)
         std::cout << "Remaining: '" << std::string(iter, end) << std::endl;

     std::cout << "Parsing ERROR.\n";
     return false;
}


------------------------------------------------------------------------------
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
[hidden email]
https://lists.sourceforge.net/lists/listinfo/spirit-general
Reply | Threaded
Open this post in threaded view
|

Re: skipper does not work

sehe
On 18-10-16 14:05, Jens Kallup wrote:
I wonder me, why the skipper don't work to parse comment lines:

&& comment
** comment
// comment
/* block */

I wonder why you wonder. I replied to you 4 days ago, and I explained exactly what was wrong with the skipper.

I also note that you made your code not-selfcontained again. If you don't want to copy the code from an answer (good idea), at least run it through a side-by-side diff tool. That can save you a lot of time.

Here's your new code, integrated with the minimum fix for the skipper: http://coliru.stacked-crooked.com/a/5c53f81291c92a67

Relevant tests:

int main() {
    for (std::string const s : {
            //"Hello",
            "Hello && comment\r\nWorld",
            "Hello ** comment\r\nWorld",
            "Hello // comment\r\nWorld",
            "Hello // /* block */World",
            }) {
        parseText(s);
    }
}

Output:

    Parsing SUCCESS.
    Parsing SUCCESS.
    Parsing SUCCESS.
    Parsing SUCCESS.


------------------------------------------------------------------------------
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
[hidden email]
https://lists.sourceforge.net/lists/listinfo/spirit-general
Reply | Threaded
Open this post in threaded view
|

Re: skipper does not work

Jens Kallup


Am 19.10.2016 um 14:29 schrieb Seth:

>
> Here's your new code, integrated with the minimum fix for the skipper:
> http://coliru.stacked-crooked.com/a/5c53f81291c92a67
>
> Relevant tests:
>
> int main() {
>     for (std::string const s : {
>             //"Hello",
>             "Hello && comment\r\nWorld",
>             "Hello ** comment\r\nWorld",
>             "Hello // comment\r\nWorld",
>             "Hello // /* block */World",
>             }) {
>         parseText(s);
>     }
> }
>
> Output:
>
>     Parsing SUCCESS.
>     Parsing SUCCESS.
>     Parsing SUCCESS.
>     Parsing SUCCESS
>

Yes, it will work, like the version on my side.
But when i have only have comments in buffer/file stream,
the skipper fails and I get error.

Jens

------------------------------------------------------------------------------
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
[hidden email]
https://lists.sourceforge.net/lists/listinfo/spirit-general
Reply | Threaded
Open this post in threaded view
|

Re: skipper does not work

sehe
On 19-10-16 16:59, Jens Kallup wrote:
But when i have only have comments in buffer/file stream,
the skipper fails and I get error.

Of course. Your grammar fails for "no content". If empty content is ok, change the grammar.

Note that in this exact case you would have saved everyone time by letting your self-contained sample SHOW that you want to parse "only comments" with success.

E.g. Failing testcase

Or perhaps even with debug enabled

In which case the simplest thing that makes the test pass would be to replace the grammar with `qi::eps`, but I suppose this could be closer to what you expect:

                run_app = - symbol;       

Make the symbol optional, so that if you don't parse one, it doesn't fail: http://coliru.stacked-crooked.com/a/1518a0e0d5adde23

Output

    Parsing SUCCESS.
    Parsing SUCCESS.
    Parsing SUCCESS.
    Parsing SUCCESS.
    Parsing SUCCESS.
    Parsing SUCCESS.
    Parsing SUCCESS.
    Parsing SUCCESS.
    Parsing SUCCESS.
    Parsing SUCCESS.






------------------------------------------------------------------------------
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
[hidden email]
https://lists.sourceforge.net/lists/listinfo/spirit-general
Reply | Threaded
Open this post in threaded view
|

Re: skipper does not work

Jens Kallup
In reply to this post by Jens Kallup
Ok,
it will work fine.
But, can you provide an example, in which is
demostrating the use of (in context of my old posts):

"varA = 12.33"

That will be very usefull for me

TIA
Jens

------------------------------------------------------------------------------
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
[hidden email]
https://lists.sourceforge.net/lists/listinfo/spirit-general
Reply | Threaded
Open this post in threaded view
|

Re: skipper does not work

sehe
On 20-10-16 00:38, Jens Kallup wrote:
> "varA = 12.33"
I have no idea what you might be referring to. Regardless, you can lift
the skipper and drop it into your bigger code.
> That will be very usefull for me

I've written a reply to your other (top) post, giving you some useful
directions if you want to help us help you.


Regards,

Seth


------------------------------------------------------------------------------
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
[hidden email]
https://lists.sourceforge.net/lists/listinfo/spirit-general