Skip to content

Commit 40b7a96

Browse files
facontidavideclaude
andcommitted
Address SonarCloud warnings in script parser and tokenizer
Tokenizer: - Extract scanHexNumber(), scanDecimalNumber(), matchTwoCharOp(), matchSingleCharOp() to reduce cognitive complexity of tokenize() - Use init-statement in if for identifier text variable (S6004) Parser: - Replace std::runtime_error with RuntimeError (S112) - Remove redundant lambda return type (S3574) - Catch RuntimeError instead of std::exception (S1181) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent c4b0ff7 commit 40b7a96

2 files changed

Lines changed: 205 additions & 227 deletions

File tree

src/script_parser.cpp

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
#include "behaviortree_cpp/scripting/operators.hpp"
1616

1717
#include <charconv>
18-
#include <stdexcept>
1918

2019
namespace BT
2120
{
@@ -84,9 +83,8 @@ class ScriptParser
8483
{
8584
if(!check(type))
8685
{
87-
throw std::runtime_error(StrCat("Parse error at position ",
88-
std::to_string(peek().pos), ": ", msg, " (got '",
89-
peek().text, "')"));
86+
throw RuntimeError(StrCat("Parse error at position ", std::to_string(peek().pos),
87+
": ", msg, " (got '", peek().text, "')"));
9088
}
9189
return advance();
9290
}
@@ -248,12 +246,12 @@ class ScriptParser
248246
// Error token from tokenizer
249247
if(tok.type == TokenType::Error)
250248
{
251-
throw std::runtime_error(
249+
throw RuntimeError(
252250
StrCat("Invalid token '", tok.text, "' at position ", std::to_string(tok.pos)));
253251
}
254252

255-
throw std::runtime_error(StrCat("Expected operand at position ",
256-
std::to_string(tok.pos), " (got '", tok.text, "')"));
253+
throw RuntimeError(StrCat("Expected operand at position ", std::to_string(tok.pos),
254+
" (got '", tok.text, "')"));
257255
}
258256

259257
/// Main Pratt expression parser
@@ -326,7 +324,7 @@ class ScriptParser
326324
op = Ast::ExprAssignment::assign_div;
327325
break;
328326
default:
329-
throw std::runtime_error("Internal error: unexpected assignment op");
327+
throw RuntimeError("Internal error: unexpected assignment op");
330328
}
331329
// Parse RHS -- use minBP=0 to allow full expression
332330
auto right = parseExpr(0);
@@ -375,7 +373,7 @@ class ScriptParser
375373
case TokenType::GreaterEqual:
376374
return Ast::ExprComparison::greater_equal;
377375
default:
378-
throw std::runtime_error("Internal error: not a comparison op");
376+
throw RuntimeError("Internal error: not a comparison op");
379377
}
380378
}
381379

@@ -416,7 +414,7 @@ class ScriptParser
416414
op = Ast::ExprBinaryArithmetic::logic_or;
417415
break;
418416
default:
419-
throw std::runtime_error("Internal error: unknown binary operator");
417+
throw RuntimeError("Internal error: unknown binary operator");
420418
}
421419
return std::make_shared<Ast::ExprBinaryArithmetic>(std::move(left), op,
422420
std::move(right));
@@ -443,7 +441,7 @@ Expected<ScriptFunction> ParseScript(const std::string& script)
443441
{
444442
return nonstd::make_unexpected("Empty Script");
445443
}
446-
return [exprs = std::move(exprs), script](Ast::Environment& env) -> Any {
444+
return [exprs = std::move(exprs), script](Ast::Environment& env) {
447445
try
448446
{
449447
for(size_t i = 0; i < exprs.size() - 1; ++i)
@@ -458,7 +456,7 @@ Expected<ScriptFunction> ParseScript(const std::string& script)
458456
}
459457
};
460458
}
461-
catch(std::runtime_error& err)
459+
catch(RuntimeError& err)
462460
{
463461
return nonstd::make_unexpected(err.what());
464462
}
@@ -473,7 +471,7 @@ Expected<Any> ParseScriptAndExecute(Ast::Environment& env, const std::string& sc
473471
{
474472
return executor.value()(env);
475473
}
476-
catch(std::exception& err)
474+
catch(RuntimeError& err)
477475
{
478476
return nonstd::make_unexpected(err.what());
479477
}
@@ -492,7 +490,7 @@ Result ValidateScript(const std::string& script)
492490
}
493491
return {};
494492
}
495-
catch(std::runtime_error& err)
493+
catch(RuntimeError& err)
496494
{
497495
return nonstd::make_unexpected(err.what());
498496
}

0 commit comments

Comments
 (0)