ExpressionParser: Add support for simple barewords control names

If an expression can't be parsed normally, we then look to see if it's a
simple device name. This keeps backwards compatibility with simple input
ocnfigurations, where people just used the Detect button.
This commit is contained in:
Jasper St. Pierre 2013-06-26 20:19:23 -04:00
parent 03fdebac09
commit f53eefb491
2 changed files with 60 additions and 29 deletions

View File

@ -209,6 +209,7 @@ public:
virtual ~ExpressionNode() {} virtual ~ExpressionNode() {}
virtual ControlState GetValue() { return 0; } virtual ControlState GetValue() { return 0; }
virtual void SetValue(ControlState state) {} virtual void SetValue(ControlState state) {}
virtual int CountNumControls() { return 0; }
virtual bool IsComplicated() { return false; } virtual bool IsComplicated() { return false; }
virtual operator std::string() { return ""; } virtual operator std::string() { return ""; }
}; };
@ -231,6 +232,11 @@ public:
control->ToOutput()->SetState(value); control->ToOutput()->SetState(value);
} }
virtual int CountNumControls()
{
return 1;
}
virtual bool IsComplicated() virtual bool IsComplicated()
{ {
return false; return false;
@ -282,6 +288,11 @@ public:
rhs->SetValue(value); rhs->SetValue(value);
} }
virtual int CountNumControls()
{
return lhs->CountNumControls() + rhs->CountNumControls();
}
virtual bool IsComplicated() virtual bool IsComplicated()
{ {
return true; return true;
@ -329,6 +340,11 @@ public:
} }
} }
virtual int CountNumControls()
{
return inner->CountNumControls();
}
virtual bool IsComplicated() virtual bool IsComplicated()
{ {
return true; return true;
@ -371,18 +387,12 @@ public:
ExpressionParseStatus Parse(Expression **expr_out) ExpressionParseStatus Parse(Expression **expr_out)
{ {
Expression *expr; ExpressionNode *node;
ExpressionNode *expr_node; ExpressionParseStatus status = Toplevel(&node);
ExpressionParseStatus status = Toplevel(&expr_node);
if (status != EXPRESSION_PARSE_SUCCESS) if (status != EXPRESSION_PARSE_SUCCESS)
return status; return status;
expr = new Expression(); *expr_out = new Expression(node);
expr->expr = expr_node;
expr->num_controls = CountNumControls();
expr->is_complicated = expr_node->IsComplicated();
*expr_out = expr;
return EXPRESSION_PARSE_SUCCESS; return EXPRESSION_PARSE_SUCCESS;
} }
@ -513,33 +523,31 @@ private:
{ {
return Binary(expr_out); return Binary(expr_out);
} }
int CountNumControls()
{
int count = 0;
for (std::vector<Token>::iterator it = tokens.begin(); it != tokens.end(); ++it)
if (it->type == TOK_CONTROL)
count++;
return count;
}
}; };
ControlState Expression::GetValue() ControlState Expression::GetValue()
{ {
return expr->GetValue(); return node->GetValue();
} }
void Expression::SetValue(ControlState value) void Expression::SetValue(ControlState value)
{ {
expr->SetValue(value); node->SetValue(value);
}
Expression::Expression(ExpressionNode *node_)
{
node = node_;
num_controls = node->CountNumControls();
is_complicated = node->IsComplicated();
} }
Expression::~Expression() Expression::~Expression()
{ {
delete expr; delete node;
} }
ExpressionParseStatus ParseExpression(std::string str, ControlFinder &finder, Expression **expr_out) ExpressionParseStatus ParseExpressionInner(std::string str, ControlFinder &finder, Expression **expr_out)
{ {
ExpressionParseStatus status; ExpressionParseStatus status;
Expression *expr; Expression *expr;
@ -563,5 +571,32 @@ ExpressionParseStatus ParseExpression(std::string str, ControlFinder &finder, Ex
return EXPRESSION_PARSE_SUCCESS; return EXPRESSION_PARSE_SUCCESS;
} }
ExpressionParseStatus ParseExpression(std::string str, ControlFinder &finder, Expression **expr_out)
{
ExpressionParseStatus status;
status = ParseExpressionInner(str, finder, expr_out);
if (status == EXPRESSION_PARSE_SUCCESS)
return status;
if (status != EXPRESSION_PARSE_SYNTAX_ERROR)
return status;
// Add compatibility with old simple expressions, which are simple
// barewords control names.
ControlQualifier qualifier;
qualifier.control_name = str;
qualifier.has_device = false;
Device::Control *control = finder.FindControl(qualifier);
if (control) {
*expr_out = new Expression(new ControlExpression(qualifier, control));
return EXPRESSION_PARSE_SUCCESS;
}
return EXPRESSION_PARSE_SYNTAX_ERROR;
}
} }
} }

View File

@ -41,22 +41,18 @@ private:
bool is_input; bool is_input;
}; };
class Parser;
class ExpressionNode; class ExpressionNode;
class Expression class Expression
{ {
friend class Parser;
public: public:
Expression() : expr(NULL) {} Expression() : node(NULL) {}
Expression(ExpressionNode *node);
~Expression(); ~Expression();
ControlState GetValue(); ControlState GetValue();
void SetValue (ControlState state); void SetValue (ControlState state);
int num_controls; int num_controls;
bool is_complicated; bool is_complicated;
ExpressionNode *node;
private:
ExpressionNode *expr;
}; };
enum ExpressionParseStatus enum ExpressionParseStatus