diff --git a/src/script_tokenizer.cpp b/src/script_tokenizer.cpp index 591f1e4ee..98e3c4174 100644 --- a/src/script_tokenizer.cpp +++ b/src/script_tokenizer.cpp @@ -239,11 +239,11 @@ std::vector tokenize(const std::string& source) const size_t start = i; - // Single-quoted string literal - if(c == '\'') + // String literal: the closing quote must match the opening quote. + if(c == '\'' || c == '"') { ++i; - while(i < len && source[i] != '\'') + while(i < len && source[i] != c) { ++i; } diff --git a/tests/script_parser_test.cpp b/tests/script_parser_test.cpp index d3cd8715b..e1ee71f3d 100644 --- a/tests/script_parser_test.cpp +++ b/tests/script_parser_test.cpp @@ -87,6 +87,60 @@ TEST(ParserTest, AnyTypes) EXPECT_EQ(result.value().cast(), 0); } +TEST(ParserTest, StringLiteralQuotes) +{ + struct TestCase + { + const char* script; + const char* expected; + }; + const TestCase cases[] = { + { "'hello world '", "hello world " }, + { R"("hello world ")", "hello world " }, + { "''", "" }, + { R"("")", "" }, + { R"('say "hello"')", "say \"hello\"" }, + { R"("it's ready")", "it's ready" }, + { R"('single'.."double")", "singledouble" }, + }; + BT::Ast::Environment env = { BT::Blackboard::create(), {} }; + for(const auto& test : cases) + { + SCOPED_TRACE(test.script); + auto result = BT::ParseScriptAndExecute(env, test.script); + ASSERT_TRUE(result.has_value()); + EXPECT_EQ(result.value().cast(), test.expected); + } + + EXPECT_FALSE(BT::ValidateScript("'unterminated")); + EXPECT_FALSE(BT::ValidateScript(R"("unterminated)")); + EXPECT_FALSE(BT::ValidateScript(R"("mismatched')")); + EXPECT_FALSE(BT::ValidateScript(R"('mismatched")")); +} + +TEST(ParserTest, DoubleQuotedStringsInXml) +{ + BT::BehaviorTreeFactory factory; + const std::string xml_text = R"( + + + +