Esempio n. 1
0
        /// <summary>
        /// Reads a NOT expression.
        /// </summary>
        /// <param name="parentProxy">Represents the parent item.</param>
        /// <returns>Returns the expression.</returns>
        private UnaryExpression GetConditionalPreprocessorNotExpression(CodeUnitProxy parentProxy)
        {
            Param.AssertNotNull(parentProxy, "parentProxy");

            this.AdvanceToNextConditionalDirectiveCodeSymbol(parentProxy);
            var expressionProxy = new CodeUnitProxy(this.document);

            Symbol symbol = this.symbols.Peek(1);

            CsLanguageService.Debug.Assert(symbol != null, "The next symbol should not be null");

            // Create the token based on the type of the symbol.
            var token = new NotOperator(this.document, symbol.Text, symbol.Location, this.symbols.Generated);

            expressionProxy.Children.Add(token);

            // Advance up to the symbol and add it to the document.
            this.symbols.Advance();

            // Get the expression after the operator.
            Expression expression = this.GetNextConditionalPreprocessorExpression(this.document, expressionProxy, ExpressionPrecedence.Unary);

            if (expression == null || expression.Children.Count == 0)
            {
                throw new SyntaxException(this.document, symbol.LineNumber);
            }

            // Create and return the expression.
            var unaryExpression = new NotExpression(expressionProxy, expression);

            parentProxy.Children.Add(unaryExpression);

            return(unaryExpression);
        }
        /// <summary>
        /// Reads a unary expression.
        /// </summary>
        /// <param name="parentProxy">Represents the parent item.</param>
        /// <param name="unsafeCode">Indicates whether the code being parsed resides in an unsafe code block.</param>
        /// <returns>Returns the expression.</returns>
        private UnaryExpression GetUnaryExpression(CodeUnitProxy parentProxy, bool unsafeCode)
        {
            Param.AssertNotNull(parentProxy, "parentProxy");
            Param.Ignore(unsafeCode);

            this.AdvanceToNextCodeSymbol(parentProxy);
            var expressionProxy = new CodeUnitProxy(this.document);

            // Create the token based on the type of the symbol.
            Symbol symbol = this.PeekNextSymbol();

            OperatorSymbolToken token;
            Func<Expression, UnaryExpression> expressionCreator = null;

            if (symbol.SymbolType == SymbolType.Plus)
            {
                token = new PositiveOperator(this.document, symbol.Text, symbol.Location, this.symbols.Generated);
                expressionCreator = (Expression value) => { return new PositiveExpression(expressionProxy, value); };
            }
            else if (symbol.SymbolType == SymbolType.Minus)
            {
                token = new NegativeOperator(this.document, symbol.Text, symbol.Location, this.symbols.Generated);
                expressionCreator = (Expression value) => { return new NegativeExpression(expressionProxy, value); };
            }
            else if (symbol.SymbolType == SymbolType.Not)
            {
                token = new NotOperator(this.document, symbol.Text, symbol.Location, this.symbols.Generated);
                expressionCreator = (Expression value) => { return new NotExpression(expressionProxy, value); };
            }
            else if (symbol.SymbolType == SymbolType.Tilde)
            {
                token = new BitwiseComplementOperator(this.document, symbol.Text, symbol.Location, this.symbols.Generated);
                expressionCreator = (Expression value) => { return new BitwiseComplementExpression(expressionProxy, value); };
            }
            else
            {
                // This is not a unary type.
                CsLanguageService.Debug.Fail("Unexpected operator type");
                return null;
            }

            expressionProxy.Children.Add(token);
            this.symbols.Advance();

            // Get the expression after the operator.
            Expression operatorExpression = this.GetNextExpression(expressionProxy, ExpressionPrecedence.Unary, unsafeCode);
            if (operatorExpression == null || operatorExpression.Children.Count == 0)
            {
                throw this.CreateSyntaxException();
            }

            UnaryExpression expression = expressionCreator(operatorExpression);

            parentProxy.Children.Add(expression);
            return expression;
        }
        /// <summary>
        /// Reads a NOT expression.
        /// </summary>
        /// <param name="parentProxy">Represents the parent item.</param>
        /// <returns>Returns the expression.</returns>
        private UnaryExpression GetConditionalPreprocessorNotExpression(CodeUnitProxy parentProxy)
        {
            Param.AssertNotNull(parentProxy, "parentProxy");

            this.AdvanceToNextConditionalDirectiveCodeSymbol(parentProxy);
            var expressionProxy = new CodeUnitProxy(this.document);

            Symbol symbol = this.symbols.Peek(1);
            CsLanguageService.Debug.Assert(symbol != null, "The next symbol should not be null");

            // Create the token based on the type of the symbol.
            var token = new NotOperator(this.document, symbol.Text, symbol.Location, this.symbols.Generated);
            expressionProxy.Children.Add(token);

            // Advance up to the symbol and add it to the document.
            this.symbols.Advance();

            // Get the expression after the operator.
            Expression expression = this.GetNextConditionalPreprocessorExpression(this.document, expressionProxy, ExpressionPrecedence.Unary);
            if (expression == null || expression.Children.Count == 0)
            {
                throw new SyntaxException(this.document, symbol.LineNumber);
            }

            // Create and return the expression.
            var unaryExpression = new NotExpression(expressionProxy, expression);
            parentProxy.Children.Add(unaryExpression);

            return unaryExpression;
        }