boost-spirit

How to write a boost::spirit::qi parser to parse an integer range from 0 to std::numeric_limits<int>::max()?

佐手、 提交于 2020-01-15 05:58:09
问题 I tried to use qi::uint_parser<int>() . But it is the same like qi::uint_ . They all match integers range from 0 to std::numeric_limits<unsigned int>::max() . Is qi::uint_parser<int>() designed to be like this? What parser shall I use to match an integer range from 0 to std::numeric_limits<int>::max() ? Thanks. 回答1: Simplest demo, attaching a semantic action to do the range check: uint_ [ _pass = (_1>=0 && _1<=std::numeric_limits<int>::max()) ]; Live On Coliru #include <boost/spirit/include

How to write a boost::spirit::qi parser to parse an integer range from 0 to std::numeric_limits<int>::max()?

落花浮王杯 提交于 2020-01-15 05:57:29
问题 I tried to use qi::uint_parser<int>() . But it is the same like qi::uint_ . They all match integers range from 0 to std::numeric_limits<unsigned int>::max() . Is qi::uint_parser<int>() designed to be like this? What parser shall I use to match an integer range from 0 to std::numeric_limits<int>::max() ? Thanks. 回答1: Simplest demo, attaching a semantic action to do the range check: uint_ [ _pass = (_1>=0 && _1<=std::numeric_limits<int>::max()) ]; Live On Coliru #include <boost/spirit/include

Generate string if boolean attribute is true (karma counterpart to qi::matches)

孤人 提交于 2020-01-13 18:55:24
问题 Imagine we want to parse and generate simple C++ member function declarations with Boost.Spirit. The Qi grammar might look like this: function_ %= type_ > id_ > "()" > matches["const"]; That means, whether the function is const is stored in a bool . How to write the corresponding generator with Karma? function_ %= type_ << ' ' << id_ << "()" << XXX[" const"]; Here, we want a directive that consumes a boolean attribute, executes the embedded generator if the attribute is true and does nothing

Generate string if boolean attribute is true (karma counterpart to qi::matches)

≯℡__Kan透↙ 提交于 2020-01-13 18:55:10
问题 Imagine we want to parse and generate simple C++ member function declarations with Boost.Spirit. The Qi grammar might look like this: function_ %= type_ > id_ > "()" > matches["const"]; That means, whether the function is const is stored in a bool . How to write the corresponding generator with Karma? function_ %= type_ << ' ' << id_ << "()" << XXX[" const"]; Here, we want a directive that consumes a boolean attribute, executes the embedded generator if the attribute is true and does nothing

Attempting to parse SQL like statement with Boost-Spirit

帅比萌擦擦* 提交于 2020-01-11 10:22:06
问题 I'm a newbie in boost::spirit. I wrote the program to parse a SQL statement, like "select * from table where conditions". It compile failed. A large of template errors reported. So, would somebody help me? #include <iostream> #include <string> #include <boost/spirit/include/qi.hpp> namespace qi = boost::spirit::qi; namespace ascii = boost::spirit::ascii; struct db_select { void exec() {} std::string filed; std::string table; std::string condition; }; std::ostream& operator<<(std::ostream& os,

Attempting to parse SQL like statement with Boost-Spirit

一个人想着一个人 提交于 2020-01-11 10:21:32
问题 I'm a newbie in boost::spirit. I wrote the program to parse a SQL statement, like "select * from table where conditions". It compile failed. A large of template errors reported. So, would somebody help me? #include <iostream> #include <string> #include <boost/spirit/include/qi.hpp> namespace qi = boost::spirit::qi; namespace ascii = boost::spirit::ascii; struct db_select { void exec() {} std::string filed; std::string table; std::string condition; }; std::ostream& operator<<(std::ostream& os,

Define parsers parameterized with sub-parsers in Boost Spirit

爱⌒轻易说出口 提交于 2020-01-11 05:22:06
问题 I would like to convert some old hand-written parsing code to Boost Spirit and learn (more of) spirit in the process. The old code uses streams and templates to parse definitions for some data-types and some containers. Some typical formats: VECTOR[number_of_items,(item_1, item_2 .... item_n)] PAIR(p1, p2) RECT[(left,top)-(right,bottom)] Point( x, y ) Size( x, y ) The parsing functions are templates with the type of the items as template parameter and use streams as input, e.g. template<class

Boost.x3: attribute accumulates between alternatives

你离开我真会死。 提交于 2020-01-10 03:15:30
问题 I have a parser for parsing an Identifier like foo, bar, baz and one for parsing also nested identifiers like foo::bar, foo::bar.baz, foo::bar.baz.baham They both parse into the same ast struct, which looks like this: struct identifier : x3::position_tagged{ std::vector <std::string> namespaces; std::vector <std::string> classes; std::string identifier; }; The parser for an identifier looks like this: #define VEC_ATR x3::attr(std::vector<std::string>({})) //ugly hack auto const identifier_def

How do you create a generic parser using qi?

我的梦境 提交于 2020-01-07 06:25:16
问题 I am attempting to create generic parser-elements using qi as I unfortunately (MSVC must be supported) can not use X3. The idea is to have a templated struct: template<class T> struct parse_type; Which I could use like this: template<class T> T from_string(std::string const& s) { T res; parse_type<T> t; ... if (phrase_parse(...,parse_type<T>(),...,t)) } or specialise like this template<class T,class Alloc> struct parse_type<std::vector<T,Alloc>> { // Parse a vector using rule '[' >> parse

How do you create a generic parser using qi?

本小妞迷上赌 提交于 2020-01-07 06:25:05
问题 I am attempting to create generic parser-elements using qi as I unfortunately (MSVC must be supported) can not use X3. The idea is to have a templated struct: template<class T> struct parse_type; Which I could use like this: template<class T> T from_string(std::string const& s) { T res; parse_type<T> t; ... if (phrase_parse(...,parse_type<T>(),...,t)) } or specialise like this template<class T,class Alloc> struct parse_type<std::vector<T,Alloc>> { // Parse a vector using rule '[' >> parse