2016-06-21 66 views
2

我有興趣,如果有一種方法來解析成一套使用助推精神x3。背景是我有一串標記,每個標記代表一個枚舉值,現在我想創建一個解析器,解析每個標記是否至多在字符串中最多一次,如果我能夠獲得所有解析,這將是一種魅力令牌在分析時分成std::set。 要獲得枚舉從我使用的是symbol_table解析字符串返回:使用助推精神解析爲一套x3

enum class foo{bar, baz, bla, huh}; 

    struct enum_table : x3::symbols<foo> { 
     enum_table() { 
      add("bar", foo::bar) 
        ("baz", foo::baz) 
        ("huh", foo::huh); 
     } 
    } const enum_parser; 

回答

4

I am interested in if there is a way to parse into a set using boost spirit x3.

精神可以解析爲std::set<>開箱(至少在升壓1.61.0的),所以按照已經

std::set<foo> foos; 
x3::phrase_parse(
    input.begin(), input.end(), 
    +enum_parser, 
    x3::space, 
    foos 
); 

Online Demo

爲了讓你:你已經證明了作品類型[R解析器在遇到重複失敗了,這是最容易實現的semantic actions

std::set<foo> foos; 
auto insert_foo_or_fail = [&foos](auto& ctx) { 
    _pass(ctx) = foos.insert(_attr(ctx)).second; 
}; 
x3::phrase_parse(
    input.begin(), input.end(), 
    +x3::omit[enum_parser[insert_foo_or_fail]], 
    x3::space 
); 

Online Demo

+0

哇哦didnt知道它可以解析爲一組開箱。感謝與語義行動的技巧 – Exagon

+1

這不適用於boost版本1.60.0我得到一個錯誤:»class std :: set «沒有名爲»push_back«'的成員,所以這真的可以工作,因爲升壓版本1.61 0.0 – Exagon