Boost spirit lex将令牌值写回输入流 [英] Boost spirit lex write token value back to input stream

查看:221
本文介绍了Boost spirit lex将令牌值写回输入流的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有一个方法在boost :: spirit :: lex写一个标记值回输入流(可能在编辑后)和再次重新扫描。我基本上找的是一个功能,像由unput()在Flex中提供的。

I'm wondering if there's a way in boost::spirit::lex to write a token value back to the input stream (possibly after editing) and rescanning again. What I'm basically looking for is a functionality like that offered by unput() in Flex.

谢谢!

推荐答案

我最终实现了自己的unput()功能,如下所示:

I ended up implementing my own unput() functionality as follows:

   struct unputImpl
   {
      template <typename Iter1T, typename Iter2T, typename StrT>
      struct result {
         typedef void type;
      };

      template <typename Iter1T, typename Iter2T, typename StrT>
      typename result<Iter1T, Iter2T, StrT>::type operator()(Iter1T& start, Iter2T& end, StrT str) const {
         start -= (str.length() - std::distance(start, end));
         std::copy(str.begin(), str.end(), start);
         end = start;
      }
   };

   phoenix::function<unputImpl> const unput = unputImpl();

这样可以像下面这样使用:

This can then be used like:

   this->self += lex::token_def<lex::omit>("{SYMBOL}\\(")
        [
           unput(_start, _end, "(" + construct<string>(_start, _end - 1) + " "),
           _pass = lex::pass_flags::pass_ignore
        ];

如果未输入的字符串长度大于匹配的标记长度,它将覆盖先前解析的输入。注意的是在开始处确保输入字符串具有足够的空间来处理对于第一个匹配的令牌调用unput()的情况。

If the unputted string length is bigger than the matched token length, it will override some of the previously parsed input. The thing you need to take care of is to make sure the input string has sufficient empty space at the very beginning to handle the case where unput() is called for the very first matched token.

这篇关于Boost spirit lex将令牌值写回输入流的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆