开发者

boost::Spirit Grammar for unsorted schema

开发者 https://www.devze.com 2022-12-18 15:04 出处:网络
I have a section of a schema开发者_如何学JAVA for a model that I need to parse. Lets say it looks like the following.

I have a section of a schema开发者_如何学JAVA for a model that I need to parse. Lets say it looks like the following.

{
  type = "Standard";
  hostname="x.y.z";
  port="123";
}

The properties are:

  1. The elements may appear unordered.
  2. All elements that are part of the schema must appear, and no other.
  3. All of the elements' synthesised attributes go into a struct.
  4. (optional) The schema might in the future depend on the type field -- i.e., different fields based on type -- however I am not concerned about this at the moment.


According to the Spirit forums, the following is the answer.

You might want to have a look at the permutation parser:

a ^ b ^ c 

Which matches a or b or c (or a combination thereof) in any sequence.

If the objective is to parse into a struct, than the best way to test weather all essential members have been initialized, the struct members should be wrapped with boost::optional<> The attribute presence may then be easily tested post-parsing during run-time.

0

精彩评论

暂无评论...
验证码 换一张
取 消