2016-07-22 41 views
2

我試圖使用升壓精神X3解析字符串成一個結構:解析字符串後面的字符串與精神X3名單列表

struct identifier { 
    std::vector<std::string> namespaces; 
    std::vector<std::string> classes; 
    std::string identifier; 

}; 

現在我有一個解析器規則匹配的字符串像這個:

foo::bar::baz.bla.blub 
foo.bar 
boo::bar 
foo 

我的解析器規則看起來像這樣。

auto const nested_identifier_def = 
     x3::lexeme[ 
       -(id_string % "::") 
       >> -(id_string % ".") 
       >> id_string 
     ]; 

其中id_string解析的alphanum組合。 我知道這個規則不能解析我想要的,因爲在解析foo.bar時,例如規則-(id_string % ".")的這部分消耗了整個字符串。 我如何更改規則在結構中正確解析?

回答

3

假設你id_string是這樣的:

auto const id_string = x3::rule<struct id_string_tag, std::string>{} = 
    x3::lexeme[ 
      (x3::alpha | '_') 
     >> *(x3::alnum | '_') 
    ]; 

話,我覺得這是你追求的:

auto const nested_identifier_def = 
     *(id_string >> "::") 
    >> *(id_string >> '.') 
    >> id_string; 

Online Demo

問題是p % delimit是簡寫爲p >> *(delimit >> p),即它總是消耗一個p之後的分隔符。但是,您要的是*(p >> delimit),因此在分隔符之後不會使用p,而是將其留給下一個規則。