tzlaine / parser

A C++ parser combinator library.
Boost Software License 1.0
70 stars 12 forks source link

Should `lexeme` turn `tuple<char...>` into `string`? #57

Closed akrzemi1 closed 6 months ago

akrzemi1 commented 6 months ago

While I accept the rationale in https://github.com/tzlaine/parser/issues/54, I still expect that when I wrap a sequence of chars into a lexeme, I would get a string. Isn't "lexem" implying a one whole?

Feel free to close it, if you disagree with my reasoning. The following is an example demonstrating this.

#ifdef WITH_BOOST_SPIRIT
#include <boost/fusion/adapted/struct/adapt_struct.hpp>
#include <boost/spirit/include/qi.hpp>
namespace bp = boost::spirit::qi;
#else
#include <boost/parser/parser.hpp>
namespace bp = boost::parser;
#endif

int main()
{
#ifdef WITH_BOOST_SPIRIT
  auto alnum = bp::ascii::alnum;
  auto alpha = bp::alpha;
#else
  auto alnum = bp::ascii::alnum;
  auto alpha = bp::ascii::alpha;
#endif

  auto myParser = bp::lexeme[(bp::ascii::alnum | bp::char_('_')) >> bp::ascii::digit];

  std::string input = "m1";
  std::string result;

#ifdef WITH_BOOST_SPIRIT
  auto begin = input.begin();
  bool b = bp::phrase_parse(begin, input.end(), myParser, bp::ascii::space, result);
#else
  auto b = bp::parse(input, myParser, bp::ws, result);
#endif

  if (b)
    std::cout << ":-) " << result << std::endl;
  else
    std::cout << ":-(" << std::endl;
}
tzlaine commented 6 months ago

I think this is now superseded by #55.