Example #1
0
        private ExpressionNode ParsePrimaryExpression()
        {
            Token tok = curtok;
            ExpressionNode result;	// TODO: handle error cases (result = null)
            switch (curtok.ID)
            {
                #region Literals
                case TokenID.Null:
                    result = new NullPrimitive(curtok);
                    Advance();
                    break;

                case TokenID.True:
                    result = new BooleanPrimitive(true, curtok);
                    Advance();
                    break;

                case TokenID.False:
                    result = new BooleanPrimitive(false, curtok);
                    Advance();
                    break;

                case TokenID.IntLiteral:
                    result = new IntegralPrimitive(strings[curtok.Data], IntegralType.Int, curtok);
                    Advance();
                    break;
                case TokenID.UIntLiteral:
                    result = new IntegralPrimitive(strings[curtok.Data], IntegralType.UInt, curtok);
                    Advance();
                    break;
                case TokenID.LongLiteral:
                    result = new IntegralPrimitive(strings[curtok.Data], IntegralType.Long, curtok);
                    Advance();
                    break;
                case TokenID.ULongLiteral:
                    result = new IntegralPrimitive(strings[curtok.Data], IntegralType.ULong, curtok);
                    Advance();
                    break;

                case TokenID.RealLiteral:
                    result = new RealPrimitive(strings[curtok.Data], curtok);
                    Advance();
                    break;

                case TokenID.CharLiteral:
                    result = new CharPrimitive(strings[curtok.Data], curtok);
                    Advance();
                    break;

                case TokenID.StringLiteral:
                    result = new StringPrimitive(strings[curtok.Data], curtok);
                    Advance();
                    break;
                #endregion

                #region Predefined Types

                case TokenID.Bool:
                case TokenID.Byte:
                case TokenID.Char:
                case TokenID.Decimal:
                case TokenID.Double:
                case TokenID.Float:
                case TokenID.Int:
                case TokenID.Long:
                case TokenID.Object:
                case TokenID.SByte:
                case TokenID.Short:
                case TokenID.String:
                case TokenID.UInt:
                case TokenID.ULong:
                case TokenID.UShort:
                    result = new PredefinedTypeNode(tok.ID, tok);
                    Advance();
                    result = ParseMemberAccess(result);
                    break;

                #endregion

                #region Keywords
                case TokenID.This:
                    {
                        Advance();
                        IdentifierExpression idExpr = new IdentifierExpression("this", tok);
                        idExpr.StartingPredefinedType = tok.ID;
                        result = idExpr;
                        break;
                    }

                case TokenID.Base:
                    Advance();   // over base
                    if (curtok.ID == TokenID.LBracket)         // base indexer expression
                    {
                        Advance();
                        result = new ElementAccessExpression(new IdentifierExpression("base", tok), ParseExpressionList());
                        AssertAndAdvance(TokenID.RBracket);
                        break;
                    }

                    if (curtok.ID != TokenID.Dot)
                    {
                        RecoverFromError(TokenID.Dot, TokenID.LBracket);
                        result = null;
                        break;
                    }

                    // base access expression
                    Advance();   // advance over dot
                    result = new BaseAccessExpression(ParseMember(false));
                    break;

                case TokenID.Checked:
                    Advance();
                    AssertAndAdvance(TokenID.LParen);
                    result = new CheckedExpression(ParseExpression());
                    AssertAndAdvance(TokenID.RParen);
                    break;

                case TokenID.Unchecked:
                    Advance();
                    AssertAndAdvance(TokenID.LParen);
                    result = new UncheckedExpression(ParseExpression());
                    AssertAndAdvance(TokenID.RParen);
                    break;

                case TokenID.Default:
                    Advance();
                    AssertAndAdvance(TokenID.LParen);
                    result = new DefaultConstantExpression(ParseType(false));
                    AssertAndAdvance(TokenID.RParen);
                    break;

                case TokenID.New:
                    Advance();

                    isNewStatement = true;

                    IType newType = ParseType(false);

                    //ApplyTypeParameters(newType); -> not sure, but a type pointer can not be generic!

                    if (curtok.ID == TokenID.LParen)
                    {
                        Advance();
                        result = new ObjectCreationExpression(newType, ParseArgumentList(), tok);
                        AssertAndAdvance(TokenID.RParen);
                    }
                    else if (curtok.ID == TokenID.LBracket)
                    {
                        result = ParseArrayCreation(newType);
                    }
                    else
                    {
                        RecoverFromError(TokenID.LParen, TokenID.LBracket);
                        result = null;
                    }

                    isNewStatement = false;
                    break;

                case TokenID.Typeof:
                    Advance();
                    AssertAndAdvance(TokenID.LParen);
                    result = new TypeOfExpression(ParseType(false));
                    CheckRankSpecifier(result);
                    AssertAndAdvance(TokenID.RParen);
                    break;

                case TokenID.Sizeof:
                    Advance();
                    AssertAndAdvance(TokenID.LParen);
                    result = new SizeOfExpression(ParseType(false));
                    AssertAndAdvance(TokenID.RParen);
                    break;

                case TokenID.Delegate:    //anonymous method
                    Advance();
                    result = ParseAnonymousMethod();
                    break;

                #endregion Keywords

                case TokenID.Ident:
                    result = ParseIdentifierOrKeyword(false, false, false, false, false);
                    if (curtok.ID == TokenID.ColonColon)
                    {
                        // id :: id type-parameter-list_opt . id type-parameter-list_opt

                        QualifiedIdentifierExpression qualID = new QualifiedIdentifierExpression(curtok);	// TODO: Replace by QualifiedAliasMember instance
                        qualID.IsNamespaceAliasQualifier = true;
                        qualID.Expressions.Add(result);

                        Advance(); // over ColonColon
                        qualID.Expressions.Add(ParseMember(false));

                        result = ParseMemberAccess(qualID);
                    }
                    else if (ParsePossibleTypeParameterNode(false, false, false, false))
                    {
                        result = new TypeNode(result);
                        ApplyTypeParameters((TypeNode)result);
                    }
                    break;

                case TokenID.LParen:
                    Advance();
                    result = ParseCastOrGroup();
                    break;

                default:
                    RecoverFromError("Unexpected token in primary expression");
                    return null;		// TODO: Invalid expression
            }
            return result;
        }
        public virtual object VisitUncheckedExpression(UncheckedExpression uncheckedExpression, object data)
        {
            stackMap.Push(uncheckedExpression);
             uncheckedExpression.Expression.AcceptVisitor(this, data);

             stackMap.Pop();
             return null;

        }