cleanup
This commit is contained in:
parent
4871e39415
commit
92ee1d56eb
3 changed files with 62 additions and 40 deletions
|
@ -7514,11 +7514,6 @@ class basic_json
|
||||||
m_limit = m_content + len;
|
m_limit = m_content + len;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// a lexer from a string literal
|
|
||||||
explicit lexer(const typename string_t::value_type* buff) noexcept
|
|
||||||
: lexer(reinterpret_cast<const lexer_char_t*>(buff), strlen(buff))
|
|
||||||
{}
|
|
||||||
|
|
||||||
/// a lexer from an input stream
|
/// a lexer from an input stream
|
||||||
explicit lexer(std::istream& s)
|
explicit lexer(std::istream& s)
|
||||||
: m_stream(&s), m_line_buffer()
|
: m_stream(&s), m_line_buffer()
|
||||||
|
@ -8881,7 +8876,9 @@ basic_json_parser_63:
|
||||||
public:
|
public:
|
||||||
/// a parser reading from a string literal
|
/// a parser reading from a string literal
|
||||||
parser(const typename string_t::value_type* buff, parser_callback_t cb = nullptr)
|
parser(const typename string_t::value_type* buff, parser_callback_t cb = nullptr)
|
||||||
: callback(cb), m_lexer(buff)
|
: callback(cb),
|
||||||
|
m_lexer(reinterpret_cast<const typename lexer::lexer_char_t*>(buff),
|
||||||
|
strlen(buff))
|
||||||
{}
|
{}
|
||||||
|
|
||||||
/// a parser reading from a string container
|
/// a parser reading from a string container
|
||||||
|
|
|
@ -7514,11 +7514,6 @@ class basic_json
|
||||||
m_limit = m_content + len;
|
m_limit = m_content + len;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// a lexer from a string literal
|
|
||||||
explicit lexer(const typename string_t::value_type* buff) noexcept
|
|
||||||
: lexer(reinterpret_cast<const lexer_char_t*>(buff), strlen(buff))
|
|
||||||
{}
|
|
||||||
|
|
||||||
/// a lexer from an input stream
|
/// a lexer from an input stream
|
||||||
explicit lexer(std::istream& s)
|
explicit lexer(std::istream& s)
|
||||||
: m_stream(&s), m_line_buffer()
|
: m_stream(&s), m_line_buffer()
|
||||||
|
@ -8178,7 +8173,9 @@ class basic_json
|
||||||
public:
|
public:
|
||||||
/// a parser reading from a string literal
|
/// a parser reading from a string literal
|
||||||
parser(const typename string_t::value_type* buff, parser_callback_t cb = nullptr)
|
parser(const typename string_t::value_type* buff, parser_callback_t cb = nullptr)
|
||||||
: callback(cb), m_lexer(buff)
|
: callback(cb),
|
||||||
|
m_lexer(reinterpret_cast<const typename lexer::lexer_char_t*>(buff),
|
||||||
|
strlen(buff))
|
||||||
{}
|
{}
|
||||||
|
|
||||||
/// a parser reading from a string container
|
/// a parser reading from a string container
|
||||||
|
|
|
@ -38,43 +38,67 @@ TEST_CASE("lexer class")
|
||||||
{
|
{
|
||||||
SECTION("structural characters")
|
SECTION("structural characters")
|
||||||
{
|
{
|
||||||
CHECK(json::lexer("[").scan() == json::lexer::token_type::begin_array);
|
CHECK(json::lexer(reinterpret_cast<const json::lexer::lexer_char_t*>("["),
|
||||||
CHECK(json::lexer("]").scan() == json::lexer::token_type::end_array);
|
1).scan() == json::lexer::token_type::begin_array);
|
||||||
CHECK(json::lexer("{").scan() == json::lexer::token_type::begin_object);
|
CHECK(json::lexer(reinterpret_cast<const json::lexer::lexer_char_t*>("]"),
|
||||||
CHECK(json::lexer("}").scan() == json::lexer::token_type::end_object);
|
1).scan() == json::lexer::token_type::end_array);
|
||||||
CHECK(json::lexer(",").scan() == json::lexer::token_type::value_separator);
|
CHECK(json::lexer(reinterpret_cast<const json::lexer::lexer_char_t*>("{"),
|
||||||
CHECK(json::lexer(":").scan() == json::lexer::token_type::name_separator);
|
1).scan() == json::lexer::token_type::begin_object);
|
||||||
|
CHECK(json::lexer(reinterpret_cast<const json::lexer::lexer_char_t*>("}"),
|
||||||
|
1).scan() == json::lexer::token_type::end_object);
|
||||||
|
CHECK(json::lexer(reinterpret_cast<const json::lexer::lexer_char_t*>(","),
|
||||||
|
1).scan() == json::lexer::token_type::value_separator);
|
||||||
|
CHECK(json::lexer(reinterpret_cast<const json::lexer::lexer_char_t*>(":"),
|
||||||
|
1).scan() == json::lexer::token_type::name_separator);
|
||||||
}
|
}
|
||||||
|
|
||||||
SECTION("literal names")
|
SECTION("literal names")
|
||||||
{
|
{
|
||||||
CHECK(json::lexer("null").scan() == json::lexer::token_type::literal_null);
|
CHECK(json::lexer(reinterpret_cast<const json::lexer::lexer_char_t*>("null"),
|
||||||
CHECK(json::lexer("true").scan() == json::lexer::token_type::literal_true);
|
4).scan() == json::lexer::token_type::literal_null);
|
||||||
CHECK(json::lexer("false").scan() == json::lexer::token_type::literal_false);
|
CHECK(json::lexer(reinterpret_cast<const json::lexer::lexer_char_t*>("true"),
|
||||||
|
4).scan() == json::lexer::token_type::literal_true);
|
||||||
|
CHECK(json::lexer(reinterpret_cast<const json::lexer::lexer_char_t*>("false"),
|
||||||
|
5).scan() == json::lexer::token_type::literal_false);
|
||||||
}
|
}
|
||||||
|
|
||||||
SECTION("numbers")
|
SECTION("numbers")
|
||||||
{
|
{
|
||||||
CHECK(json::lexer("0").scan() == json::lexer::token_type::value_number);
|
CHECK(json::lexer(reinterpret_cast<const json::lexer::lexer_char_t*>("0"),
|
||||||
CHECK(json::lexer("1").scan() == json::lexer::token_type::value_number);
|
1).scan() == json::lexer::token_type::value_number);
|
||||||
CHECK(json::lexer("2").scan() == json::lexer::token_type::value_number);
|
CHECK(json::lexer(reinterpret_cast<const json::lexer::lexer_char_t*>("1"),
|
||||||
CHECK(json::lexer("3").scan() == json::lexer::token_type::value_number);
|
1).scan() == json::lexer::token_type::value_number);
|
||||||
CHECK(json::lexer("4").scan() == json::lexer::token_type::value_number);
|
CHECK(json::lexer(reinterpret_cast<const json::lexer::lexer_char_t*>("2"),
|
||||||
CHECK(json::lexer("5").scan() == json::lexer::token_type::value_number);
|
1).scan() == json::lexer::token_type::value_number);
|
||||||
CHECK(json::lexer("6").scan() == json::lexer::token_type::value_number);
|
CHECK(json::lexer(reinterpret_cast<const json::lexer::lexer_char_t*>("3"),
|
||||||
CHECK(json::lexer("7").scan() == json::lexer::token_type::value_number);
|
1).scan() == json::lexer::token_type::value_number);
|
||||||
CHECK(json::lexer("8").scan() == json::lexer::token_type::value_number);
|
CHECK(json::lexer(reinterpret_cast<const json::lexer::lexer_char_t*>("4"),
|
||||||
CHECK(json::lexer("9").scan() == json::lexer::token_type::value_number);
|
1).scan() == json::lexer::token_type::value_number);
|
||||||
|
CHECK(json::lexer(reinterpret_cast<const json::lexer::lexer_char_t*>("5"),
|
||||||
|
1).scan() == json::lexer::token_type::value_number);
|
||||||
|
CHECK(json::lexer(reinterpret_cast<const json::lexer::lexer_char_t*>("6"),
|
||||||
|
1).scan() == json::lexer::token_type::value_number);
|
||||||
|
CHECK(json::lexer(reinterpret_cast<const json::lexer::lexer_char_t*>("7"),
|
||||||
|
1).scan() == json::lexer::token_type::value_number);
|
||||||
|
CHECK(json::lexer(reinterpret_cast<const json::lexer::lexer_char_t*>("8"),
|
||||||
|
1).scan() == json::lexer::token_type::value_number);
|
||||||
|
CHECK(json::lexer(reinterpret_cast<const json::lexer::lexer_char_t*>("9"),
|
||||||
|
1).scan() == json::lexer::token_type::value_number);
|
||||||
}
|
}
|
||||||
|
|
||||||
SECTION("whitespace")
|
SECTION("whitespace")
|
||||||
{
|
{
|
||||||
// result is end_of_input, because not token is following
|
// result is end_of_input, because not token is following
|
||||||
CHECK(json::lexer(" ").scan() == json::lexer::token_type::end_of_input);
|
CHECK(json::lexer(reinterpret_cast<const json::lexer::lexer_char_t*>(" "),
|
||||||
CHECK(json::lexer("\t").scan() == json::lexer::token_type::end_of_input);
|
1).scan() == json::lexer::token_type::end_of_input);
|
||||||
CHECK(json::lexer("\n").scan() == json::lexer::token_type::end_of_input);
|
CHECK(json::lexer(reinterpret_cast<const json::lexer::lexer_char_t*>("\t"),
|
||||||
CHECK(json::lexer("\r").scan() == json::lexer::token_type::end_of_input);
|
1).scan() == json::lexer::token_type::end_of_input);
|
||||||
CHECK(json::lexer(" \t\n\r\n\t ").scan() == json::lexer::token_type::end_of_input);
|
CHECK(json::lexer(reinterpret_cast<const json::lexer::lexer_char_t*>("\n"),
|
||||||
|
1).scan() == json::lexer::token_type::end_of_input);
|
||||||
|
CHECK(json::lexer(reinterpret_cast<const json::lexer::lexer_char_t*>("\r"),
|
||||||
|
1).scan() == json::lexer::token_type::end_of_input);
|
||||||
|
CHECK(json::lexer(reinterpret_cast<const json::lexer::lexer_char_t*>(" \t\n\r\n\t "),
|
||||||
|
7).scan() == json::lexer::token_type::end_of_input);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -100,7 +124,11 @@ TEST_CASE("lexer class")
|
||||||
{
|
{
|
||||||
for (int c = 1; c < 128; ++c)
|
for (int c = 1; c < 128; ++c)
|
||||||
{
|
{
|
||||||
auto s = std::string(1, c);
|
// create string from the ASCII code
|
||||||
|
const auto s = std::string(1, c);
|
||||||
|
// store scan() result
|
||||||
|
const auto res = json::lexer(reinterpret_cast<const json::lexer::lexer_char_t*>(s.c_str()),
|
||||||
|
1).scan();
|
||||||
|
|
||||||
switch (c)
|
switch (c)
|
||||||
{
|
{
|
||||||
|
@ -122,7 +150,7 @@ TEST_CASE("lexer class")
|
||||||
case ('8'):
|
case ('8'):
|
||||||
case ('9'):
|
case ('9'):
|
||||||
{
|
{
|
||||||
CHECK(json::lexer(s.c_str()).scan() != json::lexer::token_type::parse_error);
|
CHECK(res != json::lexer::token_type::parse_error);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -132,14 +160,14 @@ TEST_CASE("lexer class")
|
||||||
case ('\n'):
|
case ('\n'):
|
||||||
case ('\r'):
|
case ('\r'):
|
||||||
{
|
{
|
||||||
CHECK(json::lexer(s.c_str()).scan() == json::lexer::token_type::end_of_input);
|
CHECK(res == json::lexer::token_type::end_of_input);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
// anything else is not expected
|
// anything else is not expected
|
||||||
default:
|
default:
|
||||||
{
|
{
|
||||||
CHECK(json::lexer(s.c_str()).scan() == json::lexer::token_type::parse_error);
|
CHECK(res == json::lexer::token_type::parse_error);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue