ParseExpression: return a std::pair

This commit is contained in:
Michael M 2017-06-07 14:53:41 -07:00
parent 030dc11c6e
commit d2821e14fa
3 changed files with 22 additions and 26 deletions

View File

@ -30,9 +30,9 @@ bool ControlReference::InputGateOn()
void ControlReference::UpdateReference(const ciface::Core::DeviceContainer& devices, void ControlReference::UpdateReference(const ciface::Core::DeviceContainer& devices,
const ciface::Core::DeviceQualifier& default_device) const ciface::Core::DeviceQualifier& default_device)
{ {
Expression* expr;
ControlFinder finder(devices, default_device, IsInput()); ControlFinder finder(devices, default_device, IsInput());
m_parse_status = ParseExpression(expression, finder, &expr); Expression* expr;
std::tie(m_parse_status, expr) = ParseExpression(expression, finder);
m_parsed_expression.reset(expr); m_parsed_expression.reset(expr);
} }

View File

@ -369,15 +369,14 @@ public:
m_it = tokens.begin(); m_it = tokens.begin();
} }
ParseStatus Parse(Expression** expr_out) std::pair<ParseStatus, Expression*> Parse()
{ {
ExpressionNode* node; ExpressionNode* node;
ParseStatus status = Toplevel(&node); ParseStatus status = Toplevel(&node);
if (status != ParseStatus::Successful) if (status != ParseStatus::Successful)
return status; return std::make_pair(status, nullptr);
*expr_out = new Expression(node); return std::make_pair(ParseStatus::Successful, new Expression(node));
return ParseStatus::Successful;
} }
private: private:
@ -522,32 +521,28 @@ Expression::~Expression()
delete node; delete node;
} }
static ParseStatus ParseExpressionInner(const std::string& str, ControlFinder& finder, static std::pair<ParseStatus, Expression*> ParseExpressionInner(const std::string& str,
Expression** expr_out) ControlFinder& finder)
{ {
ParseStatus status;
Expression* expr;
*expr_out = nullptr;
if (str == "") if (str == "")
return ParseStatus::Successful; return std::make_pair(ParseStatus::Successful, nullptr);
Lexer l(str); Lexer l(str);
std::vector<Token> tokens; std::vector<Token> tokens;
status = l.Tokenize(tokens); ParseStatus tokenize_status = l.Tokenize(tokens);
if (status != ParseStatus::Successful) if (tokenize_status != ParseStatus::Successful)
return status; return std::make_pair(tokenize_status, nullptr);
Parser p(tokens, finder); ParseStatus status;
status = p.Parse(&expr); Expression* expr;
std::tie(status, expr) = Parser(tokens, finder).Parse();
if (status != ParseStatus::Successful) if (status != ParseStatus::Successful)
return status; return std::make_pair(status, nullptr);
*expr_out = expr; return std::make_pair(ParseStatus::Successful, expr);
return ParseStatus::Successful;
} }
ParseStatus ParseExpression(const std::string& str, ControlFinder& finder, Expression** expr_out) std::pair<ParseStatus, Expression*> ParseExpression(const std::string& str, ControlFinder& finder)
{ {
// Add compatibility with old simple expressions, which are simple // Add compatibility with old simple expressions, which are simple
// barewords control names. // barewords control names.
@ -560,11 +555,11 @@ ParseStatus ParseExpression(const std::string& str, ControlFinder& finder, Expre
Device::Control* control = finder.FindControl(qualifier); Device::Control* control = finder.FindControl(qualifier);
if (control) if (control)
{ {
*expr_out = new Expression(new ControlExpression(qualifier, device, control)); Expression* expr = new Expression(new ControlExpression(qualifier, device, control));
return ParseStatus::Successful; return std::make_pair(ParseStatus::Successful, expr);
} }
return ParseExpressionInner(str, finder, expr_out); return ParseExpressionInner(str, finder);
} }
} }
} }

View File

@ -6,6 +6,7 @@
#include <memory> #include <memory>
#include <string> #include <string>
#include <utility>
#include "InputCommon/ControllerInterface/Device.h" #include "InputCommon/ControllerInterface/Device.h"
namespace ciface namespace ciface
@ -66,6 +67,6 @@ enum class ParseStatus
NoDevice, NoDevice,
}; };
ParseStatus ParseExpression(const std::string& expr, ControlFinder& finder, Expression** expr_out); std::pair<ParseStatus, Expression*> ParseExpression(const std::string& expr, ControlFinder& finder);
} }
} }