/// <summary> /// Reads a unary expression. /// </summary> /// <param name="parentReference">The parent code part.</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(Reference<ICodePart> parentReference, bool unsafeCode) { Param.AssertNotNull(parentReference, "parentReference"); Param.Ignore(unsafeCode); var expressionReference = new Reference<ICodePart>(); // Create the token based on the type of the symbol. Symbol symbol = this.GetNextSymbol(parentReference); OperatorSymbol token; UnaryExpression.Operator operatorType; if (symbol.SymbolType == SymbolType.Plus) { operatorType = UnaryExpression.Operator.Positive; token = new OperatorSymbol(symbol.Text, OperatorCategory.Unary, OperatorType.Positive, symbol.Location, expressionReference, this.symbols.Generated); } else if (symbol.SymbolType == SymbolType.Minus) { operatorType = UnaryExpression.Operator.Negative; token = new OperatorSymbol(symbol.Text, OperatorCategory.Unary, OperatorType.Negative, symbol.Location, expressionReference, this.symbols.Generated); } else if (symbol.SymbolType == SymbolType.Not) { operatorType = UnaryExpression.Operator.Not; token = new OperatorSymbol(symbol.Text, OperatorCategory.Unary, OperatorType.Not, symbol.Location, expressionReference, this.symbols.Generated); } else if (symbol.SymbolType == SymbolType.Tilde) { operatorType = UnaryExpression.Operator.BitwiseCompliment; token = new OperatorSymbol(symbol.Text, OperatorCategory.Unary, OperatorType.BitwiseCompliment, symbol.Location, expressionReference, this.symbols.Generated); } else { // This is not a unary type. Debug.Fail("Unexpected operator type"); throw new StyleCopException(); } Node<CsToken> tokenNode = this.tokens.InsertLast(token); this.symbols.Advance(); // Get the expression after the operator. Expression innerExpression = this.GetNextExpression(ExpressionPrecedence.Unary, expressionReference, unsafeCode); if (innerExpression == null || innerExpression.Tokens.First == null) { throw this.CreateSyntaxException(); } // Create the partial token list for the expression. CsTokenList partialTokens = new CsTokenList(this.tokens, tokenNode, this.tokens.Last); // Create and return the expression. var expression = new UnaryExpression(partialTokens, operatorType, innerExpression); expressionReference.Target = expression; return expression; }
/// <summary> /// Reads a NOT expression. /// </summary> /// <param name="sourceCode">The source code containing the expression.</param> /// <param name="parentReference">The parent code part.</param> /// <returns>Returns the expression.</returns> private UnaryExpression GetConditionalPreprocessorNotExpression(SourceCode sourceCode, Reference<ICodePart> parentReference) { Param.AssertNotNull(sourceCode, "sourceCode"); Param.AssertNotNull(parentReference, "parentReference"); var expressionReference = new Reference<ICodePart>(); // Get the symbol. this.AdvanceToNextConditionalDirectiveCodeSymbol(parentReference); Symbol symbol = this.symbols.Peek(1); Debug.Assert(symbol != null, "The next symbol should not be null"); // Create the token based on the type of the symbol. OperatorSymbol token = new OperatorSymbol( symbol.Text, OperatorCategory.Unary, OperatorType.Not, symbol.Location, expressionReference, this.symbols.Generated); Node<CsToken> tokenNode = this.tokens.InsertLast(token); // Advance up to the symbol and add it to the document. this.symbols.Advance(); // Get the expression after the operator. Expression innerExpression = this.GetNextConditionalPreprocessorExpression(sourceCode, ExpressionPrecedence.Unary); if (innerExpression == null || innerExpression.Tokens.First == null) { throw new SyntaxException(sourceCode, symbol.LineNumber); } // Create the partial token list for the expression. CsTokenList partialTokens = new CsTokenList(this.tokens, tokenNode, this.tokens.Last); // Create and return the expression. var expression = new UnaryExpression(partialTokens, UnaryExpression.Operator.Not, innerExpression); expressionReference.Target = expression; return expression; }