2014-02-10 18:54:46 +00:00
|
|
|
// Copyright 2013 Dolphin Emulator Project
|
2021-07-05 01:22:19 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2013-06-14 03:09:55 +00:00
|
|
|
|
2021-04-01 21:30:01 +00:00
|
|
|
#include "InputCommon/ControlReference/ExpressionParser.h"
|
|
|
|
|
2019-10-23 00:12:55 +00:00
|
|
|
#include <algorithm>
|
2018-12-30 19:16:28 +00:00
|
|
|
#include <cmath>
|
2020-10-20 20:18:30 +00:00
|
|
|
#include <functional>
|
2013-06-14 03:09:55 +00:00
|
|
|
#include <iostream>
|
2019-10-23 21:45:21 +00:00
|
|
|
#include <map>
|
2016-06-26 03:34:09 +00:00
|
|
|
#include <memory>
|
2018-12-30 18:38:02 +00:00
|
|
|
#include <regex>
|
2014-02-17 10:18:15 +00:00
|
|
|
#include <string>
|
2019-01-26 18:17:30 +00:00
|
|
|
#include <utility>
|
2013-06-14 03:09:55 +00:00
|
|
|
#include <vector>
|
|
|
|
|
2021-04-01 21:30:01 +00:00
|
|
|
#include "Common/Assert.h"
|
2024-07-28 10:09:15 +00:00
|
|
|
#include "Common/MsgHandler.h"
|
2017-06-07 23:03:36 +00:00
|
|
|
#include "Common/StringUtil.h"
|
2019-01-26 18:17:30 +00:00
|
|
|
|
|
|
|
#include "InputCommon/ControlReference/FunctionExpression.h"
|
2014-02-17 10:18:15 +00:00
|
|
|
|
2019-06-17 20:39:24 +00:00
|
|
|
namespace ciface::ExpressionParser
|
|
|
|
{
|
2013-06-14 03:09:55 +00:00
|
|
|
using namespace ciface::Core;
|
|
|
|
|
2019-10-25 23:49:06 +00:00
|
|
|
class ControlExpression;
|
|
|
|
|
2019-10-23 21:45:21 +00:00
|
|
|
class HotkeySuppressions
|
|
|
|
{
|
|
|
|
public:
|
2019-11-02 14:55:45 +00:00
|
|
|
using Modifiers = std::vector<std::unique_ptr<ControlExpression>>;
|
2020-02-16 19:09:12 +00:00
|
|
|
|
|
|
|
struct InvokingDeleter
|
|
|
|
{
|
|
|
|
template <typename T>
|
|
|
|
void operator()(T* func)
|
|
|
|
{
|
|
|
|
(*func)();
|
2023-02-18 02:26:23 +00:00
|
|
|
delete func;
|
2020-02-16 19:09:12 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
using Suppressor = std::unique_ptr<std::function<void()>, InvokingDeleter>;
|
2019-10-23 21:45:21 +00:00
|
|
|
|
2019-10-25 23:49:06 +00:00
|
|
|
bool IsSuppressed(Device::Input* input) const
|
2019-10-23 21:45:21 +00:00
|
|
|
{
|
2019-11-02 14:55:45 +00:00
|
|
|
// Input is suppressed if it exists in the map at all.
|
|
|
|
return m_suppressions.lower_bound({input, nullptr}) !=
|
|
|
|
m_suppressions.lower_bound({input + 1, nullptr});
|
2019-10-23 21:45:21 +00:00
|
|
|
}
|
|
|
|
|
2019-11-02 14:55:45 +00:00
|
|
|
bool IsSuppressedIgnoringModifiers(Device::Input* input, const Modifiers& ignore_modifiers) const;
|
|
|
|
|
2019-10-25 23:49:06 +00:00
|
|
|
// Suppresses each input + modifier pair.
|
|
|
|
// The returned object removes the suppression on destruction.
|
2019-11-02 14:55:45 +00:00
|
|
|
Suppressor MakeSuppressor(const Modifiers* modifiers,
|
|
|
|
const std::unique_ptr<ControlExpression>* final_input);
|
2019-10-25 23:49:06 +00:00
|
|
|
|
2019-10-23 21:45:21 +00:00
|
|
|
private:
|
2019-10-25 23:49:06 +00:00
|
|
|
using Suppression = std::pair<Device::Input*, Device::Input*>;
|
|
|
|
using SuppressionLevel = u16;
|
|
|
|
|
|
|
|
void RemoveSuppression(Device::Input* modifier, Device::Input* final_input)
|
2019-10-23 21:45:21 +00:00
|
|
|
{
|
2019-10-25 23:49:06 +00:00
|
|
|
auto it = m_suppressions.find({final_input, modifier});
|
2021-04-26 16:41:31 +00:00
|
|
|
if (it != m_suppressions.end() && (--it->second) == 0)
|
2019-10-23 21:45:21 +00:00
|
|
|
m_suppressions.erase(it);
|
|
|
|
}
|
|
|
|
|
2019-10-25 23:49:06 +00:00
|
|
|
// Holds counts of suppressions for each input/modifier pair.
|
|
|
|
std::map<Suppression, SuppressionLevel> m_suppressions;
|
2019-10-23 21:45:21 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static HotkeySuppressions s_hotkey_suppressions;
|
|
|
|
|
2019-03-02 16:10:26 +00:00
|
|
|
Token::Token(TokenType type_) : type(type_)
|
2013-06-14 03:09:55 +00:00
|
|
|
{
|
2019-03-02 16:10:26 +00:00
|
|
|
}
|
2013-06-14 03:09:55 +00:00
|
|
|
|
2019-03-02 16:10:26 +00:00
|
|
|
Token::Token(TokenType type_, std::string data_) : type(type_), data(std::move(data_))
|
2013-06-14 03:09:55 +00:00
|
|
|
{
|
2019-03-02 16:10:26 +00:00
|
|
|
}
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2019-03-02 16:10:26 +00:00
|
|
|
bool Token::IsBinaryOperator() const
|
|
|
|
{
|
|
|
|
return type >= TOK_BINARY_OPS_BEGIN && type < TOK_BINARY_OPS_END;
|
|
|
|
}
|
2018-12-30 16:37:23 +00:00
|
|
|
|
2019-10-12 17:28:19 +00:00
|
|
|
Lexer::Lexer(std::string expr_) : expr(std::move(expr_))
|
2019-03-02 16:10:26 +00:00
|
|
|
{
|
|
|
|
it = expr.begin();
|
|
|
|
}
|
2019-01-05 21:31:05 +00:00
|
|
|
|
2019-03-02 16:10:26 +00:00
|
|
|
std::string Lexer::FetchDelimString(char delim)
|
|
|
|
{
|
|
|
|
const std::string result = FetchCharsWhile([delim](char c) { return c != delim; });
|
|
|
|
if (it != expr.end())
|
|
|
|
++it;
|
|
|
|
return result;
|
|
|
|
}
|
2018-12-30 18:38:02 +00:00
|
|
|
|
2019-03-02 16:10:26 +00:00
|
|
|
std::string Lexer::FetchWordChars()
|
|
|
|
{
|
2023-06-08 01:07:54 +00:00
|
|
|
return FetchCharsWhile([](char c) {
|
|
|
|
return std::isalpha(c, std::locale::classic()) || std::isdigit(c, std::locale::classic()) ||
|
|
|
|
c == '_';
|
|
|
|
});
|
2019-03-02 16:10:26 +00:00
|
|
|
}
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2019-03-02 16:10:26 +00:00
|
|
|
Token Lexer::GetDelimitedLiteral()
|
|
|
|
{
|
|
|
|
return Token(TOK_LITERAL, FetchDelimString('\''));
|
|
|
|
}
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2019-03-02 16:10:26 +00:00
|
|
|
Token Lexer::GetVariable()
|
|
|
|
{
|
|
|
|
return Token(TOK_VARIABLE, FetchWordChars());
|
|
|
|
}
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2019-03-02 16:10:26 +00:00
|
|
|
Token Lexer::GetFullyQualifiedControl()
|
|
|
|
{
|
|
|
|
return Token(TOK_CONTROL, FetchDelimString('`'));
|
|
|
|
}
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2019-04-04 22:35:49 +00:00
|
|
|
Token Lexer::GetBareword(char first_char)
|
2019-03-02 16:10:26 +00:00
|
|
|
{
|
2019-04-04 22:35:49 +00:00
|
|
|
return Token(TOK_BAREWORD, first_char + FetchWordChars());
|
2019-03-02 16:10:26 +00:00
|
|
|
}
|
2019-01-05 19:43:39 +00:00
|
|
|
|
2019-04-04 22:35:49 +00:00
|
|
|
Token Lexer::GetRealLiteral(char first_char)
|
2019-03-02 16:10:26 +00:00
|
|
|
{
|
|
|
|
std::string value;
|
2019-04-04 22:35:49 +00:00
|
|
|
value += first_char;
|
2019-03-02 16:10:26 +00:00
|
|
|
value += FetchCharsWhile([](char c) { return isdigit(c, std::locale::classic()) || ('.' == c); });
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2023-06-08 01:07:54 +00:00
|
|
|
static const std::regex re(R"(\d+(\.\d+)?)");
|
|
|
|
if (std::regex_match(value, re))
|
2019-03-02 20:47:26 +00:00
|
|
|
return Token(TOK_LITERAL, value);
|
|
|
|
|
|
|
|
return Token(TOK_INVALID);
|
2019-03-02 16:10:26 +00:00
|
|
|
}
|
|
|
|
|
2019-10-12 16:41:02 +00:00
|
|
|
Token Lexer::PeekToken()
|
|
|
|
{
|
|
|
|
const auto old_it = it;
|
|
|
|
const auto tok = NextToken();
|
|
|
|
it = old_it;
|
|
|
|
return tok;
|
|
|
|
}
|
|
|
|
|
2019-03-02 16:10:26 +00:00
|
|
|
Token Lexer::NextToken()
|
|
|
|
{
|
|
|
|
if (it == expr.end())
|
|
|
|
return Token(TOK_EOF);
|
|
|
|
|
|
|
|
char c = *it++;
|
|
|
|
switch (c)
|
|
|
|
{
|
|
|
|
case ' ':
|
|
|
|
case '\t':
|
|
|
|
case '\n':
|
|
|
|
case '\r':
|
2019-10-12 16:41:02 +00:00
|
|
|
return Token(TOK_WHITESPACE);
|
2019-03-02 16:10:26 +00:00
|
|
|
case '(':
|
|
|
|
return Token(TOK_LPAREN);
|
|
|
|
case ')':
|
|
|
|
return Token(TOK_RPAREN);
|
2019-10-23 00:12:55 +00:00
|
|
|
case '@':
|
|
|
|
return Token(TOK_HOTKEY);
|
2019-03-02 16:10:26 +00:00
|
|
|
case '&':
|
|
|
|
return Token(TOK_AND);
|
|
|
|
case '|':
|
|
|
|
return Token(TOK_OR);
|
|
|
|
case '!':
|
2019-04-04 22:35:49 +00:00
|
|
|
return Token(TOK_NOT);
|
2019-03-02 16:10:26 +00:00
|
|
|
case '+':
|
|
|
|
return Token(TOK_ADD);
|
|
|
|
case '-':
|
|
|
|
return Token(TOK_SUB);
|
|
|
|
case '*':
|
|
|
|
return Token(TOK_MUL);
|
|
|
|
case '/':
|
|
|
|
return Token(TOK_DIV);
|
|
|
|
case '%':
|
|
|
|
return Token(TOK_MOD);
|
|
|
|
case '=':
|
|
|
|
return Token(TOK_ASSIGN);
|
|
|
|
case '<':
|
|
|
|
return Token(TOK_LTHAN);
|
|
|
|
case '>':
|
|
|
|
return Token(TOK_GTHAN);
|
|
|
|
case ',':
|
|
|
|
return Token(TOK_COMMA);
|
2019-10-20 14:51:19 +00:00
|
|
|
case '^':
|
|
|
|
return Token(TOK_XOR);
|
2019-03-02 16:10:26 +00:00
|
|
|
case '\'':
|
|
|
|
return GetDelimitedLiteral();
|
|
|
|
case '$':
|
|
|
|
return GetVariable();
|
|
|
|
case '`':
|
|
|
|
return GetFullyQualifiedControl();
|
|
|
|
default:
|
|
|
|
if (isalpha(c, std::locale::classic()))
|
2019-04-04 22:35:49 +00:00
|
|
|
return GetBareword(c);
|
2019-03-02 16:10:26 +00:00
|
|
|
else if (isdigit(c, std::locale::classic()))
|
|
|
|
return GetRealLiteral(c);
|
|
|
|
else
|
|
|
|
return Token(TOK_INVALID);
|
2013-06-14 03:09:55 +00:00
|
|
|
}
|
2019-03-02 16:10:26 +00:00
|
|
|
}
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2019-03-02 16:10:26 +00:00
|
|
|
ParseStatus Lexer::Tokenize(std::vector<Token>& tokens)
|
|
|
|
{
|
|
|
|
while (true)
|
2013-06-14 03:09:55 +00:00
|
|
|
{
|
2019-03-02 16:10:26 +00:00
|
|
|
const std::size_t string_position = it - expr.begin();
|
|
|
|
Token tok = NextToken();
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2019-03-02 16:10:26 +00:00
|
|
|
tok.string_position = string_position;
|
|
|
|
tok.string_length = it - expr.begin();
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2019-10-12 16:41:02 +00:00
|
|
|
// Handle /* */ style comments.
|
|
|
|
if (tok.type == TOK_DIV && PeekToken().type == TOK_MUL)
|
|
|
|
{
|
|
|
|
const auto end_of_comment = expr.find("*/", it - expr.begin());
|
|
|
|
|
|
|
|
if (end_of_comment == std::string::npos)
|
|
|
|
return ParseStatus::SyntaxError;
|
|
|
|
|
|
|
|
tok.type = TOK_COMMENT;
|
|
|
|
tok.string_length = end_of_comment + 4;
|
|
|
|
|
|
|
|
it = expr.begin() + end_of_comment + 2;
|
|
|
|
}
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2019-03-02 16:10:26 +00:00
|
|
|
tokens.push_back(tok);
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2019-03-02 16:10:26 +00:00
|
|
|
if (tok.type == TOK_INVALID)
|
|
|
|
return ParseStatus::SyntaxError;
|
|
|
|
|
|
|
|
if (tok.type == TOK_EOF)
|
|
|
|
break;
|
2013-06-14 03:09:55 +00:00
|
|
|
}
|
2019-03-02 16:10:26 +00:00
|
|
|
return ParseStatus::Successful;
|
|
|
|
}
|
2013-06-14 03:09:55 +00:00
|
|
|
|
2017-06-08 00:30:07 +00:00
|
|
|
class ControlExpression : public Expression
|
2013-06-14 03:09:55 +00:00
|
|
|
{
|
|
|
|
public:
|
2023-12-12 19:03:39 +00:00
|
|
|
explicit ControlExpression(ControlQualifier qualifier) : m_qualifier(std::move(qualifier)) {}
|
2019-10-25 23:49:06 +00:00
|
|
|
|
2019-02-27 01:46:21 +00:00
|
|
|
ControlState GetValue() const override
|
|
|
|
{
|
2021-05-04 20:53:55 +00:00
|
|
|
if (s_hotkey_suppressions.IsSuppressed(m_input))
|
2019-10-25 23:49:06 +00:00
|
|
|
return 0;
|
2021-05-04 20:53:55 +00:00
|
|
|
return GetValueIgnoringSuppression();
|
2019-10-25 23:49:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ControlState GetValueIgnoringSuppression() const
|
|
|
|
{
|
2021-05-04 20:53:55 +00:00
|
|
|
if (!m_input)
|
2019-02-27 01:46:21 +00:00
|
|
|
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)
|
|
|
|
|
2021-05-04 20:53:55 +00:00
|
|
|
return std::max(0.0, m_input->GetState());
|
2019-02-27 01:46:21 +00:00
|
|
|
}
|
2017-06-07 22:56:49 +00:00
|
|
|
void SetValue(ControlState value) override
|
|
|
|
{
|
2021-05-04 20:53:55 +00:00
|
|
|
if (m_output)
|
|
|
|
m_output->SetState(value);
|
2017-06-07 22:56:49 +00:00
|
|
|
}
|
2021-05-04 20:53:55 +00:00
|
|
|
int CountNumControls() const override { return (m_input || m_output) ? 1 : 0; }
|
2018-12-30 22:06:29 +00:00
|
|
|
void UpdateReferences(ControlEnvironment& env) override
|
2017-06-08 01:48:17 +00:00
|
|
|
{
|
2021-05-04 20:53:55 +00:00
|
|
|
m_device = env.FindDevice(m_qualifier);
|
|
|
|
m_input = env.FindInput(m_qualifier);
|
|
|
|
m_output = env.FindOutput(m_qualifier);
|
2017-06-08 01:48:17 +00:00
|
|
|
}
|
2018-12-30 22:06:29 +00:00
|
|
|
|
2024-08-18 13:08:44 +00:00
|
|
|
Device::Input* GetInput() const { return m_input; }
|
2019-10-23 21:45:21 +00:00
|
|
|
|
2018-12-30 22:06:29 +00:00
|
|
|
private:
|
2021-05-04 20:53:55 +00:00
|
|
|
// Keep a shared_ptr to the device so the control pointer doesn't become invalid.
|
|
|
|
std::shared_ptr<Device> m_device;
|
|
|
|
ControlQualifier m_qualifier;
|
|
|
|
Device::Input* m_input = nullptr;
|
|
|
|
Device::Output* m_output = nullptr;
|
2013-06-14 03:09:55 +00:00
|
|
|
};
|
|
|
|
|
2019-11-02 14:55:45 +00:00
|
|
|
bool HotkeySuppressions::IsSuppressedIgnoringModifiers(Device::Input* input,
|
|
|
|
const Modifiers& ignore_modifiers) const
|
2019-10-25 23:49:06 +00:00
|
|
|
{
|
2019-11-02 14:55:45 +00:00
|
|
|
// Input is suppressed if it exists in the map with a modifier that we aren't ignoring.
|
|
|
|
auto it = m_suppressions.lower_bound({input, nullptr});
|
|
|
|
auto it_end = m_suppressions.lower_bound({input + 1, nullptr});
|
2019-10-25 23:49:06 +00:00
|
|
|
|
2020-01-26 19:58:20 +00:00
|
|
|
// We need to ignore L_Ctrl R_Ctrl when supplied Ctrl and vice-versa.
|
|
|
|
const auto is_same_modifier = [](Device::Input* i1, Device::Input* i2) {
|
2021-04-26 16:41:31 +00:00
|
|
|
return i1 && i2 && (i1 == i2 || i1->IsChild(i2) || i2->IsChild(i1));
|
2020-01-26 19:58:20 +00:00
|
|
|
};
|
|
|
|
|
2019-11-02 14:55:45 +00:00
|
|
|
return std::any_of(it, it_end, [&](auto& s) {
|
|
|
|
return std::none_of(begin(ignore_modifiers), end(ignore_modifiers),
|
2020-01-26 19:58:20 +00:00
|
|
|
[&](auto& m) { return is_same_modifier(m->GetInput(), s.first.second); });
|
2019-10-25 23:49:06 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-11-02 14:55:45 +00:00
|
|
|
HotkeySuppressions::Suppressor
|
|
|
|
HotkeySuppressions::MakeSuppressor(const Modifiers* modifiers,
|
|
|
|
const std::unique_ptr<ControlExpression>* final_input)
|
2019-10-25 23:49:06 +00:00
|
|
|
{
|
2019-11-02 14:55:45 +00:00
|
|
|
for (auto& modifier : *modifiers)
|
2021-04-26 16:41:31 +00:00
|
|
|
{
|
|
|
|
// Inputs might be null, don't add nullptr to the map
|
|
|
|
if ((*final_input)->GetInput() && modifier->GetInput())
|
|
|
|
{
|
|
|
|
++m_suppressions[{(*final_input)->GetInput(), modifier->GetInput()}];
|
|
|
|
}
|
|
|
|
}
|
2019-10-25 23:49:06 +00:00
|
|
|
|
2020-02-16 19:09:12 +00:00
|
|
|
return Suppressor(std::make_unique<std::function<void()>>([this, modifiers, final_input]() {
|
|
|
|
for (auto& modifier : *modifiers)
|
|
|
|
RemoveSuppression(modifier->GetInput(), (*final_input)->GetInput());
|
|
|
|
}).release(),
|
|
|
|
InvokingDeleter{});
|
2019-10-25 23:49:06 +00:00
|
|
|
}
|
|
|
|
|
2017-06-08 00:30:07 +00:00
|
|
|
class BinaryExpression : public Expression
|
2013-06-14 03:09:55 +00:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
TokenType op;
|
2017-06-08 00:30:07 +00:00
|
|
|
std::unique_ptr<Expression> lhs;
|
|
|
|
std::unique_ptr<Expression> rhs;
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2017-06-08 00:30:07 +00:00
|
|
|
BinaryExpression(TokenType op_, std::unique_ptr<Expression>&& lhs_,
|
|
|
|
std::unique_ptr<Expression>&& rhs_)
|
2017-06-07 22:29:00 +00:00
|
|
|
: op(op_), lhs(std::move(lhs_)), rhs(std::move(rhs_))
|
2013-06-14 03:09:55 +00:00
|
|
|
{
|
|
|
|
}
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2017-02-26 07:04:16 +00:00
|
|
|
ControlState GetValue() const override
|
2013-06-14 03:09:55 +00:00
|
|
|
{
|
|
|
|
switch (op)
|
|
|
|
{
|
|
|
|
case TOK_AND:
|
2018-12-30 22:29:48 +00:00
|
|
|
return std::min(lhs->GetValue(), rhs->GetValue());
|
2013-06-14 03:09:55 +00:00
|
|
|
case TOK_OR:
|
2018-12-30 22:29:48 +00:00
|
|
|
return std::max(lhs->GetValue(), rhs->GetValue());
|
2013-06-14 06:52:07 +00:00
|
|
|
case TOK_ADD:
|
2018-12-30 22:29:48 +00:00
|
|
|
return lhs->GetValue() + rhs->GetValue();
|
2018-12-30 23:32:32 +00:00
|
|
|
case TOK_SUB:
|
|
|
|
return lhs->GetValue() - rhs->GetValue();
|
2018-12-30 17:51:12 +00:00
|
|
|
case TOK_MUL:
|
2018-12-30 22:29:48 +00:00
|
|
|
return lhs->GetValue() * rhs->GetValue();
|
2018-12-30 17:51:12 +00:00
|
|
|
case TOK_DIV:
|
|
|
|
{
|
2018-12-30 22:29:48 +00:00
|
|
|
const ControlState result = lhs->GetValue() / rhs->GetValue();
|
2018-12-30 17:51:12 +00:00
|
|
|
return std::isinf(result) ? 0.0 : result;
|
|
|
|
}
|
2018-12-30 19:16:28 +00:00
|
|
|
case TOK_MOD:
|
|
|
|
{
|
2018-12-30 22:29:48 +00:00
|
|
|
const ControlState result = std::fmod(lhs->GetValue(), rhs->GetValue());
|
2018-12-30 19:16:28 +00:00
|
|
|
return std::isnan(result) ? 0.0 : result;
|
|
|
|
}
|
2018-12-30 22:06:29 +00:00
|
|
|
case TOK_ASSIGN:
|
|
|
|
{
|
2021-05-04 20:50:23 +00:00
|
|
|
// Use this carefully as it's extremely powerful and can end up in unforeseen situations
|
2018-12-30 22:29:48 +00:00
|
|
|
lhs->SetValue(rhs->GetValue());
|
|
|
|
return lhs->GetValue();
|
2018-12-30 22:06:29 +00:00
|
|
|
}
|
2018-12-30 22:11:42 +00:00
|
|
|
case TOK_LTHAN:
|
2018-12-30 22:29:48 +00:00
|
|
|
return lhs->GetValue() < rhs->GetValue();
|
2018-12-30 22:11:42 +00:00
|
|
|
case TOK_GTHAN:
|
2018-12-30 22:29:48 +00:00
|
|
|
return lhs->GetValue() > rhs->GetValue();
|
2018-12-30 23:32:32 +00:00
|
|
|
case TOK_COMMA:
|
|
|
|
{
|
|
|
|
// Eval and discard lhs:
|
|
|
|
lhs->GetValue();
|
|
|
|
return rhs->GetValue();
|
|
|
|
}
|
2019-10-20 14:51:19 +00:00
|
|
|
case TOK_XOR:
|
|
|
|
{
|
|
|
|
const auto lval = lhs->GetValue();
|
|
|
|
const auto rval = rhs->GetValue();
|
|
|
|
return std::max(std::min(1 - lval, rval), std::min(lval, 1 - rval));
|
|
|
|
}
|
2013-06-14 03:09:55 +00:00
|
|
|
default:
|
2021-04-01 21:30:01 +00:00
|
|
|
ASSERT(false);
|
2013-06-14 03:09:55 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2016-06-24 08:43:46 +00:00
|
|
|
}
|
|
|
|
|
2015-07-30 10:47:02 +00:00
|
|
|
void SetValue(ControlState value) override
|
2013-06-14 03:09:55 +00: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 08:43:46 +00:00
|
|
|
|
2017-02-26 07:04:16 +00:00
|
|
|
int CountNumControls() const override
|
|
|
|
{
|
|
|
|
return lhs->CountNumControls() + rhs->CountNumControls();
|
|
|
|
}
|
|
|
|
|
2018-12-30 22:06:29 +00:00
|
|
|
void UpdateReferences(ControlEnvironment& env) override
|
2017-06-08 01:48:17 +00:00
|
|
|
{
|
2018-12-30 22:06:29 +00:00
|
|
|
lhs->UpdateReferences(env);
|
|
|
|
rhs->UpdateReferences(env);
|
2017-06-08 01:48:17 +00:00
|
|
|
}
|
2013-06-14 03:09:55 +00:00
|
|
|
};
|
|
|
|
|
2018-12-30 16:37:23 +00:00
|
|
|
class LiteralExpression : public Expression
|
|
|
|
{
|
|
|
|
public:
|
2019-03-02 16:10:26 +00:00
|
|
|
void SetValue(ControlState) override
|
2018-12-30 16:37:23 +00:00
|
|
|
{
|
|
|
|
// Do nothing.
|
|
|
|
}
|
|
|
|
|
|
|
|
int CountNumControls() const override { return 1; }
|
|
|
|
|
2018-12-30 22:06:29 +00:00
|
|
|
void UpdateReferences(ControlEnvironment&) override
|
2018-12-30 16:37:23 +00:00
|
|
|
{
|
|
|
|
// Nothing needed.
|
|
|
|
}
|
|
|
|
|
2018-12-30 19:16:28 +00:00
|
|
|
protected:
|
|
|
|
virtual std::string GetName() const = 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
class LiteralReal : public LiteralExpression
|
|
|
|
{
|
|
|
|
public:
|
2023-12-12 19:00:17 +00:00
|
|
|
explicit LiteralReal(ControlState value) : m_value(value) {}
|
2018-12-30 19:16:28 +00:00
|
|
|
|
|
|
|
ControlState GetValue() const override { return m_value; }
|
|
|
|
|
|
|
|
std::string GetName() const override { return ValueToString(m_value); }
|
2018-12-30 16:37:23 +00:00
|
|
|
|
|
|
|
private:
|
2018-12-30 19:16:28 +00:00
|
|
|
const ControlState m_value{};
|
2018-12-30 16:37:23 +00:00
|
|
|
};
|
|
|
|
|
2023-12-12 19:03:39 +00:00
|
|
|
static ParseResult MakeLiteralExpression(const Token& token)
|
2018-12-30 19:16:28 +00:00
|
|
|
{
|
2019-01-05 21:31:05 +00:00
|
|
|
ControlState val{};
|
2019-03-02 20:47:26 +00:00
|
|
|
if (TryParse(token.data, &val))
|
|
|
|
return ParseResult::MakeSuccessfulResult(std::make_unique<LiteralReal>(val));
|
|
|
|
else
|
2024-07-28 10:09:15 +00:00
|
|
|
return ParseResult::MakeErrorResult(token, Common::GetStringT("Invalid literal."));
|
2018-12-30 19:16:28 +00:00
|
|
|
}
|
|
|
|
|
2018-12-30 22:06:29 +00:00
|
|
|
class VariableExpression : public Expression
|
|
|
|
{
|
|
|
|
public:
|
2023-12-12 19:03:39 +00:00
|
|
|
explicit VariableExpression(std::string name) : m_name(std::move(name)) {}
|
2018-12-30 22:06:29 +00:00
|
|
|
|
2021-03-11 22:01:18 +00:00
|
|
|
ControlState GetValue() const override { return m_variable_ptr ? *m_variable_ptr : 0; }
|
2018-12-30 22:06:29 +00:00
|
|
|
|
2021-03-11 22:01:18 +00:00
|
|
|
void SetValue(ControlState value) override
|
|
|
|
{
|
|
|
|
if (m_variable_ptr)
|
|
|
|
*m_variable_ptr = value;
|
|
|
|
}
|
2018-12-30 22:06:29 +00:00
|
|
|
|
|
|
|
int CountNumControls() const override { return 1; }
|
|
|
|
|
|
|
|
void UpdateReferences(ControlEnvironment& env) override
|
|
|
|
{
|
2021-03-11 22:01:18 +00:00
|
|
|
m_variable_ptr = env.GetVariablePtr(m_name);
|
2018-12-30 22:06:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
const std::string m_name;
|
2021-03-11 22:01:18 +00:00
|
|
|
std::shared_ptr<ControlState> m_variable_ptr;
|
2018-12-30 22:06:29 +00:00
|
|
|
};
|
|
|
|
|
2019-10-23 00:12:55 +00:00
|
|
|
class HotkeyExpression : public Expression
|
|
|
|
{
|
|
|
|
public:
|
2023-12-12 19:00:17 +00:00
|
|
|
explicit HotkeyExpression(std::vector<std::unique_ptr<ControlExpression>> inputs)
|
2019-10-25 23:49:06 +00:00
|
|
|
: m_modifiers(std::move(inputs))
|
2019-10-23 00:12:55 +00:00
|
|
|
{
|
2019-10-25 23:49:06 +00:00
|
|
|
m_final_input = std::move(m_modifiers.back());
|
|
|
|
m_modifiers.pop_back();
|
2019-10-23 00:12:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ControlState GetValue() const override
|
|
|
|
{
|
2021-04-26 16:41:31 +00:00
|
|
|
// True if we have no modifiers
|
2019-10-25 23:49:06 +00:00
|
|
|
const bool modifiers_pressed = std::all_of(m_modifiers.begin(), m_modifiers.end(),
|
2019-10-23 00:12:55 +00:00
|
|
|
[](const std::unique_ptr<ControlExpression>& input) {
|
2019-11-02 14:55:45 +00:00
|
|
|
return input->GetValue() > CONDITION_THRESHOLD;
|
2019-10-23 00:12:55 +00:00
|
|
|
});
|
|
|
|
|
2019-10-25 23:49:06 +00:00
|
|
|
const auto final_input_state = m_final_input->GetValueIgnoringSuppression();
|
|
|
|
|
2019-10-23 00:12:55 +00:00
|
|
|
if (modifiers_pressed)
|
|
|
|
{
|
2019-10-25 23:49:06 +00:00
|
|
|
// Ignore suppression of our own modifiers. This also allows superset modifiers to function.
|
2019-11-02 14:55:45 +00:00
|
|
|
const bool is_suppressed = s_hotkey_suppressions.IsSuppressedIgnoringModifiers(
|
|
|
|
m_final_input->GetInput(), m_modifiers);
|
2019-10-23 00:12:55 +00:00
|
|
|
|
2021-04-26 16:45:13 +00:00
|
|
|
if (final_input_state <= CONDITION_THRESHOLD)
|
2020-08-31 00:25:35 +00:00
|
|
|
m_is_blocked = false;
|
|
|
|
|
2019-10-25 23:49:06 +00:00
|
|
|
// If some other hotkey suppressed us, require a release of final input to be ready again.
|
|
|
|
if (is_suppressed)
|
2020-08-31 00:25:35 +00:00
|
|
|
m_is_blocked = true;
|
|
|
|
|
|
|
|
if (m_is_blocked)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
EnableSuppression();
|
2019-10-23 00:12:55 +00:00
|
|
|
|
2019-10-25 23:49:06 +00:00
|
|
|
// Our modifiers are active. Pass through the final input.
|
2020-08-31 00:25:35 +00:00
|
|
|
return final_input_state;
|
2019-10-23 00:12:55 +00:00
|
|
|
}
|
|
|
|
else
|
2019-10-23 21:45:21 +00:00
|
|
|
{
|
|
|
|
m_suppressor = {};
|
2020-08-31 00:25:35 +00:00
|
|
|
m_is_blocked = final_input_state > CONDITION_THRESHOLD;
|
2019-10-23 21:45:21 +00:00
|
|
|
}
|
2019-10-23 00:12:55 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SetValue(ControlState) override {}
|
|
|
|
|
|
|
|
int CountNumControls() const override
|
|
|
|
{
|
|
|
|
int result = 0;
|
2019-10-25 23:49:06 +00:00
|
|
|
for (auto& input : m_modifiers)
|
2019-10-23 00:12:55 +00:00
|
|
|
result += input->CountNumControls();
|
2019-10-25 23:49:06 +00:00
|
|
|
return result + m_final_input->CountNumControls();
|
2019-10-23 00:12:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void UpdateReferences(ControlEnvironment& env) override
|
|
|
|
{
|
2019-10-25 23:49:06 +00:00
|
|
|
for (auto& input : m_modifiers)
|
2019-10-23 00:12:55 +00:00
|
|
|
input->UpdateReferences(env);
|
2019-10-25 23:49:06 +00:00
|
|
|
|
|
|
|
m_final_input->UpdateReferences(env);
|
|
|
|
|
|
|
|
// We must update our suppression with valid pointers.
|
|
|
|
if (m_suppressor)
|
2021-04-26 16:41:31 +00:00
|
|
|
EnableSuppression(true);
|
2019-10-23 00:12:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2021-04-26 16:41:31 +00:00
|
|
|
void EnableSuppression(bool force = false) const
|
2019-10-25 23:49:06 +00:00
|
|
|
{
|
2021-04-26 16:41:31 +00:00
|
|
|
if (!m_suppressor || force)
|
2020-08-31 00:25:35 +00:00
|
|
|
m_suppressor = s_hotkey_suppressions.MakeSuppressor(&m_modifiers, &m_final_input);
|
2019-10-25 23:49:06 +00:00
|
|
|
}
|
|
|
|
|
2019-11-02 14:55:45 +00:00
|
|
|
HotkeySuppressions::Modifiers m_modifiers;
|
2019-10-25 23:49:06 +00:00
|
|
|
std::unique_ptr<ControlExpression> m_final_input;
|
2019-10-23 21:45:21 +00:00
|
|
|
mutable HotkeySuppressions::Suppressor m_suppressor;
|
2020-08-31 00:25:35 +00:00
|
|
|
mutable bool m_is_blocked = false;
|
2019-10-23 00:12:55 +00:00
|
|
|
};
|
|
|
|
|
2017-06-08 01:29:02 +00: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.
|
2021-05-04 20:50:23 +00:00
|
|
|
// Note that if you have a keyboard device as default device and the expression is a single digit
|
|
|
|
// number, this will usually resolve in a numerical key instead of a numerical value.
|
|
|
|
// Though if this expression belongs to NumericSetting, it will likely be simplifed back to a value.
|
2017-06-08 01:29:02 +00:00
|
|
|
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 17:17:05 +00:00
|
|
|
void SetValue(ControlState value) override { GetActiveChild()->SetValue(value); }
|
2017-06-08 01:29:02 +00:00
|
|
|
|
|
|
|
int CountNumControls() const override { return GetActiveChild()->CountNumControls(); }
|
2018-12-30 22:06:29 +00:00
|
|
|
void UpdateReferences(ControlEnvironment& env) override
|
2017-06-08 01:48:17 +00:00
|
|
|
{
|
2018-12-30 22:06:29 +00:00
|
|
|
m_lhs->UpdateReferences(env);
|
|
|
|
m_rhs->UpdateReferences(env);
|
2017-06-08 01:48:17 +00:00
|
|
|
}
|
|
|
|
|
2017-06-08 01:29:02 +00: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;
|
|
|
|
};
|
|
|
|
|
2023-12-12 18:50:44 +00:00
|
|
|
std::shared_ptr<Device> ControlEnvironment::FindDevice(const ControlQualifier& qualifier) const
|
2013-06-14 03:09:55 +00:00
|
|
|
{
|
|
|
|
if (qualifier.has_device)
|
|
|
|
return container.FindDevice(qualifier.device_qualifier);
|
|
|
|
else
|
|
|
|
return container.FindDevice(default_device);
|
|
|
|
}
|
|
|
|
|
2023-12-12 18:50:44 +00:00
|
|
|
Device::Input* ControlEnvironment::FindInput(const ControlQualifier& qualifier) const
|
2013-06-14 03:09:55 +00:00
|
|
|
{
|
2016-06-25 19:46:39 +00:00
|
|
|
const std::shared_ptr<Device> device = FindDevice(qualifier);
|
2013-06-26 20:54:48 +00:00
|
|
|
if (!device)
|
2014-03-09 20:14:26 +00:00
|
|
|
return nullptr;
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2018-12-30 22:06:29 +00:00
|
|
|
return device->FindInput(qualifier.control_name);
|
|
|
|
}
|
|
|
|
|
2023-12-12 18:50:44 +00:00
|
|
|
Device::Output* ControlEnvironment::FindOutput(const ControlQualifier& qualifier) const
|
2018-12-30 22:06:29 +00:00
|
|
|
{
|
|
|
|
const std::shared_ptr<Device> device = FindDevice(qualifier);
|
|
|
|
if (!device)
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
return device->FindOutput(qualifier.control_name);
|
|
|
|
}
|
|
|
|
|
2021-03-11 22:01:18 +00:00
|
|
|
std::shared_ptr<ControlState> ControlEnvironment::GetVariablePtr(const std::string& name)
|
2018-12-30 22:06:29 +00:00
|
|
|
{
|
2021-03-11 22:01:18 +00:00
|
|
|
// Do not accept an empty string as key, even if the expression parser already prevents this case.
|
|
|
|
if (name.empty())
|
|
|
|
return nullptr;
|
|
|
|
std::shared_ptr<ControlState>& variable = m_variables[name];
|
|
|
|
// If new, make a shared ptr
|
|
|
|
if (!variable)
|
|
|
|
{
|
|
|
|
variable = std::make_shared<ControlState>();
|
|
|
|
}
|
|
|
|
return variable;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ControlEnvironment::CleanUnusedVariables()
|
|
|
|
{
|
|
|
|
for (auto it = m_variables.begin(); it != m_variables.end();)
|
|
|
|
{
|
|
|
|
// Don't count ourselves as reference
|
|
|
|
if (it->second.use_count() <= 1)
|
|
|
|
m_variables.erase(it++);
|
|
|
|
else
|
|
|
|
++it;
|
|
|
|
}
|
2013-06-14 03:09:55 +00:00
|
|
|
}
|
|
|
|
|
2019-03-02 20:47:26 +00:00
|
|
|
ParseResult ParseResult::MakeEmptyResult()
|
2017-06-07 22:08:55 +00:00
|
|
|
{
|
2019-03-02 20:47:26 +00:00
|
|
|
ParseResult result;
|
|
|
|
result.status = ParseStatus::EmptyExpression;
|
|
|
|
return result;
|
|
|
|
}
|
2017-06-07 22:29:00 +00:00
|
|
|
|
2019-03-02 20:47:26 +00:00
|
|
|
ParseResult ParseResult::MakeSuccessfulResult(std::unique_ptr<Expression>&& expr)
|
|
|
|
{
|
|
|
|
ParseResult result;
|
|
|
|
result.status = ParseStatus::Successful;
|
|
|
|
result.expr = std::move(expr);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
ParseResult ParseResult::MakeErrorResult(Token token, std::string description)
|
|
|
|
{
|
|
|
|
ParseResult result;
|
|
|
|
result.status = ParseStatus::SyntaxError;
|
|
|
|
result.token = std::move(token);
|
|
|
|
result.description = std::move(description);
|
|
|
|
return result;
|
|
|
|
}
|
2017-06-07 22:08:55 +00:00
|
|
|
|
2013-06-14 03:09:55 +00:00
|
|
|
class Parser
|
|
|
|
{
|
|
|
|
public:
|
2019-03-02 20:47:26 +00:00
|
|
|
explicit Parser(const std::vector<Token>& tokens_) : tokens(tokens_) { m_it = tokens.begin(); }
|
2019-01-09 00:36:58 +00:00
|
|
|
ParseResult Parse()
|
|
|
|
{
|
|
|
|
ParseResult result = ParseToplevel();
|
|
|
|
|
2019-03-02 20:47:26 +00:00
|
|
|
if (ParseStatus::Successful != result.status)
|
|
|
|
return result;
|
|
|
|
|
2019-01-09 00:36:58 +00:00
|
|
|
if (Peek().type == TOK_EOF)
|
|
|
|
return result;
|
|
|
|
|
2024-07-28 10:09:15 +00:00
|
|
|
return ParseResult::MakeErrorResult(Peek(), Common::GetStringT("Expected end of expression."));
|
2019-01-09 00:36:58 +00:00
|
|
|
}
|
2018-04-12 12:18:04 +00:00
|
|
|
|
2013-06-14 03:09:55 +00:00
|
|
|
private:
|
2019-03-02 20:47:26 +00:00
|
|
|
const std::vector<Token>& tokens;
|
|
|
|
std::vector<Token>::const_iterator m_it;
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2019-01-20 23:44:01 +00:00
|
|
|
Token Chew()
|
|
|
|
{
|
|
|
|
const Token tok = Peek();
|
|
|
|
if (TOK_EOF != tok.type)
|
|
|
|
++m_it;
|
|
|
|
return tok;
|
|
|
|
}
|
|
|
|
|
2013-06-14 03:09:55 +00:00
|
|
|
Token Peek() { return *m_it; }
|
2019-01-09 00:26:36 +00:00
|
|
|
|
2013-06-14 03:09:55 +00:00
|
|
|
bool Expects(TokenType type)
|
|
|
|
{
|
|
|
|
Token tok = Chew();
|
|
|
|
return tok.type == type;
|
|
|
|
}
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2019-10-12 00:38:18 +00:00
|
|
|
ParseResult ParseFunctionArguments(const std::string_view& func_name,
|
|
|
|
std::unique_ptr<FunctionExpression>&& func,
|
2019-04-04 22:35:49 +00:00
|
|
|
const Token& func_tok)
|
2019-01-09 00:26:36 +00:00
|
|
|
{
|
2019-01-20 23:44:01 +00:00
|
|
|
std::vector<std::unique_ptr<Expression>> args;
|
|
|
|
|
|
|
|
if (TOK_LPAREN != Peek().type)
|
|
|
|
{
|
|
|
|
// Single argument with no parens (useful for unary ! function)
|
2019-04-04 22:35:49 +00:00
|
|
|
const auto tok = Chew();
|
|
|
|
auto arg = ParseAtom(tok);
|
2019-01-20 23:44:01 +00:00
|
|
|
if (ParseStatus::Successful != arg.status)
|
2019-04-04 22:35:49 +00:00
|
|
|
return arg;
|
2019-01-20 23:44:01 +00:00
|
|
|
|
|
|
|
args.emplace_back(std::move(arg.expr));
|
|
|
|
}
|
2019-04-04 22:35:49 +00:00
|
|
|
else
|
2019-01-20 23:44:01 +00:00
|
|
|
{
|
2019-04-04 22:35:49 +00:00
|
|
|
// Chew the L-Paren
|
2019-01-20 23:44:01 +00:00
|
|
|
Chew();
|
2019-04-04 22:35:49 +00:00
|
|
|
|
|
|
|
// Check for empty argument list:
|
|
|
|
if (TOK_RPAREN == Peek().type)
|
|
|
|
{
|
|
|
|
Chew();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
while (true)
|
|
|
|
{
|
|
|
|
// Read one argument.
|
|
|
|
// Grab an expression, but stop at comma.
|
|
|
|
auto arg = ParseBinary(BinaryOperatorPrecedence(TOK_COMMA));
|
|
|
|
if (ParseStatus::Successful != arg.status)
|
|
|
|
return arg;
|
|
|
|
|
|
|
|
args.emplace_back(std::move(arg.expr));
|
|
|
|
|
|
|
|
// Right paren is the end of our arguments.
|
|
|
|
const Token tok = Chew();
|
|
|
|
if (TOK_RPAREN == tok.type)
|
|
|
|
break;
|
|
|
|
|
|
|
|
// Comma before the next argument.
|
|
|
|
if (TOK_COMMA != tok.type)
|
2024-07-28 10:09:15 +00:00
|
|
|
return ParseResult::MakeErrorResult(tok, Common::GetStringT("Expected comma."));
|
2019-04-04 22:35:49 +00:00
|
|
|
};
|
|
|
|
}
|
2019-01-20 23:44:01 +00:00
|
|
|
}
|
2019-01-09 00:26:36 +00:00
|
|
|
|
2019-10-12 00:38:18 +00:00
|
|
|
const auto argument_validation = func->SetArguments(std::move(args));
|
|
|
|
|
|
|
|
if (std::holds_alternative<FunctionExpression::ExpectedArguments>(argument_validation))
|
2019-01-09 00:26:36 +00:00
|
|
|
{
|
2019-10-12 00:38:18 +00:00
|
|
|
const auto text = std::string(func_name) + '(' +
|
|
|
|
std::get<FunctionExpression::ExpectedArguments>(argument_validation).text +
|
|
|
|
')';
|
|
|
|
|
2024-07-28 10:09:15 +00:00
|
|
|
return ParseResult::MakeErrorResult(func_tok,
|
|
|
|
Common::FmtFormatT("Expected arguments: {0}", text));
|
2019-01-09 00:26:36 +00:00
|
|
|
}
|
2019-04-04 22:35:49 +00:00
|
|
|
|
|
|
|
return ParseResult::MakeSuccessfulResult(std::move(func));
|
2019-01-09 00:26:36 +00:00
|
|
|
}
|
|
|
|
|
2019-01-09 00:36:58 +00:00
|
|
|
ParseResult ParseAtom(const Token& tok)
|
2013-06-14 03:09:55 +00:00
|
|
|
{
|
|
|
|
switch (tok.type)
|
|
|
|
{
|
2019-04-04 22:35:49 +00:00
|
|
|
case TOK_BAREWORD:
|
2019-01-05 21:31:05 +00:00
|
|
|
{
|
2019-01-06 15:08:35 +00:00
|
|
|
auto func = MakeFunctionExpression(tok.data);
|
2019-03-02 20:47:26 +00:00
|
|
|
|
|
|
|
if (!func)
|
|
|
|
{
|
2019-04-04 22:35:49 +00:00
|
|
|
// Invalid function, interpret this as a bareword control.
|
|
|
|
Token control_tok(tok);
|
|
|
|
control_tok.type = TOK_CONTROL;
|
|
|
|
return ParseAtom(control_tok);
|
2019-03-02 20:47:26 +00:00
|
|
|
}
|
2019-01-06 15:08:35 +00:00
|
|
|
|
2019-10-12 00:38:18 +00:00
|
|
|
return ParseFunctionArguments(tok.data, std::move(func), tok);
|
2019-01-05 21:31:05 +00:00
|
|
|
}
|
2013-06-14 03:09:55 +00:00
|
|
|
case TOK_CONTROL:
|
2018-12-30 16:37:23 +00:00
|
|
|
{
|
|
|
|
ControlQualifier cq;
|
|
|
|
cq.FromString(tok.data);
|
2019-03-02 20:47:26 +00:00
|
|
|
return ParseResult::MakeSuccessfulResult(std::make_unique<ControlExpression>(cq));
|
2018-12-30 16:37:23 +00:00
|
|
|
}
|
2019-04-04 22:35:49 +00:00
|
|
|
case TOK_NOT:
|
|
|
|
{
|
2019-10-12 00:38:18 +00:00
|
|
|
return ParseFunctionArguments("not", MakeFunctionExpression("not"), tok);
|
2019-04-04 22:35:49 +00:00
|
|
|
}
|
2018-12-30 16:37:23 +00:00
|
|
|
case TOK_LITERAL:
|
|
|
|
{
|
2019-03-02 20:47:26 +00:00
|
|
|
return MakeLiteralExpression(tok);
|
2018-12-30 16:37:23 +00:00
|
|
|
}
|
2018-12-30 22:06:29 +00:00
|
|
|
case TOK_VARIABLE:
|
|
|
|
{
|
2021-03-11 22:01:18 +00:00
|
|
|
if (tok.data.empty())
|
2024-07-28 10:09:15 +00:00
|
|
|
return ParseResult::MakeErrorResult(tok, Common::GetStringT("Expected variable name."));
|
2021-03-11 22:01:18 +00:00
|
|
|
else
|
|
|
|
return ParseResult::MakeSuccessfulResult(std::make_unique<VariableExpression>(tok.data));
|
2018-12-30 22:06:29 +00:00
|
|
|
}
|
2013-06-14 03:09:55 +00:00
|
|
|
case TOK_LPAREN:
|
2019-01-05 21:31:05 +00:00
|
|
|
{
|
2019-01-09 00:36:58 +00:00
|
|
|
return ParseParens();
|
2019-01-05 21:31:05 +00:00
|
|
|
}
|
2019-10-23 00:12:55 +00:00
|
|
|
case TOK_HOTKEY:
|
|
|
|
{
|
|
|
|
return ParseHotkeys();
|
|
|
|
}
|
2019-01-06 16:03:21 +00:00
|
|
|
case TOK_SUB:
|
|
|
|
{
|
|
|
|
// An atom was expected but we got a subtraction symbol.
|
|
|
|
// Interpret it as a unary minus function.
|
2019-10-12 00:38:18 +00:00
|
|
|
return ParseFunctionArguments("minus", MakeFunctionExpression("minus"), tok);
|
2019-01-06 16:03:21 +00:00
|
|
|
}
|
2024-03-21 02:59:08 +00:00
|
|
|
case TOK_ADD:
|
|
|
|
{
|
|
|
|
// An atom was expected but we got an addition symbol.
|
|
|
|
// Interpret it as a unary plus.
|
|
|
|
return ParseFunctionArguments("plus", MakeFunctionExpression("plus"), tok);
|
|
|
|
}
|
2013-06-14 03:09:55 +00:00
|
|
|
default:
|
2019-04-04 22:35:49 +00:00
|
|
|
{
|
2024-07-28 10:09:15 +00:00
|
|
|
return ParseResult::MakeErrorResult(tok, Common::GetStringT("Expected start of expression."));
|
2016-06-24 08:43:46 +00:00
|
|
|
}
|
2019-04-04 22:35:49 +00:00
|
|
|
}
|
2013-06-14 03:09:55 +00:00
|
|
|
}
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2018-12-31 01:50:20 +00:00
|
|
|
static int BinaryOperatorPrecedence(TokenType type)
|
2013-06-14 03:09:55 +00:00
|
|
|
{
|
2018-12-31 01:50:20 +00:00
|
|
|
switch (type)
|
|
|
|
{
|
|
|
|
case TOK_MUL:
|
|
|
|
case TOK_DIV:
|
|
|
|
case TOK_MOD:
|
|
|
|
return 1;
|
|
|
|
case TOK_ADD:
|
|
|
|
case TOK_SUB:
|
|
|
|
return 2;
|
|
|
|
case TOK_GTHAN:
|
|
|
|
case TOK_LTHAN:
|
|
|
|
return 3;
|
|
|
|
case TOK_AND:
|
|
|
|
return 4;
|
2019-10-20 14:51:19 +00:00
|
|
|
case TOK_XOR:
|
2018-12-31 01:50:20 +00:00
|
|
|
return 5;
|
2019-10-20 14:51:19 +00:00
|
|
|
case TOK_OR:
|
2018-12-31 01:50:20 +00:00
|
|
|
return 6;
|
2019-10-20 14:51:19 +00:00
|
|
|
case TOK_ASSIGN:
|
2018-12-31 01:50:20 +00:00
|
|
|
return 7;
|
2019-10-20 14:51:19 +00:00
|
|
|
case TOK_COMMA:
|
|
|
|
return 8;
|
2018-12-31 01:50:20 +00:00
|
|
|
default:
|
2021-04-01 21:30:01 +00:00
|
|
|
ASSERT(false);
|
2018-12-31 01:50:20 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-09 00:36:58 +00:00
|
|
|
ParseResult ParseBinary(int precedence = 999)
|
2018-12-31 01:50:20 +00:00
|
|
|
{
|
2019-01-09 00:36:58 +00:00
|
|
|
ParseResult lhs = ParseAtom(Chew());
|
2018-12-31 01:50:20 +00:00
|
|
|
|
|
|
|
if (lhs.status == ParseStatus::SyntaxError)
|
|
|
|
return lhs;
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2018-12-31 01:50:20 +00:00
|
|
|
std::unique_ptr<Expression> expr = std::move(lhs.expr);
|
|
|
|
|
|
|
|
// TODO: handle LTR/RTL associativity?
|
2019-03-02 16:10:26 +00:00
|
|
|
while (Peek().IsBinaryOperator() && BinaryOperatorPrecedence(Peek().type) < precedence)
|
2013-06-14 03:09:55 +00:00
|
|
|
{
|
2018-12-31 01:50:20 +00:00
|
|
|
const Token tok = Chew();
|
2019-01-09 00:36:58 +00:00
|
|
|
ParseResult rhs = ParseBinary(BinaryOperatorPrecedence(tok.type));
|
2018-12-31 01:50:20 +00:00
|
|
|
if (rhs.status == ParseStatus::SyntaxError)
|
2013-06-14 03:09:55 +00:00
|
|
|
{
|
2018-12-31 01:50:20 +00:00
|
|
|
return rhs;
|
2013-06-14 03:09:55 +00:00
|
|
|
}
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2018-12-31 01:50:20 +00:00
|
|
|
expr = std::make_unique<BinaryExpression>(tok.type, std::move(expr), std::move(rhs.expr));
|
2013-06-14 03:09:55 +00:00
|
|
|
}
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2019-03-02 20:47:26 +00:00
|
|
|
return ParseResult::MakeSuccessfulResult(std::move(expr));
|
2013-06-14 03:09:55 +00:00
|
|
|
}
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2019-01-09 00:36:58 +00:00
|
|
|
ParseResult ParseParens()
|
2013-06-14 03:09:55 +00:00
|
|
|
{
|
|
|
|
// lparen already chewed
|
2019-01-09 00:36:58 +00:00
|
|
|
ParseResult result = ParseToplevel();
|
2017-06-07 22:08:55 +00:00
|
|
|
if (result.status != ParseStatus::Successful)
|
|
|
|
return result;
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2019-03-02 20:47:26 +00:00
|
|
|
const auto rparen = Chew();
|
|
|
|
if (rparen.type != TOK_RPAREN)
|
2013-06-14 03:09:55 +00:00
|
|
|
{
|
2024-07-28 10:09:15 +00:00
|
|
|
return ParseResult::MakeErrorResult(rparen, Common::GetStringT("Expected closing paren."));
|
2013-06-14 03:09:55 +00:00
|
|
|
}
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2017-06-07 22:08:55 +00:00
|
|
|
return result;
|
2013-06-14 03:09:55 +00:00
|
|
|
}
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2019-10-23 00:12:55 +00:00
|
|
|
ParseResult ParseHotkeys()
|
|
|
|
{
|
|
|
|
Token tok = Chew();
|
|
|
|
if (tok.type != TOK_LPAREN)
|
2024-07-28 10:09:15 +00:00
|
|
|
return ParseResult::MakeErrorResult(tok, Common::GetStringT("Expected opening paren."));
|
2019-10-23 00:12:55 +00:00
|
|
|
|
|
|
|
std::vector<std::unique_ptr<ControlExpression>> inputs;
|
|
|
|
|
|
|
|
while (true)
|
|
|
|
{
|
|
|
|
tok = Chew();
|
|
|
|
|
|
|
|
if (tok.type != TOK_CONTROL && tok.type != TOK_BAREWORD)
|
2024-07-28 10:09:15 +00:00
|
|
|
return ParseResult::MakeErrorResult(tok, Common::GetStringT("Expected name of input."));
|
2019-10-23 00:12:55 +00:00
|
|
|
|
|
|
|
ControlQualifier cq;
|
|
|
|
cq.FromString(tok.data);
|
|
|
|
inputs.emplace_back(std::make_unique<ControlExpression>(std::move(cq)));
|
|
|
|
|
|
|
|
tok = Chew();
|
|
|
|
|
|
|
|
if (tok.type == TOK_ADD)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (tok.type == TOK_RPAREN)
|
|
|
|
break;
|
|
|
|
|
2024-07-28 10:09:15 +00:00
|
|
|
return ParseResult::MakeErrorResult(tok, Common::GetStringT("Expected + or closing paren."));
|
2019-10-23 00:12:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return ParseResult::MakeSuccessfulResult(std::make_unique<HotkeyExpression>(std::move(inputs)));
|
|
|
|
}
|
|
|
|
|
2019-01-09 00:36:58 +00:00
|
|
|
ParseResult ParseToplevel() { return ParseBinary(); }
|
2018-12-31 01:50:20 +00:00
|
|
|
}; // namespace ExpressionParser
|
2013-06-14 03:09:55 +00:00
|
|
|
|
2019-03-02 20:47:26 +00:00
|
|
|
ParseResult ParseTokens(const std::vector<Token>& tokens)
|
|
|
|
{
|
|
|
|
return Parser(tokens).Parse();
|
|
|
|
}
|
|
|
|
|
2017-06-08 01:48:17 +00:00
|
|
|
static ParseResult ParseComplexExpression(const std::string& str)
|
2013-06-14 03:09:55 +00:00
|
|
|
{
|
|
|
|
Lexer l(str);
|
|
|
|
std::vector<Token> tokens;
|
2019-03-02 20:47:26 +00:00
|
|
|
const ParseStatus tokenize_status = l.Tokenize(tokens);
|
2017-06-07 21:53:41 +00:00
|
|
|
if (tokenize_status != ParseStatus::Successful)
|
2024-07-28 10:09:15 +00:00
|
|
|
return ParseResult::MakeErrorResult(Token(TOK_INVALID),
|
|
|
|
Common::GetStringT("Tokenizing failed."));
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2019-10-12 16:41:02 +00:00
|
|
|
RemoveInertTokens(&tokens);
|
2019-03-02 20:47:26 +00:00
|
|
|
return ParseTokens(tokens);
|
2013-06-14 03:09:55 +00:00
|
|
|
}
|
|
|
|
|
2019-10-12 16:41:02 +00:00
|
|
|
void RemoveInertTokens(std::vector<Token>* tokens)
|
|
|
|
{
|
2023-12-12 18:31:58 +00:00
|
|
|
std::erase_if(*tokens, [](const Token& tok) {
|
|
|
|
return tok.type == TOK_COMMENT || tok.type == TOK_WHITESPACE;
|
|
|
|
});
|
2019-10-12 16:41:02 +00:00
|
|
|
}
|
|
|
|
|
2017-06-08 01:48:17 +00:00
|
|
|
static std::unique_ptr<Expression> ParseBarewordExpression(const std::string& str)
|
2013-06-27 00:19:23 +00:00
|
|
|
{
|
|
|
|
ControlQualifier qualifier;
|
|
|
|
qualifier.control_name = str;
|
|
|
|
qualifier.has_device = false;
|
|
|
|
|
2021-05-04 20:50:23 +00:00
|
|
|
// This control expression will only work (find the specified control) with the default device.
|
2017-06-08 01:48:17 +00:00
|
|
|
return std::make_unique<ControlExpression>(qualifier);
|
2017-06-08 01:29:02 +00:00
|
|
|
}
|
|
|
|
|
2019-03-02 20:47:26 +00:00
|
|
|
ParseResult ParseExpression(const std::string& str)
|
2017-06-08 01:29:02 +00:00
|
|
|
{
|
2022-07-19 22:13:26 +00:00
|
|
|
if (StripWhitespace(str).empty())
|
2019-03-02 20:47:26 +00:00
|
|
|
return ParseResult::MakeEmptyResult();
|
2017-06-08 01:29:02 +00:00
|
|
|
|
2017-06-08 01:48:17 +00:00
|
|
|
auto bareword_expr = ParseBarewordExpression(str);
|
|
|
|
ParseResult complex_result = ParseComplexExpression(str);
|
2017-06-08 01:29:02 +00:00
|
|
|
|
|
|
|
if (complex_result.status != ParseStatus::Successful)
|
2014-08-30 20:44:28 +00:00
|
|
|
{
|
2019-03-02 20:47:26 +00:00
|
|
|
// This is a bit odd.
|
|
|
|
// Return the error status of the complex expression with the fallback barewords expression.
|
|
|
|
complex_result.expr = std::move(bareword_expr);
|
|
|
|
return complex_result;
|
2013-06-27 00:19:23 +00:00
|
|
|
}
|
|
|
|
|
2019-03-02 20:47:26 +00:00
|
|
|
complex_result.expr = std::make_unique<CoalesceExpression>(std::move(bareword_expr),
|
|
|
|
std::move(complex_result.expr));
|
|
|
|
return complex_result;
|
2013-06-27 00:19:23 +00:00
|
|
|
}
|
2019-06-17 20:39:24 +00:00
|
|
|
} // namespace ciface::ExpressionParser
|