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();
}

View File

@@ -30,8 +30,6 @@
#ifndef TOKENIZER_HXX
#define TOKENIZER_HXX
class Error;
class Tokenizer {
char *input;
@@ -58,50 +56,42 @@ public:
}
/**
* Reads the next word.
* Reads the next word. Throws std::runtime_error on error.
*
* @param error if this function returns nullptr and
* **input_p!=0, it provides an #Error object in
* this argument
* @return a pointer to the null-terminated word, or nullptr
* on error or end of line
* on end of line
*/
char *NextWord(Error &error);
char *NextWord();
/**
* Reads the next unquoted word from the input string.
* Reads the next unquoted word from the input string. Throws
* std::runtime_error on error.
*
* @param error if this function returns nullptr and **input_p!=0, it
* provides an #Error object in this argument
* @return a pointer to the null-terminated word, or nullptr
* on error or end of line
* on end of line
*/
char *NextUnquoted(Error &error);
char *NextUnquoted();
/**
* Reads the next quoted string from the input string. A backslash
* escapes the following character. This function modifies the input
* string.
* Reads the next quoted string from the input string. A
* backslash escapes the following character. This function
* modifies the input string. Throws std::runtime_error on
* error.
*
* @param error if this function returns nullptr and **input_p!=0, it
* provides an #Error object in this argument
* @return a pointer to the null-terminated string, or nullptr on error
* or end of line
* @return a pointer to the null-terminated string, or nullptr
* end of line
*/
char *NextString(Error &error);
char *NextString();
/**
* Reads the next unquoted word or quoted string from the
* input. This is a wrapper for NextUnquoted() and
* NextString().
* NextString(). Throws std::runtime_error on error.
*
* @param error if this function returns nullptr and
* **input_p!=0, it provides an #Error object in
* this argument
* @return a pointer to the null-terminated string, or nullptr
* on error or end of line
* on end of line
*/
char *NextParam(Error &error);
char *NextParam();
};
#endif