Skip to content

Commit

Permalink
Merge pull request #219 from fktn-k/bugfix/217_fix_parse_single_quote…
Browse files Browse the repository at this point in the history
…s_in_strings

#217 Fix parse errors on strings containing single quotes
  • Loading branch information
fktn-k authored Nov 20, 2023
2 parents 0c796af + 13b991b commit 254d1cd
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 16 deletions.
24 changes: 10 additions & 14 deletions include/fkYAML/detail/input/lexical_analyzer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -813,23 +813,19 @@ class lexical_analyzer
// Handle single quotation marks.
if (current == '\'')
{
if (needs_last_double_quote || !needs_last_single_quote)
{
throw fkyaml::exception("Invalid single quotation mark found in a string token.");
}

// If single quotation marks are repeated twice in a single-quoted string token. they are considered as
// an escaped single quotation mark.
bool is_next_single_quote = m_input_handler.test_next_char('\'');
if (is_next_single_quote)
if (needs_last_single_quote)
{
m_value_buffer.push_back(char_traits_type::to_char_type(m_input_handler.get_next()));
continue;
// If single quotation marks are repeated twice in a single-quoted string token, they are considered
// as an escaped single quotation mark.
current = m_input_handler.get_next();
if (current != '\'')
{
return lexical_token_t::STRING_VALUE;
}
}

// move the current position for next scanning.
m_input_handler.get_next();
return lexical_token_t::STRING_VALUE;
m_value_buffer.push_back(char_traits_type::to_char_type(current));
continue;
}

// Handle colons.
Expand Down
2 changes: 2 additions & 0 deletions test/unit_test/test_input_handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,8 @@ TEST_CASE("InputHandlerTest_TestNextCharTest", "[InputHandlerTest]")

REQUIRE(handler.get_next() == 's');
REQUIRE(handler.get_next() == 't');
REQUIRE(handler.test_next_char('t') == false);

REQUIRE(handler.get_next() == pchar_input_handler::char_traits_type::eof());
REQUIRE(handler.test_next_char('t') == false);
REQUIRE(handler.get_cur_pos_in_line() == 4);
Expand Down
4 changes: 2 additions & 2 deletions test/unit_test/test_lexical_analyzer_class.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,9 @@ TEST_CASE("LexicalAnalyzerClassTest_ScanStringTokenTest", "[LexicalAnalyzerClass
value_pair_t(std::string("foo]"), fkyaml::node::string_type("foo")),
value_pair_t(std::string("foo:bar"), fkyaml::node::string_type("foo:bar")),
value_pair_t(std::string("foo bar"), fkyaml::node::string_type("foo bar")),
value_pair_t(std::string("foo\'s bar"), fkyaml::node::string_type("foo\'s bar")),
value_pair_t(std::string("\"foo bar\""), fkyaml::node::string_type("foo bar")),
value_pair_t(std::string("\"foo's bar\""), fkyaml::node::string_type("foo's bar")),
value_pair_t(std::string("\"foo:bar\""), fkyaml::node::string_type("foo:bar")),
value_pair_t(std::string("\"foo,bar\""), fkyaml::node::string_type("foo,bar")),
value_pair_t(std::string("\"foo]bar\""), fkyaml::node::string_type("foo]bar")),
Expand Down Expand Up @@ -639,10 +641,8 @@ TEST_CASE("LexicalAnalyzerClassTest_ScanInvalidStringTokenTest", "[LexicalAnalyz
{
auto buffer = GENERATE(
std::string("foo\"bar"),
std::string("foo\'bar"),
std::string("foo\\tbar"),
std::string("-.a"),
std::string("\"foo\'bar\""),
std::string("\"test"),
std::string("\'test"),
std::string("\'test\n\'"),
Expand Down

0 comments on commit 254d1cd

Please sign in to comment.