2

How does one prevent X3 symbol parsers from matching partial tokens? In the example below, I want to match "foo", but not "foobar". I tried throwing the symbol parser in a lexeme directive as one would for an identifier, but then nothing matches.

Thanks for any insights!

#include <string>
#include <iostream>
#include <iomanip>

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


int main() {

  boost::spirit::x3::symbols<int> sym;
  sym.add("foo", 1);

  for (std::string const input : {
      "foo",
      "foobar",
      "barfoo"
        })
    {
      using namespace boost::spirit::x3;

      std::cout << "\nParsing " << std::left << std::setw(20) << ("'" + input + "':");

      int v;
      auto iter = input.begin();
      auto end  = input.end();
      bool ok;
      {
        // what's right rule??

        // this matches nothing
        // auto r = lexeme[sym - alnum];

        // this matchs prefix strings
        auto r = sym;

        ok = phrase_parse(iter, end, r, space, v);
      }

      if (ok) {
        std::cout << v << " Remaining: " << std::string(iter, end);
      } else {
        std::cout << "Parse failed";
      }
    }
}
Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
KentH
  • 1,204
  • 1
  • 14
  • 23

1 Answers1

4

Qi used to have distinct in their repository.

X3 doesn't.

The thing that solves it for the case you showed is a simple lookahead assertion:

auto r = lexeme [ sym >> !alnum ];

You could make a distinct helper easily too, e.g.:

auto kw = [](auto p) { return lexeme [ p >> !(alnum | '_') ]; };

Now you can just parse kw(sym).

Live On Coliru

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

int main() {

    boost::spirit::x3::symbols<int> sym;
    sym.add("foo", 1);

    for (std::string const input : { "foo", "foobar", "barfoo" }) {

        std::cout << "\nParsing '" << input << "': ";

        auto iter      = input.begin();
        auto const end = input.end();

        int v = -1;
        bool ok;
        {
            using namespace boost::spirit::x3;
            auto kw = [](auto p) { return lexeme [ p >> !(alnum | '_') ]; };

            ok = phrase_parse(iter, end, kw(sym), space, v);
        }

        if (ok) {
            std::cout << v << " Remaining: '" << std::string(iter, end) << "'\n";
        } else {
            std::cout << "Parse failed";
        }
    }
}

Prints

Parsing 'foo': 1 Remaining: ''

Parsing 'foobar': Parse failed
Parsing 'barfoo': Parse failed
sehe
  • 374,641
  • 47
  • 450
  • 633
  • thanks a bunch. I was so close -- the '-' parser works while parsing chars. Needed to reread the (limited) docs for a look ahead parsers. bonus for the lambda help function! – KentH Nov 16 '15 at 04:42