我有一个语法,其中包含针对规则集输入的关键字。
下面的一些伪代码..如果有人现在输入“XPUBLIC”作为输入,则解析器会在catch处理程序中为“boost::spirit::qi::expectation_failureparser::Iterator::what_”创建一个“distinct”异常。
可以,但是解析器还可以返回此节点上可能的条目的列表。同样,on_error处理程序获得相等的输入。有没有办法使解析器上的可能的输入符号失败?在此示例中,我希望获得“PUBLIC”,“PRIVATE” ..
#define nocaselit(p) distinct(boost::spirit::standard_wide::alnum | L'_')[no_case[p]]
rule=
nocaselit(L"PUBLIC")
| nocaselit(L"PRIVATE")
| nocaselit(L"PROTECTED")
| nocaselit(L"SHARED")
最佳答案
当我们简化情况以跳过distinct
指令时,这就是您如何从期望失败中处理info
来提取替代方案的方法:
Live On Coliru
#include <boost/spirit/include/qi.hpp>
namespace enc = boost::spirit::standard_wide;
namespace qi = boost::spirit::qi;
using It = std::wstring::const_iterator;
#define kw(p) qi::lit(p)
int main() {
qi::rule<It> rule;
rule
= qi::eps >
( kw(L"PUBLIC")
| kw(L"PRIVATE")
| kw(L"PROTECTED")
| kw(L"SHARED")
)
;
for (std::wstring const input : {
L"PUBLIC",
L"PRIVATE",
L"PROTECTED",
L"SHARED",
L"XPUBLIC", // obviously no match
L"PUBLICX", // actually test of distinct
})
try {
It f = begin(input), l = end(input);
auto ok = qi::parse(f, l, rule);
std::wcout << input << " " << std::boolalpha << ok << std::endl;
} catch(qi::expectation_failure<It> const& ef) {
auto value = ef.what_.value;
auto elements = boost::get<std::list<boost::spirit::info> >(value);
std::ostringstream oss;
oss << ef.what_.tag << "(";
for (auto el : elements) oss << " " << el;
oss << " )";
std::wcout << input << " -> Expected " << oss.str().c_str() << std::endl;
}
}
版画PUBLIC true
PRIVATE true
PROTECTED true
SHARED true
XPUBLIC -> Expected alternative( "PUBLIC" "PRIVATE" "PROTECTED" "SHARED" )
PUBLICX true
Note that the internals of
spirit::info
assumestd::string
in UTF8 encoding. I'm not overly explicit here with codecvt facets. I'll leave a reliable conversion to wide strings as an exercise to the reader.
用
distinct()
天真的方法将导致以下输出:PUBLIC true
PRIVATE true
PROTECTED true
SHARED true
XPUBLIC -> Expected alternative( <distinct> <distinct> <distinct> <disti
nct> )
PUBLICX -> Expected alternative( <distinct> <distinct> <distinct> <disti
nct> )
可悲的是,这可能不是您想要的。更糟糕的是,这也在存储库指令中进行了硬编码:template <typename Context>
info what(Context& /*ctx*/) const
{
return info("distinct");
}
如果我们可以只是更改为:template <typename Context>
info what(Context& ctx) const
{
return info("distinct", subject.what(ctx));
}
我们本来可以解决的。为了不过多影响库的实现细节,让我们将distinct
指令子类化为my_distinct
:template <typename Subject, typename Tail, typename Modifier>
struct my_distinct_parser
: distinct_parser<Subject, Tail, Modifier>
{
using distinct_parser<Subject, Tail, Modifier>::distinct_parser;
template <typename Context> info what(Context& ctx) const {
return info("my_distinct", this->subject.what(ctx));
}
};
可悲的是,我们需要更多的繁文ta节来使事物在解析器编译和组合机制中注册:my_distinct.hpp
#pragma once
#include <boost/spirit/repository/home/qi/directive/distinct.hpp>
namespace boost::spirit::repository {
BOOST_SPIRIT_DEFINE_TERMINALS_NAME_EX(( my_distinct, my_distinct_type ))
}
namespace boost::spirit {
template <typename Tail>
struct use_directive<qi::domain
, terminal_ex<repository::tag::my_distinct, fusion::vector1<Tail> > >
: mpl::true_ {};
template <>
struct use_lazy_directive<qi::domain, repository::tag::my_distinct, 1>
: mpl::true_ {};
}
namespace boost::spirit::repository::qi {
using repository::my_distinct;
using repository::my_distinct_type;
template <typename Subject, typename Tail, typename Modifier>
struct my_distinct_parser
: distinct_parser<Subject, Tail, Modifier>
{
using distinct_parser<Subject, Tail, Modifier>::distinct_parser;
template <typename Context> info what(Context& ctx) const {
return info("my_distinct", this->subject.what(ctx));
}
};
}
namespace boost::spirit::qi {
template <typename Tail, typename Subject, typename Modifiers>
struct make_directive<
terminal_ex<repository::tag::my_distinct, fusion::vector1<Tail> >
, Subject, Modifiers>
{
typedef typename result_of::compile<qi::domain, Tail, Modifiers>::type
tail_type;
typedef repository::qi::my_distinct_parser<
Subject, tail_type, Modifiers> result_type;
template <typename Terminal>
result_type operator()(Terminal const& term, Subject const& subject
, Modifiers const& modifiers) const
{
return result_type(subject
, compile<qi::domain>(fusion::at_c<0>(term.args), modifiers));
}
};
}
namespace boost::spirit::traits {
template <typename Subject, typename Tail, typename Modifier>
struct has_semantic_action<
repository::qi::my_distinct_parser<Subject, Tail, Modifier> >
: unary_has_semantic_action<Subject> {};
}
现场演示Live On Wandbox
#include <boost/spirit/include/qi.hpp>
#include "my_distinct.hpp"
namespace enc = boost::spirit::standard_wide;
namespace qi = boost::spirit::qi;
namespace qr = boost::spirit::repository::qi;
using It = std::wstring::const_iterator;
#define kw(p) qr::my_distinct(enc::alnum | L'_') \
[ enc::no_case[p] ]
int main() {
qr::my_distinct(enc::alnum | L'_');
qi::rule<It> rule;
rule
= qi::eps >
( kw(L"public")
| kw(L"private")
| kw(L"protected")
| kw(L"shared")
)
;
for (std::wstring const input : {
L"PUBLIC",
L"private",
L"PROTECTED",
L"shared",
L"XPUBLIC", // obviously no match
L"PUBLICX", // actually test of my_distinct
})
try {
It f = begin(input), l = end(input);
auto ok = qi::parse(f, l, rule);
std::wcout << input << " " << std::boolalpha << ok << std::endl;
} catch(qi::expectation_failure<It> const& ef) {
auto value = ef.what_.value;
auto elements = boost::get<std::list<boost::spirit::info> >(value);
std::ostringstream oss;
oss << ef.what_.tag << "(";
for (auto el : elements) oss << " " << el;
oss << " )";
std::wcout << input << " -> Expected " << oss.str().c_str() << std::endl;
}
}
版画PUBLIC true
private true
PROTECTED true
shared true
XPUBLIC -> Expected alternative( "public" "private" "protected" "shared" )
PUBLICX -> Expected alternative( "public" "private" "protected" "shared" )
关于c++ - 如何在错误消息 'distinct'可能的符号中获得增强精神?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65087474/