c++ - parsing 3 floats for glm::vec3 using boost::spirit::qi (error_invalid_expression) -
i can parse 1 float , print it. (test1
, test2
) somehow unable build rule parses 3 floats. final goal parse 3 floats , save them glm::vec3
.
- my rule seems incorrect:
qi::lexeme[qi::float_ << ' ' << qi::float_ << ' ' << qi::float_]
- i not sure if using
boost_fusion_adapt_struct
correctly
here source show came far:
#include <iostream> #include <string> #include <glm\glm.hpp> #include <boost\spirit\include\qi.hpp> #include <boost\fusion\include\adapt_struct.hpp> namespace qi = boost::spirit::qi; void test1() { std::string test = "1.2"; auto = test.begin(); if (qi::phrase_parse(it, test.end(), qi::float_, qi::space) && (it == test.end())) std::cout << "test 1 ok" << std::endl; else std::cout << "test 1 not ok" << std::endl; } void test2() { std::string test = "1.2"; auto = test.begin(); float f; if (qi::phrase_parse(it, test.end(), qi::float_, qi::space, f) && (it == test.end())) std::cout << "test 2 ok " << f << std::endl; else std::cout << "test 2 not ok" << std::endl; } void test3() { std::string test = "1.2 2.2 3.3"; qi::rule<std::string::iterator, qi::space_type> vec3; //error_invalid_expression vec3 = qi::lexeme[qi::float_ << ' ' << qi::float_ << ' ' << qi::float_]; auto = test.begin(); if (qi::phrase_parse(it, test.end(), vec3, qi::space) && (it == test.end())) std::cout << "test 3 ok " << std::endl; else std::cout << "test 3 not ok" << std::endl; } boost_fusion_adapt_struct( glm::vec3, (float, x) (float, y) (float, z) ) void test4() { std::string test = "1.2 2.2 3.3"; qi::rule<std::string::iterator, qi::space_type> vec3; //error_invalid_expression vec3 = qi::lexeme[qi::float_ << ' ' << qi::float_ << ' ' << qi::float_]; glm::vec3 result; auto = test.begin(); if (qi::phrase_parse(it, test.end(), vec3, qi::space, result) && (it == test.end())) { std::cout << "test 4 ok (" << result.x << ", " << result.y << ", " << result.z << ")"<< std::endl; } else std::cout << "test 4 not ok" << std::endl; } int main(int argc, char ** argv) { test1(); test2(); test3(); test4(); }
the test4
function contains try done.
edit:
as suggested 2 things need changed:
void test4() { std::string test = "1.2 2.2 3.3"; qi::rule<std::string::iterator, glm::vec3(), qi::space_type> vec3; //error_invalid_expression vec3 = qi::lexeme[qi::float_ >> ' ' >> qi::float_ >> ' ' >> qi::float_]; glm::vec3 result; auto = test.begin(); if (qi::phrase_parse(it, test.end(), vec3, qi::space, result) && (it == test.end())) { std::cout << "test 4 ok (" << result.x << ", " << result.y << ", " << result.z << ")"<< std::endl; } else std::cout << "test 4 not ok" << std::endl; }
the sequence parser operator >>
, use instead of <<
in rules
vec3 = qi::lexeme[qi::float_ >> ' ' >> qi::float_ >> ' ' >> qi::float_];
you've specified whitespace skipper in rule, can further simplified removing lexeme
directive , letting skipper skip spaces automatically (unless want ensure there single space character in between inputs).
vec3 = qi::float_ >> qi::float_ >> qi::float_;
also, if want rule return value, need add signature rule type
qi::rule<std::string::iterator, glm::vec3(), qi::space_type> vec3;
this unrelated compilation error you're seeing, use forward slashes in #include
directives, works on platforms.
#include <boost/spirit/include/qi.hpp>
Comments
Post a Comment