str parse cleanup

master
Daniel Kolesa 2016-09-26 02:26:02 +02:00
parent 15e692f755
commit 9995fcf2e1
1 changed files with 17 additions and 18 deletions

View File

@ -186,19 +186,23 @@ done:
} }
namespace util { namespace util {
static ostd::ConstCharRange cs_parse_str(ostd::ConstCharRange str) { static ostd::ConstCharRange cs_parse_str(
CsState &cs, ostd::ConstCharRange str
) {
ostd::ConstCharRange orig = str;
++str;
while (!str.empty()) { while (!str.empty()) {
switch (*str) { switch (*str) {
case '\r': case '\r':
case '\n': case '\n':
case '\"': case '\"':
return str; goto end;
case '^': case '^':
++str; ++str;
if (!str.empty()) { if (!str.empty()) {
break; break;
} }
return str; goto end;
case '\\': case '\\':
++str; ++str;
if (!str.empty() && ((*str == '\r') || (*str == '\n'))) { if (!str.empty() && ((*str == '\r') || (*str == '\n'))) {
@ -212,7 +216,13 @@ namespace util {
} }
++str; ++str;
} }
return str; end:
if (str.empty() || (*str != '\"')) {
throw CsErrorException(
cs, "unfinished string '%s'", ostd::slice_until(orig, str)
);
}
return str + 1;
} }
static ostd::ConstCharRange cs_parse_word(ostd::ConstCharRange str) { static ostd::ConstCharRange cs_parse_word(ostd::ConstCharRange str) {
@ -280,17 +290,9 @@ namespace util {
switch (*input) { switch (*input) {
case '"': case '"':
quote = input; quote = input;
++input; input = cs_parse_str(p_state, input);
item = input;
input = cs_parse_str(input);
item = ostd::slice_until(item, input);
if (input.empty() || (*input != '\"')) {
throw CsErrorException(
p_state, "unfinished string '\"%s'", item
);
}
input.pop_front();
quote = ostd::slice_until(quote, input); quote = ostd::slice_until(quote, input);
item = quote.slice(1, quote.size() - 1);
break; break;
case '(': case '(':
case '[': { case '[': {
@ -310,10 +312,7 @@ namespace util {
++input; ++input;
switch (c) { switch (c) {
case '"': case '"':
input = cs_parse_str(input); input = cs_parse_str(p_state, input);
if (!input.empty() && (*input == '"')) {
++input;
}
break; break;
case '/': case '/':
if (!input.empty() && (*input == '/')) { if (!input.empty() && (*input == '/')) {