解析与提振精神逃脱串 [英] Parsing escaped strings with boost spirit

查看:100
本文介绍了解析与提振精神逃脱串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

I'm与精神2.4工作,我会想解析这样的结构:

I´m working with Spirit 2.4 and I'd want to parse a structure like this:

文字{text_field};

Text{text_field};

问题的关键是,在text_field是一个转义字符串与符号'{','}'和'\\'。
我想创建一个解析器使用这个气。我一直在想这样的:

The point is that in text_field is a escaped string with the symbols '{', '}' and '\'. I would like to create a parser for this using qi. I've been trying this:

using boost::spirit::standard::char_;
using boost::spirit::standard::string;
using qi::lexeme;
using qi::lit;

qi::rule< IteratorT, std::string(), ascii::space_type > text;
qi::rule< IteratorT, std::string(), ascii::space_type > content;
qi::rule< IteratorT, std::string(), ascii::space_type > escChar;


text %= 
  lit( "Text" ) >> '{' >>
    content >>
  "};"
  ;

content %= lexeme[ +( +(char_ - ( lit( '\\' ) | '}' ) )  >> escChar ) ];

escChar %= string( "\\\\" ) 
  | string( "\\{" ) 
  | string( "\\}" );

但不编译。任何想法?

But doesn't even compile. Any idea?

推荐答案

您的语法可以写成:

qi::rule< IteratorT, std::string(), ascii::space_type > text; 
qi::rule< IteratorT, std::string() > content;   
qi::rule< IteratorT, char() > escChar;   

text = "Text{" >> content >> "};";  
content = +(~char_('}') | escChar); 
escChar = '\\' >> char_("\\{}");

即。


  • 文本的是文本{之后的内容,然后按}

  • text is Text{ followed by content followed by }

内容的是至少有一个实例
    或者是字符(但没有} )或
    一个escChar

content is at least one instance of either a character (but no }) or an escChar

escChar 的是单个转义 \\\\ {}

请注意,在 escChar 的规则现在返回单个字符,并丢弃逃逸 \\\\ 。我不知道如果这就是你所需要的。此外,我删除了队长的的内容的和的 escChar 的规则,允许离开关语义[] (没有队长的规则就像一个隐含的语义)。

Note, the escChar rule now returns a single character and discards the escaping \\. I'm not sure if that's what you need. Additionally, I removed the skipper for the content and escChar rules, which allows to leave off the lexeme[] (a rule without skipper acts like an implicit lexeme).

这篇关于解析与提振精神逃脱串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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