expose word parsing into API and unify with parser

master
Daniel Kolesa 2016-09-29 20:57:47 +02:00
parent 9995fcf2e1
commit cd515a4cff
3 changed files with 13 additions and 48 deletions

View File

@ -761,6 +761,10 @@ namespace util {
return ret;
}
OSTD_EXPORT ostd::ConstCharRange parse_word(
CsState &cs, ostd::ConstCharRange str
);
struct ListParser {
ostd::ConstCharRange input;
ostd::ConstCharRange quote = ostd::ConstCharRange();

View File

@ -174,50 +174,9 @@ void GenState::skip_comments() {
}
}
static ostd::ConstCharRange parseword(ostd::ConstCharRange p) {
for (;;) {
p = ostd::find_one_of(p, ostd::ConstCharRange("\"/;\\()[] \t\r\n"));
if (p.empty()) {
return p;
}
switch (*p) {
case '"':
case ';':
case '\\':
case ' ':
case '\t':
case '\r':
case '\n':
return p;
case '/':
if ((p.size() > 1) && (p[1] == '/')) {
return p;
}
break;
case '[':
p = parseword(p + 1);
if (p.empty() || (*p != ']')) {
return p;
}
break;
case '(':
p = parseword(p + 1);
if (p.empty() || (*p != ')')) {
return p;
}
break;
case ']':
case ')':
return p;
}
++p;
}
return p;
}
ostd::ConstCharRange GenState::get_word() {
auto beg = source;
source = parseword(source);
source = util::parse_word(cs, source);
return ostd::slice_until(beg, source);
}

View File

@ -225,7 +225,9 @@ end:
return str + 1;
}
static ostd::ConstCharRange cs_parse_word(ostd::ConstCharRange str) {
OSTD_EXPORT ostd::ConstCharRange parse_word(
CsState &cs, ostd::ConstCharRange str
) {
for (;;) {
str = ostd::find_one_of(str, ostd::ConstCharRange("\"/;()[] \t\r\n"));
if (str.empty()) {
@ -245,15 +247,15 @@ end:
}
break;
case '[':
str = cs_parse_word(str + 1);
str = parse_word(cs, str + 1);
if (str.empty() || (*str != ']')) {
return str;
throw CsErrorException(cs, "missing \"]\"");
}
break;
case '(':
str = cs_parse_word(str + 1);
str = parse_word(cs, str + 1);
if (str.empty() || (*str != ')')) {
return str;
throw CsErrorException(cs, "missing \")\"");
}
break;
case ']':
@ -345,7 +347,7 @@ endblock:
case ']':
return false;
default: {
ostd::ConstCharRange e = cs_parse_word(input);
ostd::ConstCharRange e = parse_word(p_state, input);
quote = item = ostd::slice_until(input, e);
input = e;
break;