I want to parse a file into an AST using Boost Spirit.
The root of my AST is a class with only one attribute :
typedef boost::variant<FunctionDeclaration, GlobalVariableDeclaration> FirstLevelBlock;
struct Program {
std::vector<FirstLevelBlock> blocks;
};
BOOST_FUSION_ADAPT_STRUCT(
::Program,
(std::vector<eddic::FirstLevelBlock>, blocks)
)
If I parse using a single rule :
program %= *(function | globalDeclaration);
it doesn’t compiles, but if I add a single string name to Program, it works well. I could use the vector as the root, but I want to use the class, because I want to add some methods to the Program class.
EDIT :
If I surround my program with braces, it works well :
program %= lexer.left_brace >> *(function | globalDeclaration) >> lexer.right_brace;
compiles and works fine, but :
program %= *(function | globalDeclaration);
does not compile…
Is there something in Boost Spirit that prevent using such simple rules ?
Edited question version 2
Firstly, we can’t really tell without the defintion for
functionandglobalDeclaration.Secondly I tried, changing my PoC lines to
Lo and behold, I get your compiler error! Now I would certainly agree that this looks very much like an attribute conversion bug. Also, Here is a preliminary workaround:
As you can see,
qi::epsto the rescueAnswer to original question version 1
Mmm. I think you need to post a minimal working sample. Here is a proof of concept starting from your question, and it all works rather nicely.
Note that I compiled with
g++ -std=c++0xin order to get the defaultAttrparameter argument on thetestfunction.Output: