使用 boost 精神解析转义字符串

发布于 2024-09-29 01:22:59 字数 743 浏览 2 评论 0原文

我正在使用 Spirit 2.4,我想解析这样的结构:

Text{text_field};

要点是,text_field 中是一个带有符号“{”、“}”和“\”的转义字符串。 我想使用 qi 为此创建一个解析器。我一直在尝试这个:

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( "\\}" );

但甚至无法编译。有什么想法吗?

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

Text{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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

木格 2024-10-06 01:22:59

您的语法可以写为:

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_("\\{}");

  • textText{ 后跟内容,后跟 }

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

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

注意,escChar规则现在返回单个字符并丢弃转义的 \\。我不确定这是否是您所需要的。此外,我删除了 contentescChar 规则的船长,这允许省略 lexeme[] (没有船长的规则就像隐式词位)。

Your grammar could be written as:

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_("\\{}");

i.e.

  • text is Text{ followed by content followed by }

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

  • escChar is a single escaped \\, {, or }

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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文