Exemplo n.º 1
0
        private static Executable ParseFor(Parser parser, TokenStream tokens, Executable owner)
        {
            Token forToken = tokens.PopExpected("for");

            tokens.PopExpected("(");

            if (Parser.IsValidIdentifier(tokens.PeekValue()) && tokens.PeekValue(1) == ":")
            {
                Token iteratorToken = tokens.Pop();
                if (Parser.IsReservedKeyword(iteratorToken.Value))
                {
                    throw new ParserException(iteratorToken, "Cannot use this name for an iterator.");
                }
                tokens.PopExpected(":");
                Expression iterationExpression = ExpressionParser.Parse(tokens, owner);
                tokens.PopExpected(")");
                IList <Executable> body = Parser.ParseBlock(parser, tokens, false, owner);

                return(new ForEachLoop(forToken, iteratorToken, iterationExpression, body, owner));
            }
            else
            {
                List <Executable> init = new List <Executable>();
                while (!tokens.PopIfPresent(";"))
                {
                    if (init.Count > 0)
                    {
                        tokens.PopExpected(",");
                    }
                    init.Add(Parse(parser, tokens, true, false, false, owner));
                }
                Expression condition = null;
                if (!tokens.PopIfPresent(";"))
                {
                    condition = ExpressionParser.Parse(tokens, owner);
                    tokens.PopExpected(";");
                }
                List <Executable> step = new List <Executable>();
                while (!tokens.PopIfPresent(")"))
                {
                    if (step.Count > 0)
                    {
                        tokens.PopExpected(",");
                    }
                    step.Add(Parse(parser, tokens, true, false, false, owner));
                }

                IList <Executable> body = Parser.ParseBlock(parser, tokens, false, owner);

                return(new ForLoop(forToken, init, condition, step, body, owner));
            }
        }
Exemplo n.º 2
0
        private void Initialize(Token annotationToken, TokenStream proxyTokenStream)
        {
            this.Token = annotationToken;

            Token token = proxyTokenStream.Pop();

            if (!Parser.IsValidIdentifier(token.Value))
            {
                throw new ParserException(annotationToken, "Invalid type");
            }

            this.Name = token.Value;

            while (proxyTokenStream.PopIfPresent("."))
            {
                this.Name += ".";
                this.Name += proxyTokenStream.PopValue();
            }

            List <AnnotatedType> generics = new List <AnnotatedType>();

            if (proxyTokenStream.PopIfPresent("<"))
            {
                while (proxyTokenStream.HasMore && !proxyTokenStream.IsNext(">"))
                {
                    if (generics.Count > 0 && !proxyTokenStream.PopIfPresent(","))
                    {
                        throw new ParserException(annotationToken, "Expected comma in generic list");
                    }

                    AnnotatedType genericItem = new AnnotatedType(annotationToken, proxyTokenStream);
                    generics.Add(genericItem);
                }

                if (!proxyTokenStream.PopIfPresent(">"))
                {
                    throw new ParserException(annotationToken, "Unclosed generic bracket.");
                }
            }

            this.Generics = generics.ToArray();
        }