| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
#pragma once |
| 5 |
|
| 6 |
#include <map> |
| 7 |
#include <memory> |
| 8 |
#include <optional> |
| 9 |
#include <string> |
| 10 |
|
| 11 |
#include "InputCommon/ControllerInterface/CoreDevice.h" |
| 12 |
|
| 13 |
namespace ciface::ExpressionParser |
| 14 |
{ |
| 15 |
enum TokenType |
| 16 |
{ |
| 17 |
TOK_WHITESPACE, |
| 18 |
TOK_INVALID, |
| 19 |
TOK_EOF, |
| 20 |
TOK_LPAREN, |
| 21 |
TOK_RPAREN, |
| 22 |
TOK_NOT, |
| 23 |
TOK_CONTROL, |
| 24 |
TOK_LITERAL, |
| 25 |
TOK_VARIABLE, |
| 26 |
TOK_BAREWORD, |
| 27 |
TOK_COMMENT, |
| 28 |
TOK_HOTKEY, |
| 29 |
TOK_QUESTION, |
| 30 |
TOK_COLON, |
| 31 |
|
| 32 |
TOK_BINARY_OPS_BEGIN, |
| 33 |
TOK_COMPOUND_ASSIGN_OPS_BEGIN = TOK_BINARY_OPS_BEGIN, |
| 34 |
TOK_AND = TOK_BINARY_OPS_BEGIN, |
| 35 |
TOK_OR, |
| 36 |
TOK_XOR, |
| 37 |
TOK_ADD, |
| 38 |
TOK_SUB, |
| 39 |
TOK_MUL, |
| 40 |
TOK_DIV, |
| 41 |
TOK_MOD, |
| 42 |
TOK_COMPOUND_ASSIGN_OPS_END, |
| 43 |
TOK_ASSIGN = TOK_COMPOUND_ASSIGN_OPS_END, |
| 44 |
TOK_LTHAN, |
| 45 |
TOK_GTHAN, |
| 46 |
TOK_COMMA, |
| 47 |
TOK_BINARY_OPS_END, |
| 48 |
}; |
| 49 |
|
| 50 |
class Token |
| 51 |
{ |
| 52 |
public: |
| 53 |
TokenType type; |
| 54 |
std::string data; |
| 55 |
|
| 56 |
|
| 57 |
std::size_t string_position = 0; |
| 58 |
std::size_t string_length = 0; |
| 59 |
|
| 60 |
explicit Token(TokenType type_); |
| 61 |
Token(TokenType type_, std::string data_); |
| 62 |
|
| 63 |
bool IsBinaryOperator() const; |
| 64 |
}; |
| 65 |
|
| 66 |
enum class ParseStatus |
| 67 |
{ |
| 68 |
Successful, |
| 69 |
|
| 70 |
SyntaxError, |
| 71 |
|
| 72 |
EmptyExpression, |
| 73 |
}; |
| 74 |
|
| 75 |
class Lexer |
| 76 |
{ |
| 77 |
public: |
| 78 |
std::string expr; |
| 79 |
std::string::iterator it; |
| 80 |
|
| 81 |
explicit Lexer(std::string expr_); |
| 82 |
|
| 83 |
ParseStatus Tokenize(std::vector<Token>& tokens); |
| 84 |
|
| 85 |
private: |
| 86 |
template <typename F> |
| 87 |
std::string FetchCharsWhile(F&& func) |
| 88 |
{ |
| 89 |
std::string value; |
| 90 |
while (it != expr.end() && func(*it)) |
| 91 |
{ |
| 92 |
value += *it; |
| 93 |
++it; |
| 94 |
} |
| 95 |
return value; |
| 96 |
} |
| 97 |
|
| 98 |
std::string FetchWordChars(); |
| 99 |
Token GetDelimitedToken(TokenType type, char delimiter); |
| 100 |
Token GetDelimitedLiteral(); |
| 101 |
Token GetVariable(); |
| 102 |
Token GetFullyQualifiedControl(); |
| 103 |
Token GetBareword(char c); |
| 104 |
Token GetRealLiteral(char c); |
| 105 |
|
| 106 |
Token PeekToken(); |
| 107 |
Token NextToken(); |
| 108 |
}; |
| 109 |
|
| 110 |
class ControlQualifier |
| 111 |
{ |
| 112 |
public: |
| 113 |
bool has_device; |
| 114 |
Core::DeviceQualifier device_qualifier; |
| 115 |
|
| 116 |
std::string control_name; |
| 117 |
|
| 118 |
ControlQualifier() : has_device(false) {} |
| 119 |
|
| 120 |
operator std::string() const |
| 121 |
{ |
| 122 |
if (has_device) |
| 123 |
return device_qualifier.ToString() + ":" + control_name; |
| 124 |
else |
| 125 |
return control_name; |
| 126 |
} |
| 127 |
|
| 128 |
void FromString(const std::string& str) |
| 129 |
{ |
| 130 |
const auto col_pos = str.find_last_of(':'); |
| 131 |
|
| 132 |
has_device = (str.npos != col_pos); |
| 133 |
if (has_device) |
| 134 |
{ |
| 135 |
device_qualifier.FromString(str.substr(0, col_pos)); |
| 136 |
control_name = str.substr(col_pos + 1); |
| 137 |
} |
| 138 |
else |
| 139 |
{ |
| 140 |
device_qualifier.FromString(""); |
| 141 |
control_name = str; |
| 142 |
} |
| 143 |
} |
| 144 |
}; |
| 145 |
|
| 146 |
class ControlEnvironment |
| 147 |
{ |
| 148 |
public: |
| 149 |
using VariableContainer = std::map<std::string, std::shared_ptr<ControlState>>; |
| 150 |
|
| 151 |
ControlEnvironment(const Core::DeviceContainer& container_, const Core::DeviceQualifier& default_, |
| 152 |
VariableContainer& vars) |
| 153 |
: m_variables(vars), container(container_), default_device(default_) |
| 154 |
{ |
| 155 |
} |
| 156 |
|
| 157 |
std::shared_ptr<Core::Device> FindDevice(const ControlQualifier& qualifier) const; |
| 158 |
Core::Device::Input* FindInput(const ControlQualifier& qualifier) const; |
| 159 |
Core::Device::Output* FindOutput(const ControlQualifier& qualifier) const; |
| 160 |
|
| 161 |
std::shared_ptr<ControlState> GetVariablePtr(const std::string& name); |
| 162 |
|
| 163 |
void CleanUnusedVariables(); |
| 164 |
|
| 165 |
private: |
| 166 |
VariableContainer& m_variables; |
| 167 |
const Core::DeviceContainer& container; |
| 168 |
const Core::DeviceQualifier& default_device; |
| 169 |
}; |
| 170 |
|
| 171 |
class Expression |
| 172 |
{ |
| 173 |
public: |
| 174 |
virtual ~Expression() = default; |
| 175 |
virtual ControlState GetValue() = 0; |
| 176 |
virtual void SetValue(ControlState state) = 0; |
| 177 |
virtual int CountNumControls() const = 0; |
| 178 |
virtual void UpdateReferences(ControlEnvironment& finder) = 0; |
| 179 |
|
| 180 |
|
| 181 |
virtual Expression* GetLValue(); |
| 182 |
}; |
| 183 |
|
| 184 |
class ParseResult |
| 185 |
{ |
| 186 |
public: |
| 187 |
static ParseResult MakeEmptyResult(); |
| 188 |
static ParseResult MakeSuccessfulResult(std::unique_ptr<Expression>&& expr); |
| 189 |
static ParseResult MakeErrorResult(Token token, std::string description); |
| 190 |
|
| 191 |
ParseStatus status = ParseStatus::EmptyExpression; |
| 192 |
std::unique_ptr<Expression> expr; |
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
std::optional<Token> token; |
| 197 |
std::optional<std::string> description; |
| 198 |
|
| 199 |
private: |
| 200 |
ParseResult() = default; |
| 201 |
}; |
| 202 |
|
| 203 |
ParseResult ParseExpression(const std::string& expr); |
| 204 |
ParseResult ParseTokens(const std::vector<Token>& tokens); |
| 205 |
|
| 206 |
} |