From f3192ca06de1007545bf6c3a49ddc83fb615a642 Mon Sep 17 00:00:00 2001 From: Jordan Woyak Date: Sun, 30 Dec 2018 10:37:23 -0600 Subject: ExpressionParser: Add support for literals. --- .../InputCommon/ControlReference/ExpressionParser.h | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) (limited to 'Source/Core/InputCommon/ControlReference/ExpressionParser.h') diff --git a/Source/Core/InputCommon/ControlReference/ExpressionParser.h b/Source/Core/InputCommon/ControlReference/ExpressionParser.h index 56c0340f49..0a42d90c62 100644 --- a/Source/Core/InputCommon/ControlReference/ExpressionParser.h +++ b/Source/Core/InputCommon/ControlReference/ExpressionParser.h @@ -19,6 +19,7 @@ public: std::string control_name; ControlQualifier() : has_device(false) {} + operator std::string() const { if (has_device) @@ -26,6 +27,23 @@ public: else return control_name; } + + 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; + } + } }; class ControlFinder -- cgit v1.2.3 From e896835f86e52051b1998a344549dfa8ba79847a Mon Sep 17 00:00:00 2001 From: Jordan Woyak Date: Sun, 30 Dec 2018 16:06:29 -0600 Subject: ExpressionParser: Renamed ControlFinder to ControlEnvironment. Added support for variables and assignment operator. ControlExpression objects now reference a matching input and output so the two can me mixed in any expression. (you can set rumble directly from inputs) --- .../InputCommon/ControlReference/ExpressionParser.h | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) (limited to 'Source/Core/InputCommon/ControlReference/ExpressionParser.h') diff --git a/Source/Core/InputCommon/ControlReference/ExpressionParser.h b/Source/Core/InputCommon/ControlReference/ExpressionParser.h index 0a42d90c62..d9f7a5eb2b 100644 --- a/Source/Core/InputCommon/ControlReference/ExpressionParser.h +++ b/Source/Core/InputCommon/ControlReference/ExpressionParser.h @@ -4,6 +4,7 @@ #pragma once +#include #include #include #include @@ -46,21 +47,26 @@ public: } }; -class ControlFinder +class ControlEnvironment { public: - ControlFinder(const Core::DeviceContainer& container_, const Core::DeviceQualifier& default_, - const bool is_input_) - : container(container_), default_device(default_), is_input(is_input_) + using VariableContainer = std::map; + + ControlEnvironment(const Core::DeviceContainer& container_, const Core::DeviceQualifier& default_, + VariableContainer& vars) + : m_variables(vars), container(container_), default_device(default_) { } + std::shared_ptr FindDevice(ControlQualifier qualifier) const; - Core::Device::Control* FindControl(ControlQualifier qualifier) const; + Core::Device::Input* FindInput(ControlQualifier qualifier) const; + Core::Device::Output* FindOutput(ControlQualifier qualifier) const; + ControlState* GetVariablePtr(const std::string& name); private: + VariableContainer& m_variables; const Core::DeviceContainer& container; const Core::DeviceQualifier& default_device; - bool is_input; }; class Expression @@ -70,7 +76,7 @@ public: virtual ControlState GetValue() const = 0; virtual void SetValue(ControlState state) = 0; virtual int CountNumControls() const = 0; - virtual void UpdateReferences(ControlFinder& finder) = 0; + virtual void UpdateReferences(ControlEnvironment& finder) = 0; virtual operator std::string() const = 0; }; -- cgit v1.2.3 From fd07ae8cec5c77ac60788350610739e6ac094547 Mon Sep 17 00:00:00 2001 From: Jordan Woyak Date: Sat, 26 Jan 2019 12:17:30 -0600 Subject: ExpressionParser: Move FunctionExpression type definitions into another file. --- Source/Core/InputCommon/ControlReference/ExpressionParser.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'Source/Core/InputCommon/ControlReference/ExpressionParser.h') diff --git a/Source/Core/InputCommon/ControlReference/ExpressionParser.h b/Source/Core/InputCommon/ControlReference/ExpressionParser.h index d9f7a5eb2b..0cb84af5cd 100644 --- a/Source/Core/InputCommon/ControlReference/ExpressionParser.h +++ b/Source/Core/InputCommon/ControlReference/ExpressionParser.h @@ -7,7 +7,7 @@ #include #include #include -#include + #include "InputCommon/ControllerInterface/Device.h" namespace ciface::ExpressionParser -- cgit v1.2.3 From c8b2188e1972f14aec24ac66908a641dbdbd106f Mon Sep 17 00:00:00 2001 From: Jordan Woyak Date: Sat, 2 Mar 2019 10:10:26 -0600 Subject: DolphinQT: Add syntax highlighting from tokenizer data. --- .../ControlReference/ExpressionParser.h | 93 ++++++++++++++++++++-- 1 file changed, 86 insertions(+), 7 deletions(-) (limited to 'Source/Core/InputCommon/ControlReference/ExpressionParser.h') diff --git a/Source/Core/InputCommon/ControlReference/ExpressionParser.h b/Source/Core/InputCommon/ControlReference/ExpressionParser.h index 0cb84af5cd..8e3e6cc1a9 100644 --- a/Source/Core/InputCommon/ControlReference/ExpressionParser.h +++ b/Source/Core/InputCommon/ControlReference/ExpressionParser.h @@ -12,6 +12,92 @@ namespace ciface::ExpressionParser { +enum TokenType +{ + TOK_DISCARD, + TOK_INVALID, + TOK_EOF, + TOK_LPAREN, + TOK_RPAREN, + TOK_FUNCTION, + TOK_CONTROL, + TOK_LITERAL, + TOK_VARIABLE, + // 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, + 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; + + Token(TokenType type_); + Token(TokenType type_, std::string data_); + + bool IsBinaryOperator() const; + operator std::string() const; +}; + +enum class ParseStatus +{ + Successful, + SyntaxError, + EmptyExpression, +}; + +class Lexer +{ +public: + std::string expr; + std::string::iterator it; + + Lexer(const std::string& expr_); + + ParseStatus Tokenize(std::vector& tokens); + +private: + template + 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 GetFunction(); + Token GetDelimitedLiteral(); + Token GetVariable(); + Token GetFullyQualifiedControl(); + Token GetBarewordsControl(char c); + Token GetRealLiteral(char c); + + Token NextToken(); +}; + class ControlQualifier { public: @@ -80,12 +166,5 @@ public: virtual operator std::string() const = 0; }; -enum class ParseStatus -{ - Successful, - SyntaxError, - EmptyExpression, -}; - std::pair> ParseExpression(const std::string& expr); } // namespace ciface::ExpressionParser -- cgit v1.2.3 From ca7ce674500b7664ed049ac1019a2e40636187dc Mon Sep 17 00:00:00 2001 From: Jordan Woyak Date: Sat, 2 Mar 2019 14:47:26 -0600 Subject: ExpressionParser/DolphinQt: Added parse results to UI. --- .../ControlReference/ExpressionParser.h | 26 ++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) (limited to 'Source/Core/InputCommon/ControlReference/ExpressionParser.h') diff --git a/Source/Core/InputCommon/ControlReference/ExpressionParser.h b/Source/Core/InputCommon/ControlReference/ExpressionParser.h index 8e3e6cc1a9..3dac67e9db 100644 --- a/Source/Core/InputCommon/ControlReference/ExpressionParser.h +++ b/Source/Core/InputCommon/ControlReference/ExpressionParser.h @@ -6,6 +6,7 @@ #include #include +#include #include #include "InputCommon/ControllerInterface/Device.h" @@ -49,7 +50,7 @@ public: std::size_t string_position = 0; std::size_t string_length = 0; - Token(TokenType type_); + explicit Token(TokenType type_); Token(TokenType type_, std::string data_); bool IsBinaryOperator() const; @@ -166,5 +167,26 @@ public: virtual operator std::string() const = 0; }; -std::pair> ParseExpression(const std::string& expr); +class ParseResult +{ +public: + static ParseResult MakeEmptyResult(); + static ParseResult MakeSuccessfulResult(std::unique_ptr&& expr); + static ParseResult MakeErrorResult(Token token, std::string description); + + ParseStatus status; + std::unique_ptr expr; + + // Used for parse errors: + // TODO: This should probably be moved elsewhere: + std::optional token; + std::optional description; + +private: + ParseResult() = default; +}; + +ParseResult ParseExpression(const std::string& expr); +ParseResult ParseTokens(const std::vector& tokens); + } // namespace ciface::ExpressionParser -- cgit v1.2.3 From b57178d246640270779d791b00d3a5b8fd7c13f2 Mon Sep 17 00:00:00 2001 From: Jordan Woyak Date: Thu, 4 Apr 2019 17:35:49 -0500 Subject: ExpressionParser: Remove ! character from function syntax. Remove unused serialization functions. --- Source/Core/InputCommon/ControlReference/ExpressionParser.h | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) (limited to 'Source/Core/InputCommon/ControlReference/ExpressionParser.h') diff --git a/Source/Core/InputCommon/ControlReference/ExpressionParser.h b/Source/Core/InputCommon/ControlReference/ExpressionParser.h index 3dac67e9db..2f41d2fde7 100644 --- a/Source/Core/InputCommon/ControlReference/ExpressionParser.h +++ b/Source/Core/InputCommon/ControlReference/ExpressionParser.h @@ -20,10 +20,11 @@ enum TokenType TOK_EOF, TOK_LPAREN, TOK_RPAREN, - TOK_FUNCTION, + TOK_NOT, TOK_CONTROL, TOK_LITERAL, TOK_VARIABLE, + TOK_BAREWORD, // Binary Ops: TOK_BINARY_OPS_BEGIN, TOK_AND = TOK_BINARY_OPS_BEGIN, @@ -54,7 +55,6 @@ public: Token(TokenType type_, std::string data_); bool IsBinaryOperator() const; - operator std::string() const; }; enum class ParseStatus @@ -89,11 +89,10 @@ private: std::string FetchDelimString(char delim); std::string FetchWordChars(); - Token GetFunction(); Token GetDelimitedLiteral(); Token GetVariable(); Token GetFullyQualifiedControl(); - Token GetBarewordsControl(char c); + Token GetBareword(char c); Token GetRealLiteral(char c); Token NextToken(); @@ -164,7 +163,6 @@ public: virtual void SetValue(ControlState state) = 0; virtual int CountNumControls() const = 0; virtual void UpdateReferences(ControlEnvironment& finder) = 0; - virtual operator std::string() const = 0; }; class ParseResult -- cgit v1.2.3 From 72302d9c4224271a606f2782dec7dc725658b150 Mon Sep 17 00:00:00 2001 From: Jordan Woyak Date: Sat, 12 Oct 2019 11:41:02 -0500 Subject: ExpressionParser: Add support for /* */ style comments. --- Source/Core/InputCommon/ControlReference/ExpressionParser.h | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'Source/Core/InputCommon/ControlReference/ExpressionParser.h') diff --git a/Source/Core/InputCommon/ControlReference/ExpressionParser.h b/Source/Core/InputCommon/ControlReference/ExpressionParser.h index 2f41d2fde7..994f8c201b 100644 --- a/Source/Core/InputCommon/ControlReference/ExpressionParser.h +++ b/Source/Core/InputCommon/ControlReference/ExpressionParser.h @@ -15,7 +15,7 @@ namespace ciface::ExpressionParser { enum TokenType { - TOK_DISCARD, + TOK_WHITESPACE, TOK_INVALID, TOK_EOF, TOK_LPAREN, @@ -25,6 +25,7 @@ enum TokenType TOK_LITERAL, TOK_VARIABLE, TOK_BAREWORD, + TOK_COMMENT, // Binary Ops: TOK_BINARY_OPS_BEGIN, TOK_AND = TOK_BINARY_OPS_BEGIN, @@ -95,6 +96,7 @@ private: Token GetBareword(char c); Token GetRealLiteral(char c); + Token PeekToken(); Token NextToken(); }; @@ -186,5 +188,6 @@ private: ParseResult ParseExpression(const std::string& expr); ParseResult ParseTokens(const std::vector& tokens); +void RemoveInertTokens(std::vector* tokens); } // namespace ciface::ExpressionParser -- cgit v1.2.3 From 7295458c11e1465c9e0a823620ea935396c4ea39 Mon Sep 17 00:00:00 2001 From: Jordan Woyak Date: Sat, 12 Oct 2019 12:28:19 -0500 Subject: ExpressionParser: Make Lexer ctor explicit and move argument. --- Source/Core/InputCommon/ControlReference/ExpressionParser.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'Source/Core/InputCommon/ControlReference/ExpressionParser.h') diff --git a/Source/Core/InputCommon/ControlReference/ExpressionParser.h b/Source/Core/InputCommon/ControlReference/ExpressionParser.h index 994f8c201b..cab6096c0e 100644 --- a/Source/Core/InputCommon/ControlReference/ExpressionParser.h +++ b/Source/Core/InputCommon/ControlReference/ExpressionParser.h @@ -71,7 +71,7 @@ public: std::string expr; std::string::iterator it; - Lexer(const std::string& expr_); + explicit Lexer(std::string expr_); ParseStatus Tokenize(std::vector& tokens); -- cgit v1.2.3