2014-02-10 18:54:46 +00:00
|
|
|
// Copyright 2013 Dolphin Emulator Project
|
2015-05-17 23:08:10 +00:00
|
|
|
// Licensed under GPLv2+
|
2014-02-10 18:54:46 +00:00
|
|
|
// Refer to the license.txt file included.
|
2013-06-14 03:09:55 +00:00
|
|
|
|
2014-02-10 18:54:46 +00:00
|
|
|
#pragma once
|
2013-06-14 03:09:55 +00:00
|
|
|
|
2018-12-30 22:06:29 +00:00
|
|
|
#include <map>
|
2016-06-26 03:34:09 +00:00
|
|
|
#include <memory>
|
2019-03-02 20:47:26 +00:00
|
|
|
#include <optional>
|
2013-06-14 03:09:55 +00:00
|
|
|
#include <string>
|
2019-01-26 18:17:30 +00:00
|
|
|
|
2020-09-15 11:34:41 +00:00
|
|
|
#include "InputCommon/ControllerInterface/CoreDevice.h"
|
2013-06-14 03:09:55 +00:00
|
|
|
|
2019-06-17 20:39:24 +00:00
|
|
|
namespace ciface::ExpressionParser
|
2013-06-14 03:09:55 +00:00
|
|
|
{
|
2019-03-02 16:10:26 +00:00
|
|
|
enum TokenType
|
|
|
|
{
|
2019-10-12 16:41:02 +00:00
|
|
|
TOK_WHITESPACE,
|
2019-03-02 16:10:26 +00:00
|
|
|
TOK_INVALID,
|
|
|
|
TOK_EOF,
|
|
|
|
TOK_LPAREN,
|
|
|
|
TOK_RPAREN,
|
2019-04-04 22:35:49 +00:00
|
|
|
TOK_NOT,
|
2019-03-02 16:10:26 +00:00
|
|
|
TOK_CONTROL,
|
|
|
|
TOK_LITERAL,
|
|
|
|
TOK_VARIABLE,
|
2019-04-04 22:35:49 +00:00
|
|
|
TOK_BAREWORD,
|
2019-10-12 16:41:02 +00:00
|
|
|
TOK_COMMENT,
|
2019-10-23 00:12:55 +00:00
|
|
|
TOK_HOTKEY,
|
2019-03-02 16:10:26 +00:00
|
|
|
// Binary Ops:
|
|
|
|
TOK_BINARY_OPS_BEGIN,
|
|
|
|
TOK_AND = TOK_BINARY_OPS_BEGIN,
|
|
|
|
TOK_OR,
|
|
|
|
TOK_ADD,
|
|
|
|
TOK_SUB,
|
|
|
|
TOK_MUL,
|
|
|
|
TOK_DIV,
|
|
|
|
TOK_MOD,
|
|
|
|
TOK_ASSIGN,
|
|
|
|
TOK_LTHAN,
|
|
|
|
TOK_GTHAN,
|
|
|
|
TOK_COMMA,
|
2019-10-20 14:51:19 +00:00
|
|
|
TOK_XOR,
|
2019-03-02 16:10:26 +00:00
|
|
|
TOK_BINARY_OPS_END,
|
|
|
|
};
|
|
|
|
|
|
|
|
class Token
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
TokenType type;
|
|
|
|
std::string data;
|
|
|
|
|
|
|
|
// Position in the input string:
|
|
|
|
std::size_t string_position = 0;
|
|
|
|
std::size_t string_length = 0;
|
|
|
|
|
2019-03-02 20:47:26 +00:00
|
|
|
explicit Token(TokenType type_);
|
2019-03-02 16:10:26 +00:00
|
|
|
Token(TokenType type_, std::string data_);
|
|
|
|
|
|
|
|
bool IsBinaryOperator() const;
|
|
|
|
};
|
|
|
|
|
|
|
|
enum class ParseStatus
|
|
|
|
{
|
|
|
|
Successful,
|
|
|
|
SyntaxError,
|
|
|
|
EmptyExpression,
|
|
|
|
};
|
|
|
|
|
|
|
|
class Lexer
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
std::string expr;
|
|
|
|
std::string::iterator it;
|
|
|
|
|
2019-10-12 17:28:19 +00:00
|
|
|
explicit Lexer(std::string expr_);
|
2019-03-02 16:10:26 +00:00
|
|
|
|
|
|
|
ParseStatus Tokenize(std::vector<Token>& tokens);
|
|
|
|
|
|
|
|
private:
|
|
|
|
template <typename F>
|
|
|
|
std::string FetchCharsWhile(F&& func)
|
|
|
|
{
|
|
|
|
std::string value;
|
|
|
|
while (it != expr.end() && func(*it))
|
|
|
|
{
|
|
|
|
value += *it;
|
|
|
|
++it;
|
|
|
|
}
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string FetchDelimString(char delim);
|
|
|
|
std::string FetchWordChars();
|
|
|
|
Token GetDelimitedLiteral();
|
|
|
|
Token GetVariable();
|
|
|
|
Token GetFullyQualifiedControl();
|
2019-04-04 22:35:49 +00:00
|
|
|
Token GetBareword(char c);
|
2019-03-02 16:10:26 +00:00
|
|
|
Token GetRealLiteral(char c);
|
|
|
|
|
2019-10-12 16:41:02 +00:00
|
|
|
Token PeekToken();
|
2019-03-02 16:10:26 +00:00
|
|
|
Token NextToken();
|
|
|
|
};
|
|
|
|
|
2013-06-14 03:09:55 +00:00
|
|
|
class ControlQualifier
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
bool has_device;
|
|
|
|
Core::DeviceQualifier device_qualifier;
|
|
|
|
std::string control_name;
|
|
|
|
|
|
|
|
ControlQualifier() : has_device(false) {}
|
2018-12-30 16:37:23 +00:00
|
|
|
|
2017-02-26 07:04:16 +00:00
|
|
|
operator std::string() const
|
2013-06-14 03:09:55 +00:00
|
|
|
{
|
|
|
|
if (has_device)
|
|
|
|
return device_qualifier.ToString() + ":" + control_name;
|
|
|
|
else
|
|
|
|
return control_name;
|
|
|
|
}
|
2018-12-30 16:37:23 +00:00
|
|
|
|
|
|
|
void FromString(const std::string& str)
|
|
|
|
{
|
|
|
|
const auto col_pos = str.find_last_of(':');
|
|
|
|
|
|
|
|
has_device = (str.npos != col_pos);
|
|
|
|
if (has_device)
|
|
|
|
{
|
|
|
|
device_qualifier.FromString(str.substr(0, col_pos));
|
|
|
|
control_name = str.substr(col_pos + 1);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
device_qualifier.FromString("");
|
|
|
|
control_name = str;
|
|
|
|
}
|
|
|
|
}
|
2013-06-14 03:09:55 +00:00
|
|
|
};
|
|
|
|
|
2018-12-30 22:06:29 +00:00
|
|
|
class ControlEnvironment
|
2013-06-14 03:09:55 +00:00
|
|
|
{
|
|
|
|
public:
|
2018-12-30 22:06:29 +00:00
|
|
|
using VariableContainer = std::map<std::string, ControlState>;
|
|
|
|
|
|
|
|
ControlEnvironment(const Core::DeviceContainer& container_, const Core::DeviceQualifier& default_,
|
|
|
|
VariableContainer& vars)
|
|
|
|
: m_variables(vars), container(container_), default_device(default_)
|
2013-06-14 03:09:55 +00:00
|
|
|
{
|
|
|
|
}
|
2018-12-30 22:06:29 +00:00
|
|
|
|
2017-02-26 07:04:16 +00:00
|
|
|
std::shared_ptr<Core::Device> FindDevice(ControlQualifier qualifier) const;
|
2018-12-30 22:06:29 +00:00
|
|
|
Core::Device::Input* FindInput(ControlQualifier qualifier) const;
|
|
|
|
Core::Device::Output* FindOutput(ControlQualifier qualifier) const;
|
|
|
|
ControlState* GetVariablePtr(const std::string& name);
|
2013-06-14 03:09:55 +00:00
|
|
|
|
|
|
|
private:
|
2018-12-30 22:06:29 +00:00
|
|
|
VariableContainer& m_variables;
|
2013-06-14 03:09:55 +00:00
|
|
|
const Core::DeviceContainer& container;
|
|
|
|
const Core::DeviceQualifier& default_device;
|
|
|
|
};
|
|
|
|
|
|
|
|
class Expression
|
|
|
|
{
|
|
|
|
public:
|
2017-06-08 00:30:07 +00:00
|
|
|
virtual ~Expression() = default;
|
|
|
|
virtual ControlState GetValue() const = 0;
|
|
|
|
virtual void SetValue(ControlState state) = 0;
|
|
|
|
virtual int CountNumControls() const = 0;
|
2018-12-30 22:06:29 +00:00
|
|
|
virtual void UpdateReferences(ControlEnvironment& finder) = 0;
|
2013-06-14 03:09:55 +00:00
|
|
|
};
|
|
|
|
|
2019-03-02 20:47:26 +00:00
|
|
|
class ParseResult
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
static ParseResult MakeEmptyResult();
|
|
|
|
static ParseResult MakeSuccessfulResult(std::unique_ptr<Expression>&& expr);
|
|
|
|
static ParseResult MakeErrorResult(Token token, std::string description);
|
|
|
|
|
|
|
|
ParseStatus status;
|
|
|
|
std::unique_ptr<Expression> expr;
|
|
|
|
|
|
|
|
// Used for parse errors:
|
|
|
|
// TODO: This should probably be moved elsewhere:
|
|
|
|
std::optional<Token> token;
|
|
|
|
std::optional<std::string> description;
|
|
|
|
|
|
|
|
private:
|
|
|
|
ParseResult() = default;
|
|
|
|
};
|
|
|
|
|
|
|
|
ParseResult ParseExpression(const std::string& expr);
|
|
|
|
ParseResult ParseTokens(const std::vector<Token>& tokens);
|
2019-10-12 16:41:02 +00:00
|
|
|
void RemoveInertTokens(std::vector<Token>* tokens);
|
2019-03-02 20:47:26 +00:00
|
|
|
|
2019-06-17 20:39:24 +00:00
|
|
|
} // namespace ciface::ExpressionParser
|