script_parser.cpp
Go to the documentation of this file.
3 
4 #include <lexy/action/parse.hpp>
8 
9 namespace BT
10 {
11 
13 
14 Expected<ScriptFunction> ParseScript(const std::string& script)
15 {
16  char error_msgs_buffer[2048];
17 
18  auto input = lexy::string_input<lexy::utf8_encoding>(script);
19  auto result =
20  lexy::parse<BT::Grammar::stmt>(input, ErrorReport().to(error_msgs_buffer));
21  if(result.has_value() && result.error_count() == 0)
22  {
23  try
24  {
25  std::vector<BT::Ast::ExprBase::Ptr> exprs = LEXY_MOV(result).value();
26  if(exprs.empty())
27  {
28  return nonstd::make_unexpected("Empty Script");
29  }
30 
31  return [exprs, script](Ast::Environment& env) -> Any {
32  try
33  {
34  for(auto i = 0u; i < exprs.size() - 1; ++i)
35  {
36  exprs[i]->evaluate(env);
37  }
38  return exprs.back()->evaluate(env);
39  }
40  catch(RuntimeError& err)
41  {
42  throw RuntimeError(StrCat("Error in script [", script, "]\n", err.what()));
43  }
44  };
45  }
46  catch(std::runtime_error& err)
47  {
48  return nonstd::make_unexpected(err.what());
49  }
50  }
51  else
52  {
53  return nonstd::make_unexpected(error_msgs_buffer);
54  }
55 }
56 
58 {
59  auto executor = ParseScript(script);
60  if(executor)
61  {
62  return executor.value()(env);
63  }
64  else // forward the error
65  {
66  return nonstd::make_unexpected(executor.error());
67  }
68 }
69 
70 Result ValidateScript(const std::string& script)
71 {
72  char error_msgs_buffer[2048];
73 
74  auto input = lexy::string_input<lexy::utf8_encoding>(script);
75  auto result =
76  lexy::parse<BT::Grammar::stmt>(input, ErrorReport().to(error_msgs_buffer));
77  if(result.has_value() && result.error_count() == 0)
78  {
79  try
80  {
81  std::vector<BT::Ast::ExprBase::Ptr> exprs = LEXY_MOV(result).value();
82  if(exprs.empty())
83  {
84  return nonstd::make_unexpected("Empty Script");
85  }
86  // valid script
87  return {};
88  }
89  catch(std::runtime_error& err)
90  {
91  return nonstd::make_unexpected(err.what());
92  }
93  }
94  return nonstd::make_unexpected(error_msgs_buffer);
95 }
96 
97 } // namespace BT
BT
Definition: ex01_wrap_legacy.cpp:29
LEXY_MOV
#define LEXY_MOV(...)
Definition: config.hpp:21
lexy_ext::_report_error
Definition: report_error.hpp:321
BT::ErrorReport
lexy_ext::_report_error< char * > ErrorReport
Definition: script_parser.cpp:12
BT::ParseScript
Expected< ScriptFunction > ParseScript(const std::string &script)
Definition: script_parser.cpp:14
BT::Any
Definition: safe_any.hpp:36
BT::Expected
nonstd::expected< T, std::string > Expected
Definition: basic_types.h:85
BT::BehaviorTreeException::what
const char * what() const noexcept
Definition: exceptions.h:34
parse.hpp
BT::ParseScriptAndExecute
Expected< Any > ParseScriptAndExecute(Ast::Environment &env, const std::string &script)
Definition: script_parser.cpp:57
report_error.hpp
BT::RuntimeError
Definition: exceptions.h:58
BT::StrCat
std::string StrCat()
Definition: strcat.hpp:46
validate.hpp
string_input.hpp
operators.hpp
BT::ValidateScript
Result ValidateScript(const std::string &script)
ValidateScript will check if a certain string is valid.
Definition: script_parser.cpp:70
BT::Result
Expected< std::monostate > Result
Definition: basic_types.h:333
script_parser.hpp
lexy::string_input
An input that refers to a string.
Definition: string_input.hpp:18
BT::Ast::Environment
The Environment class is used to encapsulate the information and states needed by the scripting langu...
Definition: script_parser.hpp:31


behaviortree_cpp_v4
Author(s): Davide Faconti
autogenerated on Fri Jun 28 2024 02:20:08