internal IsExpression(CsTokenList tokens, Expression value, LiteralExpression type)
     : base(ExpressionType.Is, tokens)
 {
     this.value = value;
     this.type = CodeParser.ExtractTypeTokenFromLiteralExpression(type);
     base.AddExpression(value);
     base.AddExpression(type);
 }
 internal MemberAccessExpression(CsTokenList tokens, Operator operatorType, Expression leftHandSide, LiteralExpression rightHandSide)
     : base(ExpressionType.MemberAccess, tokens)
 {
     this.operatorType = operatorType;
     this.leftHandSide = leftHandSide;
     this.rightHandSide = rightHandSide;
     base.AddExpression(leftHandSide);
     base.AddExpression(rightHandSide);
 }
        /// <summary>
        /// Initializes a new instance of the LabelStatement class.
        /// </summary>
        /// <param name="tokens">The list of tokens that form the statement.</param>
        /// <param name="identifier">The label identifier.</param>
        internal LabelStatement(CsTokenList tokens, LiteralExpression identifier)
            : base(StatementType.Label, tokens)
        {
            Param.AssertNotNull(tokens, "tokens");
            Param.AssertNotNull(identifier, "identifier");

            this.identifier = identifier;
            this.AddExpression(identifier);
        }
        /// <summary>
        /// Initializes a new instance of the DefaultValueExpression class.
        /// </summary>
        /// <param name="tokens">The list of tokens that form the expression.</param>
        /// <param name="type">The type to obtain the default value of.</param>
        internal DefaultValueExpression(CsTokenList tokens, LiteralExpression type)
            : base(ExpressionType.DefaultValue, tokens)
        {
            Param.AssertNotNull(tokens, "tokens");
            Param.AssertNotNull(type, "type");

            this.type = CodeParser.ExtractTypeTokenFromLiteralExpression(type);
            this.AddExpression(type);
        }
 internal AttributeExpression(CsTokenList tokens, LiteralExpression target, Expression initialization)
     : base(ExpressionType.Attribute, tokens)
 {
     this.target = target;
     if (target != null)
     {
         base.AddExpression(target);
     }
     this.initialization = initialization;
     base.AddExpression(initialization);
 }
 internal VariableDeclarationExpression(CsTokenList tokens, LiteralExpression type, ICollection<VariableDeclaratorExpression> declarators)
     : base(ExpressionType.VariableDeclaration, tokens)
 {
     this.declarators = declarators;
     base.AddExpression(type);
     this.type = CodeParser.ExtractTypeTokenFromLiteralExpression(type);
     foreach (VariableDeclaratorExpression expression in declarators)
     {
         base.AddExpression(expression);
         expression.ParentVariable = this;
     }
 }
        /// <summary>
        /// Initializes a new instance of the AsExpression class.
        /// </summary>
        /// <param name="tokens">The list of tokens that form the expression.</param>
        /// <param name="value">The value to convert.</param>
        /// <param name="type">The type of the conversion.</param>
        internal AsExpression(CsTokenList tokens, Expression value, LiteralExpression type)
            : base(ExpressionType.As, tokens)
        {
            Param.AssertNotNull(tokens, "tokens");
            Param.AssertNotNull(value, "value");
            Param.AssertNotNull(type, "type");

            this.value = value;
            this.type = CodeParser.ExtractTypeTokenFromLiteralExpression(type);

            this.AddExpression(value);
            this.AddExpression(type);
        }
        /// <summary>
        /// Initializes a new instance of the CastExpression class.
        /// </summary>
        /// <param name="tokens">The list of tokens that form the expression.</param>
        /// <param name="type">The cast type.</param>
        /// <param name="castedExpression">The expression being casted.</param>
        internal CastExpression(CsTokenList tokens, LiteralExpression type, Expression castedExpression)
            : base(ExpressionType.Cast, tokens)
        {
            Param.AssertNotNull(tokens, "tokens");
            Param.AssertNotNull(type, "type");
            Param.AssertNotNull(castedExpression, "castedExpression");

            this.type = CodeParser.ExtractTypeTokenFromLiteralExpression(type);
            this.castedExpression = castedExpression;

            this.AddExpression(type);
            this.AddExpression(castedExpression);
        }
        /// <summary>
        /// Initializes a new instance of the AttributeExpression class.
        /// </summary>
        /// <param name="tokens">The list of tokens that form the expression.</param>
        /// <param name="target">The attribute target, if any.</param>
        /// <param name="initialization">The attribute initialization call.</param>
        internal AttributeExpression(CsTokenList tokens, LiteralExpression target, Expression initialization)
            : base(ExpressionType.Attribute, tokens)
        {
            Param.AssertNotNull(tokens, "tokens");
            Param.Ignore(target);
            Param.AssertNotNull(initialization, "initialization");

            // Add the target expression.
            this.target = target;
            if (target != null)
            {
                this.AddExpression(target);
            }

            // Add the initialization expression.
            this.initialization = initialization;
            this.AddExpression(initialization);
        }
        /// <summary>
        /// Initializes a new instance of the MemberAccessExpression class.
        /// </summary>
        /// <param name="tokens">The list of tokens that form the expression.</param>
        /// <param name="operatorType">The type of opertion being performed.</param>
        /// <param name="leftHandSide">The left side of the operation.</param>
        /// <param name="rightHandSide">The member being accessed.</param>
        internal MemberAccessExpression(
            CsTokenList tokens,
            Operator operatorType,
            Expression leftHandSide, 
            LiteralExpression rightHandSide)
            : base(ExpressionType.MemberAccess, tokens)
        {
            Param.AssertNotNull(tokens, "tokens");
            Param.Ignore(operatorType);
            Param.AssertNotNull(leftHandSide, "leftHandSide");
            Param.AssertNotNull(rightHandSide, "rightHandSide");

            this.operatorType = operatorType;
            this.leftHandSide = leftHandSide;
            this.rightHandSide = rightHandSide;

            this.AddExpression(leftHandSide);
            this.AddExpression(rightHandSide);
        }
        /// <summary>
        /// Initializes a new instance of the EventDeclaratorExpression class.
        /// </summary>
        /// <param name="tokens">The list of tokens that form the statement.</param>
        /// <param name="identifier">The identifier name of the event.</param>
        /// <param name="initializer">The initialization expression for the event.</param>
        internal EventDeclaratorExpression(
            CsTokenList tokens,
            LiteralExpression identifier,
            Expression initializer)
            : base(ExpressionType.EventDeclarator, tokens)
        {
            Param.AssertNotNull(tokens, "tokens");
            Param.AssertNotNull(identifier, "identifier");
            Param.Ignore(initializer);

            this.identifier = identifier;
            this.initializer = initializer;

            this.AddExpression(identifier);

            if (initializer != null)
            {
                this.AddExpression(initializer);
            }
        }
        /// <summary>
        /// Initializes a new instance of the VariableDeclarationExpression class.
        /// </summary>
        /// <param name="tokens">The list of tokens that form the statement.</param>
        /// <param name="type">The type of the variable or variables being declared.</param>
        /// <param name="declarators">The list of declarators in the expression.</param>
        internal VariableDeclarationExpression(
            CsTokenList tokens, 
            LiteralExpression type, 
            ICollection<VariableDeclaratorExpression> declarators)
            : base(ExpressionType.VariableDeclaration, tokens)
        {
            Param.AssertNotNull(tokens, "tokens");
            Param.AssertNotNull(type, "type");
            Param.AssertNotNull(declarators, "declarators");

            this.declarators = declarators;

            Debug.Assert(declarators.IsReadOnly, "The declarators collection should be read-only.");

            this.AddExpression(type);
            this.type = CodeParser.ExtractTypeTokenFromLiteralExpression(type);

            foreach (VariableDeclaratorExpression expression in declarators)
            {
                this.AddExpression(expression);
                expression.ParentVariable = this;
            }
        }
        /// <summary>
        /// Reads an expression starting with an unknown word.
        /// </summary>
        /// <returns>Returns the expression.</returns>
        private LiteralExpression GetConditionalPreprocessorConstantExpression()
        {
            // Get the first symbol.
            Symbol symbol = this.symbols.Peek(1);
            Debug.Assert(symbol != null && symbol.SymbolType == SymbolType.Other, "Expected a text symbol");

            var expressionReference = new Reference<ICodePart>();

            // Convert the symbol to a token.
            this.symbols.Advance();
            CsToken literalToken = new CsToken(symbol.Text, CsTokenType.Other, symbol.Location, expressionReference, this.symbols.Generated);
            Node<CsToken> literalTokenNode = this.tokens.InsertLast(literalToken);

            // Create a literal expression from this token.
            var expression = new LiteralExpression(this.tokens, literalTokenNode);
            expressionReference.Target = expression;

            return expression;
        }
        /// <summary>
        /// Converts the given expression into a literal expression containing a type token.
        /// </summary>
        /// <param name="expression">The expression to convert.</param>
        /// <returns>Returns the converted expression.</returns>
        private LiteralExpression ConvertTypeExpression(Expression expression)
        {
            Param.AssertNotNull(expression, "expression");

            // Get the first and last token in the expression.
            Node<CsToken> firstTokenNode = expression.Tokens.First;
            Node<CsToken> lastTokenNode = expression.Tokens.Last;

            var typeTokenReference = new Reference<ICodePart>();
            var expressionReference = new Reference<ICodePart>();

            // Create a new token list containing these tokens.
            List<CsToken> tokenList = new List<CsToken>();
            foreach (CsToken token in expression.Tokens)
            {
                tokenList.Add(token);
                token.ParentRef = typeTokenReference;
            }

            // Remove the extra tokens from the master list.
            if (firstTokenNode != null && expression.Tokens.First != null)
            {
                Node<CsToken> temp = firstTokenNode.Next;
                if (!expression.Tokens.OutOfBounds(temp))
                {
                    this.tokens.RemoveRange(temp, expression.Tokens.Last);
                }
            }

            // Add the child tokens to a token list.
            MasterList<CsToken> childTokens = new MasterList<CsToken>(tokenList);

            // Create the new type token.
            TypeToken typeToken = new TypeToken(
                childTokens, CsToken.JoinLocations(firstTokenNode, lastTokenNode), expressionReference,  firstTokenNode.Value.Generated);
            typeTokenReference.Target = typeToken;

            // Insert the new token.
            Node<CsToken> typeTokenNode = this.tokens.Replace(firstTokenNode, typeToken);

            // Create the literal expression.
            var literalExpression = new LiteralExpression(
                new CsTokenList(this.tokens, firstTokenNode, firstTokenNode),
                typeTokenNode);

            expressionReference.Target = literalExpression;

            return literalExpression;
        }
        private Expression GetNextExpression(
            ExpressionPrecedence previousPrecedence, 
            Reference<ICodePart> parentReference,
            bool unsafeCode, 
            bool allowVariableDeclaration, 
            bool typeExpression)
        {
            Param.Ignore(previousPrecedence);
            Param.AssertNotNull(parentReference, "parentReference");
            Param.Ignore(unsafeCode);
            Param.Ignore(allowVariableDeclaration);
            Param.Ignore(typeExpression);

            // Saves the next expression.
            Expression expression = null;

            // Get the next symbol.
            Symbol symbol = this.GetNextSymbol(parentReference);

            if (symbol != null)
            {
                switch (symbol.SymbolType)
                {
                    case SymbolType.Other:
                        if (this.IsLambdaExpression())
                        {
                            expression = this.GetLambdaExpression(parentReference, unsafeCode);
                        }
                        else if (this.IsQueryExpression(unsafeCode))
                        {
                            expression = this.GetQueryExpression(parentReference, unsafeCode);
                        }

                        // If the expression is still null now, this is just a regular 'other' expression.
                        if (expression == null)
                        {
                            expression = this.GetOtherExpression(parentReference, allowVariableDeclaration, unsafeCode);
                        }

                        break;

                    case SymbolType.Checked:
                        expression = this.GetCheckedExpression(parentReference, unsafeCode);
                        break;

                    case SymbolType.Unchecked:
                        expression = this.GetUncheckedExpression(parentReference, unsafeCode);
                        break;

                    case SymbolType.New:
                        expression = this.GetNewAllocationExpression(parentReference, unsafeCode);
                        break;

                    case SymbolType.Stackalloc:
                        expression = this.GetStackallocExpression(parentReference, unsafeCode);
                        break;

                    case SymbolType.Sizeof:
                        expression = this.GetSizeofExpression(parentReference, unsafeCode);
                        break;

                    case SymbolType.Typeof:
                        expression = this.GetTypeofExpression(parentReference, unsafeCode);
                        break;

                    case SymbolType.Default:
                        expression = this.GetDefaultValueExpression(parentReference, unsafeCode);
                        break;

                    case SymbolType.Delegate:
                        expression = this.GetAnonymousMethodExpression(parentReference, unsafeCode);
                        break;

                    case SymbolType.Increment:
                        if (this.IsUnaryExpression())
                        {
                            expression = this.GetUnaryIncrementExpression(unsafeCode);
                        }

                        break;

                    case SymbolType.Decrement:
                        if (this.IsUnaryExpression())
                        {
                            expression = this.GetUnaryDecrementExpression(unsafeCode);
                        }

                        break;

                    case SymbolType.Plus:
                    case SymbolType.Minus:
                        if (this.IsUnaryExpression())
                        {
                            expression = this.GetUnaryExpression(parentReference, unsafeCode);
                        }

                        break;

                    case SymbolType.Not:
                    case SymbolType.Tilde:
                        expression = this.GetUnaryExpression(parentReference, unsafeCode);
                        break;

                    case SymbolType.OpenParenthesis:
                        if (this.IsLambdaExpression())
                        {
                            expression = this.GetLambdaExpression(parentReference, unsafeCode);
                        }
                        else
                        {
                            expression = this.GetOpenParenthesisExpression(previousPrecedence, unsafeCode);
                        }

                        break;

                    case SymbolType.Number:
                        var numberExpressionReference = new Reference<ICodePart>();
                        Node<CsToken> tokenNode = this.tokens.InsertLast(this.GetToken(CsTokenType.Number, SymbolType.Number, numberExpressionReference));
                        expression = new LiteralExpression(new CsTokenList(this.tokens, tokenNode, tokenNode), tokenNode);
                        numberExpressionReference.Target = expression;
                        break;

                    case SymbolType.String:
                        var stringExpressionReference = new Reference<ICodePart>();
                        tokenNode = this.tokens.InsertLast(this.GetToken(CsTokenType.String, SymbolType.String, stringExpressionReference));
                        expression = new LiteralExpression(new CsTokenList(this.tokens, tokenNode, tokenNode), tokenNode);
                        stringExpressionReference.Target = expression;
                        break;

                    case SymbolType.True:
                        var trueExpressionReference = new Reference<ICodePart>();
                        tokenNode = this.tokens.InsertLast(this.GetToken(CsTokenType.True, SymbolType.True, trueExpressionReference));
                        expression = new LiteralExpression(new CsTokenList(this.tokens, tokenNode, tokenNode), tokenNode);
                        trueExpressionReference.Target = expression;
                        break;

                    case SymbolType.False:
                        var falseExpressionReference = new Reference<ICodePart>();
                        tokenNode = this.tokens.InsertLast(this.GetToken(CsTokenType.False, SymbolType.False, falseExpressionReference));
                        expression = new LiteralExpression(new CsTokenList(this.tokens, tokenNode, tokenNode), tokenNode);
                        falseExpressionReference.Target = expression;
                        break;

                    case SymbolType.Null:
                        var nullExpressionReference = new Reference<ICodePart>();
                        tokenNode = this.tokens.InsertLast(this.GetToken(CsTokenType.Null, SymbolType.Null, nullExpressionReference));
                        expression = new LiteralExpression(new CsTokenList(this.tokens, tokenNode, tokenNode), tokenNode);
                        nullExpressionReference.Target = expression;
                        break;

                    case SymbolType.This:
                        var thisExpressionReference = new Reference<ICodePart>();
                        tokenNode = this.tokens.InsertLast(this.GetToken(CsTokenType.This, SymbolType.This, thisExpressionReference));
                        expression = new LiteralExpression(new CsTokenList(this.tokens, tokenNode, tokenNode), tokenNode);
                        thisExpressionReference.Target = expression;
                        break;

                    case SymbolType.Base:
                        var baseExpressionReference = new Reference<ICodePart>();
                        tokenNode = this.tokens.InsertLast(this.GetToken(CsTokenType.Base, SymbolType.Base, baseExpressionReference));
                        expression = new LiteralExpression(new CsTokenList(this.tokens, tokenNode, tokenNode), tokenNode);
                        baseExpressionReference.Target = expression;
                        break;

                    case SymbolType.Multiplication:
                        if (!unsafeCode)
                        {
                            goto default;
                        }

                        expression = this.GetUnsafeAccessExpression(parentReference, unsafeCode);
                        break;

                    case SymbolType.LogicalAnd:
                        if (!unsafeCode)
                        {
                            goto default;
                        }

                        expression = this.GetUnsafeAccessExpression(parentReference, unsafeCode);
                        break;

                    default:
                        throw new SyntaxException(this.document.SourceCode, symbol.LineNumber);
                }
            }

            // Gather up all extensions to this expression.
            while (expression != null)
            {
                var expressionReference = new Reference<ICodePart>(expression);

                // Check if there is an extension to this expression.
                Expression extension = this.GetExpressionExtension(expression, previousPrecedence, expressionReference, unsafeCode, typeExpression, allowVariableDeclaration);
                if (extension != null)
                {
                    // The larger expression is what we want to return here.
                    expression = extension;
                }
                else
                {
                    // There are no more extensions.
                    break;
                }
            }

            // Return the expression.
            return expression;
        }
        /// <summary>
        /// Reads an expression starting with an unknown word.
        /// </summary>
        /// <param name="parentReference">The parent code part.</param>
        /// <param name="unsafeCode">Indicates whether the code is marked as unsafe.</param>
        /// <returns>Returns the expression.</returns>
        private LiteralExpression GetLiteralExpression(Reference<ICodePart> parentReference, bool unsafeCode)
        {
            Param.Ignore(unsafeCode);
            Param.AssertNotNull(parentReference, "parentReference");

            // Get the first symbol.
            Symbol symbol = this.GetNextSymbol(parentReference);

            // Create a reference to the literal expression we're creating.
            var expressionReference = new Reference<ICodePart>();

            // First, check if this is a generic.
            CsToken literalToken = null;
            int temp;
            bool generic = false;
            if (symbol.SymbolType == SymbolType.Other && this.HasTypeSignature(1, false, out temp, out generic) && generic)
            {
                literalToken = this.GetGenericToken(expressionReference, unsafeCode);
            }

            if (literalToken == null)
            {
                // This is not a generic. Just convert the symbol to a token.
                literalToken = this.GetToken(CsTokenType.Other, SymbolType.Other, expressionReference);
            }

            // Add the token to the document.
            Node<CsToken> literalTokenNode = this.tokens.InsertLast(literalToken);

            // Create a literal expression from this token.
            var expression = new LiteralExpression(this.tokens, literalTokenNode);
            expressionReference.Target = expression;

            return expression;
        }
Exemple #17
0
 private GotoStatement ParseGotoStatement(bool unsafeCode)
 {
     CsToken item = this.GetToken(CsTokenType.Goto, SymbolType.Goto);
     Microsoft.StyleCop.Node<CsToken> firstItemNode = this.tokens.InsertLast(item);
     Symbol nextSymbol = this.GetNextSymbol();
     Expression identifier = null;
     if (nextSymbol.SymbolType == SymbolType.Default)
     {
         Microsoft.StyleCop.Node<CsToken> tokenNode = this.tokens.InsertLast(this.GetToken(CsTokenType.Other, SymbolType.Default));
         identifier = new LiteralExpression(new CsTokenList(this.tokens, tokenNode, tokenNode), tokenNode);
     }
     else if (nextSymbol.SymbolType == SymbolType.Case)
     {
         this.tokens.Add(this.GetToken(CsTokenType.Other, SymbolType.Case));
         identifier = this.GetNextExpression(ExpressionPrecedence.None, unsafeCode);
     }
     else
     {
         identifier = this.GetNextExpression(ExpressionPrecedence.None, unsafeCode);
     }
     this.tokens.Add(this.GetToken(CsTokenType.Semicolon, SymbolType.Semicolon));
     return new GotoStatement(new CsTokenList(this.tokens, firstItemNode, firstItemNode), identifier);
 }
Exemple #18
0
 private Constructor ParseConstructor(CsElement parent, bool unsafeCode, bool generated, XmlHeader xmlHeader, ICollection<Microsoft.StyleCop.CSharp.Attribute> attributes)
 {
     Microsoft.StyleCop.Node<CsToken> last = this.tokens.Last;
     AccessModifierType @private = AccessModifierType.Private;
     Dictionary<CsTokenType, CsToken> elementModifiers = this.GetElementModifiers(ref @private, ConstructorModifiers);
     unsafeCode |= elementModifiers.ContainsKey(CsTokenType.Unsafe);
     CsToken elementNameToken = this.GetElementNameToken(unsafeCode);
     this.tokens.Add(elementNameToken);
     IList<Parameter> parameters = this.ParseParameterList(unsafeCode, SymbolType.OpenParenthesis);
     MethodInvocationExpression initializerExpression = null;
     if (this.GetNextSymbol().SymbolType == SymbolType.Colon)
     {
         this.tokens.Add(this.GetToken(CsTokenType.BaseColon, SymbolType.Colon));
         Symbol nextSymbol = this.GetNextSymbol();
         if ((nextSymbol.SymbolType != SymbolType.This) && (nextSymbol.SymbolType != SymbolType.Base))
         {
             throw this.CreateSyntaxException();
         }
         Microsoft.StyleCop.Node<CsToken> tokenNode = this.tokens.InsertLast(this.GetToken(CsTokenType.Other, nextSymbol.SymbolType));
         LiteralExpression methodName = new LiteralExpression(this.tokens, tokenNode);
         initializerExpression = this.GetMethodInvocationExpression(methodName, ExpressionPrecedence.None, unsafeCode);
     }
     Microsoft.StyleCop.Node<CsToken> firstItemNode = (last == null) ? this.tokens.First : last.Next;
     CsTokenList tokens = new CsTokenList(this.tokens, firstItemNode, this.tokens.Last);
     Declaration declaration = new Declaration(tokens, elementNameToken.Text, ElementType.Constructor, @private, elementModifiers);
     Constructor element = new Constructor(this.document, parent, xmlHeader, attributes, declaration, parameters, initializerExpression, unsafeCode, generated);
     if (elementModifiers.ContainsKey(CsTokenType.Extern))
     {
         this.tokens.Add(this.GetToken(CsTokenType.Semicolon, SymbolType.Semicolon));
         return element;
     }
     this.ParseStatementContainer(element, true, unsafeCode);
     return element;
 }
Exemple #19
0
        private Expression GetNextExpression(ExpressionPrecedence previousPrecedence, bool unsafeCode, bool allowVariableDeclaration, bool typeExpression)
        {
            Expression leftSide = null;
            Symbol nextSymbol = this.GetNextSymbol();
            if (nextSymbol != null)
            {
                Microsoft.StyleCop.Node<CsToken> node;
                switch (nextSymbol.SymbolType)
                {
                    case SymbolType.Not:
                    case SymbolType.Tilde:
                        leftSide = this.GetUnaryExpression(unsafeCode);
                        goto Label_03B9;

                    case SymbolType.Base:
                        node = this.tokens.InsertLast(this.GetToken(CsTokenType.Base, SymbolType.Base));
                        leftSide = new LiteralExpression(new CsTokenList(this.tokens, node, node), node);
                        goto Label_03B9;

                    case SymbolType.Plus:
                    case SymbolType.Minus:
                        if (this.IsUnaryExpression())
                        {
                            leftSide = this.GetUnaryExpression(unsafeCode);
                        }
                        goto Label_03B9;

                    case SymbolType.PlusEquals:
                    case SymbolType.MinusEquals:
                    case SymbolType.Static:
                    case SymbolType.Struct:
                    case SymbolType.Switch:
                    case SymbolType.Throw:
                    case SymbolType.Try:
                    case SymbolType.WhiteSpace:
                    case SymbolType.EndOfLine:
                        goto Label_0390;

                    case SymbolType.Multiplication:
                        if (!unsafeCode)
                        {
                            goto Label_0390;
                        }
                        leftSide = this.GetUnsafeAccessExpression(unsafeCode);
                        goto Label_03B9;

                    case SymbolType.OpenParenthesis:
                        if (this.IsLambdaExpression())
                        {
                            leftSide = this.GetLambdaExpression(unsafeCode);
                        }
                        else
                        {
                            leftSide = this.GetOpenParenthesisExpression(unsafeCode);
                        }
                        goto Label_03B9;

                    case SymbolType.Increment:
                        if (this.IsUnaryExpression())
                        {
                            leftSide = this.GetUnaryIncrementExpression(unsafeCode);
                        }
                        goto Label_03B9;

                    case SymbolType.Decrement:
                        if (this.IsUnaryExpression())
                        {
                            leftSide = this.GetUnaryDecrementExpression(unsafeCode);
                        }
                        goto Label_03B9;

                    case SymbolType.LogicalAnd:
                        if (!unsafeCode)
                        {
                            goto Label_0390;
                        }
                        leftSide = this.GetUnsafeAccessExpression(unsafeCode);
                        goto Label_03B9;

                    case SymbolType.Default:
                        leftSide = this.GetDefaultValueExpression(unsafeCode);
                        goto Label_03B9;

                    case SymbolType.Delegate:
                        leftSide = this.GetAnonymousMethodExpression(unsafeCode);
                        goto Label_03B9;

                    case SymbolType.False:
                        node = this.tokens.InsertLast(this.GetToken(CsTokenType.False, SymbolType.False));
                        leftSide = new LiteralExpression(new CsTokenList(this.tokens, node, node), node);
                        goto Label_03B9;

                    case SymbolType.Checked:
                        leftSide = this.GetCheckedExpression(unsafeCode);
                        goto Label_03B9;

                    case SymbolType.New:
                        leftSide = this.GetNewAllocationExpression(unsafeCode);
                        goto Label_03B9;

                    case SymbolType.Null:
                        node = this.tokens.InsertLast(this.GetToken(CsTokenType.Null, SymbolType.Null));
                        leftSide = new LiteralExpression(new CsTokenList(this.tokens, node, node), node);
                        goto Label_03B9;

                    case SymbolType.Sizeof:
                        leftSide = this.GetSizeofExpression(unsafeCode);
                        goto Label_03B9;

                    case SymbolType.Stackalloc:
                        leftSide = this.GetStackallocExpression(unsafeCode);
                        goto Label_03B9;

                    case SymbolType.This:
                        node = this.tokens.InsertLast(this.GetToken(CsTokenType.This, SymbolType.This));
                        leftSide = new LiteralExpression(new CsTokenList(this.tokens, node, node), node);
                        goto Label_03B9;

                    case SymbolType.True:
                        node = this.tokens.InsertLast(this.GetToken(CsTokenType.True, SymbolType.True));
                        leftSide = new LiteralExpression(new CsTokenList(this.tokens, node, node), node);
                        goto Label_03B9;

                    case SymbolType.Typeof:
                        leftSide = this.GetTypeofExpression(unsafeCode);
                        goto Label_03B9;

                    case SymbolType.Unchecked:
                        leftSide = this.GetUncheckedExpression(unsafeCode);
                        goto Label_03B9;

                    case SymbolType.Other:
                        if (!this.IsLambdaExpression())
                        {
                            if (this.IsQueryExpression(unsafeCode))
                            {
                                leftSide = this.GetQueryExpression(unsafeCode);
                            }
                            break;
                        }
                        leftSide = this.GetLambdaExpression(unsafeCode);
                        break;

                    case SymbolType.String:
                        node = this.tokens.InsertLast(this.GetToken(CsTokenType.String, SymbolType.String));
                        leftSide = new LiteralExpression(new CsTokenList(this.tokens, node, node), node);
                        goto Label_03B9;

                    case SymbolType.Number:
                        node = this.tokens.InsertLast(this.GetToken(CsTokenType.Number, SymbolType.Number));
                        leftSide = new LiteralExpression(new CsTokenList(this.tokens, node, node), node);
                        goto Label_03B9;

                    default:
                        goto Label_0390;
                }
                if (leftSide == null)
                {
                    leftSide = this.GetOtherExpression(allowVariableDeclaration, unsafeCode);
                }
            }
            goto Label_03B9;
            Label_0390:
            throw new SyntaxException(this.document.SourceCode, nextSymbol.LineNumber);
            Label_03B9:
            while (leftSide != null)
            {
                Expression expression2 = this.GetExpressionExtension(leftSide, previousPrecedence, unsafeCode, typeExpression, allowVariableDeclaration);
                if (expression2 == null)
                {
                    return leftSide;
                }
                leftSide = expression2;
            }
            return leftSide;
        }
Exemple #20
0
        private Expression GetNextConditionalPreprocessorExpression(SourceCode sourceCode, ExpressionPrecedence previousPrecedence)
        {
            this.AdvanceToNextConditionalDirectiveCodeSymbol();
            Expression leftSide = null;
            Symbol symbol = this.symbols.Peek(1);
            if (symbol != null)
            {
                CsToken token;
                Microsoft.StyleCop.Node<CsToken> node;
                SymbolType symbolType = symbol.SymbolType;
                if (symbolType <= SymbolType.Not)
                {
                    switch (symbolType)
                    {
                        case SymbolType.OpenParenthesis:
                            leftSide = this.GetConditionalPreprocessorParenthesizedExpression(sourceCode);
                            goto Label_012D;

                        case SymbolType.Not:
                            leftSide = this.GetConditionalPreprocessorNotExpression(sourceCode);
                            goto Label_012D;
                    }
                    goto Label_010E;
                }
                switch (symbolType)
                {
                    case SymbolType.False:
                        this.symbols.Advance();
                        token = new CsToken(symbol.Text, CsTokenType.False, symbol.Location, this.symbols.Generated);
                        node = this.tokens.InsertLast(token);
                        leftSide = new LiteralExpression(new CsTokenList(this.tokens, node, node), node);
                        goto Label_012D;

                    case SymbolType.True:
                        this.symbols.Advance();
                        token = new CsToken(symbol.Text, CsTokenType.True, symbol.Location, this.symbols.Generated);
                        node = this.tokens.InsertLast(token);
                        leftSide = new LiteralExpression(new CsTokenList(this.tokens, node, node), node);
                        goto Label_012D;
                }
                if (symbolType != SymbolType.Other)
                {
                    goto Label_010E;
                }
                leftSide = this.GetConditionalPreprocessorConstantExpression();
            }
            goto Label_012D;
            Label_010E:
            throw new SyntaxException(sourceCode, symbol.LineNumber);
            Label_012D:
            while (leftSide != null)
            {
                Expression expression2 = this.GetConditionalPreprocessorExpressionExtension(sourceCode, leftSide, previousPrecedence);
                if (expression2 == null)
                {
                    return leftSide;
                }
                leftSide = expression2;
            }
            return leftSide;
        }
 internal LabelStatement(CsTokenList tokens, LiteralExpression identifier)
     : base(StatementType.Label, tokens)
 {
     this.identifier = identifier;
     base.AddExpression(identifier);
 }
        /// <summary>
        /// Reads the next goto-statement from the file and returns it.
        /// </summary>
        /// <param name="parentReference">The parent code unit.</param>
        /// <param name="unsafeCode">Indicates whether the code being parsed resides in an unsafe code block.</param>
        /// <returns>Returns the statement.</returns>
        private GotoStatement ParseGotoStatement(Reference<ICodePart> parentReference, bool unsafeCode)
        {
            Param.AssertNotNull(parentReference, "parentReference");
            Param.Ignore(unsafeCode);

            var statementReference = new Reference<ICodePart>();

            // Move past the goto keyword.
            CsToken firstToken = this.GetToken(CsTokenType.Goto, SymbolType.Goto, parentReference, statementReference);
            Node<CsToken> firstTokenNode = this.tokens.InsertLast(firstToken);

            // Get the next symbol.
            Symbol symbol = this.GetNextSymbol(statementReference);

            Expression identifier = null;
            if (symbol.SymbolType == SymbolType.Default)
            {
                Node<CsToken> tokenNode = this.tokens.InsertLast(this.GetToken(CsTokenType.Other, SymbolType.Default, statementReference));
                identifier = new LiteralExpression(new CsTokenList(this.tokens, tokenNode, tokenNode), tokenNode);
            }
            else if (symbol.SymbolType == SymbolType.Case)
            {
                this.tokens.Add(this.GetToken(CsTokenType.Other, SymbolType.Case, statementReference));
                identifier = this.GetNextExpression(ExpressionPrecedence.None, statementReference, unsafeCode);
            }
            else
            {
                identifier = this.GetNextExpression(ExpressionPrecedence.None, statementReference, unsafeCode);
            }

            // Get the closing semicolon.
            this.tokens.Add(this.GetToken(CsTokenType.Semicolon, SymbolType.Semicolon, statementReference));

            // Create the token list for the statement.
            CsTokenList partialTokens = new CsTokenList(this.tokens, firstTokenNode, firstTokenNode);

            // Create and return the goto-statement.
            var statement = new GotoStatement(partialTokens, identifier);
            statementReference.Target = statement;

            return statement;
        }
 internal TypeofExpression(CsTokenList tokens, LiteralExpression type)
     : base(ExpressionType.Typeof, tokens)
 {
     this.type = CodeParser.ExtractTypeTokenFromLiteralExpression(type);
     base.AddExpression(type);
 }
 internal DefaultValueExpression(CsTokenList tokens, LiteralExpression type)
     : base(ExpressionType.DefaultValue, tokens)
 {
     this.type = CodeParser.ExtractTypeTokenFromLiteralExpression(type);
     base.AddExpression(type);
 }
        /// <summary>
        /// Parses and returns a constructor.
        /// </summary>
        /// <param name="parent">The parent of the element.</param>
        /// <param name="elementReference">A reference to the element being created.</param>
        /// <param name="unsafeCode">Indicates whether the code is marked as unsafe.</param>
        /// <param name="generated">Indicates whether the code is marked as generated code.</param>
        /// <param name="xmlHeader">The element's documentation header.</param>
        /// <param name="attributes">The attributes on the element.</param>
        /// <returns>Returns the element.</returns>
        private Constructor ParseConstructor(
            CsElement parent, 
            Reference<ICodePart> elementReference,
            bool unsafeCode, 
            bool generated, 
            XmlHeader xmlHeader, 
            ICollection<Attribute> attributes)
        {
            Param.AssertNotNull(parent, "parent");
            Param.AssertNotNull(elementReference, "elementReference");
            Param.Ignore(unsafeCode);
            Param.Ignore(generated);
            Param.Ignore(xmlHeader);
            Param.Ignore(attributes);

            Node<CsToken> previousTokenNode = this.tokens.Last;

            // Get the modifiers and access.
            AccessModifierType accessModifier = AccessModifierType.Private;
            Dictionary<CsTokenType, CsToken> modifiers = this.GetElementModifiers(elementReference, ref accessModifier, ConstructorModifiers);

            unsafeCode |= modifiers.ContainsKey(CsTokenType.Unsafe);

            // Get the name of the constructor.
            CsToken name = this.GetElementNameToken(elementReference, unsafeCode);
            this.tokens.Add(name);

            // Get the parameter list.
            IList<Parameter> parameters = this.ParseParameterList(elementReference, unsafeCode, SymbolType.OpenParenthesis);

            // Get the constructor initializer if there is one.
            MethodInvocationExpression constructorInitializer = null;

            Symbol symbol = this.GetNextSymbol(elementReference);
            if (symbol.SymbolType == SymbolType.Colon)
            {
                this.tokens.Add(this.GetToken(CsTokenType.BaseColon, SymbolType.Colon, elementReference));

                // The next symbol must be the keyword base or this.
                symbol = this.GetNextSymbol(elementReference);
                if (symbol.SymbolType != SymbolType.This && symbol.SymbolType != SymbolType.Base)
                {
                    throw this.CreateSyntaxException();
                }

                var initializerNameExpressionReference = new Reference<ICodePart>();
                Node<CsToken> initializerNameTokenNode = this.tokens.InsertLast(this.GetToken(CsTokenType.Other, symbol.SymbolType, initializerNameExpressionReference));

                // Get the name expression.
                LiteralExpression initializerNameExpression = new LiteralExpression(this.tokens, initializerNameTokenNode);
                initializerNameExpressionReference.Target = initializerNameExpression;

                // Get the initializer expression.
                constructorInitializer = this.GetMethodInvocationExpression(
                    initializerNameExpression, ExpressionPrecedence.None, unsafeCode);
            }

            // Create the declaration.
            Node<CsToken> firstTokenNode = previousTokenNode == null ? this.tokens.First : previousTokenNode.Next;
            CsTokenList declarationTokens = new CsTokenList(this.tokens, firstTokenNode, this.tokens.Last);
            Declaration declaration = new Declaration(
                declarationTokens, name.Text, ElementType.Constructor, accessModifier, modifiers);

            Constructor constructor = new Constructor(
                this.document,
                parent,
                xmlHeader,
                attributes,
                declaration,
                parameters,
                constructorInitializer,
                unsafeCode,
                generated);

            elementReference.Target = constructor;

            // If the constructor is extern, it will not have a body.
            if (modifiers.ContainsKey(CsTokenType.Extern))
            {
                // Get the closing semicolon.
                this.tokens.Add(this.GetToken(CsTokenType.Semicolon, SymbolType.Semicolon, elementReference));
            }
            else
            {
                // Get the body.
                this.ParseStatementContainer(constructor, true, unsafeCode);
            }

            return constructor;
        }
        /// <summary>
        /// Reads the next expression from a conditional preprocessor directive.
        /// </summary>
        /// <param name="sourceCode">The source code.</param>
        /// <param name="previousPrecedence">The precedence of the expression just before this one.</param>
        /// <returns>Returns the expression.</returns>
        private Expression GetNextConditionalPreprocessorExpression(SourceCode sourceCode, ExpressionPrecedence previousPrecedence)
        {
            Param.AssertNotNull(sourceCode, "sourceCode");
            Param.Ignore(previousPrecedence);

            var parentReference = new Reference<ICodePart>();

            // Move past comments and whitepace.
            this.AdvanceToNextConditionalDirectiveCodeSymbol(parentReference);

            // Saves the next expression.
            Expression expression = null;

            // Get the next symbol.
            Symbol symbol = this.symbols.Peek(1);
            if (symbol != null)
            {
                switch (symbol.SymbolType)
                {
                    case SymbolType.Other:
                        expression = this.GetConditionalPreprocessorConstantExpression();
                        break;

                    case SymbolType.Not:
                        expression = this.GetConditionalPreprocessorNotExpression(sourceCode, parentReference);
                        break;

                    case SymbolType.OpenParenthesis:
                        expression = this.GetConditionalPreprocessorParenthesizedExpression(sourceCode, parentReference);
                        break;

                    case SymbolType.False:
                        this.symbols.Advance();

                        var falseExpressionReference = new Reference<ICodePart>();
                        CsToken token = new CsToken(symbol.Text, CsTokenType.False, symbol.Location, falseExpressionReference, this.symbols.Generated);
                        Node<CsToken> tokenNode = this.tokens.InsertLast(token);
                        expression = new LiteralExpression(new CsTokenList(this.tokens, tokenNode, tokenNode), tokenNode);
                        falseExpressionReference.Target = expression;
                        break;

                    case SymbolType.True:
                        this.symbols.Advance();

                        var trueExpressionReference = new Reference<ICodePart>();
                        token = new CsToken(symbol.Text, CsTokenType.True, symbol.Location, trueExpressionReference, this.symbols.Generated);
                        tokenNode = this.tokens.InsertLast(token);
                        expression = new LiteralExpression(new CsTokenList(this.tokens, tokenNode, tokenNode), tokenNode);
                        trueExpressionReference.Target = expression;
                        break;

                    default:
                        throw new SyntaxException(sourceCode, symbol.LineNumber);
                }
            }

            // Gather up all extensions to this expression.
            while (expression != null)
            {
                var expressionReference = new Reference<ICodePart>(expression);

                // Check if there is an extension to this expression.
                Expression extension = this.GetConditionalPreprocessorExpressionExtension(
                    sourceCode, expressionReference, expression, previousPrecedence);
                if (extension != null)
                {
                    // The larger expression is what we want to return here.
                    expression = extension;
                }
                else
                {
                    // There are no more extensions.
                    break;
                }
            }

            // Return the expression.
            return expression;
        }
Exemple #27
0
        /// <summary>
        /// Extracts a TypeToken from the literal expression, assuming that one exists.
        /// </summary>
        /// <param name="literal">The literal expression.</param>
        /// <returns>Returns the type token.</returns>
        internal static TypeToken ExtractTypeTokenFromLiteralExpression(LiteralExpression literal)
        {
            Param.AssertNotNull(literal, "literal");

            Debug.Assert(
                literal.TokenNode != null &&
                literal.TokenNode.Value != null &&
                (literal.TokenNode.Value.CsTokenClass == CsTokenClass.Type ||
                 literal.TokenNode.Value.CsTokenClass == CsTokenClass.GenericType),
                "The literal expression does not contain a TypeToken");

            return (TypeToken)literal.TokenNode.Value;
        }
Exemple #28
0
 internal static TypeToken ExtractTypeTokenFromLiteralExpression(LiteralExpression literal)
 {
     return (TypeToken) literal.TokenNode.Value;
 }