boost::spirit::lex 不区分大小写的关键字

Case-insensitive keywords with boost::spirit::lex

有没有办法不区分大小写地识别特定模式?

例如如果我有

literal_bool = L"True|False";
this->self.add(literal_bool, TokenId_LiteralBool);

如何匹配 trueTRUEtRuE,同时避免为每个关键字写 [Tt][Rr][Uu][Ee]

Regular expressions supported by boost::spirit::lex 包括区分大小写的控件:

(?r-s:pattern)

apply option 'r' and omit option 's' while interpreting pattern. Options may be zero or more of the characters 'i' or 's'. 'i' means case-insensitive. '-i' means case-sensitive. 's' alters the meaning of the '.' syntax to match any single character whatsoever. '-s' alters the meaning of '.' to match any character except '\n'.

因此你可以这样写:

literal_bool = L"(?i:true|false)";
this->self.add(literal_bool, TokenId_LiteralBool);

原回答

引入一个使模式不区分大小写的函数:

literal_bool = L"True|False";
this->self.add(make_case_insensitive(literal_bool), TokenId_LiteralBool);

常规(非宽)字符串的实现:

std::string make_case_insensitive(const std::string& s)
{
    std::string r;
    std::string cC = "[xX]";
    for(char c : s)
    {
        if ( std::isalpha(c) )
        {
            cC[1] = std::tolower(c);
            cC[2] = std::toupper(c);
            r += cC;
        }
        else
            r += c;
    }
    return r;
}