From ed80f4563a944dbe67a407e7d211cc394210a0f3 Mon Sep 17 00:00:00 2001 From: Bojidar Marinov Date: Fri, 26 Aug 2016 14:15:45 +0300 Subject: Adds enums to GDScript Fixes #2966 (cherry picked from commit 4ee82a2c38c57fb980df1ed4727d47959ba9e983) --- modules/gdscript/gd_parser.cpp | 84 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 84 insertions(+) (limited to 'modules/gdscript/gd_parser.cpp') diff --git a/modules/gdscript/gd_parser.cpp b/modules/gdscript/gd_parser.cpp index 74a3d5e9e..c2b262c8d 100644 --- a/modules/gdscript/gd_parser.cpp +++ b/modules/gdscript/gd_parser.cpp @@ -3152,6 +3152,90 @@ void GDParser::_parse_class(ClassNode *p_class) { return; } + } break; + case GDTokenizer::TK_PR_ENUM: { + //mutiple constant declarations.. + + int last_assign = -1; // Incremented by 1 right before the assingment. + + tokenizer->advance(); + if (tokenizer->get_token() != GDTokenizer::TK_CURLY_BRACKET_OPEN) { + _set_error("Expected '{' in enum declaration"); + return; + } + tokenizer->advance(); + + while (true) { + if (tokenizer->get_token() == GDTokenizer::TK_NEWLINE) { + + tokenizer->advance(); // Ignore newlines + } else if (tokenizer->get_token() == GDTokenizer::TK_CURLY_BRACKET_CLOSE) { + + tokenizer->advance(); + break; // End of enum + } else if (tokenizer->get_token() != GDTokenizer::TK_IDENTIFIER) { + + if (tokenizer->get_token() == GDTokenizer::TK_EOF) { + _set_error("Unexpected end of file."); + } else { + _set_error(String("Unexpected ") + GDTokenizer::get_token_name(tokenizer->get_token()) + ", expected identifier"); + } + + return; + } else { // tokenizer->get_token()==GDTokenizer::TK_IDENTIFIER + ClassNode::Constant constant; + + constant.identifier = tokenizer->get_token_identifier(); + + tokenizer->advance(); + + if (tokenizer->get_token() == GDTokenizer::TK_OP_ASSIGN) { + tokenizer->advance(); + + Node *subexpr = NULL; + + subexpr = _parse_and_reduce_expression(p_class, true, true); + if (!subexpr) { + if (_recover_from_completion()) { + break; + } + return; + } + + if (subexpr->type != Node::TYPE_CONSTANT) { + _set_error("Expected constant expression"); + } + + const ConstantNode *subexpr_const = static_cast(subexpr); + + if (subexpr_const->value.get_type() != Variant::INT) { + _set_error("Expected an int value for enum"); + } + + last_assign = subexpr_const->value; + + constant.expression = subexpr; + + } else { + last_assign = last_assign + 1; + ConstantNode *cn = alloc_node(); + cn->value = last_assign; + constant.expression = cn; + } + + if (tokenizer->get_token() == GDTokenizer::TK_COMMA) { + tokenizer->advance(); + } + + p_class->constant_expressions.push_back(constant); + } + } + + if (!_end_statement()) { + _set_error("Expected end of statement (enum)"); + return; + } + } break; default: { -- cgit v1.2.3-70-g09d2