我仍然不确定您希望我们如何了解 glsl 的全部内容。所以我真的只能对实际的输入格式做一个大致的猜测。
假设我以我认为合适的最简单的方式来解释这一点(没有荒谬的无用):
annot = "@bind" >> ident >> eol;
declaration =
omit [ +(ident >> !char_(';')) ] // omit the type, TODO
>> ident >> ';' >> eol;
现在,我们只需要一种简单的方法来忽略整行,直到找到包含注释的行:
ignore = !annot >> *(char_ - eol) >> eol;
如果您想忽略@bind
后面没有声明的行,您可能需要使用!combi
而不是!annot
.
这对你来说只是一个开始。此外,并非所有这些对可忽略行的“隐含”定义都可能导致大量回溯。所以不要指望一流的性能。
#define BOOST_SPIRIT_DEBUG
#include <boost/spirit/include/qi.hpp>
#include <boost/fusion/adapted.hpp>
#include <map>
namespace qi = boost::spirit::qi;
typedef std::map<std::string, std::string> Map;
template <typename It>
struct grammar : qi::grammar<It, Map(), qi::blank_type>
{
grammar() : grammar::base_type(start)
{
using namespace qi;
ident = lexeme [ alpha >> *alnum ];
annot = "@bind" >> ident >> eol;
declaration =
omit [ +(ident >> !char_(';')) ] // omit the type, TODO
>> ident >> ';' >> eol;
ignore = !annot >> *(char_ - eol) >> eol;
combi = annot >> declaration;
start = *ignore >> combi % *ignore;
BOOST_SPIRIT_DEBUG_NODE(start);
BOOST_SPIRIT_DEBUG_NODE(combi);
BOOST_SPIRIT_DEBUG_NODE(ignore);
BOOST_SPIRIT_DEBUG_NODE(declaration);
BOOST_SPIRIT_DEBUG_NODE(annot);
BOOST_SPIRIT_DEBUG_NODE(ident);
}
private:
qi::rule<It, qi::blank_type> ignore;
qi::rule<It, std::string(), qi::blank_type> ident, declaration, annot;
qi::rule<It, std::pair<std::string, std::string>(), qi::blank_type> combi;
qi::rule<It, Map(), qi::blank_type> start;
};
template <typename It>
void test(It f, It l)
{
grammar<It> p;
Map mappings;
bool ok = qi::phrase_parse(f, l, p, qi::blank, mappings);
if (ok)
{
for (auto it = mappings.begin(); it!=mappings.end(); ++it)
std::cout << "'" << it->second << "' annotated with name '" << it->first << "'\n";
}
if (f!=l)
std::cerr << "warning: remaing unparsed: '" << std::string(f,l) << "'\n";
}
int main()
{
const std::string input(
"#include <reality>\n"
"@bind VarName\n"
"uniform int myVariable;\n"
"// other stuff\n"
"@bind Var2Name\n"
"uniform int myVariable2;\n");
test(input.begin(), input.end());
}
这将打印:
'myVariable2' annotated with name 'Var2Name'
'myVariable' annotated with name 'VarName'
在liveworkspace.org上实时查看详细 (DEBUG) 输出