boost-spirit

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:

Passing each element of a parsed sequence to a function that returns a rule's attribute type

牧云@^-^@ 提交于 2019-12-12 14:16:31
问题 I want to parse CSS color functions (for simplicity, all of the arguments are numbers between 0 and 255) rgb(r,g,b) rgba(r,g,b,a) hsl(h,s,l) hsla(h,s,l,a) into struct color { color(std::uint8_t r, std::uint8_t g, std::uint8_t b, std::uint8_t a) : red{r}, green{g}, blue{b}, alpha{a} {} static color hsl(std::uint8_t h, std::uint8_t s, std::uint8_t l, std::uint8_t a) { ... } std::uint8_t red; std::uint8_t green; std::uint8_t blue; std::uint8_t alpha; } I have a working hsl function

In boost spirit, use of multi_pass with streaming file input, which iterator needed

孤街醉人 提交于 2019-12-12 13:30:52
问题 I want to input a significant size csv file to parse it with spirit qi (using boost 1.59.0). There are examples of this and it looks straight forward, but the obvious setup to this results in a compile error where the first parameter to qi::phrase_parse(...) is not accepted. What works here? (One example is at: How to pass the iterator to a function in spirit qi ) The code: #define BOOST_SPIRIT_DEBUG //#define BOOST_SPIRIT_DEBUG_PRINT_SOME 200 //#define BOOST_SPIRIT_DEBUG_OUT std::cerr

Avoid throwing expectation_failure when expectation parser fails

隐身守侯 提交于 2019-12-12 13:19:21
问题 How to avoid throwing an exception, when expectation parser fails? I have a rule "function" > (!x3::lexeme[keyword >> !(x3::alnum | '_')] >> symbol) > ('(' > -lvalue_list > ')') > statements > "end" to parse code like: function a() return one end keyword s is ( zero , one , function , return , end etc). If I feed the parser with function one() return zero end code, then in function expect_directive::parse exception thrown from here: if (!r) { boost::throw_exception( expectation_failure

boost spirit semantic action using non-void function objects

∥☆過路亽.° 提交于 2019-12-12 12:01:58
问题 In my semantic action I'm not just trying to print out what has been parsed. But the semantic action function is supposed to create some new object which in turn is supposed to be the value created by the parser. Let's assume the following task: The parser should get passed the address/reference to an object typedef std::pair< std::map<std::string, std::size_t>, std::map<std::size_t, std::string> > mapping; and the parser should convert all non-white-space strings into a std::size_t return a

boost spirit skipper - compile-time error

我只是一个虾纸丫 提交于 2019-12-12 11:07:37
问题 I have the following code: #include <boost/fusion/include/define_struct.hpp> #include <boost/spirit/include/qi.hpp> #include <iostream> #include <string> BOOST_FUSION_DEFINE_STRUCT( (), foo, (int, bar) (int, baz) ) template <typename Iterator> struct parser : boost::spirit::qi::grammar<Iterator, foo(), boost::spirit::qi::ascii::space_type> { parser() : parser::base_type(start) { start %= boost::spirit::qi::int_ >> boost::spirit::qi::int_; } boost::spirit::qi::rule<Iterator, foo(), boost:

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

basic boost spirit semantic action doesn't compile

余生长醉 提交于 2019-12-12 09:04:42
问题 I am trying to add a greater than operator > to a ast: the code is 95% identical to the code in the docs. Two points of interest below A block of code where I'm trying to write support for greater than: commented in the code below. A single line in the parse for term which fails to compile because I don't yet understand semantic actions yet: not sure how to bind the lhs of lhs > rhs through phoenix and semantic actions. The solution should be trivial for regular users of Spirit, but I am

Spirit parser segfaults

丶灬走出姿态 提交于 2019-12-12 04:38:14
问题 I'm getting a segfault when I run this. It looks like the debug prints, but when I debug it I just get an endless loop of backtrace. If anyone can help point me in the right direction, I'd appreciate it. I'd also appreciate, if possible any tips/tricks for cleaning up this grammar. Thanks! //code here: /*** *I-EBNF parser * *This defines a grammar for BNF. */ //Speeds up compilation times. //This is a relatively small grammar, this is useful. #define BOOST_SPIRIT_NO_PREDEFINED_TERMINALS

Implementing “NOT” in boost::spirit mini_c

别来无恙 提交于 2019-12-11 19:15:31
问题 I tried to modify the mini_c example of boost::spirit to match to my existing vocabulary. I therefore added a operator "NOT that should behave equal as "!": unary_expr = primary_expr | ("NOT" > primary_expr [op(op_not)]) // This does not work | ('!' > primary_expr [op(op_not)]) | ('-' > primary_expr [op(op_neg)]) | ('+' > primary_expr) ; I can compile the modified source code, but when i try to execute it it fails to parse. How can i solve this? EDIT: As my want to access external variables,