Esempio n. 1
0
        /// <summary>
        /// Initializes a new instance of the IsExpression class.
        /// </summary>
        /// <param name="tokens">
        /// The list of tokens that form the expression.
        /// </param>
        /// <param name="leftSideExpression">
        /// The left hand side of the expression.
        /// </param>
        /// <param name="rightSideExpression">
        /// The right hand side of the expression.
        /// </param>
        /// <param name="matchVariable">
        /// The variable declared as part of pattern match of the expression.
        /// </param>
        internal IsExpression(CsTokenList tokens, Expression leftSideExpression, Expression rightSideExpression, Expression matchVariable)
            : base(ExpressionType.Is, tokens)
        {
            Param.AssertNotNull(tokens, "tokens");
            Param.AssertNotNull(leftSideExpression, "leftSideExpression");
            Param.AssertNotNull(rightSideExpression, "rightSideExpression");
            Param.Ignore(matchVariable);

            this.leftHandSideExpression  = leftSideExpression;
            this.rightHandSideExpression = rightSideExpression;
            this.matchVariable           = matchVariable;

            // Extract the type being compared to, if possible.
            LiteralExpression le = this.rightHandSideExpression as LiteralExpression;

            if (le != null)
            {
                this.type = CodeParser.TryExtractTypeTokenFromLiteralExpression(le);
            }

            this.AddExpression(this.leftHandSideExpression);
            this.AddExpression(this.rightHandSideExpression);

            if (this.matchVariable != null)
            {
                this.AddExpression(this.matchVariable);
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Initializes a new instance of the Parameter class.
        /// </summary>
        /// <param name="type">
        /// The type of the parameter.
        /// </param>
        /// <param name="name">
        /// The name of the parameter.
        /// </param>
        /// <param name="parent">
        /// The parent of the parameter.
        /// </param>
        /// <param name="modifiers">
        /// Modifiers applied to this parameter.
        /// </param>
        /// <param name="defaultArgument">
        /// The optional default argument for the parameter.
        /// </param>
        /// <param name="location">
        /// The location of the parameter in the code.
        /// </param>
        /// <param name="tokens">
        /// The tokens that form the parameter.
        /// </param>
        /// <param name="generated">
        /// Indicates whether the parameter is located within a block of generated code.
        /// </param>
        internal Parameter(
            TypeToken type,
            string name,
            Reference <ICodePart> parent,
            ParameterModifiers modifiers,
            Expression defaultArgument,
            CodeLocation location,
            CsTokenList tokens,
            bool generated)
        {
            Param.Ignore(type);
            Param.AssertValidString(name, "name");
            Param.AssertNotNull(parent, "parent");
            Param.Ignore(modifiers);
            Param.Ignore(defaultArgument);
            Param.AssertNotNull(location, "location");
            Param.Ignore(tokens);
            Param.Ignore(generated);

            this.type            = type;
            this.name            = CodeLexer.DecodeEscapedText(name, true);
            this.parent          = parent;
            this.modifiers       = modifiers;
            this.defaultArgument = defaultArgument;
            this.location        = location;
            this.tokens          = tokens;
            this.generated       = generated;
        }
        /// <summary>
        /// Initializes a new instance of the LocalFunctionStatement class.
        /// </summary>
        /// <param name="tokens">
        /// The list of tokens that form the statement.
        /// </param>
        /// <param name="returnType">
        /// The return type of this local function.
        /// </param>
        /// <param name="returnTypeIsRef">
        /// The return type of this local function is ref.
        /// </param>
        /// <param name="identifier">
        /// The identifier of this local function.
        /// </param>
        /// <param name="parameters">
        /// The parameter information of this local function.
        /// </param>
        /// <param name="typeConstraints">
        /// The list of type constraints on the element.
        /// </param>
        private LocalFunctionStatement(
            CsTokenList tokens,
            TypeToken returnType,
            bool returnTypeIsRef,
            LiteralExpression identifier,
            IList <Parameter> parameters,
            ICollection <TypeParameterConstraintClause> typeConstraints)
            : base(StatementType.LocalFunction, tokens)
        {
            Param.AssertNotNull(returnType, nameof(returnType));
            Param.Ignore(returnTypeIsRef);
            Param.AssertNotNull(identifier, nameof(identifier));
            Param.AssertNotNull(parameters, nameof(parameters));
            Param.Ignore(typeConstraints);

            this.returnType      = returnType;
            this.returnTypeIsRef = returnTypeIsRef;
            this.identifier      = identifier;
            this.parameters      = parameters;
            this.typeConstraints = typeConstraints;

            this.AddExpression(this.identifier);

            // Set the parent of the type constraint clauses.
            if (typeConstraints != null)
            {
                foreach (TypeParameterConstraintClause constraint in typeConstraints)
                {
                    constraint.ParentElement = this;
                }
            }
        }
Esempio n. 4
0
        /// <summary>
        /// Checks the placement and formatting of parameters to a method invocation or a method declaration.
        /// </summary>
        /// <param name="element">
        /// The element.
        /// </param>
        /// <param name="parameterListTokens">
        /// The tokens that form the parameter list.
        /// </param>
        /// <param name="methodArguments">
        /// The arguments or parameters to the method.
        /// </param>
        /// <param name="methodStartLineNumber">
        /// The line number on which the method begins.
        /// </param>
        /// <param name="openBracketType">
        /// The type of the parameter list opening bracket.
        /// </param>
        /// <param name="closeBracketType">
        /// The type of the parameter list closing bracket.
        /// </param>
        /// <param name="friendlyTypeText">
        /// The text to use for violations.
        /// </param>
        private void CheckParameters(
            CsElement element,
            CsTokenList parameterListTokens,
            IArgumentList methodArguments,
            int methodStartLineNumber,
            CsTokenType openBracketType,
            CsTokenType closeBracketType,
            string friendlyTypeText)
        {
            Param.AssertNotNull(element, "element");
            Param.AssertNotNull(parameterListTokens, "parameterListTokens");
            Param.AssertNotNull(methodArguments, "methodArguments");
            Param.AssertGreaterThanZero(methodStartLineNumber, "methodStartLineNumber");
            Param.Ignore(openBracketType);
            Param.Ignore(closeBracketType);

            Node <CsToken> openingBracketNode = this.CheckMethodOpeningBracket(element, parameterListTokens, openBracketType, friendlyTypeText);

            if (openingBracketNode != null)
            {
                this.CheckMethodClosingBracket(element, parameterListTokens, openingBracketNode, closeBracketType, methodArguments);

                if (methodArguments.Count > 0)
                {
                    this.CheckMethodArgumentList(element, methodArguments, openingBracketNode, methodStartLineNumber, friendlyTypeText);
                }
            }
        }
Esempio n. 5
0
        /// <summary>
        /// Initializes a new instance of the SwitchStatement class.
        /// </summary>
        /// <param name="tokens">
        /// The list of tokens that form the statement.
        /// </param>
        /// <param name="switchItem">
        /// The expression to switch off of.
        /// </param>
        /// <param name="caseStatements">
        /// The list of case statements under the switch statement.
        /// </param>
        /// <param name="defaultStatement">
        /// The default statement under the switch statement.
        /// </param>
        internal SwitchStatement(CsTokenList tokens, Expression switchItem, ICollection <SwitchCaseStatement> caseStatements, SwitchDefaultStatement defaultStatement)
            : base(StatementType.Switch, tokens)
        {
            Param.AssertNotNull(tokens, "tokens");
            Param.AssertNotNull(switchItem, "switchItem");
            Param.AssertNotNull(caseStatements, "caseStatements");
            Param.Ignore(defaultStatement);

            this.switchItem       = switchItem;
            this.caseStatements   = caseStatements;
            this.defaultStatement = defaultStatement;

            Debug.Assert(caseStatements.IsReadOnly, "The collection of case statements should be read-only.");

            this.AddExpression(switchItem);

            foreach (Statement statement in caseStatements)
            {
                this.AddStatement(statement);
            }

            if (defaultStatement != null)
            {
                this.AddStatement(defaultStatement);
            }
        }
Esempio n. 6
0
        /// <summary>
        /// Initializes a new instance of the Argument class.
        /// </summary>
        /// <param name="name">
        /// The optional name of the argument.
        /// </param>
        /// <param name="modifiers">
        /// Modifiers applied to this argument.
        /// </param>
        /// <param name="argumentExpression">
        /// The expression that forms the body of the argument.
        /// </param>
        /// <param name="location">
        /// The location of the argument in the code.
        /// </param>
        /// <param name="parent">
        /// The parent code part.
        /// </param>
        /// <param name="tokens">
        /// The tokens that form the argument.
        /// </param>
        /// <param name="generated">
        /// Indicates whether the argument is located within a block of generated code.
        /// </param>
        internal Argument(
            CsToken name,
            ParameterModifiers modifiers,
            Expression argumentExpression,
            CodeLocation location,
            Reference <ICodePart> parent,
            CsTokenList tokens,
            bool generated)
        {
            Param.Ignore(name);
            Param.Ignore(modifiers);
            Param.AssertNotNull(argumentExpression, "argumentExpression");
            Param.AssertNotNull(location, "location");
            Param.AssertNotNull(parent, "parent");
            Param.Ignore(tokens);
            Param.Ignore(generated);

            this.name               = name;
            this.modifiers          = modifiers;
            this.argumentExpression = argumentExpression;
            this.location           = location;
            this.parent             = parent;
            this.tokens             = tokens;
            this.generated          = generated;
        }
Esempio n. 7
0
        /// <summary>
        /// Determines whether the token list contains the given strings, skipping whitespace tokens and
        /// comments.
        /// </summary>
        /// <param name="comparisonType">
        /// The string comparison type to use.
        /// </param>
        /// <param name="values">
        /// The collection of strings to match against.
        /// </param>
        /// <returns>
        /// Returns true if the tokens match the collection of strings.
        /// </returns>
        public bool MatchTokens(StringComparison comparisonType, params string[] values)
        {
            Param.RequireNotNull(values, "values");
            Param.Ignore(comparisonType);

            return(CsTokenList.MatchTokens(comparisonType, this.First, values));
        }
Esempio n. 8
0
        /// <summary>
        /// Determines whether the given token list contains the given strings, skipping whitespace tokens and
        /// comments.
        /// </summary>
        /// <param name="start">
        /// Begins matching the given strings with this token.
        /// </param>
        /// <param name="values">
        /// The collection of strings to match against.
        /// </param>
        /// <returns>
        /// Returns true if the tokens match the collection of strings.
        /// </returns>
        public static bool MatchTokens(Node <CsToken> start, params string[] values)
        {
            Param.RequireNotNull(start, "start");
            Param.RequireNotNull(values, "values");

            return(CsTokenList.MatchTokens(StringComparison.Ordinal, start, values));
        }
        /// <summary>
        /// Initializes a new instance of the CollectionInitializerExpression class.
        /// </summary>
        /// <param name="tokens">
        /// The list of tokens that form the expression.
        /// </param>
        /// <param name="initializers">
        /// The list of variable initializers within the
        /// array initializer expression.
        /// </param>
        internal CollectionInitializerExpression(CsTokenList tokens, IEnumerable <Expression> initializers)
            : base(ExpressionType.CollectionInitializer, tokens)
        {
            Param.AssertNotNull(tokens, "tokens");
            Param.AssertNotNull(initializers, "initializers");

            this.AddExpressions(initializers);
        }
Esempio n. 10
0
        /// <summary>
        /// Gets the tokens forming the parameter list for a method declaration.
        /// </summary>
        /// <param name="tokens">
        /// The tokens forming the method declaration.
        /// </param>
        /// <param name="openBracketType">
        /// The type of the opening bracket.
        /// </param>
        /// <param name="closeBracketType">
        /// The type of the closing bracket.
        /// </param>
        /// <returns>
        /// Returns the parameter list or null if it cannot be found.
        /// </returns>
        private static CsTokenList GetParameterListTokens(CsTokenList tokens, CsTokenType openBracketType, CsTokenType closeBracketType)
        {
            Param.AssertNotNull(tokens, "tokens");
            Param.Ignore(openBracketType);
            Param.Ignore(closeBracketType);

            return(GetArgumentListTokens(tokens, tokens.First, openBracketType, closeBracketType));
        }
Esempio n. 11
0
        /// <summary>
        /// Initializes a new instance of the NameofExpression class.
        /// </summary>
        /// <param name="tokens">
        /// The list of tokens that form the expression.
        /// </param>
        /// <param name="name">
        /// The type literal to get the name of.
        /// </param>
        internal NameofExpression(CsTokenList tokens, LiteralExpression name)
            : base(ExpressionType.NameOf, tokens)
        {
            Param.AssertNotNull(tokens, "tokens");
            Param.AssertNotNull(name, "name");

            this.type = CodeParser.ExtractTypeTokenFromLiteralExpression(name);
            this.AddExpression(name);
        }
Esempio n. 12
0
        /// <summary>
        /// Initializes a new instance of the IfStatement class.
        /// </summary>
        /// <param name="tokens">
        /// The list of tokens that form the statement.
        /// </param>
        /// <param name="conditionExpression">
        /// The expression within the if-statement.
        /// </param>
        internal IfStatement(CsTokenList tokens, Expression conditionExpression)
            : base(StatementType.If, tokens)
        {
            Param.AssertNotNull(tokens, "tokens");
            Param.AssertNotNull(conditionExpression, "conditionExpression");

            this.conditionExpression = conditionExpression;
            this.AddExpression(conditionExpression);
        }
Esempio n. 13
0
        /// <summary>
        /// Initializes a new instance of the StackallocExpression class.
        /// </summary>
        /// <param name="tokens">
        /// The list of tokens that form the expression.
        /// </param>
        /// <param name="type">
        /// The array type.
        /// </param>
        internal StackallocExpression(CsTokenList tokens, ArrayAccessExpression type)
            : base(ExpressionType.Stackalloc, tokens)
        {
            Param.AssertNotNull(tokens, "tokens");
            Param.AssertNotNull(type, "type");

            this.type = type;
            this.AddExpression(type);
        }
Esempio n. 14
0
        /// <summary>
        /// Initializes a new instance of the TypeofExpression class.
        /// </summary>
        /// <param name="tokens">
        /// The list of tokens that form the expression.
        /// </param>
        /// <param name="type">
        /// The type literal to get the type of.
        /// </param>
        internal TypeofExpression(CsTokenList tokens, LiteralExpression type)
            : base(ExpressionType.Typeof, tokens)
        {
            Param.AssertNotNull(tokens, "tokens");
            Param.AssertNotNull(type, "type");

            this.type = CodeParser.ExtractTypeTokenFromLiteralExpression(type);
            this.AddExpression(type);
        }
Esempio n. 15
0
        /// <summary>
        /// Initializes a new instance of the SizeofExpression class.
        /// </summary>
        /// <param name="tokens">
        /// The list of tokens that form the expression.
        /// </param>
        /// <param name="type">
        /// The type to get the size of.
        /// </param>
        internal SizeofExpression(CsTokenList tokens, Expression type)
            : base(ExpressionType.Sizeof, tokens)
        {
            Param.AssertNotNull(tokens, "tokens");
            Param.AssertNotNull(type, "type");

            this.type = type;
            this.AddExpression(type);
        }
Esempio n. 16
0
        /// <summary>
        /// Initializes a new instance of the FileHeader class.
        /// </summary>
        /// <param name="headerText">
        /// The header text.
        /// </param>
        /// <param name="tokens">
        /// The collection of tokens in the header.
        /// </param>
        /// <param name="parent">
        /// The parent of the header.
        /// </param>
        internal FileHeader(string headerText, CsTokenList tokens, Reference <ICodePart> parent)
        {
            Param.AssertNotNull(headerText, "headerText");
            Param.AssertNotNull(tokens, "tokens");
            Param.AssertNotNull(parent, "parent");

            this.headerText = headerText;
            this.tokens     = tokens;
            this.parent     = parent;

            this.location = this.tokens.First != null?CsToken.JoinLocations(this.tokens.First, this.tokens.Last) : CodeLocation.Empty;

            // Attempt to load this into an Xml document.
            try
            {
                if (this.headerText.Length > 0)
                {
                    this.headerXml = string.Format(CultureInfo.InvariantCulture, "<root>{0}</root>", HtmlEncode(this.headerText));

                    XmlDocument doc = new XmlDocument();
                    doc.LoadXml(this.headerXml);

                    // Check whether the header has the autogenerated tag.
                    if (doc.DocumentElement != null)
                    {
                        XmlNode node = doc.DocumentElement["autogenerated"];
                        if (node != null)
                        {
                            // Set this as generated code.
                            this.generated = true;
                        }
                        else
                        {
                            node = doc.DocumentElement["auto-generated"];
                            if (node != null)
                            {
                                // Set this as generated code.
                                this.generated = true;
                            }
                        }

                        StringCollection unstyledElements = new StringCollection();
                        unstyledElements.AddRange(new[] { "unstyled", "stylecopoff", "nostyle" });

                        XmlNodeList childNodes = doc.DocumentElement.ChildNodes;
                        if (childNodes.Cast <XmlNode>().Any(xmlNode => unstyledElements.Contains(xmlNode.Name.ToLowerInvariant())))
                        {
                            this.UnStyled = true;
                        }
                    }
                }
            }
            catch (XmlException)
            {
            }
        }
Esempio n. 17
0
        /// <summary>
        /// Checks a type to determine whether it should use one of the built-in types.
        /// </summary>
        /// <param name="type">
        /// The type to check.
        /// </param>
        /// <param name="document">
        /// The parent document.
        /// </param>
        private void CheckBuiltInType(Node <CsToken> type, CsDocument document)
        {
            Param.AssertNotNull(type, "type");
            Param.AssertNotNull(document, "document");

            Debug.Assert(type.Value is TypeToken, "The type must be a TypeToken");
            TypeToken typeToken = (TypeToken)type.Value;

            if (type.Value.CsTokenClass != CsTokenClass.GenericType)
            {
                for (int i = 0; i < this.builtInTypes.Length; ++i)
                {
                    string[] builtInType = this.builtInTypes[i];

                    if (CsTokenList.MatchTokens(typeToken.ChildTokens.First, builtInType[0]) ||
                        CsTokenList.MatchTokens(typeToken.ChildTokens.First, "System", ".", builtInType[0]))
                    {
                        // If the previous token is an equals sign, then this is a using alias directive. For example:
                        // using SomeAlias = System.String;
                        // If the previous token is the 'static' keyword, then this is a using static directive. For example:
                        // using static System.String;
                        bool shouldBuiltInTypeAliasBeUsed = true;
                        for (Node <CsToken> previous = type.Previous; previous != null; previous = previous.Previous)
                        {
                            if (previous.Value.CsTokenType != CsTokenType.EndOfLine && previous.Value.CsTokenType != CsTokenType.MultiLineComment &&
                                previous.Value.CsTokenType != CsTokenType.SingleLineComment && previous.Value.CsTokenType != CsTokenType.WhiteSpace)
                            {
                                if (previous.Value.Text == "=" || previous.Value.Text == "static")
                                {
                                    shouldBuiltInTypeAliasBeUsed = false;
                                }

                                break;
                            }
                        }

                        if (shouldBuiltInTypeAliasBeUsed)
                        {
                            this.AddViolation(
                                typeToken.FindParentElement(), typeToken.LineNumber, Rules.UseBuiltInTypeAlias, builtInType[2], builtInType[0], builtInType[1]);
                        }

                        break;
                    }
                }
            }

            for (Node <CsToken> childToken = typeToken.ChildTokens.First; childToken != null; childToken = childToken.Next)
            {
                if (childToken.Value.CsTokenClass == CsTokenClass.Type || childToken.Value.CsTokenClass == CsTokenClass.GenericType)
                {
                    this.CheckBuiltInType(childToken, document);
                }
            }
        }
Esempio n. 18
0
        /// <summary>
        /// Reads an expression wrapped in parenthesis.
        /// </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 ParenthesizedExpression GetConditionalPreprocessorParenthesizedExpression(SourceCode sourceCode, Reference <ICodePart> parentReference)
        {
            Param.AssertNotNull(sourceCode, "sourceCode");
            Param.AssertNotNull(parentReference, "parentReference");

            // Get the opening parenthesis.
            this.AdvanceToNextConditionalDirectiveCodeSymbol(parentReference);
            Symbol firstSymbol = this.symbols.Peek(1);

            if (firstSymbol == null || firstSymbol.SymbolType != SymbolType.OpenParenthesis)
            {
                throw new SyntaxException(sourceCode, firstSymbol.LineNumber);
            }

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

            this.symbols.Advance();
            Bracket openParenthesis = new Bracket(firstSymbol.Text, CsTokenType.OpenParenthesis, firstSymbol.Location, expressionReference, this.symbols.Generated);

            Node <CsToken> openParenthesisNode = this.tokens.InsertLast(openParenthesis);

            // Get the inner expression.
            Expression innerExpression = this.GetNextConditionalPreprocessorExpression(sourceCode, ExpressionPrecedence.None);

            if (innerExpression == null)
            {
                throw new SyntaxException(sourceCode, firstSymbol.LineNumber);
            }

            // Get the closing parenthesis.
            this.AdvanceToNextConditionalDirectiveCodeSymbol(expressionReference);
            Symbol symbol = this.symbols.Peek(1);

            if (symbol == null || symbol.SymbolType != SymbolType.CloseParenthesis)
            {
                throw new SyntaxException(sourceCode, firstSymbol.LineNumber);
            }

            this.symbols.Advance();
            Bracket        closeParenthesis     = new Bracket(symbol.Text, CsTokenType.CloseParenthesis, symbol.Location, expressionReference, this.symbols.Generated);
            Node <CsToken> closeParenthesisNode = this.tokens.InsertLast(closeParenthesis);

            openParenthesis.MatchingBracketNode  = closeParenthesisNode;
            closeParenthesis.MatchingBracketNode = openParenthesisNode;

            // Create the token list for the expression.
            CsTokenList partialTokens = new CsTokenList(this.tokens, openParenthesisNode, this.tokens.Last);

            // Create and return the expression.
            ParenthesizedExpression expression = new ParenthesizedExpression(partialTokens, innerExpression);

            expressionReference.Target = expression;

            return(expression);
        }
Esempio n. 19
0
        /// <summary>
        /// Initializes a new instance of the DictionaryItemInitializationExpression class.
        /// </summary>
        /// <param name="tokens">The list of tokens that form the statement.</param>
        /// <param name="initializer">The initializer expression or null.</param>
        internal DictionaryItemInitializationExpression(CsTokenList tokens, Expression initializer)
            : base(ExpressionType.ArrayInitializer, tokens)
        {
            Param.AssertNotNull(tokens, "tokens");
            Param.Ignore(initializer);

            if (initializer != null)
            {
                this.AddExpression(initializer);
            }
        }
Esempio n. 20
0
        /// <summary>
        /// Returns true if the node Contains any sort of <see cref="Nullable"/>.
        /// </summary>
        /// <param name="token">
        /// The token to check.
        /// </param>
        /// <returns>
        /// True if <see cref="Nullable"/> otherwise False.
        /// </returns>
        public static bool TokenContainNullable(Node <CsToken> token)
        {
            if (CsTokenList.MatchTokens(StringComparison.Ordinal, token, new[] { "System", ".", "Nullable" }) ||
                CsTokenList.MatchTokens(StringComparison.Ordinal, token, new[] { "global", "::", "System", ".", "Nullable" }) ||
                token.Value.Text.Equals("Nullable", StringComparison.Ordinal))
            {
                return(true);
            }

            return(false);
        }
Esempio n. 21
0
        /// <summary>
        /// Initializes a new instance of the CodeUnit class.
        /// </summary>
        /// <param name="codeUnitType">
        /// The type of the code unit.
        /// </param>
        /// <param name="tokens">
        /// The list of tokens that form the code unit.
        /// </param>
        internal CodeUnit(CodePartType codeUnitType, CsTokenList tokens)
            : this(codeUnitType)
        {
            Param.Ignore(codeUnitType);
            Param.AssertNotNull(tokens, "tokens");

            this.tokens = tokens;
            this.tokens.Trim();

            Debug.Assert(this.tokens.First != null, "The tokens should not be empty");
        }
Esempio n. 22
0
        /// <summary>
        /// Checks a method or method invocation to ensure that the closing bracket is
        /// on the same line as the last parameter.
        /// </summary>
        /// <param name="element">
        /// The element containing the expression.
        /// </param>
        /// <param name="parameterListTokens">
        /// The tokens that form the parameter list.
        /// </param>
        /// <param name="openingBracketNode">
        /// The opening bracket.
        /// </param>
        /// <param name="closingBracketType">
        /// The type of the closing bracket.
        /// </param>
        /// <param name="arguments">
        /// The arguments to the method.
        /// </param>
        private void CheckMethodClosingBracket(
            CsElement element, CsTokenList parameterListTokens, Node <CsToken> openingBracketNode, CsTokenType closingBracketType, IArgumentList arguments)
        {
            Param.AssertNotNull(element, "element");
            Param.AssertNotNull(parameterListTokens, "parameterListTokens");
            Param.AssertNotNull(openingBracketNode, "openingBracket");
            Param.Ignore(closingBracketType);
            Param.AssertNotNull(arguments, "arguments");

            // Find the closing bracket.
            Node <CsToken> closingBracketNode = null;

            for (Node <CsToken> tokenNode = parameterListTokens.Last; tokenNode != null; tokenNode = tokenNode.Previous)
            {
                if (tokenNode.Value.CsTokenType == closingBracketType)
                {
                    closingBracketNode = tokenNode;
                    break;
                }
            }

            if (closingBracketNode != null)
            {
                if (arguments.Count == 0)
                {
                    // The closing bracket must be on the same line as the opening bracket.
                    if (openingBracketNode.Value.LineNumber != closingBracketNode.Value.LineNumber)
                    {
                        // If the brackets are not on the same line, determine if this is because there are comments
                        // between the brackets.
                        int commentLineSpan = MeasureCommentLinesBetween(openingBracketNode, closingBracketNode, false);

                        if (openingBracketNode.Value.LineNumber + commentLineSpan != closingBracketNode.Value.LineNumber)
                        {
                            this.AddViolation(element, closingBracketNode.Value.LineNumber, Rules.ClosingParenthesisMustBeOnLineOfOpeningParenthesis);
                        }
                    }
                }
                else
                {
                    // The closing bracket must be on the same line as the end of the last method argument.
                    int lastArgumentEndLine = arguments.Location(arguments.Count - 1).EndPoint.LineNumber;
                    if (lastArgumentEndLine != closingBracketNode.Value.LineNumber)
                    {
                        int commentLineSpan = MeasureCommentLinesBetween(arguments.Tokens(arguments.Count - 1).Last, closingBracketNode, false);

                        if (lastArgumentEndLine + commentLineSpan != closingBracketNode.Value.LineNumber)
                        {
                            this.AddViolation(element, closingBracketNode.Value.LineNumber, Rules.ClosingParenthesisMustBeOnLineOfLastParameter);
                        }
                    }
                }
            }
        }
Esempio n. 23
0
        /// <summary>
        /// Initializes a new instance of the ArrayInitializerExpression class.
        /// </summary>
        /// <param name="tokens">
        /// The list of tokens that form the expression.
        /// </param>
        /// <param name="initializers">
        /// The list of variable initializers within the
        /// array initializer expression.
        /// </param>
        internal ArrayInitializerExpression(CsTokenList tokens, ICollection <Expression> initializers)
            : base(ExpressionType.ArrayInitializer, tokens)
        {
            Param.AssertNotNull(tokens, "tokens");
            Param.AssertNotNull(initializers, "initializers");

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

            this.AddExpressions(initializers);
        }
        /// <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>
        /// <param name="parent">
        /// The parent reference of this expression.
        /// </param>
        internal DefaultValueExpression(CsTokenList tokens, LiteralExpression type, Reference <ICodePart> parent)
            : base(ExpressionType.DefaultValue, tokens)
        {
            Param.AssertNotNull(tokens, "tokens");
            Param.AssertNotNull(type, "type");

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

            this.parent = parent;
        }
        /// <summary>
        /// Initializes a new instance of the VariableDeclarationStatement class.
        /// </summary>
        /// <param name="tokens">
        /// The list of tokens that form the statement.
        /// </param>
        /// <param name="constant">
        /// Indicates whether the item is constant.
        /// </param>
        /// <param name="expression">
        /// The inner expression.
        /// </param>
        internal VariableDeclarationStatement(CsTokenList tokens, bool constant, VariableDeclarationExpression expression)
            : base(StatementType.VariableDeclaration, tokens)
        {
            Param.AssertNotNull(tokens, "tokens");
            Param.Ignore(constant);
            Param.AssertNotNull(expression, "expression");

            this.constant   = constant;
            this.expression = expression;

            this.AddExpression(expression);
        }
Esempio n. 26
0
        /// <summary>
        /// Initializes a new instance of the UnaryExpression class.
        /// </summary>
        /// <param name="tokens">
        /// The list of tokens that form the expression.
        /// </param>
        /// <param name="operatorType">
        /// The type of operation being performed.
        /// </param>
        /// <param name="value">
        /// The value the operator is being applied to.
        /// </param>
        internal UnaryExpression(CsTokenList tokens, Operator operatorType, Expression value)
            : base(ExpressionType.Unary, tokens)
        {
            Param.AssertNotNull(tokens, "tokens");
            Param.Ignore(operatorType);
            Param.AssertNotNull(value, "value");

            this.operatorType = operatorType;
            this.value        = value;

            this.AddExpression(value);
        }
Esempio n. 27
0
        /// <summary>
        /// Initializes a new instance of the DecrementExpression class.
        /// </summary>
        /// <param name="tokens">
        /// The list of tokens that form the expression.
        /// </param>
        /// <param name="value">
        /// The value being decremented.
        /// </param>
        /// <param name="decrementType">
        /// The type of decrement being performed.
        /// </param>
        internal DecrementExpression(CsTokenList tokens, Expression value, DecrementType decrementType)
            : base(ExpressionType.Decrement, tokens)
        {
            Param.AssertNotNull(tokens, "tokens");
            Param.AssertNotNull(value, "value");
            Param.Ignore(decrementType);

            this.value         = value;
            this.decrementType = decrementType;

            this.AddExpression(value);
        }
Esempio n. 28
0
        /// <summary>
        /// Initializes a new instance of the IncrementExpression class.
        /// </summary>
        /// <param name="tokens">
        /// The list of tokens that form the expression.
        /// </param>
        /// <param name="value">
        /// The value being incremented.
        /// </param>
        /// <param name="incrementType">
        /// The type of increment being performed.
        /// </param>
        internal IncrementExpression(CsTokenList tokens, Expression value, IncrementType incrementType)
            : base(ExpressionType.Increment, tokens)
        {
            Param.AssertNotNull(tokens, "tokens");
            Param.AssertNotNull(value, "value");
            Param.Ignore(incrementType);

            this.value         = value;
            this.incrementType = incrementType;

            this.AddExpression(value);
        }
Esempio n. 29
0
        /// <summary>
        /// Initializes a new instance of the QueryExpression class.
        /// </summary>
        /// <param name="tokens">
        /// The list of tokens that form the expression.
        /// </param>
        /// <param name="clauses">
        /// The collection of clauses in the expression.
        /// </param>
        internal QueryExpression(CsTokenList tokens, ICollection <QueryClause> clauses)
            : base(ExpressionType.Query, tokens)
        {
            Param.AssertNotNull(tokens, "tokens");
            Param.AssertNotNull(clauses, "clauses");

            this.clauses = new CodeUnitCollection <QueryClause>(this);
            this.clauses.AddRange(clauses);
            this.InitializeFromClauses(clauses);

            Debug.Assert(clauses.IsReadOnly, "The collection of query clauses should be read-only.");
        }
Esempio n. 30
0
        /// <summary>
        /// Initializes a new instance of the ElseStatement class.
        /// </summary>
        /// <param name="tokens">
        /// The list of tokens that form the statement.
        /// </param>
        /// <param name="conditionExpression">
        /// The expression within the if portion of this
        /// else-statement, if any.
        /// </param>
        internal ElseStatement(CsTokenList tokens, Expression conditionExpression)
            : base(StatementType.Else, tokens)
        {
            Param.AssertNotNull(tokens, "tokens");
            Param.Ignore(conditionExpression);

            this.conditionExpression = conditionExpression;

            if (conditionExpression != null)
            {
                this.AddExpression(conditionExpression);
            }
        }