boost-spirit-karma

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

Output of a boost::variant type using boost::spirit::karma

左心房为你撑大大i 提交于 2019-12-24 13:19:46
问题 I'm trying to output parameters, they can either be a single parameter or a vector of parameters. The following code does not what I'd like it to do: #include <iostream> #include <string> #include <boost/variant.hpp> #include <boost/spirit/include/karma.hpp> namespace karma = boost::spirit::karma; typedef std::vector<int> ParameterList; typedef boost::variant<int, ParameterList> Parameter; main() { using karma::int_; using karma::eol; using karma::lit; std::string generated; std::back_insert

reuse parsed variable with boost karma

拈花ヽ惹草 提交于 2019-12-23 19:39:01
问题 I have a code base which is quite equivalent to the code below. I try to generate a text file with two times the content of a variable. I feel that the answer is in semantic actions and _a and _val but cannot manage to get through even with the documentation. How will you do to have : "toto" in str and output : toto some stuff toto i.e how to reuse a parsed variable in karma ? struct data { std::string str; }; BOOST_FUSION_ADAPT_STRUCT( data, (std::string, str) ) template <typename Iterator>

Use karma to generate output for a vector of pointers

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-23 18:39:45
问题 I'm having some trouble using karma to generate output for a struct that is held in a vector of boost::shared_ptrs. I've got a small test case using ints that doesn't compile. I was thinking I could use the deref_iterator customization point to handle this case or that perhaps that out-of-the-box spirit would notice that my container held a pointer type and do the extra dereference. Anyway here's the test case: #include <boost/spirit/include/karma.hpp> #include <boost/shared_ptr.hpp> #include

Inconsistent Generator directive column behavior in boost karma

↘锁芯ラ 提交于 2019-12-20 03:48:21
问题 I am writing a karma generator to generate a HTML page and I am experiencing inconsistent behavior while using column directive. It could very well be my understanding of how it works. Basically I am generating a grid which requires me to insert some delimiters after every 2 occurrences of the data. The below is a basic program I adopted to do a test run. #include <boost/config/warning_disable.hpp> #include <boost/spirit/include/karma.hpp> #include <boost/fusion/include/struct.hpp> #include

casting attribute to boost::variant

廉价感情. 提交于 2019-12-13 02:40:21
问题 While learning how to use boost spirit, phoenix and fusion libraries, I came accross this minimal example that does not compile on msvc (2015, version 14) and boost 1.61.0 #include <boost/spirit/include/karma.hpp> #include <boost/variant/variant.hpp> namespace ka = boost::spirit::karma; struct U /* a kind of union (legacy code)*/ { bool kind; double foo; /* if kind = true */ size_t bar; /* if kind = false */ }; typedef boost::variant<double, size_t> UVariant; namespace boost { namespace

Generating default value when none is found

狂风中的少年 提交于 2019-12-12 14:23:24
问题 I have an input vector that can have any size between empty and 3 elements. I want the generated string to always be 3 floats separated by spaces, where a default value is used if there aren't enough elements in the vector. So far I've managed to output only the contents of the vector: #include <iostream> #include <iterator> #include <vector> #include "boost/spirit/include/karma.hpp" namespace karma = boost::spirit::karma; namespace phx = boost::phoenix; typedef std::back_insert_iterator<std:

Boost Karma - non consuming predicate

天涯浪子 提交于 2019-12-12 09:56:37
问题 I need to print a std::complex but omitting imaginary part if it's equal zero. So I have a rule with two productions: karma::rule<OutputIterator, std::complex<double>()> complexRule = '(' << double_ << ", " double_ << ')' | double_ << omit[double_]; This way Karma will always choose the first production, so I need some kind of predicate which will make a decission. Boost Karma tutorial comes with that solution which requires adapting std::complex as a three element tuple. BOOST_FUSION_ADAPT

boost karma semantic action call map

最后都变了- 提交于 2019-12-11 07:59:10
问题 I need to map values to a std::string ( with the following map, and BOOST_FUSION_ADAPT_STRUCT ) std::map< TYPEX, std::string> author2name; struct Emp { std::string name; TYPEX author; }; With the following code i want to generate my output: karma::rule< it, std::string()> quote = '"' >> karma::string >> '"'; karma::rule< it, Emp> emp = karma::delimit('\t')[ quite << quite[ author2name[ karma::_1] ]]; Emp x; karma::generate( std::ostream_iterator<char>(std::cout), emp, x); But it doesn't