Example #1
0
        private Expression ParseCallOrMemberTail(Expression ret)
        {
            while (true)
            {
                if (MaybeEat(TokenKind.LeftParenthesis))
                {
                    List<Arg> arguments = new List<Arg>();
                    if (!MaybeEat(TokenKind.RightParenthesis))
                    {
                        do
                        {
                            arguments.Add(ParseArg());
                        } while (MaybeEat(TokenKind.Comma));
                        Eat(TokenKind.RightParenthesis);
                    }

                    ret = new CallExpression(ret, arguments.ToArray());
                    ret.SetLoc(_globalParent, ret.StartIndex, GetEnd());
                }
                else if (MaybeEat(TokenKind.Dot))
                {
                    ret = new MemberExpression(ret, ((NameToken)NextToken()).Name);
                    ret.SetLoc(_globalParent, ret.StartIndex, GetEnd());
                }
                else if (MaybeEat(TokenKind.LeftBracket))
                {
                    ret = new IndexExpression(ret, new[] { ParseExpression() }); // TODO: make possible with multiple indexes
                    ret.SetLoc(_globalParent, ret.StartIndex, GetEnd());
                    Eat(TokenKind.RightBracket);
                }
                else
                    return ret;
            }
        }
Example #2
0
        // member_expression: primary_expression ( ( '.' identifier ) | ( '[' expression ']' ) )*
        private Expression ParseMemberExpression()
        {
            Expression ret = ParsePrimaryExpression();
            if (ret == null)
                return ret;

            while (PeekToken().Kind == TokenKind.LeftBracket || PeekToken().Kind == TokenKind.Dot)
            {
                var start = ret.StartIndex;
                if (MaybeEat(TokenKind.Dot))
                {
                    ret = new MemberExpression(ret, ((NameToken)NextToken()).Name);
                    ret.SetLoc(_globalParent, start, GetEnd());
                }
                else if (Eat(TokenKind.LeftBracket))
                {
                    ret = new IndexExpression(ret, new[] { ParseExpression() }); // TODO: make possible with multiple indexes
                    ret.SetLoc(_globalParent, start, GetEnd());
                    Eat(TokenKind.RightBracket);
                }
            }
            return ret;
        }