Esempio n. 1
0
        public EnumDefinition ParseEnumDefinition(TokenStream tokens)
        {
            Token          enumToken = tokens.PopExpected("enum");
            Token          nameToken = EnsureTokenIsValidName(tokens.Pop(), "Invalid name for an enum.");
            EnumDefinition enumDef   = new EnumDefinition(enumToken, nameToken, this.context);

            this.currentCodeOwner = enumDef;
            List <Token>      valueTokens      = new List <Token>();
            List <Expression> valueExpressions = new List <Expression>();

            tokens.PopExpected("{");
            bool first = true;

            while (!tokens.PopIfPresent("}"))
            {
                if (!first)
                {
                    tokens.PopExpected(",");
                }
                else
                {
                    first = false;
                }

                if (tokens.PopIfPresent("}"))
                {
                    break;
                }

                Token valueToken = EnsureTokenIsValidName(tokens.Pop(), "Invalid name for a enum value.");
                valueTokens.Add(valueToken);
                if (tokens.PopIfPresent("="))
                {
                    Expression value = ParseExpression(tokens);
                    valueExpressions.Add(value);
                }
                else
                {
                    valueExpressions.Add(null);
                }
            }

            enumDef.InitializeValues(valueTokens, valueExpressions);
            this.currentCodeOwner = null;
            return(enumDef);
        }