ExpressionParser: Convert parse state enum into an enum class

This commit is contained in:
Lioncash 2017-02-28 04:58:03 -05:00
parent e375c96693
commit d104e5e916
5 changed files with 47 additions and 49 deletions

View File

@ -341,10 +341,10 @@ void ControlDialog::UpdateGUI()
switch (control_reference->GetParseStatus()) switch (control_reference->GetParseStatus())
{ {
case EXPRESSION_PARSE_SYNTAX_ERROR: case ParseStatus::SyntaxError:
m_error_label->SetLabel(_("Syntax error")); m_error_label->SetLabel(_("Syntax error"));
break; break;
case EXPRESSION_PARSE_NO_DEVICE: case ParseStatus::NoDevice:
m_error_label->SetLabel(_("Device not found")); m_error_label->SetLabel(_("Device not found"));
break; break;
default: default:
@ -404,8 +404,8 @@ bool ControlDialog::Validate()
UpdateGUI(); UpdateGUI();
return (control_reference->GetParseStatus() == EXPRESSION_PARSE_SUCCESS || const auto parse_status = control_reference->GetParseStatus();
control_reference->GetParseStatus() == EXPRESSION_PARSE_NO_DEVICE); return parse_status == ParseStatus::Success || parse_status == ParseStatus::NoDevice;
} }
void InputConfigDialog::SetDevice(wxCommandEvent&) void InputConfigDialog::SetDevice(wxCommandEvent&)

View File

@ -43,7 +43,7 @@ int ControlReference::BoundCount() const
return 0; return 0;
} }
ExpressionParseStatus ControlReference::GetParseStatus() const ParseStatus ControlReference::GetParseStatus() const
{ {
return m_parse_status; return m_parse_status;
} }

View File

@ -31,7 +31,7 @@ public:
virtual bool IsInput() const = 0; virtual bool IsInput() const = 0;
int BoundCount() const; int BoundCount() const;
ciface::ExpressionParser::ExpressionParseStatus GetParseStatus() const; ciface::ExpressionParser::ParseStatus GetParseStatus() const;
void UpdateReference(const ciface::Core::DeviceContainer& devices, void UpdateReference(const ciface::Core::DeviceContainer& devices,
const ciface::Core::DeviceQualifier& default_device); const ciface::Core::DeviceQualifier& default_device);
@ -41,7 +41,7 @@ public:
protected: protected:
ControlReference(); ControlReference();
std::unique_ptr<ciface::ExpressionParser::Expression> m_parsed_expression; std::unique_ptr<ciface::ExpressionParser::Expression> m_parsed_expression;
ciface::ExpressionParser::ExpressionParseStatus m_parse_status; ciface::ExpressionParser::ParseStatus m_parse_status;
}; };
// //

View File

@ -183,7 +183,7 @@ public:
} }
} }
ExpressionParseStatus Tokenize(std::vector<Token>& tokens) ParseStatus Tokenize(std::vector<Token>& tokens)
{ {
while (true) while (true)
{ {
@ -195,7 +195,7 @@ public:
if (tok.type == TOK_INVALID) if (tok.type == TOK_INVALID)
{ {
tokens.clear(); tokens.clear();
return EXPRESSION_PARSE_SYNTAX_ERROR; return ParseStatus::SyntaxError;
} }
tokens.push_back(tok); tokens.push_back(tok);
@ -203,7 +203,7 @@ public:
if (tok.type == TOK_EOF) if (tok.type == TOK_EOF)
break; break;
} }
return EXPRESSION_PARSE_SUCCESS; return ParseStatus::Success;
} }
}; };
@ -369,15 +369,15 @@ public:
m_it = tokens.begin(); m_it = tokens.begin();
} }
ExpressionParseStatus Parse(Expression** expr_out) ParseStatus Parse(Expression** expr_out)
{ {
ExpressionNode* node; ExpressionNode* node;
ExpressionParseStatus status = Toplevel(&node); ParseStatus status = Toplevel(&node);
if (status != EXPRESSION_PARSE_SUCCESS) if (status != ParseStatus::Success)
return status; return status;
*expr_out = new Expression(node); *expr_out = new Expression(node);
return EXPRESSION_PARSE_SUCCESS; return ParseStatus::Success;
} }
private: private:
@ -393,7 +393,7 @@ private:
return tok.type == type; return tok.type == type;
} }
ExpressionParseStatus Atom(ExpressionNode** expr_out) ParseStatus Atom(ExpressionNode** expr_out)
{ {
Token tok = Chew(); Token tok = Chew();
switch (tok.type) switch (tok.type)
@ -405,16 +405,16 @@ private:
if (control == nullptr) if (control == nullptr)
{ {
*expr_out = new DummyExpression(tok.qualifier); *expr_out = new DummyExpression(tok.qualifier);
return EXPRESSION_PARSE_NO_DEVICE; return ParseStatus::NoDevice;
} }
*expr_out = new ControlExpression(tok.qualifier, device, control); *expr_out = new ControlExpression(tok.qualifier, device, control);
return EXPRESSION_PARSE_SUCCESS; return ParseStatus::Success;
} }
case TOK_LPAREN: case TOK_LPAREN:
return Paren(expr_out); return Paren(expr_out);
default: default:
return EXPRESSION_PARSE_SYNTAX_ERROR; return ParseStatus::SyntaxError;
} }
} }
@ -429,17 +429,17 @@ private:
} }
} }
ExpressionParseStatus Unary(ExpressionNode** expr_out) ParseStatus Unary(ExpressionNode** expr_out)
{ {
if (IsUnaryExpression(Peek().type)) if (IsUnaryExpression(Peek().type))
{ {
Token tok = Chew(); Token tok = Chew();
ExpressionNode* atom_expr; ExpressionNode* atom_expr;
ExpressionParseStatus status = Atom(&atom_expr); ParseStatus status = Atom(&atom_expr);
if (status == EXPRESSION_PARSE_SYNTAX_ERROR) if (status == ParseStatus::SyntaxError)
return status; return status;
*expr_out = new UnaryExpression(tok.type, atom_expr); *expr_out = new UnaryExpression(tok.type, atom_expr);
return EXPRESSION_PARSE_SUCCESS; return ParseStatus::Success;
} }
return Atom(expr_out); return Atom(expr_out);
@ -458,10 +458,10 @@ private:
} }
} }
ExpressionParseStatus Binary(ExpressionNode** expr_out) ParseStatus Binary(ExpressionNode** expr_out)
{ {
ExpressionParseStatus status = Unary(expr_out); ParseStatus status = Unary(expr_out);
if (status == EXPRESSION_PARSE_SYNTAX_ERROR) if (status == ParseStatus::SyntaxError)
return status; return status;
while (IsBinaryToken(Peek().type)) while (IsBinaryToken(Peek().type))
@ -469,7 +469,7 @@ private:
Token tok = Chew(); Token tok = Chew();
ExpressionNode* unary_expr; ExpressionNode* unary_expr;
status = Unary(&unary_expr); status = Unary(&unary_expr);
if (status == EXPRESSION_PARSE_SYNTAX_ERROR) if (status == ParseStatus::SyntaxError)
{ {
delete *expr_out; delete *expr_out;
return status; return status;
@ -478,27 +478,27 @@ private:
*expr_out = new BinaryExpression(tok.type, *expr_out, unary_expr); *expr_out = new BinaryExpression(tok.type, *expr_out, unary_expr);
} }
return EXPRESSION_PARSE_SUCCESS; return ParseStatus::Success;
} }
ExpressionParseStatus Paren(ExpressionNode** expr_out) ParseStatus Paren(ExpressionNode** expr_out)
{ {
ExpressionParseStatus status; ParseStatus status;
// lparen already chewed // lparen already chewed
if ((status = Toplevel(expr_out)) != EXPRESSION_PARSE_SUCCESS) if ((status = Toplevel(expr_out)) != ParseStatus::Success)
return status; return status;
if (!Expects(TOK_RPAREN)) if (!Expects(TOK_RPAREN))
{ {
delete *expr_out; delete *expr_out;
return EXPRESSION_PARSE_SYNTAX_ERROR; return ParseStatus::SyntaxError;
} }
return EXPRESSION_PARSE_SUCCESS; return ParseStatus::Success;
} }
ExpressionParseStatus Toplevel(ExpressionNode** expr_out) { return Binary(expr_out); } ParseStatus Toplevel(ExpressionNode** expr_out) { return Binary(expr_out); }
}; };
ControlState Expression::GetValue() const ControlState Expression::GetValue() const
@ -522,33 +522,32 @@ Expression::~Expression()
delete node; delete node;
} }
static ExpressionParseStatus ParseExpressionInner(const std::string& str, ControlFinder& finder, static ParseStatus ParseExpressionInner(const std::string& str, ControlFinder& finder,
Expression** expr_out) Expression** expr_out)
{ {
ExpressionParseStatus status; ParseStatus status;
Expression* expr; Expression* expr;
*expr_out = nullptr; *expr_out = nullptr;
if (str == "") if (str == "")
return EXPRESSION_PARSE_SUCCESS; return ParseStatus::Success;
Lexer l(str); Lexer l(str);
std::vector<Token> tokens; std::vector<Token> tokens;
status = l.Tokenize(tokens); status = l.Tokenize(tokens);
if (status != EXPRESSION_PARSE_SUCCESS) if (status != ParseStatus::Success)
return status; return status;
Parser p(tokens, finder); Parser p(tokens, finder);
status = p.Parse(&expr); status = p.Parse(&expr);
if (status != EXPRESSION_PARSE_SUCCESS) if (status != ParseStatus::Success)
return status; return status;
*expr_out = expr; *expr_out = expr;
return EXPRESSION_PARSE_SUCCESS; return ParseStatus::Success;
} }
ExpressionParseStatus ParseExpression(const std::string& str, ControlFinder& finder, ParseStatus ParseExpression(const std::string& str, ControlFinder& finder, Expression** expr_out)
Expression** expr_out)
{ {
// Add compatibility with old simple expressions, which are simple // Add compatibility with old simple expressions, which are simple
// barewords control names. // barewords control names.
@ -562,7 +561,7 @@ ExpressionParseStatus ParseExpression(const std::string& str, ControlFinder& fin
if (control) if (control)
{ {
*expr_out = new Expression(new ControlExpression(qualifier, device, control)); *expr_out = new Expression(new ControlExpression(qualifier, device, control));
return EXPRESSION_PARSE_SUCCESS; return ParseStatus::Success;
} }
return ParseExpressionInner(str, finder, expr_out); return ParseExpressionInner(str, finder, expr_out);

View File

@ -59,14 +59,13 @@ public:
ExpressionNode* node; ExpressionNode* node;
}; };
enum ExpressionParseStatus enum class ParseStatus
{ {
EXPRESSION_PARSE_SUCCESS = 0, Success,
EXPRESSION_PARSE_SYNTAX_ERROR, SyntaxError,
EXPRESSION_PARSE_NO_DEVICE, NoDevice,
}; };
ExpressionParseStatus ParseExpression(const std::string& expr, ControlFinder& finder, ParseStatus ParseExpression(const std::string& expr, ControlFinder& finder, Expression** expr_out);
Expression** expr_out);
} }
} }