util/Tokenizer: use std::runtime_error on syntax error

This commit is contained in:
Max Kellermann
2015-12-16 11:34:26 +01:00
parent d256a0e98f
commit 51168169e7
4 changed files with 80 additions and 141 deletions

View File

@@ -31,10 +31,8 @@
#include "Tokenizer.hxx"
#include "CharUtil.hxx"
#include "StringUtil.hxx"
#include "Error.hxx"
#include "Domain.hxx"
static constexpr Domain tokenizer_domain("tokenizer");
#include <stdexcept>
static inline bool
valid_word_first_char(char ch)
@@ -49,7 +47,7 @@ valid_word_char(char ch)
}
char *
Tokenizer::NextWord(Error &error)
Tokenizer::NextWord()
{
char *const word = input;
@@ -58,10 +56,8 @@ Tokenizer::NextWord(Error &error)
/* check the first character */
if (!valid_word_first_char(*input)) {
error.Set(tokenizer_domain, "Letter expected");
return nullptr;
}
if (!valid_word_first_char(*input))
throw std::runtime_error("Letter expected");
/* now iterate over the other characters until we find a
whitespace or end-of-string */
@@ -75,10 +71,8 @@ Tokenizer::NextWord(Error &error)
break;
}
if (!valid_word_char(*input)) {
error.Set(tokenizer_domain, "Invalid word character");
return nullptr;
}
if (!valid_word_char(*input))
throw std::runtime_error("Invalid word character");
}
/* end of string: the string is already null-terminated
@@ -94,7 +88,7 @@ valid_unquoted_char(char ch)
}
char *
Tokenizer::NextUnquoted(Error &error)
Tokenizer::NextUnquoted()
{
char *const word = input;
@@ -103,10 +97,8 @@ Tokenizer::NextUnquoted(Error &error)
/* check the first character */
if (!valid_unquoted_char(*input)) {
error.Set(tokenizer_domain, "Invalid unquoted character");
return nullptr;
}
if (!valid_unquoted_char(*input))
throw std::runtime_error("Invalid unquoted character");
/* now iterate over the other characters until we find a
whitespace or end-of-string */
@@ -120,11 +112,8 @@ Tokenizer::NextUnquoted(Error &error)
break;
}
if (!valid_unquoted_char(*input)) {
error.Set(tokenizer_domain,
"Invalid unquoted character");
return nullptr;
}
if (!valid_unquoted_char(*input))
throw std::runtime_error("Invalid unquoted character");
}
/* end of string: the string is already null-terminated
@@ -134,7 +123,7 @@ Tokenizer::NextUnquoted(Error &error)
}
char *
Tokenizer::NextString(Error &error)
Tokenizer::NextString()
{
char *const word = input, *dest = input;
@@ -144,10 +133,8 @@ Tokenizer::NextString(Error &error)
/* check for the opening " */
if (*input != '"') {
error.Set(tokenizer_domain, "'\"' expected");
return nullptr;
}
if (*input != '"')
throw std::runtime_error("'\"' expected");
++input;
@@ -159,14 +146,8 @@ Tokenizer::NextString(Error &error)
character */
++input;
if (*input == 0) {
/* return input-1 so the caller can see the
difference between "end of line" and
"error" */
--input;
error.Set(tokenizer_domain, "Missing closing '\"'");
return nullptr;
}
if (*input == 0)
throw std::runtime_error("Missing closing '\"'");
/* copy one character */
*dest++ = *input++;
@@ -176,11 +157,8 @@ Tokenizer::NextString(Error &error)
line) */
++input;
if (!IsWhitespaceFast(*input)) {
error.Set(tokenizer_domain,
"Space expected after closing '\"'");
return nullptr;
}
if (!IsWhitespaceFast(*input))
throw std::runtime_error("Space expected after closing '\"'");
/* finish the string and return it */
@@ -190,10 +168,10 @@ Tokenizer::NextString(Error &error)
}
char *
Tokenizer::NextParam(Error &error)
Tokenizer::NextParam()
{
if (*input == '"')
return NextString(error);
return NextString();
else
return NextUnquoted(error);
return NextUnquoted();
}