您需要一个简单的解析器。
这是我最喜欢的Boost swiss-army knife for quick parsers。
我创建了一个非常灵活的“标记”语法,它尊重(嵌套)括号和双引号字符串文字(可能带有嵌入的转义引号和括号):
token = raw [ *(
'(' >> -token_list >> ')'
| '[' >> -token_list >> ']'
| '{' >> -token_list >> '}'
| string_literal
| lexeme[ + ~char_(")]}([{\"',") ]
) ];
其中 token_list 和 string_literal 定义为
string_literal = lexeme [
'"' >> *('\\' >> char_ | ~char_('"')) >> '"'
];
token_list = token % ',';
现在=IF(condition, true_part, false_part) 的解析器表达式很简单:
if_expr
= '=' >> no_case["if"]
>> '(' >> token >> ',' >> token >> ',' >> token >> ')';
为了好玩,我将 IF 关键字设置为不区分大小写
演示
Live On Coliru
//#define BOOST_SPIRIT_X3_DEBUG
#include <boost/spirit/home/x3.hpp>
#include <boost/fusion/adapted/std_tuple.hpp>
#include <iostream>
#include <iomanip>
namespace x3 = boost::spirit::x3;
namespace parser {
using namespace x3;
static rule<struct token_, std::string> const token = "token";
static auto const string_literal = lexeme [
'"' >> *('\\' >> char_ | ~char_('"')) >> '"'
];
static auto const token_list = token % ',';
static auto const token_def = raw [ *(
'(' >> -token_list >> ')'
| '[' >> -token_list >> ']'
| '{' >> -token_list >> '}'
| string_literal
| +~char_(")]}([{\"',") // glue together everything else
) ];
BOOST_SPIRIT_DEFINE(token)
static auto const if_expr
= '=' >> no_case["if"]
>> '(' >> token >> ',' >> token >> ',' >> token >> ')';
}
int main() {
for (std::string const& input : {
R"(=IF(ISNUMBER,ON.TIME,CLOSE))",
R"(=IF(ISNUMBER(SEARCH("Windows")),ON.TIME(NOW()+"00:00:02","abcdef"),CLOSE(TRUE)))",
R"(=IF(ISNUMBER(SEARCH("Windows",GETWORKSPACE(1))),ON.TIME(NOW()+"00:00:02","abcdef"),CLOSE(TRUE)))",
" = if( isnumber, on .time, close ) ",
R"( = if( "foo, bar", if( isnumber, on .time, close ), IF("[ISN(UM}B\"ER")) )",
})
{
auto f = input.begin(), l = input.end();
std::cout << "=== " << std::quoted(input) << ":\n";
std::string condition, true_part, false_part;
auto attr = std::tie(condition, true_part, false_part);
if (phrase_parse(f, l, parser::if_expr, x3::blank, attr)) {
std::cout << "Parsed: \n"
<< " - condition: " << std::quoted(condition) << "\n"
<< " - true_part: " << std::quoted(true_part) << "\n"
<< " - false_part: " << std::quoted(false_part) << "\n";
} else {
std::cout << "Parse failed\n";
}
if (f!=l) {
std::cout << "Remaining unparsed: " << std::quoted(std::string(f,l)) << "\n";
}
}
}
打印
=== "=IF(ISNUMBER,ON.TIME,CLOSE)":
Parsed:
- condition: "ISNUMBER"
- true_part: "ON.TIME"
- false_part: "CLOSE"
=== "=IF(ISNUMBER(SEARCH(\"Windows\")),ON.TIME(NOW()+\"00:00:02\",\"abcdef\"),CLOSE(TRUE))":
Parsed:
- condition: "ISNUMBER(SEARCH(\"Windows\"))"
- true_part: "ON.TIME(NOW()+\"00:00:02\",\"abcdef\")"
- false_part: "CLOSE(TRUE)"
=== "=IF(ISNUMBER(SEARCH(\"Windows\",GETWORKSPACE(1))),ON.TIME(NOW()+\"00:00:02\",\"abcdef\"),CLOSE(TRUE))":
Parsed:
- condition: "ISNUMBER(SEARCH(\"Windows\",GETWORKSPACE(1)))"
- true_part: "ON.TIME(NOW()+\"00:00:02\",\"abcdef\")"
- false_part: "CLOSE(TRUE)"
=== " = if( isnumber, on .time, close ) ":
Parsed:
- condition: "isnumber"
- true_part: "on .time"
- false_part: "close "
=== " = if( \"foo, bar\", if( isnumber, on .time, close ), IF(\"[ISN(UM}B\\\"ER\")) ":
Parsed:
- condition: "\"foo, bar\""
- true_part: "if( isnumber, on .time, close )"
- false_part: "IF(\"[ISN(UM}B\\\"ER\")"