如何在精神X3中添加条件期望点

时间:2016-08-20 11:51:52

标签: c++ parsing c++14 boost-spirit boost-spirit-x3

我目前正在X3中为我的语法添加期望点。 现在我遇到了一个看似这样的规则。

-(void)addImagesToScrollView:(NSMutableArray*)jsonArray{


if (!jsonArray || jsonArray.count ==0) {
    return;
}

int imageXPos=0;

int imageWidth=50;
int imageHeight=50;

for (int index=0; index<[jsonArray count]; index++)
{
    NSMutableDictionary *dict=[jsonArray objectAtIndex:index];
    NSString *thumbUrl=[JsonUtil getString:dict forKey:@"thumb"];

    UIImageView *imageView = [[UIImageView alloc] init ];
    [imageView setImageWithURL:[NSURL URLWithString:thumbUrl]
              placeholderImage:[UIImage imageNamed:@"place_image"]];
    imageView.contentMode = UIViewContentModeScaleToFill;
    imageView.clipsToBounds = YES;

    imageView.frame = CGRectMake(imageXPos,1, imageWidth, imageHeight);

    [self.placeImageScrollView addSubview:imageView];

    imageXPos = imageXPos + imageView.frame.size.width +2;//2 is padding

}
 self.placeImageScrollView.contentSize = CGSizeMake(imageXPos, self.placeImageScrollView.frame.size.height);
}

我想知道如何在此规则中添加条件期望点。 喜欢&#34;如果有&#34; ::&#34;然后那里有一个auto const id_string = +x3::char("A-Za-z0-9_); auto const nested_identifier_def = x3::lexeme[ *(id_string >> "::") >> *(id_string >> ".") >> id_string ]; &#34;或者&#34;当有id_string时,那么就有.&#34; 等等。 我怎样才能为这样的规则实现这样的行为?

1 个答案:

答案 0 :(得分:2)

我会按照你想要的方式写出来:

auto const identifier 
    = lexeme [+char_("A-Za-z0-9_")];

auto const qualified_id 
    = identifier >> *("::" > identifier);

auto const simple_expression // only member expressions supported now
    = qualified_id >> *('.' > identifier);

使用相应的AST:

namespace AST {
    using identifier = std::string;

    struct qualified_id : std::vector<identifier> { using std::vector<identifier>::vector; };

    struct simple_expression {
        qualified_id lhs;
        std::vector<identifier> rhs;
    };
}

现场演示

<强> Live On Coliru

#include <iostream>
#include <string>
#include <vector>

namespace AST {
    using identifier = std::string;

    struct qualified_id : std::vector<identifier> { using std::vector<identifier>::vector; };

    struct simple_expression {
        qualified_id lhs;
        std::vector<identifier> rhs;
    };
}

#include <boost/fusion/adapted.hpp>
BOOST_FUSION_ADAPT_STRUCT(AST::simple_expression, lhs, rhs)

#include <boost/spirit/home/x3.hpp>

namespace Parser {
    using namespace boost::spirit::x3;

    auto const identifier 
        = rule<struct identifier_, AST::identifier> {}
        = lexeme [+char_("A-Za-z0-9_")];

    auto const qualified_id 
        = rule<struct qualified_id_, AST::qualified_id> {}
        = identifier >> *("::" > identifier);

    auto const simple_expression // only member expressions supported now
        = rule<struct simple_expression_, AST::simple_expression> {}
        = qualified_id >> *('.' > identifier);
}

int main() {
    using It = std::string::const_iterator;

    for (std::string const input : { "foo", "foo::bar", "foo.member", "foo::bar.member.subobject" }) {
        It f = input.begin(), l = input.end();
        AST::simple_expression data;

        bool ok = phrase_parse(f, l, Parser::simple_expression, boost::spirit::x3::space, data);

        if (ok) {
            std::cout << "Parse success: ";
            for (auto& el : data.lhs) std::cout << "::" << el;
            for (auto& el : data.rhs) std::cout << "." << el;
            std::cout << "\n";
        }
        else {
            std::cout << "Parse failure ('" << input << "')\n";
        }

        if (f != l)
            std::cout << "Remaining unparsed input: '" << std::string(f, l) << "'\n";
    }
}

打印

Parse success: ::foo
Parse success: ::foo::bar
Parse success: ::foo.member
Parse success: ::foo::bar.member.subobject