From d917f14715f9f0530ce35ed0510661ffd2f8dc8b Mon Sep 17 00:00:00 2001 From: Griswald Brooks Date: Thu, 15 Oct 2026 15:15:02 -0400 Subject: [PATCH] Accept double-quoted string literals in scripts again The lexy grammar accepted both 'single' and "double" quoted strings (dsl::single_quoted | dsl::quoted in any_types.hpp). The hand-written tokenizer from #1099 only recognizes single quotes, so scripts that worked in 4.8, such as label := "item "..index, now fail with Invalid token '"'. Recognize both delimiters and require the closing quote to match the opening one. Fixes #1305 Co-Authored-By: David Vadovszki --- src/script_tokenizer.cpp | 6 ++-- tests/script_parser_test.cpp | 54 ++++++++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+), 3 deletions(-) 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"( + + + +