The Select Parser 选择分析器 |
Select parsers may be used to identify a single parser from a given list of parsers, which successfully recognizes the current input sequence. Example:
选择分析器可用于从给定的分析器表中选择一个成功识别当前输入的分析器。例子:
rule<> rule_select = select_p ( parser_a , parser_b /* ... */ , parser_n );
The parsers (parser_a, parser_b etc.) are tried sequentially from left to right until a parser matches the current input sequence. If there is a matching parser found, the select_p parser returns the parser's position (zero based index). For instance, in the example above, 1 is returned if parser_b matches.
列表中的分析器(parser_a, parser_b 等等)将从左到右逐个测试,直到有一个分析器匹配当前输入。如果找到一个匹配的分析器,select_p将返回分析器的位置(零基数索引)。比如,上面的例子里,parser_b匹配时返回的是1。
There are two predefined parsers of the select parser family: select_p and select_fail_p. These parsers differ in the way the no match case is handled (when none of the parsers match the current input sequence). While the select_p parser will return -1 if no matching parser is found, the select_fail_p parser will not match at all.
在选择分析器族中有两个预定义的分析器:select_p和select_fail_p。这些分析器的不同之处在于无匹配情况的处理上(即没有分析器匹配当前输入串)。如果没有匹配,select_p分析器返回-1而select_fail_p分析器将完全不匹配。
The following sample shows how the select parser may be used very conveniently in conjunction with a switch parser:
下面的例子展示了选择分析器是如何便利地与跳转分析器一起使用的:
int choice = -1; rule<> rule_select = select_fail_p('a', 'b', 'c', 'd')[assign_a(choice)] >> switch_p(var(choice)) [
case_p<0>(int_p),
case_p<1>(ch_p(',')),
case_p<2>(str_p("bcd")),
default_p
]
;
This example shows a rule, which matches:
这个例子里的规则将匹配:
- 'a' followed by an integer 'a'之后紧接着整数
- 'b' followed by a ',' 'b'之后跟着a
- 'c' followed by "bcd" 'c'后面跟着"bcd"
- a single 'd'. 单独的"b"
For other input sequences the give rule does not match at all.
对于其他的输入分析器完全不匹配。
BOOST_SPIRIT_SELECT_LIMIT select_p中的条目的数量受限于Spirit的编译期常量BOOST_SPIRIT_SELECT_LIMIT,默认值为3。这个值不能比编译时常量PHOENIX_LIMIT (见 phoenix)更大。例子: // Define these before including anything else |
Copyright © 2003-2004 Hartmut Kaiser
Use, modification and distribution is subject to the Boost Software License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
Powered by Zoundry