Я пытаюсь разобрать простой SQL-запрос, используя boost spirit x3. Был похожий пост для предыдущей версии дух. Но с Spirit x3 нам не нужна грамматика.
Итак, вот моя попытка:
// SELECT chr, pos FROM table
// Select chr, pos FROM table WHERE a=5 and b = 6
// SELECT chr, pos FROM table WHERE a =5 and b=6 INSIDE region
// I would like to extract :
// vector<string> fields = {chr, pos}
// string tablename = "table"
// string where = "a=5 and b=6"
// string region = "region"
string source ="SELECT chr,pos FROM table WHERE a=5 AND b=6 INSIDE region";
// varname = chr, pos, table, region
auto varname = x3::rule<class varname, string>()
= x3::lexeme[(x3::alpha >> *(x3::alnum|'_'|'.'))];
// any character except "INSIDE"
auto condition = x3::rule<class condition, string>()
= x3::lexeme[*x3::char_]-"INSIDE";
// fields selection
auto selectRule = x3::rule<class fields, vector<string>> ()
= "SELECT" >> varname % ",";
// table name
auto fromRule = x3::rule<class fromRule, string>()
= "FROM" >> varname;
// where clause
auto whereRule = x3::rule<class whereRule, vector<string>>()
= "WHERE" >> condition ;
// inside clause
auto insideRule = x3::rule<class insideRule, string>()
= "INSIDE" >> varname;
auto begin = source.begin();
auto end = source.end();
vector<string> results;
x3::phrase_parse(begin,end,
selectRule >> fromRule >> -(whereRule >>-insideRule),
x3::space, results);
if (begin != end)
cout<<"COULD NOT PARSE"<<endl;
else
cout<<"parse succes"<<endl;
for (auto i : results)
cout<<i<<endl;
Он возвращает:
chr
pos
table
a=5 AND b=6 INSIDE region
results содержит все извлеченные данные атрибута, кроме данных атрибута insideRule, которые хранятся в строке условия. Любая подсказка, чтобы заставить мой парсер работать?