File size: 2,403 Bytes
d1be154 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 | #include <ot/sdc/sdc.hpp>
namespace ot::sdc {
// Function: tokenize
std::vector<std::string> tokenize(const std::string& line) {
// Parse the token.
std::string token;
std::vector<std::string> tokens;
auto add_token = [] (auto& tokens, auto& token) {
if(!token.empty()) {
tokens.push_back(std::move(token));
}
};
for(auto itr = line.begin(); itr != line.end(); ++itr) {
if(*itr == '#' || *itr == '\n' || *itr == '\r') {
break;
}
if(*itr == ' ') {
add_token(tokens, token);
}
// "quotation string"
else if(*itr == '"') {
OT_LOGF_IF(!token.empty(), "syntax error in parsing '\"'");
if(auto [b, e] = find_quote_pair(itr, line.end()); b != e) {
while(++b != e) {
token.push_back(*b);
}
add_token(tokens, token);
itr = e;
}
else {
OT_LOGF("can't find quote pair \" \"");
}
}
// [ ... ]
else if(*itr == '[') {
OT_LOGF_IF(!token.empty(), "syntax error in parsing '['");
token.push_back('[');
add_token(tokens, token);
auto [b, e] = find_bracket_pair(itr, line.end());
OT_LOGF_IF(b==line.end(), "can't find bracket pair [ ]");
for(auto i=std::next(b); i != e; ++i) {
if(*i == '{' || *i == '}') continue;
if(*i == ' ') {
add_token(tokens, token);
}
else {
token.push_back(*i);
}
}
add_token(tokens, token);
token.push_back(']');
add_token(tokens, token);
itr = e;
}
// { ... }
else if(*itr == '{') {
OT_LOGF_IF(!token.empty(), "syntax error in parsing '{'");
token.push_back('{');
add_token(tokens, token);
auto [b, e] = find_brace_pair(itr, line.end());
OT_LOGF_IF(b == line.end(), "can't find brace pair { }");
for(auto i=std::next(b); i!=e; ++i) {
if(*i == '{' || *i == '}') continue;
if(*i == ' ') {
add_token(tokens, token);
}
else {
token.push_back(*i);
}
}
add_token(tokens, token);
token.push_back('}');
add_token(tokens, token);
itr = e;
}
else {
token.push_back(*itr);
}
}
add_token(tokens, token);
return tokens;
}
}; // end of namespace ot::sdc. ------------------------------------------------------------------
|