DolphinQT: Add syntax highlighting from tokenizer data.

This commit is contained in:
Jordan Woyak
2019-03-02 10:10:26 -06:00
parent e3cf2ae0d4
commit c8b2188e19
3 changed files with 399 additions and 244 deletions

View File

@ -17,6 +17,7 @@
#include <QPushButton> #include <QPushButton>
#include <QSlider> #include <QSlider>
#include <QSpinBox> #include <QSpinBox>
#include <QSyntaxHighlighter>
#include <QVBoxLayout> #include <QVBoxLayout>
#include "Core/Core.h" #include "Core/Core.h"
@ -26,11 +27,115 @@
#include "DolphinQt/QtUtils/BlockUserInputFilter.h" #include "DolphinQt/QtUtils/BlockUserInputFilter.h"
#include "InputCommon/ControlReference/ControlReference.h" #include "InputCommon/ControlReference/ControlReference.h"
#include "InputCommon/ControlReference/ExpressionParser.h"
#include "InputCommon/ControllerEmu/ControllerEmu.h" #include "InputCommon/ControllerEmu/ControllerEmu.h"
#include "InputCommon/ControllerInterface/ControllerInterface.h" #include "InputCommon/ControllerInterface/ControllerInterface.h"
constexpr int SLIDER_TICK_COUNT = 100; constexpr int SLIDER_TICK_COUNT = 100;
namespace
{
QTextCharFormat GetSpecialCharFormat()
{
QTextCharFormat format;
format.setFontWeight(QFont::Weight::Bold);
return format;
}
QTextCharFormat GetOperatorCharFormat()
{
QTextCharFormat format;
format.setFontWeight(QFont::Weight::Bold);
format.setForeground(QBrush{Qt::darkBlue});
return format;
}
QTextCharFormat GetLiteralCharFormat()
{
QTextCharFormat format;
format.setForeground(QBrush{Qt::darkMagenta});
return format;
}
QTextCharFormat GetInvalidCharFormat()
{
QTextCharFormat format;
format.setUnderlineStyle(QTextCharFormat::WaveUnderline);
format.setUnderlineColor(Qt::darkRed);
return format;
}
QTextCharFormat GetControlCharFormat()
{
QTextCharFormat format;
format.setForeground(QBrush{Qt::darkGreen});
return format;
}
QTextCharFormat GetVariableCharFormat()
{
QTextCharFormat format;
format.setForeground(QBrush{Qt::magenta});
return format;
}
QTextCharFormat GetFunctionCharFormat()
{
QTextCharFormat format;
format.setForeground(QBrush{Qt::darkCyan});
return format;
}
class SyntaxHighlighter : public QSyntaxHighlighter
{
public:
SyntaxHighlighter(QTextDocument* parent) : QSyntaxHighlighter(parent) {}
void highlightBlock(const QString& text) final override
{
// TODO: This is going to result in improper highlighting with non-ascii characters:
ciface::ExpressionParser::Lexer lexer(text.toStdString());
std::vector<ciface::ExpressionParser::Token> tokens;
lexer.Tokenize(tokens);
using ciface::ExpressionParser::TokenType;
for (auto& token : tokens)
{
switch (token.type)
{
case TokenType::TOK_INVALID:
setFormat(token.string_position, token.string_length, GetInvalidCharFormat());
break;
case TokenType::TOK_LPAREN:
case TokenType::TOK_RPAREN:
case TokenType::TOK_COMMA:
setFormat(token.string_position, token.string_length, GetSpecialCharFormat());
break;
case TokenType::TOK_LITERAL:
setFormat(token.string_position, token.string_length, GetLiteralCharFormat());
break;
case TokenType::TOK_CONTROL:
setFormat(token.string_position, token.string_length, GetControlCharFormat());
break;
case TokenType::TOK_FUNCTION:
setFormat(token.string_position, token.string_length, GetFunctionCharFormat());
break;
case TokenType::TOK_VARIABLE:
setFormat(token.string_position, token.string_length, GetVariableCharFormat());
break;
default:
if (token.IsBinaryOperator())
setFormat(token.string_position, token.string_length, GetOperatorCharFormat());
break;
}
}
}
};
} // namespace
IOWindow::IOWindow(QWidget* parent, ControllerEmu::EmulatedController* controller, IOWindow::IOWindow(QWidget* parent, ControllerEmu::EmulatedController* controller,
ControlReference* ref, IOWindow::Type type) ControlReference* ref, IOWindow::Type type)
: QDialog(parent), m_reference(ref), m_controller(controller), m_type(type) : QDialog(parent), m_reference(ref), m_controller(controller), m_type(type)
@ -54,13 +159,16 @@ void IOWindow::CreateMainLayout()
m_select_button = new QPushButton(tr("Select")); m_select_button = new QPushButton(tr("Select"));
m_detect_button = new QPushButton(tr("Detect")); m_detect_button = new QPushButton(tr("Detect"));
m_test_button = new QPushButton(tr("Test")); m_test_button = new QPushButton(tr("Test"));
m_expression_text = new QPlainTextEdit();
m_button_box = new QDialogButtonBox(); m_button_box = new QDialogButtonBox();
m_clear_button = new QPushButton(tr("Clear")); m_clear_button = new QPushButton(tr("Clear"));
m_apply_button = new QPushButton(tr("Apply")); m_apply_button = new QPushButton(tr("Apply"));
m_range_slider = new QSlider(Qt::Horizontal); m_range_slider = new QSlider(Qt::Horizontal);
m_range_spinbox = new QSpinBox(); m_range_spinbox = new QSpinBox();
m_expression_text = new QPlainTextEdit();
m_expression_text->setFont(QFontDatabase::systemFont(QFontDatabase::FixedFont));
new SyntaxHighlighter(m_expression_text->document());
m_operators_combo = new QComboBox(); m_operators_combo = new QComboBox();
m_operators_combo->addItem(tr("Operators")); m_operators_combo->addItem(tr("Operators"));
m_operators_combo->insertSeparator(1); m_operators_combo->insertSeparator(1);

View File

@ -20,33 +20,6 @@ namespace ciface::ExpressionParser
{ {
using namespace ciface::Core; using namespace ciface::Core;
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,
};
inline std::string OpName(TokenType op) inline std::string OpName(TokenType op)
{ {
switch (op) switch (op)
@ -83,16 +56,21 @@ inline std::string OpName(TokenType op)
} }
} }
class Token Token::Token(TokenType type_) : type(type_)
{ {
public: }
TokenType type;
std::string data;
Token(TokenType type_) : type(type_) {} Token::Token(TokenType type_, std::string data_) : type(type_), data(std::move(data_))
Token(TokenType type_, std::string data_) : type(type_), data(std::move(data_)) {} {
operator std::string() const }
{
bool Token::IsBinaryOperator() const
{
return type >= TOK_BINARY_OPS_BEGIN && type < TOK_BINARY_OPS_END;
}
Token::operator std::string() const
{
switch (type) switch (type)
{ {
case TOK_DISCARD: case TOK_DISCARD:
@ -138,39 +116,23 @@ public:
} }
return "Invalid"; return "Invalid";
} }
};
class Lexer Lexer::Lexer(const std::string& expr_) : expr(expr_)
{ {
public: it = expr.begin();
std::string expr; }
std::string::iterator it;
Lexer(const std::string& expr_) : expr(expr_) { it = expr.begin(); } std::string Lexer::FetchDelimString(char delim)
{
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)
{
const std::string result = FetchCharsWhile([delim](char c) { return c != delim; }); const std::string result = FetchCharsWhile([delim](char c) { return c != delim; });
if (it != expr.end()) if (it != expr.end())
++it; ++it;
return result; return result;
} }
std::string FetchWordChars() std::string Lexer::FetchWordChars()
{ {
// Words must start with a letter or underscore. // Words must start with a letter or underscore.
if (expr.end() == it || (!std::isalpha(*it, std::locale::classic()) && ('_' != *it))) if (expr.end() == it || (!std::isalpha(*it, std::locale::classic()) && ('_' != *it)))
return ""; return "";
@ -179,18 +141,30 @@ public:
std::regex rx("[a-z0-9_]", std::regex_constants::icase); std::regex rx("[a-z0-9_]", std::regex_constants::icase);
return FetchCharsWhile([&rx](char c) { return std::regex_match(std::string(1, c), rx); }); return FetchCharsWhile([&rx](char c) { return std::regex_match(std::string(1, c), rx); });
} }
Token GetFunction() { return Token(TOK_FUNCTION, FetchWordChars()); } Token Lexer::GetFunction()
{
return Token(TOK_FUNCTION, FetchWordChars());
}
Token GetDelimitedLiteral() { return Token(TOK_LITERAL, FetchDelimString('\'')); } Token Lexer::GetDelimitedLiteral()
{
return Token(TOK_LITERAL, FetchDelimString('\''));
}
Token GetVariable() { return Token(TOK_VARIABLE, FetchWordChars()); } Token Lexer::GetVariable()
{
return Token(TOK_VARIABLE, FetchWordChars());
}
Token GetFullyQualifiedControl() { return Token(TOK_CONTROL, FetchDelimString('`')); } Token Lexer::GetFullyQualifiedControl()
{
return Token(TOK_CONTROL, FetchDelimString('`'));
}
Token GetBarewordsControl(char c) Token Lexer::GetBarewordsControl(char c)
{ {
std::string name; std::string name;
name += c; name += c;
name += FetchCharsWhile([](char c) { return std::isalpha(c, std::locale::classic()); }); name += FetchCharsWhile([](char c) { return std::isalpha(c, std::locale::classic()); });
@ -198,20 +172,19 @@ public:
ControlQualifier qualifier; ControlQualifier qualifier;
qualifier.control_name = name; qualifier.control_name = name;
return Token(TOK_CONTROL, qualifier); return Token(TOK_CONTROL, qualifier);
} }
Token GetRealLiteral(char c) Token Lexer::GetRealLiteral(char c)
{ {
std::string value; std::string value;
value += c; value += c;
value += value += FetchCharsWhile([](char c) { return isdigit(c, std::locale::classic()) || ('.' == c); });
FetchCharsWhile([](char c) { return isdigit(c, std::locale::classic()) || ('.' == c); });
return Token(TOK_LITERAL, value); return Token(TOK_LITERAL, value);
} }
Token NextToken() Token Lexer::NextToken()
{ {
if (it == expr.end()) if (it == expr.end())
return Token(TOK_EOF); return Token(TOK_EOF);
@ -265,31 +238,31 @@ public:
else else
return Token(TOK_INVALID); return Token(TOK_INVALID);
} }
} }
ParseStatus Tokenize(std::vector<Token>& tokens) ParseStatus Lexer::Tokenize(std::vector<Token>& tokens)
{ {
while (true) while (true)
{ {
const std::size_t string_position = it - expr.begin();
Token tok = NextToken(); Token tok = NextToken();
tok.string_position = string_position;
tok.string_length = it - expr.begin();
if (tok.type == TOK_DISCARD) if (tok.type == TOK_DISCARD)
continue; continue;
if (tok.type == TOK_INVALID)
{
tokens.clear();
return ParseStatus::SyntaxError;
}
tokens.push_back(tok); tokens.push_back(tok);
if (tok.type == TOK_INVALID)
return ParseStatus::SyntaxError;
if (tok.type == TOK_EOF) if (tok.type == TOK_EOF)
break; break;
} }
return ParseStatus::Successful; return ParseStatus::Successful;
} }
};
class ControlExpression : public Expression class ControlExpression : public Expression
{ {
@ -418,7 +391,7 @@ public:
class LiteralExpression : public Expression class LiteralExpression : public Expression
{ {
public: public:
void SetValue(ControlState value) override void SetValue(ControlState) override
{ {
// Do nothing. // Do nothing.
} }
@ -695,11 +668,6 @@ private:
} }
} }
static bool IsBinaryToken(TokenType type)
{
return type >= TOK_BINARY_OPS_BEGIN && type < TOK_BINARY_OPS_END;
}
static int BinaryOperatorPrecedence(TokenType type) static int BinaryOperatorPrecedence(TokenType type)
{ {
switch (type) switch (type)
@ -738,7 +706,7 @@ private:
std::unique_ptr<Expression> expr = std::move(lhs.expr); std::unique_ptr<Expression> expr = std::move(lhs.expr);
// TODO: handle LTR/RTL associativity? // TODO: handle LTR/RTL associativity?
while (IsBinaryToken(Peek().type) && BinaryOperatorPrecedence(Peek().type) < precedence) while (Peek().IsBinaryOperator() && BinaryOperatorPrecedence(Peek().type) < precedence)
{ {
const Token tok = Chew(); const Token tok = Chew();
ParseResult rhs = ParseBinary(BinaryOperatorPrecedence(tok.type)); ParseResult rhs = ParseBinary(BinaryOperatorPrecedence(tok.type));

View File

@ -12,6 +12,92 @@
namespace ciface::ExpressionParser 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<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 GetFunction();
Token GetDelimitedLiteral();
Token GetVariable();
Token GetFullyQualifiedControl();
Token GetBarewordsControl(char c);
Token GetRealLiteral(char c);
Token NextToken();
};
class ControlQualifier class ControlQualifier
{ {
public: public:
@ -80,12 +166,5 @@ public:
virtual operator std::string() const = 0; virtual operator std::string() const = 0;
}; };
enum class ParseStatus
{
Successful,
SyntaxError,
EmptyExpression,
};
std::pair<ParseStatus, std::unique_ptr<Expression>> ParseExpression(const std::string& expr); std::pair<ParseStatus, std::unique_ptr<Expression>> ParseExpression(const std::string& expr);
} // namespace ciface::ExpressionParser } // namespace ciface::ExpressionParser