2014-02-10 11:54:46 -07:00
|
|
|
// Copyright 2013 Dolphin Emulator Project
|
2015-05-17 17:08:10 -06:00
|
|
|
// Licensed under GPLv2+
|
2014-02-10 11:54:46 -07:00
|
|
|
// Refer to the license.txt file included.
|
2013-06-13 21:09:55 -06:00
|
|
|
|
2014-02-17 03:18:15 -07:00
|
|
|
#include <algorithm>
|
2013-06-13 21:09:55 -06:00
|
|
|
#include <cassert>
|
|
|
|
#include <iostream>
|
|
|
|
#include <map>
|
2016-06-25 21:34:09 -06:00
|
|
|
#include <memory>
|
2014-02-17 03:18:15 -07:00
|
|
|
#include <string>
|
2013-06-13 21:09:55 -06:00
|
|
|
#include <vector>
|
|
|
|
|
2017-06-07 17:03:36 -06:00
|
|
|
#include "Common/StringUtil.h"
|
2016-10-11 18:48:38 -06:00
|
|
|
#include "InputCommon/ControlReference/ExpressionParser.h"
|
2014-02-17 03:18:15 -07:00
|
|
|
|
2019-06-17 14:39:24 -06:00
|
|
|
namespace ciface::ExpressionParser
|
|
|
|
{
|
2013-06-13 21:09:55 -06:00
|
|
|
using namespace ciface::Core;
|
|
|
|
|
|
|
|
enum TokenType
|
|
|
|
{
|
|
|
|
TOK_DISCARD,
|
|
|
|
TOK_INVALID,
|
|
|
|
TOK_EOF,
|
|
|
|
TOK_LPAREN,
|
|
|
|
TOK_RPAREN,
|
|
|
|
TOK_AND,
|
|
|
|
TOK_OR,
|
|
|
|
TOK_NOT,
|
2013-06-14 00:52:07 -06:00
|
|
|
TOK_ADD,
|
2018-12-30 10:51:12 -07:00
|
|
|
TOK_MUL,
|
|
|
|
TOK_DIV,
|
2013-06-13 21:09:55 -06:00
|
|
|
TOK_CONTROL,
|
2018-12-30 09:37:23 -07:00
|
|
|
TOK_LITERAL,
|
2013-06-13 21:09:55 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
inline std::string OpName(TokenType op)
|
|
|
|
{
|
|
|
|
switch (op)
|
|
|
|
{
|
|
|
|
case TOK_AND:
|
|
|
|
return "And";
|
|
|
|
case TOK_OR:
|
|
|
|
return "Or";
|
|
|
|
case TOK_NOT:
|
|
|
|
return "Not";
|
2013-06-14 00:52:07 -06:00
|
|
|
case TOK_ADD:
|
|
|
|
return "Add";
|
2018-12-30 10:51:12 -07:00
|
|
|
case TOK_MUL:
|
|
|
|
return "Mul";
|
|
|
|
case TOK_DIV:
|
|
|
|
return "Div";
|
2013-06-13 21:09:55 -06:00
|
|
|
default:
|
|
|
|
assert(false);
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class Token
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
TokenType type;
|
2018-12-30 09:37:23 -07:00
|
|
|
std::string data;
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2013-06-13 21:09:55 -06:00
|
|
|
Token(TokenType type_) : type(type_) {}
|
2018-12-30 09:37:23 -07:00
|
|
|
Token(TokenType type_, std::string data_) : type(type_), data(std::move(data_)) {}
|
2017-02-26 00:04:16 -07:00
|
|
|
operator std::string() const
|
2013-06-13 21:09:55 -06:00
|
|
|
{
|
|
|
|
switch (type)
|
|
|
|
{
|
|
|
|
case TOK_DISCARD:
|
|
|
|
return "Discard";
|
|
|
|
case TOK_EOF:
|
|
|
|
return "EOF";
|
|
|
|
case TOK_LPAREN:
|
|
|
|
return "(";
|
|
|
|
case TOK_RPAREN:
|
|
|
|
return ")";
|
|
|
|
case TOK_AND:
|
|
|
|
return "&";
|
|
|
|
case TOK_OR:
|
|
|
|
return "|";
|
|
|
|
case TOK_NOT:
|
|
|
|
return "!";
|
2013-06-14 00:52:07 -06:00
|
|
|
case TOK_ADD:
|
|
|
|
return "+";
|
2018-12-30 10:51:12 -07:00
|
|
|
case TOK_MUL:
|
|
|
|
return "*";
|
|
|
|
case TOK_DIV:
|
|
|
|
return "/";
|
2013-06-13 21:09:55 -06:00
|
|
|
case TOK_CONTROL:
|
2018-12-30 09:37:23 -07:00
|
|
|
return "Device(" + data + ")";
|
|
|
|
case TOK_LITERAL:
|
|
|
|
return '\'' + data + '\'';
|
2015-09-09 20:41:47 -06:00
|
|
|
case TOK_INVALID:
|
|
|
|
break;
|
2013-06-13 21:09:55 -06:00
|
|
|
}
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2015-09-09 20:41:47 -06:00
|
|
|
return "Invalid";
|
2013-06-13 21:09:55 -06:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class Lexer
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
std::string expr;
|
|
|
|
std::string::iterator it;
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2015-05-28 18:28:48 -06:00
|
|
|
Lexer(const std::string& expr_) : expr(expr_) { it = expr.begin(); }
|
2018-12-30 09:37:23 -07:00
|
|
|
|
|
|
|
bool FetchDelimString(std::string& value, char delim)
|
2013-06-13 21:09:55 -06:00
|
|
|
{
|
2013-07-12 11:26:09 -06:00
|
|
|
value = "";
|
2013-06-13 21:09:55 -06:00
|
|
|
while (it != expr.end())
|
|
|
|
{
|
|
|
|
char c = *it;
|
2014-02-12 08:00:34 -07:00
|
|
|
++it;
|
2018-12-30 09:37:23 -07:00
|
|
|
if (c == delim)
|
2013-06-13 21:09:55 -06:00
|
|
|
return true;
|
|
|
|
value += c;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2018-12-30 09:37:23 -07:00
|
|
|
Token GetLiteral()
|
2013-06-13 21:09:55 -06:00
|
|
|
{
|
|
|
|
std::string value;
|
2018-12-30 09:37:23 -07:00
|
|
|
FetchDelimString(value, '\'');
|
|
|
|
return Token(TOK_LITERAL, value);
|
|
|
|
}
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2018-12-30 09:37:23 -07:00
|
|
|
Token GetFullyQualifiedControl()
|
|
|
|
{
|
|
|
|
std::string value;
|
|
|
|
FetchDelimString(value, '`');
|
|
|
|
return Token(TOK_CONTROL, value);
|
2013-06-13 21:09:55 -06:00
|
|
|
}
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2013-06-14 01:10:27 -06:00
|
|
|
Token GetBarewordsControl(char c)
|
|
|
|
{
|
|
|
|
std::string name;
|
|
|
|
name += c;
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2014-08-30 14:44:28 -06:00
|
|
|
while (it != expr.end())
|
|
|
|
{
|
2013-06-14 01:10:27 -06:00
|
|
|
c = *it;
|
|
|
|
if (!isalpha(c))
|
|
|
|
break;
|
|
|
|
name += c;
|
2014-02-12 08:00:34 -07:00
|
|
|
++it;
|
2013-06-14 01:10:27 -06:00
|
|
|
}
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2013-06-14 01:10:27 -06:00
|
|
|
ControlQualifier qualifier;
|
|
|
|
qualifier.control_name = name;
|
|
|
|
return Token(TOK_CONTROL, qualifier);
|
|
|
|
}
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2013-06-13 21:09:55 -06:00
|
|
|
Token NextToken()
|
|
|
|
{
|
|
|
|
if (it == expr.end())
|
|
|
|
return Token(TOK_EOF);
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2013-06-13 21:09:55 -06:00
|
|
|
char c = *it++;
|
|
|
|
switch (c)
|
|
|
|
{
|
|
|
|
case ' ':
|
|
|
|
case '\t':
|
|
|
|
case '\n':
|
|
|
|
case '\r':
|
|
|
|
return Token(TOK_DISCARD);
|
|
|
|
case '(':
|
|
|
|
return Token(TOK_LPAREN);
|
|
|
|
case ')':
|
|
|
|
return Token(TOK_RPAREN);
|
|
|
|
case '&':
|
|
|
|
return Token(TOK_AND);
|
|
|
|
case '|':
|
|
|
|
return Token(TOK_OR);
|
|
|
|
case '!':
|
|
|
|
return Token(TOK_NOT);
|
2013-06-14 00:52:07 -06:00
|
|
|
case '+':
|
|
|
|
return Token(TOK_ADD);
|
2018-12-30 10:51:12 -07:00
|
|
|
case '*':
|
|
|
|
return Token(TOK_MUL);
|
|
|
|
case '/':
|
|
|
|
return Token(TOK_DIV);
|
2018-12-30 09:37:23 -07:00
|
|
|
case '\'':
|
|
|
|
return GetLiteral();
|
2013-06-13 21:09:55 -06:00
|
|
|
case '`':
|
2013-06-14 01:10:27 -06:00
|
|
|
return GetFullyQualifiedControl();
|
2013-06-13 21:09:55 -06:00
|
|
|
default:
|
2013-06-14 01:10:27 -06:00
|
|
|
if (isalpha(c))
|
|
|
|
return GetBarewordsControl(c);
|
|
|
|
else
|
|
|
|
return Token(TOK_INVALID);
|
2016-06-24 02:43:46 -06:00
|
|
|
}
|
2013-06-13 21:09:55 -06:00
|
|
|
}
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2017-02-28 02:58:03 -07:00
|
|
|
ParseStatus Tokenize(std::vector<Token>& tokens)
|
2013-06-13 21:09:55 -06:00
|
|
|
{
|
|
|
|
while (true)
|
|
|
|
{
|
|
|
|
Token tok = NextToken();
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2013-06-13 21:09:55 -06:00
|
|
|
if (tok.type == TOK_DISCARD)
|
|
|
|
continue;
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2013-06-13 21:09:55 -06:00
|
|
|
if (tok.type == TOK_INVALID)
|
|
|
|
{
|
2013-07-02 19:42:44 -06:00
|
|
|
tokens.clear();
|
2017-02-28 02:58:03 -07:00
|
|
|
return ParseStatus::SyntaxError;
|
2013-06-13 21:09:55 -06:00
|
|
|
}
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2013-06-13 21:09:55 -06:00
|
|
|
tokens.push_back(tok);
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2013-06-13 21:09:55 -06:00
|
|
|
if (tok.type == TOK_EOF)
|
|
|
|
break;
|
|
|
|
}
|
2017-04-02 04:13:12 -06:00
|
|
|
return ParseStatus::Successful;
|
2013-06-13 21:09:55 -06:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-06-07 18:30:07 -06:00
|
|
|
class ControlExpression : public Expression
|
2013-06-13 21:09:55 -06:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
ControlQualifier qualifier;
|
2017-06-07 19:48:17 -06:00
|
|
|
Device::Control* control = nullptr;
|
2017-06-07 16:54:59 -06:00
|
|
|
// Keep a shared_ptr to the device so the control pointer doesn't become invalid
|
|
|
|
std::shared_ptr<Device> m_device;
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2017-06-07 20:02:16 -06:00
|
|
|
explicit ControlExpression(ControlQualifier qualifier_) : qualifier(qualifier_) {}
|
2019-02-26 18:46:21 -07:00
|
|
|
ControlState GetValue() const override
|
|
|
|
{
|
|
|
|
if (!control)
|
|
|
|
return 0.0;
|
|
|
|
|
|
|
|
// Note: Inputs may return negative values in situations where opposing directions are
|
|
|
|
// activated. We clamp off the negative values here.
|
|
|
|
|
|
|
|
// FYI: Clamping values greater than 1.0 is purposely not done to support unbounded values in
|
|
|
|
// the future. (e.g. raw accelerometer/gyro data)
|
|
|
|
|
|
|
|
return std::max(0.0, control->ToInput()->GetState());
|
|
|
|
}
|
2017-06-07 16:56:49 -06:00
|
|
|
void SetValue(ControlState value) override
|
|
|
|
{
|
|
|
|
if (control)
|
|
|
|
control->ToOutput()->SetState(value);
|
|
|
|
}
|
|
|
|
int CountNumControls() const override { return control ? 1 : 0; }
|
2017-06-07 19:48:17 -06:00
|
|
|
void UpdateReferences(ControlFinder& finder) override
|
|
|
|
{
|
|
|
|
m_device = finder.FindDevice(qualifier);
|
|
|
|
control = finder.FindControl(qualifier);
|
|
|
|
}
|
2017-06-07 16:54:59 -06:00
|
|
|
operator std::string() const override { return "`" + static_cast<std::string>(qualifier) + "`"; }
|
2013-06-13 21:09:55 -06:00
|
|
|
};
|
|
|
|
|
2017-06-07 18:30:07 -06:00
|
|
|
class BinaryExpression : public Expression
|
2013-06-13 21:09:55 -06:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
TokenType op;
|
2017-06-07 18:30:07 -06:00
|
|
|
std::unique_ptr<Expression> lhs;
|
|
|
|
std::unique_ptr<Expression> rhs;
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2017-06-07 18:30:07 -06:00
|
|
|
BinaryExpression(TokenType op_, std::unique_ptr<Expression>&& lhs_,
|
|
|
|
std::unique_ptr<Expression>&& rhs_)
|
2017-06-07 16:29:00 -06:00
|
|
|
: op(op_), lhs(std::move(lhs_)), rhs(std::move(rhs_))
|
2013-06-13 21:09:55 -06:00
|
|
|
{
|
|
|
|
}
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2017-02-26 00:04:16 -07:00
|
|
|
ControlState GetValue() const override
|
2013-06-13 21:09:55 -06:00
|
|
|
{
|
|
|
|
ControlState lhsValue = lhs->GetValue();
|
|
|
|
ControlState rhsValue = rhs->GetValue();
|
|
|
|
switch (op)
|
|
|
|
{
|
|
|
|
case TOK_AND:
|
|
|
|
return std::min(lhsValue, rhsValue);
|
|
|
|
case TOK_OR:
|
|
|
|
return std::max(lhsValue, rhsValue);
|
2013-06-14 00:52:07 -06:00
|
|
|
case TOK_ADD:
|
2018-12-30 10:51:12 -07:00
|
|
|
return lhsValue + rhsValue;
|
|
|
|
case TOK_MUL:
|
|
|
|
return lhsValue * rhsValue;
|
|
|
|
case TOK_DIV:
|
|
|
|
{
|
|
|
|
const ControlState result = lhsValue / rhsValue;
|
|
|
|
return std::isinf(result) ? 0.0 : result;
|
|
|
|
}
|
2013-06-13 21:09:55 -06:00
|
|
|
default:
|
|
|
|
assert(false);
|
|
|
|
return 0;
|
|
|
|
}
|
2016-06-24 02:43:46 -06:00
|
|
|
}
|
|
|
|
|
2015-07-30 04:47:02 -06:00
|
|
|
void SetValue(ControlState value) override
|
2013-06-13 21:09:55 -06:00
|
|
|
{
|
|
|
|
// Don't do anything special with the op we have.
|
|
|
|
// Treat "A & B" the same as "A | B".
|
|
|
|
lhs->SetValue(value);
|
|
|
|
rhs->SetValue(value);
|
|
|
|
}
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2017-02-26 00:04:16 -07:00
|
|
|
int CountNumControls() const override
|
|
|
|
{
|
|
|
|
return lhs->CountNumControls() + rhs->CountNumControls();
|
|
|
|
}
|
|
|
|
|
2017-06-07 19:48:17 -06:00
|
|
|
void UpdateReferences(ControlFinder& finder) override
|
|
|
|
{
|
|
|
|
lhs->UpdateReferences(finder);
|
|
|
|
rhs->UpdateReferences(finder);
|
|
|
|
}
|
|
|
|
|
2017-02-26 00:04:16 -07:00
|
|
|
operator std::string() const override
|
2013-06-13 21:09:55 -06:00
|
|
|
{
|
|
|
|
return OpName(op) + "(" + (std::string)(*lhs) + ", " + (std::string)(*rhs) + ")";
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-06-07 18:30:07 -06:00
|
|
|
class UnaryExpression : public Expression
|
2013-06-13 21:09:55 -06:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
TokenType op;
|
2017-06-07 18:30:07 -06:00
|
|
|
std::unique_ptr<Expression> inner;
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2017-06-07 18:30:07 -06:00
|
|
|
UnaryExpression(TokenType op_, std::unique_ptr<Expression>&& inner_)
|
2017-06-07 16:29:00 -06:00
|
|
|
: op(op_), inner(std::move(inner_))
|
|
|
|
{
|
|
|
|
}
|
2017-02-26 00:04:16 -07:00
|
|
|
ControlState GetValue() const override
|
2013-06-13 21:09:55 -06:00
|
|
|
{
|
|
|
|
ControlState value = inner->GetValue();
|
|
|
|
switch (op)
|
|
|
|
{
|
|
|
|
case TOK_NOT:
|
2014-11-09 13:02:18 -07:00
|
|
|
return 1.0 - value;
|
2013-06-13 21:09:55 -06:00
|
|
|
default:
|
|
|
|
assert(false);
|
|
|
|
return 0;
|
|
|
|
}
|
2016-06-24 02:43:46 -06:00
|
|
|
}
|
|
|
|
|
2015-07-30 04:47:02 -06:00
|
|
|
void SetValue(ControlState value) override
|
2013-06-13 21:09:55 -06:00
|
|
|
{
|
|
|
|
switch (op)
|
|
|
|
{
|
|
|
|
case TOK_NOT:
|
2014-11-09 13:02:18 -07:00
|
|
|
inner->SetValue(1.0 - value);
|
2015-05-22 13:02:22 -06:00
|
|
|
break;
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2013-06-13 21:09:55 -06:00
|
|
|
default:
|
|
|
|
assert(false);
|
|
|
|
}
|
2016-06-24 02:43:46 -06:00
|
|
|
}
|
|
|
|
|
2017-02-26 00:04:16 -07:00
|
|
|
int CountNumControls() const override { return inner->CountNumControls(); }
|
2017-06-07 19:48:17 -06:00
|
|
|
void UpdateReferences(ControlFinder& finder) override { inner->UpdateReferences(finder); }
|
2017-02-26 00:04:16 -07:00
|
|
|
operator std::string() const override { return OpName(op) + "(" + (std::string)(*inner) + ")"; }
|
2013-06-13 21:09:55 -06:00
|
|
|
};
|
|
|
|
|
2018-12-30 09:37:23 -07:00
|
|
|
class LiteralExpression : public Expression
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
explicit LiteralExpression(const std::string& str)
|
|
|
|
{
|
|
|
|
// If it fails to parse it will just be the default: 0.0
|
|
|
|
TryParse(str, &m_value);
|
|
|
|
}
|
|
|
|
|
|
|
|
ControlState GetValue() const override { return m_value; }
|
|
|
|
|
|
|
|
void SetValue(ControlState value) override
|
|
|
|
{
|
|
|
|
// Do nothing.
|
|
|
|
}
|
|
|
|
|
|
|
|
int CountNumControls() const override { return 1; }
|
|
|
|
|
|
|
|
void UpdateReferences(ControlFinder&) override
|
|
|
|
{
|
|
|
|
// Nothing needed.
|
|
|
|
}
|
|
|
|
|
|
|
|
operator std::string() const override { return '\'' + ValueToString(m_value) + '\''; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
ControlState m_value{};
|
|
|
|
};
|
|
|
|
|
2017-06-07 19:29:02 -06:00
|
|
|
// This class proxies all methods to its either left-hand child if it has bound controls, or its
|
|
|
|
// right-hand child. Its intended use is for supporting old-style barewords expressions.
|
|
|
|
class CoalesceExpression : public Expression
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
CoalesceExpression(std::unique_ptr<Expression>&& lhs, std::unique_ptr<Expression>&& rhs)
|
|
|
|
: m_lhs(std::move(lhs)), m_rhs(std::move(rhs))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
ControlState GetValue() const override { return GetActiveChild()->GetValue(); }
|
2018-12-22 10:17:05 -07:00
|
|
|
void SetValue(ControlState value) override { GetActiveChild()->SetValue(value); }
|
2017-06-07 19:29:02 -06:00
|
|
|
|
|
|
|
int CountNumControls() const override { return GetActiveChild()->CountNumControls(); }
|
|
|
|
operator std::string() const override
|
|
|
|
{
|
|
|
|
return "Coalesce(" + static_cast<std::string>(*m_lhs) + ", " +
|
|
|
|
static_cast<std::string>(*m_rhs) + ')';
|
|
|
|
}
|
|
|
|
|
2017-06-07 19:48:17 -06:00
|
|
|
void UpdateReferences(ControlFinder& finder) override
|
|
|
|
{
|
|
|
|
m_lhs->UpdateReferences(finder);
|
|
|
|
m_rhs->UpdateReferences(finder);
|
|
|
|
}
|
|
|
|
|
2017-06-07 19:29:02 -06:00
|
|
|
private:
|
|
|
|
const std::unique_ptr<Expression>& GetActiveChild() const
|
|
|
|
{
|
|
|
|
return m_lhs->CountNumControls() > 0 ? m_lhs : m_rhs;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::unique_ptr<Expression> m_lhs;
|
|
|
|
std::unique_ptr<Expression> m_rhs;
|
|
|
|
};
|
|
|
|
|
2017-02-26 00:04:16 -07:00
|
|
|
std::shared_ptr<Device> ControlFinder::FindDevice(ControlQualifier qualifier) const
|
2013-06-13 21:09:55 -06:00
|
|
|
{
|
|
|
|
if (qualifier.has_device)
|
|
|
|
return container.FindDevice(qualifier.device_qualifier);
|
|
|
|
else
|
|
|
|
return container.FindDevice(default_device);
|
|
|
|
}
|
|
|
|
|
2017-02-26 00:04:16 -07:00
|
|
|
Device::Control* ControlFinder::FindControl(ControlQualifier qualifier) const
|
2013-06-13 21:09:55 -06:00
|
|
|
{
|
2016-06-25 13:46:39 -06:00
|
|
|
const std::shared_ptr<Device> device = FindDevice(qualifier);
|
2013-06-26 14:54:48 -06:00
|
|
|
if (!device)
|
2014-03-09 14:14:26 -06:00
|
|
|
return nullptr;
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2013-06-13 21:09:55 -06:00
|
|
|
if (is_input)
|
|
|
|
return device->FindInput(qualifier.control_name);
|
|
|
|
else
|
|
|
|
return device->FindOutput(qualifier.control_name);
|
|
|
|
}
|
|
|
|
|
2017-06-07 16:08:55 -06:00
|
|
|
struct ParseResult
|
|
|
|
{
|
2017-06-07 18:30:07 -06:00
|
|
|
ParseResult(ParseStatus status_, std::unique_ptr<Expression>&& expr_ = {})
|
2017-06-07 16:29:00 -06:00
|
|
|
: status(status_), expr(std::move(expr_))
|
2017-06-07 16:08:55 -06:00
|
|
|
{
|
|
|
|
}
|
2017-06-07 16:29:00 -06:00
|
|
|
|
2017-06-07 16:08:55 -06:00
|
|
|
ParseStatus status;
|
2017-06-07 18:30:07 -06:00
|
|
|
std::unique_ptr<Expression> expr;
|
2017-06-07 16:08:55 -06:00
|
|
|
};
|
|
|
|
|
2013-06-13 21:09:55 -06:00
|
|
|
class Parser
|
|
|
|
{
|
|
|
|
public:
|
2017-06-07 19:48:17 -06:00
|
|
|
explicit Parser(std::vector<Token> tokens_) : tokens(tokens_) { m_it = tokens.begin(); }
|
2017-06-07 16:08:55 -06:00
|
|
|
ParseResult Parse() { return Toplevel(); }
|
2018-04-12 06:18:04 -06:00
|
|
|
|
2013-06-13 21:09:55 -06:00
|
|
|
private:
|
|
|
|
std::vector<Token> tokens;
|
|
|
|
std::vector<Token>::iterator m_it;
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2013-06-13 21:09:55 -06:00
|
|
|
Token Chew() { return *m_it++; }
|
|
|
|
Token Peek() { return *m_it; }
|
|
|
|
bool Expects(TokenType type)
|
|
|
|
{
|
|
|
|
Token tok = Chew();
|
|
|
|
return tok.type == type;
|
|
|
|
}
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2017-06-07 16:08:55 -06:00
|
|
|
ParseResult Atom()
|
2013-06-13 21:09:55 -06:00
|
|
|
{
|
|
|
|
Token tok = Chew();
|
|
|
|
switch (tok.type)
|
|
|
|
{
|
|
|
|
case TOK_CONTROL:
|
2018-12-30 09:37:23 -07:00
|
|
|
{
|
|
|
|
ControlQualifier cq;
|
|
|
|
cq.FromString(tok.data);
|
|
|
|
return {ParseStatus::Successful, std::make_unique<ControlExpression>(cq)};
|
|
|
|
}
|
|
|
|
case TOK_LITERAL:
|
|
|
|
{
|
|
|
|
return {ParseStatus::Successful, std::make_unique<LiteralExpression>(tok.data)};
|
|
|
|
}
|
2013-06-13 21:09:55 -06:00
|
|
|
case TOK_LPAREN:
|
2017-06-07 16:08:55 -06:00
|
|
|
return Paren();
|
2013-06-13 21:09:55 -06:00
|
|
|
default:
|
2017-06-07 16:08:55 -06:00
|
|
|
return {ParseStatus::SyntaxError};
|
2016-06-24 02:43:46 -06:00
|
|
|
}
|
2013-06-13 21:09:55 -06:00
|
|
|
}
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2013-06-13 21:09:55 -06:00
|
|
|
bool IsUnaryExpression(TokenType type)
|
|
|
|
{
|
|
|
|
switch (type)
|
|
|
|
{
|
|
|
|
case TOK_NOT:
|
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
return false;
|
2016-06-24 02:43:46 -06:00
|
|
|
}
|
2013-06-13 21:09:55 -06:00
|
|
|
}
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2017-06-07 16:08:55 -06:00
|
|
|
ParseResult Unary()
|
2013-06-13 21:09:55 -06:00
|
|
|
{
|
|
|
|
if (IsUnaryExpression(Peek().type))
|
|
|
|
{
|
|
|
|
Token tok = Chew();
|
2017-06-07 16:08:55 -06:00
|
|
|
ParseResult result = Atom();
|
|
|
|
if (result.status == ParseStatus::SyntaxError)
|
|
|
|
return result;
|
2017-06-07 16:29:00 -06:00
|
|
|
return {ParseStatus::Successful,
|
|
|
|
std::make_unique<UnaryExpression>(tok.type, std::move(result.expr))};
|
2013-06-13 21:09:55 -06:00
|
|
|
}
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2017-06-07 16:08:55 -06:00
|
|
|
return Atom();
|
2013-06-13 21:09:55 -06:00
|
|
|
}
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2013-06-13 21:09:55 -06:00
|
|
|
bool IsBinaryToken(TokenType type)
|
|
|
|
{
|
|
|
|
switch (type)
|
|
|
|
{
|
|
|
|
case TOK_AND:
|
|
|
|
case TOK_OR:
|
2013-06-14 00:52:07 -06:00
|
|
|
case TOK_ADD:
|
2018-12-30 10:51:12 -07:00
|
|
|
case TOK_MUL:
|
|
|
|
case TOK_DIV:
|
2013-06-13 21:09:55 -06:00
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
return false;
|
2016-06-24 02:43:46 -06:00
|
|
|
}
|
2013-06-13 21:09:55 -06:00
|
|
|
}
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2017-06-07 16:08:55 -06:00
|
|
|
ParseResult Binary()
|
2013-06-13 21:09:55 -06:00
|
|
|
{
|
2017-06-07 16:08:55 -06:00
|
|
|
ParseResult result = Unary();
|
|
|
|
if (result.status == ParseStatus::SyntaxError)
|
|
|
|
return result;
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2017-06-07 18:30:07 -06:00
|
|
|
std::unique_ptr<Expression> expr = std::move(result.expr);
|
2013-06-13 21:09:55 -06:00
|
|
|
while (IsBinaryToken(Peek().type))
|
|
|
|
{
|
|
|
|
Token tok = Chew();
|
2017-06-07 16:08:55 -06:00
|
|
|
ParseResult unary_result = Unary();
|
|
|
|
if (unary_result.status == ParseStatus::SyntaxError)
|
2013-06-13 21:09:55 -06:00
|
|
|
{
|
2017-06-07 16:08:55 -06:00
|
|
|
return unary_result;
|
2013-06-13 21:09:55 -06:00
|
|
|
}
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2017-06-07 16:29:00 -06:00
|
|
|
expr = std::make_unique<BinaryExpression>(tok.type, std::move(expr),
|
|
|
|
std::move(unary_result.expr));
|
2013-06-13 21:09:55 -06:00
|
|
|
}
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2017-06-07 16:29:00 -06:00
|
|
|
return {ParseStatus::Successful, std::move(expr)};
|
2013-06-13 21:09:55 -06:00
|
|
|
}
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2017-06-07 16:08:55 -06:00
|
|
|
ParseResult Paren()
|
2013-06-13 21:09:55 -06:00
|
|
|
{
|
|
|
|
// lparen already chewed
|
2017-06-07 16:08:55 -06:00
|
|
|
ParseResult result = Toplevel();
|
|
|
|
if (result.status != ParseStatus::Successful)
|
|
|
|
return result;
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2013-06-13 21:09:55 -06:00
|
|
|
if (!Expects(TOK_RPAREN))
|
|
|
|
{
|
2017-06-07 16:08:55 -06:00
|
|
|
return {ParseStatus::SyntaxError};
|
2013-06-13 21:09:55 -06:00
|
|
|
}
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2017-06-07 16:08:55 -06:00
|
|
|
return result;
|
2013-06-13 21:09:55 -06:00
|
|
|
}
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2017-06-07 16:08:55 -06:00
|
|
|
ParseResult Toplevel() { return Binary(); }
|
2013-06-13 21:09:55 -06:00
|
|
|
};
|
|
|
|
|
2017-06-07 19:48:17 -06:00
|
|
|
static ParseResult ParseComplexExpression(const std::string& str)
|
2013-06-13 21:09:55 -06:00
|
|
|
{
|
|
|
|
Lexer l(str);
|
|
|
|
std::vector<Token> tokens;
|
2017-06-07 15:53:41 -06:00
|
|
|
ParseStatus tokenize_status = l.Tokenize(tokens);
|
|
|
|
if (tokenize_status != ParseStatus::Successful)
|
2017-06-07 18:30:07 -06:00
|
|
|
return {tokenize_status};
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2017-06-08 01:23:38 -06:00
|
|
|
return Parser(std::move(tokens)).Parse();
|
2013-06-13 21:09:55 -06:00
|
|
|
}
|
|
|
|
|
2017-06-07 19:48:17 -06:00
|
|
|
static std::unique_ptr<Expression> ParseBarewordExpression(const std::string& str)
|
2013-06-26 18:19:23 -06:00
|
|
|
{
|
|
|
|
ControlQualifier qualifier;
|
|
|
|
qualifier.control_name = str;
|
|
|
|
qualifier.has_device = false;
|
|
|
|
|
2017-06-07 19:48:17 -06:00
|
|
|
return std::make_unique<ControlExpression>(qualifier);
|
2017-06-07 19:29:02 -06:00
|
|
|
}
|
|
|
|
|
2017-06-07 19:48:17 -06:00
|
|
|
std::pair<ParseStatus, std::unique_ptr<Expression>> ParseExpression(const std::string& str)
|
2017-06-07 19:29:02 -06:00
|
|
|
{
|
|
|
|
if (StripSpaces(str).empty())
|
|
|
|
return std::make_pair(ParseStatus::EmptyExpression, nullptr);
|
|
|
|
|
2017-06-07 19:48:17 -06:00
|
|
|
auto bareword_expr = ParseBarewordExpression(str);
|
|
|
|
ParseResult complex_result = ParseComplexExpression(str);
|
2017-06-07 19:29:02 -06:00
|
|
|
|
|
|
|
if (complex_result.status != ParseStatus::Successful)
|
2014-08-30 14:44:28 -06:00
|
|
|
{
|
2017-06-07 19:29:02 -06:00
|
|
|
return std::make_pair(complex_result.status, std::move(bareword_expr));
|
2013-06-26 18:19:23 -06:00
|
|
|
}
|
|
|
|
|
2017-06-07 19:29:02 -06:00
|
|
|
auto combined_expr = std::make_unique<CoalesceExpression>(std::move(bareword_expr),
|
|
|
|
std::move(complex_result.expr));
|
|
|
|
return std::make_pair(complex_result.status, std::move(combined_expr));
|
2013-06-26 18:19:23 -06:00
|
|
|
}
|
2019-06-17 14:39:24 -06:00
|
|
|
} // namespace ciface::ExpressionParser
|