/// <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); } } }
private bool IsValueExpression(Expression expression) { if (expression is LiteralExpression) { CsTokenType tokenType = ((LiteralExpression)expression).Token.CsTokenType; return(tokenType == CsTokenType.Number || tokenType == CsTokenType.String || tokenType == CsTokenType.Null || tokenType == CsTokenType.True || tokenType == CsTokenType.False); } if (expression is ParenthesizedExpression) { return(this.IsValueExpression(((ParenthesizedExpression)expression).InnerExpression)); } if (expression is RelationalExpression) { RelationalExpression re = (RelationalExpression)expression; return(this.IsValueExpression(re.LeftHandSide) && this.IsValueExpression(re.RightHandSide)); } if (expression is ArithmeticExpression) { ArithmeticExpression ae = (ArithmeticExpression)expression; return(this.IsValueExpression(ae.LeftHandSide) && this.IsValueExpression(ae.RightHandSide)); } return(false); }
/// <summary> /// Determines the amount of offset to add to the line number of the next parameter /// for a comment or attribute. /// </summary> /// <param name="tokenNode"> /// The token node. /// </param> /// <returns> /// Returns the amount of offset to add. /// </returns> private static int ParameterPrewordOffset(Node <CsToken> tokenNode) { Param.AssertNotNull(tokenNode, "tokenNode"); Debug.Assert( tokenNode.Value.CsTokenType == CsTokenType.Attribute || tokenNode.Value.CsTokenType == CsTokenType.SingleLineComment || tokenNode.Value.CsTokenType == CsTokenType.MultiLineComment, "The token must be an attribute or a comment."); // Find the start of the next parameter. for (Node <CsToken> node = tokenNode.Next; node != null; node = node.Next) { CsTokenType tokenType = node.Value.CsTokenType; if (tokenType == CsTokenType.EndOfLine) { return(tokenNode.Value.Location.LineSpan); } else if (tokenType != CsTokenType.WhiteSpace && tokenType != CsTokenType.MultiLineComment && tokenType != CsTokenType.SingleLineComment && tokenType != CsTokenType.Attribute) { return(Math.Max(0, node.Value.Location.StartPoint.LineNumber - tokenNode.Value.Location.StartPoint.LineNumber)); } } return(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)); }
public static bool HasToken(this ICodeUnit codeUnit, CsTokenType tokenType) { if (codeUnit == null) { return false; } return codeUnit.Tokens.Any(x => x.CsTokenType == tokenType); }
public static string GetTokenType(CsTokenType pTokenType) { if (_typeRef.ContainsKey(pTokenType)) { return(_typeRef[pTokenType]); } throw new Exception(@"Unknown Typeref: " + pTokenType); }
/// <summary> /// Initializes a new instance of the Bracket class. /// </summary> /// <param name="text"> /// The token string. /// </param> /// <param name="tokenType"> /// The token type. /// </param> /// <param name="location"> /// The location of the token within the code document. /// </param> /// <param name="parent"> /// The parent code part. /// </param> /// <param name="generated"> /// True if the token is inside of a block of generated code. /// </param> internal Bracket(string text, CsTokenType tokenType, CodeLocation location, Reference <ICodePart> parent, bool generated) : base(text, tokenType, CsTokenClass.Bracket, location, parent, generated) { Param.Ignore(text, tokenType, location, parent, generated); Debug.Assert( tokenType == CsTokenType.OpenCurlyBracket || tokenType == CsTokenType.CloseCurlyBracket || tokenType == CsTokenType.OpenSquareBracket || tokenType == CsTokenType.CloseSquareBracket || tokenType == CsTokenType.OpenParenthesis || tokenType == CsTokenType.CloseParenthesis || tokenType == CsTokenType.OpenGenericBracket || tokenType == CsTokenType.CloseGenericBracket || tokenType == CsTokenType.OpenAttributeBracket || tokenType == CsTokenType.CloseAttributeBracket, "The symbol is not a bracket type."); }
/// <summary> /// Initializes a new instance of the Bracket class. /// </summary> /// <param name="text"> /// The token string. /// </param> /// <param name="tokenType"> /// The token type. /// </param> /// <param name="location"> /// The location of the token within the code document. /// </param> /// <param name="parent"> /// The parent code part. /// </param> /// <param name="generated"> /// True if the token is inside of a block of generated code. /// </param> internal Bracket(string text, CsTokenType tokenType, CodeLocation location, Reference<ICodePart> parent, bool generated) : base(text, tokenType, CsTokenClass.Bracket, location, parent, generated) { Param.Ignore(text, tokenType, location, parent, generated); Debug.Assert( tokenType == CsTokenType.OpenCurlyBracket || tokenType == CsTokenType.CloseCurlyBracket || tokenType == CsTokenType.OpenSquareBracket || tokenType == CsTokenType.CloseSquareBracket || tokenType == CsTokenType.OpenParenthesis || tokenType == CsTokenType.CloseParenthesis || tokenType == CsTokenType.OpenGenericBracket || tokenType == CsTokenType.CloseGenericBracket || tokenType == CsTokenType.OpenAttributeBracket || tokenType == CsTokenType.CloseAttributeBracket, "The symbol is not a bracket type."); }
/// <summary> /// Initializes a new instance of the CsToken class. /// </summary> /// <param name="tokenType"> /// The token type. /// </param> /// <param name="tokenClass"> /// The token class. /// </param> /// <param name="location"> /// The location of the token within the code document. /// </param> /// <param name="parent"> /// References the parent code part. /// </param> /// <param name="generated"> /// True if the token is inside of a block of generated code. /// </param> internal CsToken(CsTokenType tokenType, CsTokenClass tokenClass, CodeLocation location, Reference <ICodePart> parent, bool generated) { Param.Ignore(tokenType); Param.Ignore(tokenClass); Param.AssertNotNull(location, "location"); Param.AssertNotNull(parent, "parent"); Param.Ignore(generated); this.tokenType = tokenType; this.tokenClass = tokenClass; this.location = location; this.parent = parent; this.generated = generated; }
private void ProcessStringOperator(CsTokenType csTokenType) { switch (csTokenType) { case CsTokenType.tkPLUS: var stringType = typeof(string); var parms = new Type[] { stringType, stringType }; var method = stringType.GetMethod("Concat", BindingFlags.Public | BindingFlags.Static | BindingFlags.InvokeMethod, null, parms, null); methodGenerator.EmitCall(OpCodes.Call, method, parms); break; } }
/// <summary> /// Checks a method or method invocation to ensure that the opening bracket is /// on the same line as the method declaration. /// </summary> /// <param name="element"> /// The element containing the expression. /// </param> /// <param name="parameterListTokens"> /// The tokens in the parameter list. /// </param> /// <param name="openingBracketType"> /// The type of the bracket that opens the parameter list. /// </param> /// <param name="textToUseForContainingElement"> /// The text to use in the violation. /// </param> /// <returns> /// Returns the opening bracket. /// </returns> private Node <CsToken> CheckMethodOpeningBracket( CsElement element, CsTokenList parameterListTokens, CsTokenType openingBracketType, string textToUseForContainingElement) { Param.AssertNotNull(element, "element"); Param.AssertNotNull(parameterListTokens, "parameterListTokens"); Param.Ignore(openingBracketType); // Find the opening bracket. Node <CsToken> openingBracketNode = null; for (Node <CsToken> tokenNode = parameterListTokens.First; tokenNode != null; tokenNode = tokenNode.Next) { if (tokenNode.Value.CsTokenType == openingBracketType) { openingBracketNode = tokenNode; break; } } CsToken lastWord = null; if (openingBracketNode != null) { // Find the last word before the opening bracket. for (Node <CsToken> tokenNode = openingBracketNode.Previous; tokenNode != null; tokenNode = tokenNode.Previous) { if (tokenNode.Value.CsTokenType != CsTokenType.WhiteSpace && tokenNode.Value.CsTokenType != CsTokenType.EndOfLine && tokenNode.Value.CsTokenType != CsTokenType.SingleLineComment && tokenNode.Value.CsTokenType != CsTokenType.MultiLineComment) { lastWord = tokenNode.Value; break; } } } if (lastWord != null) { if (openingBracketNode.Value.LineNumber != lastWord.LineNumber) { this.AddViolation(element, openingBracketNode.Value.LineNumber, Rules.OpeningParenthesisMustBeOnDeclarationLine, textToUseForContainingElement); } } return(openingBracketNode); }
private static CsTokenType convertToken(CsTokenType pInToken) { switch (pInToken) { case CsTokenType.tkPLUS_EQ: return(CsTokenType.tkPLUS); case CsTokenType.tkMINUS_EQ: return(CsTokenType.tkMINUS); case CsTokenType.tkDIV_EQ: return(CsTokenType.tkDIV); case CsTokenType.tkMUL_EQ: return(CsTokenType.tkSTAR); case CsTokenType.tkMOD_EQ: return(CsTokenType.tkMOD); default: throw new Exception(); } }
/// <summary> /// Processes the given element. /// </summary> /// <param name="element"> /// The element being visited. /// </param> private void CheckMethodParameters(CsElement element) { Param.AssertNotNull(element, "element"); IList <Parameter> parameters = null; CsTokenType openBracketType = CsTokenType.OpenParenthesis; CsTokenType closeBracketType = CsTokenType.CloseParenthesis; if (element.ElementType == ElementType.Constructor) { parameters = ((Constructor)element).Parameters; } else if (element.ElementType == ElementType.Delegate) { parameters = ((Delegate)element).Parameters; } else if (element.ElementType == ElementType.Method) { parameters = ((Method)element).Parameters; } else if (element.ElementType == ElementType.Indexer) { parameters = ((Indexer)element).Parameters; openBracketType = CsTokenType.OpenSquareBracket; closeBracketType = CsTokenType.CloseSquareBracket; } if (parameters != null) { ParameterList parameterList = new ParameterList(parameters); CsTokenList parameterListTokens = GetParameterListTokens(element.Declaration.Tokens, openBracketType, closeBracketType); if (parameterListTokens != null) { this.CheckParameters(element, parameterListTokens, parameterList, element.LineNumber, openBracketType, closeBracketType, element.FriendlyTypeText); } } }
internal Bracket(string text, CsTokenType tokenType, CodeLocation location, bool generated) : base(text, tokenType, CsTokenClass.Bracket, location, generated) { }
/* /// <summary> /// Measures the number of lines taken up by comments and attributes before the given word. /// </summary> /// <param name="start">The start token.</param> /// <returns>Returs the number of lines taken up.</returns> private static int MeasureCommentLinesBefore(Node<CsToken> start) { Param.AssertNotNull(start, "start"); int lineSpan = 0; int nextLineSpan = -1; int nextStartLineNumber = -1; for (Node<CsToken> tokenNode = start.Previous; tokenNode != null; tokenNode = tokenNode.Previous) { if (tokenNode.Value.CsTokenType == CsTokenType.SingleLineComment || tokenNode.Value.CsTokenType == CsTokenType.MultiLineComment || tokenNode.Value.CsTokenType == CsTokenType.Attribute) { int itemLineSpan = ParameterPrewordOffset(tokenNode); if (nextStartLineNumber > 0 && tokenNode.Value.Location.EndPoint.LineNumber == nextStartLineNumber && nextLineSpan > 0) { --itemLineSpan; } lineSpan += itemLineSpan; nextLineSpan = itemLineSpan; nextStartLineNumber = tokenNode.Value.LineNumber; } } return lineSpan; } */ /// <summary> /// Gets the tokens forming the argument list for a method call. /// </summary> /// <param name="tokens"> /// The tokens forming the method call. /// </param> /// <param name="methodNameLastToken"> /// The last token before the argument list begins. /// </param> /// <param name="openBracketType"> /// The type of the opening bracket. /// </param> /// <param name="closeBracketType"> /// The type of the closing bracket. /// </param> /// <returns> /// Returns the argument list or null if it cannot be found. /// </returns> private static CsTokenList GetArgumentListTokens(CsTokenList tokens, Node<CsToken> methodNameLastToken, CsTokenType openBracketType, CsTokenType closeBracketType) { Param.AssertNotNull(tokens, "tokens"); Param.AssertNotNull(methodNameLastToken, "methodNameLastToken"); Param.Ignore(openBracketType); Param.Ignore(closeBracketType); Debug.Assert(methodNameLastToken.Index <= tokens.Last.Index && methodNameLastToken.Index >= tokens.First.Index, "The token is not within the given list"); Node<CsToken> start = null; Node<CsToken> end = null; int bracketCount = 0; for (Node<CsToken> tokenNode = methodNameLastToken.Next; tokenNode != null; tokenNode = tokenNode.Next) { if (tokenNode.Value.CsTokenType == openBracketType) { ++bracketCount; if (bracketCount == 1) { start = tokenNode; } } else if (tokenNode.Value.CsTokenType == closeBracketType) { --bracketCount; if (bracketCount == 0) { end = tokenNode; break; } } } if (start == null || end == null) { return null; } return new CsTokenList(tokens.MasterList, start, end); }
/// <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); }
/// <summary> /// Initializes a new instance of the CsToken class. /// </summary> /// <param name="text"> /// The token string. /// </param> /// <param name="tokenType"> /// The token type. /// </param> /// <param name="location"> /// The location of the token within the code document. /// </param> /// <param name="parent"> /// References the parent code part. /// </param> /// <param name="generated"> /// True if the token is inside of a block of generated code. /// </param> internal CsToken(string text, CsTokenType tokenType, CodeLocation location, Reference<ICodePart> parent, bool generated) : this(text, tokenType, CsTokenClass.Token, location, parent, generated) { Param.Ignore(text, tokenType, location, parent, generated); }
/// <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); } } } } }
/// <summary> /// Gets a token of a specific type. /// </summary> /// <param name="tokenType">The type of the token to retrieve.</param> /// <param name="symbolType">The type of the expected symbol.</param> /// <param name="parentReference">Reference to the parent code part.</param> /// <param name="tokenParentReference">Reference to the parent of the new token.</param> /// <returns>Returns the token.</returns> private CsToken GetToken(CsTokenType tokenType, SymbolType symbolType, Reference<ICodePart> parentReference, Reference<ICodePart> tokenParentReference) { Param.Ignore(tokenType); Param.Ignore(symbolType); Param.AssertNotNull(parentReference, "parentReference"); Param.AssertNotNull(tokenParentReference, "tokenParentReference"); // Determine whether to skip past all standard types, or whether we should stop when we get // to one of these types, if that is the type we're looking for. SkipSymbols skip = SkipSymbols.All; if (symbolType == SymbolType.WhiteSpace) { skip &= ~SkipSymbols.WhiteSpace; } else if (symbolType == SymbolType.EndOfLine) { skip &= ~SkipSymbols.EndOfLine; } else if (symbolType == SymbolType.SingleLineComment) { skip &= ~SkipSymbols.SingleLineComment; } else if (symbolType == SymbolType.MultiLineComment) { skip &= ~SkipSymbols.MultiLineComment; } else if (symbolType == SymbolType.PreprocessorDirective) { skip &= ~SkipSymbols.Preprocessor; } else if (symbolType == SymbolType.XmlHeaderLine) { skip &= ~SkipSymbols.XmlHeader; } // Get the next symbol. Symbol symbol = this.GetNextSymbol(symbolType, skip, parentReference); this.symbols.Advance(); // Convert the symbol and return the token. return this.ConvertSymbol(symbol, tokenType, tokenParentReference); }
/// <summary> /// Gets a bracket token of a specific type. /// </summary> /// <param name="tokenType">The type of the token to retrieve.</param> /// <param name="symbolType">The type of the symbol.</param> /// <param name="parentReference">The parent code unit.</param> /// <returns>Returns the token.</returns> private Bracket GetBracketToken(CsTokenType tokenType, SymbolType symbolType, Reference<ICodePart> parentReference) { Param.Ignore(tokenType); Param.Ignore(symbolType); Param.AssertNotNull(parentReference, "parentReference"); Debug.Assert( tokenType == CsTokenType.OpenParenthesis || tokenType == CsTokenType.CloseParenthesis || tokenType == CsTokenType.OpenSquareBracket || tokenType == CsTokenType.CloseSquareBracket || tokenType == CsTokenType.OpenCurlyBracket || tokenType == CsTokenType.CloseCurlyBracket || tokenType == CsTokenType.OpenAttributeBracket || tokenType == CsTokenType.CloseAttributeBracket || tokenType == CsTokenType.OpenGenericBracket || tokenType == CsTokenType.CloseGenericBracket, "The token type is not a bracket."); Symbol symbol = this.GetNextSymbol(symbolType, parentReference); this.symbols.Advance(); return new Bracket(symbol.Text, tokenType, symbol.Location, parentReference, this.symbols.Generated); }
/// <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); } } } } }
public static string GetTokenType(CsTokenType pTokenType) { if (_typeRef.ContainsKey(pTokenType)) { return _typeRef[pTokenType]; } throw new Exception(@"Unknown Typeref: " + pTokenType); }
public AllTokensByTypePrecededByBannedElementIssueLocator(Func<CsElement, IEnumerable<CsToken>> getTokens, CsTokenType tokenTypeToInspect, IEnumerable<CsTokenType> bannedPredecessors) : base(getTokens, (token, violation) => token.CsTokenType == tokenTypeToInspect, bannedPredecessors) { }
public AllTokensByTypePrecededByBannedElementIssueLocator(Func <CsElement, IEnumerable <CsToken> > getTokens, CsTokenType tokenTypeToInspect, params CsTokenType[] bannedPredecessors) : base(getTokens, (token, violation) => token.CsTokenType == tokenTypeToInspect, bannedPredecessors) { }
/* * /// <summary> * /// Measures the number of lines taken up by comments and attributes before the given word. * /// </summary> * /// <param name="start">The start token.</param> * /// <returns>Returs the number of lines taken up.</returns> * private static int MeasureCommentLinesBefore(Node<CsToken> start) * { * Param.AssertNotNull(start, "start"); * * int lineSpan = 0; * int nextLineSpan = -1; * int nextStartLineNumber = -1; * * for (Node<CsToken> tokenNode = start.Previous; tokenNode != null; tokenNode = tokenNode.Previous) * { * if (tokenNode.Value.CsTokenType == CsTokenType.SingleLineComment || * tokenNode.Value.CsTokenType == CsTokenType.MultiLineComment || * tokenNode.Value.CsTokenType == CsTokenType.Attribute) * { * int itemLineSpan = ParameterPrewordOffset(tokenNode); * * if (nextStartLineNumber > 0 && tokenNode.Value.Location.EndPoint.LineNumber == nextStartLineNumber && nextLineSpan > 0) * { * --itemLineSpan; * } * * lineSpan += itemLineSpan; * nextLineSpan = itemLineSpan; * nextStartLineNumber = tokenNode.Value.LineNumber; * } * } * * return lineSpan; * } */ /// <summary> /// Gets the tokens forming the argument list for a method call. /// </summary> /// <param name="tokens"> /// The tokens forming the method call. /// </param> /// <param name="methodNameLastToken"> /// The last token before the argument list begins. /// </param> /// <param name="openBracketType"> /// The type of the opening bracket. /// </param> /// <param name="closeBracketType"> /// The type of the closing bracket. /// </param> /// <returns> /// Returns the argument list or null if it cannot be found. /// </returns> private static CsTokenList GetArgumentListTokens(CsTokenList tokens, Node <CsToken> methodNameLastToken, CsTokenType openBracketType, CsTokenType closeBracketType) { Param.AssertNotNull(tokens, "tokens"); Param.AssertNotNull(methodNameLastToken, "methodNameLastToken"); Param.Ignore(openBracketType); Param.Ignore(closeBracketType); Debug.Assert(methodNameLastToken.Index <= tokens.Last.Index && methodNameLastToken.Index >= tokens.First.Index, "The token is not within the given list"); Node <CsToken> start = null; Node <CsToken> end = null; int bracketCount = 0; for (Node <CsToken> tokenNode = methodNameLastToken.Next; tokenNode != null; tokenNode = tokenNode.Next) { if (tokenNode.Value.CsTokenType == openBracketType) { ++bracketCount; if (bracketCount == 1) { start = tokenNode; } } else if (tokenNode.Value.CsTokenType == closeBracketType) { --bracketCount; if (bracketCount == 0) { end = tokenNode; break; } } } if (start == null || end == null) { return(null); } return(new CsTokenList(tokens.MasterList, start, end)); }
private CsToken GetToken(CsTokenType tokenType, SymbolType symbolType) { SkipSymbols all = SkipSymbols.All; if (symbolType == SymbolType.WhiteSpace) { all &= ~SkipSymbols.WhiteSpace; } else if (symbolType == SymbolType.EndOfLine) { all &= ~SkipSymbols.EndOfLine; } else if (symbolType == SymbolType.SingleLineComment) { all &= ~SkipSymbols.SingleLineComment; } else if (symbolType == SymbolType.MultiLineComment) { all &= ~SkipSymbols.MultiLineComment; } else if (symbolType == SymbolType.PreprocessorDirective) { all &= ~SkipSymbols.Preprocessor; } else if (symbolType == SymbolType.XmlHeaderLine) { all &= ~SkipSymbols.XmlHeader; } Symbol nextSymbol = this.GetNextSymbol(symbolType, all); this.symbols.Advance(); return this.ConvertSymbol(nextSymbol, tokenType); }
/// <summary> /// Initializes a new instance of the CsToken class. /// </summary> /// <param name="text"> /// The token string. /// </param> /// <param name="tokenType"> /// The token type. /// </param> /// <param name="location"> /// The location of the token within the code document. /// </param> /// <param name="parent"> /// References the parent code part. /// </param> /// <param name="generated"> /// True if the token is inside of a block of generated code. /// </param> internal CsToken(string text, CsTokenType tokenType, CodeLocation location, Reference <ICodePart> parent, bool generated) : this(text, tokenType, CsTokenClass.Token, location, parent, generated) { Param.Ignore(text, tokenType, location, parent, generated); }
/// <summary> /// Converts a symbol to the given token type. /// </summary> /// <param name="symbol">The symbol to convert.</param> /// <param name="tokenType">The type of the token to retrieve.</param> /// <param name="parentReference">The parent code part.</param> /// <returns>Returns the token.</returns> private CsToken ConvertSymbol(Symbol symbol, CsTokenType tokenType, Reference<ICodePart> parentReference) { Param.AssertNotNull(symbol, "symbol"); Param.Ignore(tokenType); Param.AssertNotNull(parentReference, "parentReference"); // Create the appropriate token based on the type of the symbol. if (symbol.SymbolType == SymbolType.WhiteSpace) { Debug.Assert(tokenType == CsTokenType.WhiteSpace, "The token type is wrong."); return new Whitespace(symbol.Text, symbol.Location, parentReference, this.symbols.Generated); } else if (symbol.SymbolType == SymbolType.Number) { Debug.Assert(tokenType == CsTokenType.Number, "The token type is wrong."); return new Number(symbol.Text, symbol.Location, parentReference, this.symbols.Generated); } else if (symbol.SymbolType == SymbolType.PreprocessorDirective) { Debug.Assert(tokenType == CsTokenType.PreprocessorDirective, "The token type is wrong."); return this.GetPreprocessorDirectiveToken(symbol, parentReference, this.symbols.Generated); } else { // Brackets are created using the GetBracketToken method. Debug.Assert(symbol.SymbolType != SymbolType.OpenParenthesis, "Do not use this method for converting brackets."); Debug.Assert(symbol.SymbolType != SymbolType.CloseParenthesis, "Do not use this method for converting brackets."); Debug.Assert(symbol.SymbolType != SymbolType.OpenSquareBracket, "Do not use this method for converting brackets."); Debug.Assert(symbol.SymbolType != SymbolType.CloseSquareBracket, "Do not use this method for converting brackets."); Debug.Assert(symbol.SymbolType != SymbolType.OpenCurlyBracket, "Do not use this method for converting brackets."); Debug.Assert(symbol.SymbolType != SymbolType.CloseCurlyBracket, "Do not use this method for converting brackets."); Debug.Assert(symbol.SymbolType != SymbolType.Attribute, "Do not use this method for converting attributes."); return new CsToken(symbol.Text, tokenType, CsTokenClass.Token, symbol.Location, parentReference, this.symbols.Generated); } }
public AllTokensByTypeNotPrecededByRequiredElementIssueLocator(Func<CsElement, IEnumerable<CsToken>> getTokens, CsTokenType tokenTypeToInspect, params CsTokenType[] requiredPredecessors) : base(getTokens, (token, violation) => token.CsTokenType == tokenTypeToInspect, requiredPredecessors) { }
/// <summary> /// Gets a token of a specific type. /// </summary> /// <param name="tokenType">The type of the token to retrieve.</param> /// <param name="symbolType">The type of the expected symbol.</param> /// <param name="parentReference">Reference to the parent code part.</param> /// <returns>Returns the token.</returns> private CsToken GetToken(CsTokenType tokenType, SymbolType symbolType, Reference<ICodePart> parentReference) { Param.Ignore(tokenType); Param.Ignore(symbolType); Param.AssertNotNull(parentReference, "parentReference"); return this.GetToken(tokenType, symbolType, parentReference, parentReference); }
/// <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); } } }
private void CheckDeclarationKeywordOrder(CsElement element) { Param.AssertNotNull(element, "element"); int accessModifierIndex = -1; int staticIndex = -1; int otherWordIndex = -1; int index = 0; foreach (CsToken token in element.Declaration.Tokens) { CsTokenType type = token.CsTokenType; if (type == CsTokenType.Private || type == CsTokenType.Public || type == CsTokenType.Protected || type == CsTokenType.Internal) { if (accessModifierIndex == -1) { accessModifierIndex = index++; } } else if (type == CsTokenType.Static) { if (staticIndex == -1) { staticIndex = index++; } } else if (type != CsTokenType.WhiteSpace && type != CsTokenType.EndOfLine && type != CsTokenType.SingleLineComment && type != CsTokenType.MultiLineComment) { if (otherWordIndex == -1) { otherWordIndex = index++; } } } if (accessModifierIndex != -1) { if (staticIndex > -1 && staticIndex < accessModifierIndex) { this.AddViolation( element, Rules.DeclarationKeywordsMustFollowOrder, Strings.AccessModifier, string.Format(CultureInfo.InvariantCulture, "'{0}'", Strings.Static)); } if (otherWordIndex > -1 && otherWordIndex < accessModifierIndex) { this.AddViolation( element, Rules.DeclarationKeywordsMustFollowOrder, Strings.AccessModifier, string.Format(CultureInfo.InvariantCulture, "'{0}'", Strings.Other)); } } if (staticIndex > -1) { if (otherWordIndex > -1 && otherWordIndex < staticIndex) { this.AddViolation( element, Rules.DeclarationKeywordsMustFollowOrder, string.Format(CultureInfo.InvariantCulture, "'{0}'", Strings.Static), string.Format(CultureInfo.InvariantCulture, "'{0}'", Strings.Other)); } } // Check to make sure that 'protected' comes just before 'internal'. if (element.Declaration.AccessModifierType == AccessModifierType.ProtectedInternal) { bool foundProtected = false; foreach (CsToken token in element.Declaration.Tokens) { if (foundProtected) { if (token.CsTokenType == CsTokenType.Internal) { break; } else if (token.CsTokenType != CsTokenType.WhiteSpace) { this.AddViolation(element, Rules.ProtectedMustComeBeforeInternal); break; } } else { if (token.CsTokenType == CsTokenType.Protected) { foundProtected = true; } else if (token.CsTokenType == CsTokenType.Internal) { this.AddViolation(element, Rules.ProtectedMustComeBeforeInternal); break; } } } } }
/// <summary> /// Checks a method or method invocation to ensure that the opening bracket is /// on the same line as the method declaration. /// </summary> /// <param name="element"> /// The element containing the expression. /// </param> /// <param name="parameterListTokens"> /// The tokens in the parameter list. /// </param> /// <param name="openingBracketType"> /// The type of the bracket that opens the parameter list. /// </param> /// <param name="textToUseForContainingElement"> /// The text to use in the violation. /// </param> /// <returns> /// Returns the opening bracket. /// </returns> private Node<CsToken> CheckMethodOpeningBracket( CsElement element, CsTokenList parameterListTokens, CsTokenType openingBracketType, string textToUseForContainingElement) { Param.AssertNotNull(element, "element"); Param.AssertNotNull(parameterListTokens, "parameterListTokens"); Param.Ignore(openingBracketType); // Find the opening bracket. Node<CsToken> openingBracketNode = null; for (Node<CsToken> tokenNode = parameterListTokens.First; tokenNode != null; tokenNode = tokenNode.Next) { if (tokenNode.Value.CsTokenType == openingBracketType) { openingBracketNode = tokenNode; break; } } CsToken lastWord = null; if (openingBracketNode != null) { // Find the last word before the opening bracket. for (Node<CsToken> tokenNode = openingBracketNode.Previous; tokenNode != null; tokenNode = tokenNode.Previous) { if (tokenNode.Value.CsTokenType != CsTokenType.WhiteSpace && tokenNode.Value.CsTokenType != CsTokenType.EndOfLine && tokenNode.Value.CsTokenType != CsTokenType.SingleLineComment && tokenNode.Value.CsTokenType != CsTokenType.MultiLineComment) { lastWord = tokenNode.Value; break; } } } if (lastWord != null) { if (openingBracketNode.Value.LineNumber != lastWord.LineNumber) { this.AddViolation(element, openingBracketNode.Value.LineNumber, Rules.OpeningParenthesisMustBeOnDeclarationLine, textToUseForContainingElement); } } return openingBracketNode; }
/// <summary> /// Initializes a new instance of the CsToken class. /// </summary> /// <param name="text"> /// The token string. /// </param> /// <param name="tokenType"> /// The token type. /// </param> /// <param name="tokenClass"> /// The token class. /// </param> /// <param name="location"> /// The location of the token within the code document. /// </param> /// <param name="parent"> /// References the parent code part. /// </param> /// <param name="generated"> /// True if the token is inside of a block of generated code. /// </param> internal CsToken(string text, CsTokenType tokenType, CsTokenClass tokenClass, CodeLocation location, Reference<ICodePart> parent, bool generated) { Param.AssertNotNull(text, "text"); Param.Ignore(tokenType); Param.Ignore(tokenClass); Param.Ignore(location); Param.AssertNotNull(parent, "parent"); Param.Ignore(generated); this.text = text; this.tokenType = tokenType; this.tokenClass = tokenClass; this.location = location; this.parent = parent; this.generated = generated; }
/// <summary> /// Removes whitespace and comments from the beginning and end of the token list. /// </summary> /// <param name="types"> /// The types to trim. /// </param> /// <returns> /// Returns the number of tokens that were trimmed from the beginning of the list. /// </returns> internal int Trim(params CsTokenType[] types) { Param.Ignore(types); if (types == null || types.Length == 0) { return(0); } Node <CsToken> begin = null; Node <CsToken> end = null; int trimCount = 0; // Trim the beginning of the list. for (Node <CsToken> tokenNode = this.First; !this.OutOfBounds(tokenNode); tokenNode = tokenNode.Next) { CsTokenType tokenType = tokenNode.Value.CsTokenType; bool match = false; for (int i = 0; i < types.Length; ++i) { if (tokenType == types[i]) { match = true; break; } } if (!match) { begin = tokenNode; break; } ++trimCount; } // Trim the end of the list. for (Node <CsToken> tokenNode = this.Last; !this.OutOfBounds(tokenNode); tokenNode = tokenNode.Previous) { CsTokenType tokenType = tokenNode.Value.CsTokenType; bool match = false; for (int i = 0; i < types.Length; ++i) { if (tokenType == types[i]) { match = true; break; } } if (!match) { end = tokenNode; break; } } this.First = begin; this.Last = end; return(trimCount); }
public AllTokensByTypeNotPrecededByRequiredElementIssueLocator(Func <CsElement, IEnumerable <CsToken> > getTokens, CsTokenType tokenTypeToInspect, IEnumerable <CsTokenType> requiredPredecessors) : base(getTokens, (token, violation) => token.CsTokenType == tokenTypeToInspect, requiredPredecessors) { }
private CsToken ConvertSymbol(Symbol symbol, CsTokenType tokenType) { if (symbol.SymbolType == SymbolType.WhiteSpace) { return new Whitespace(symbol.Text, symbol.Location, this.symbols.Generated); } if (symbol.SymbolType == SymbolType.Number) { return new Microsoft.StyleCop.CSharp.Number(symbol.Text, symbol.Location, this.symbols.Generated); } if (symbol.SymbolType == SymbolType.PreprocessorDirective) { return this.GetPreprocessorDirectiveToken(symbol, this.symbols.Generated); } return new CsToken(symbol.Text, tokenType, CsTokenClass.Token, symbol.Location, this.symbols.Generated); }
public AllTokensByTypeFollowedByBannedElementIssueLocator(Func<CsElement, IEnumerable<CsToken>> getTokens, CsTokenType tokenTypeToInspect, params CsTokenType[] bannedFollowers) : base(getTokens, (token, violation) => token.CsTokenType == tokenTypeToInspect, bannedFollowers) { }
private Bracket GetBracketToken(CsTokenType tokenType, SymbolType symbolType) { Symbol nextSymbol = this.GetNextSymbol(symbolType); this.symbols.Advance(); return new Bracket(nextSymbol.Text, tokenType, nextSymbol.Location, this.symbols.Generated); }
private static CsTokenType convertToken(CsTokenType pInToken) { switch (pInToken) { case CsTokenType.tkPLUS_EQ: return CsTokenType.tkPLUS; case CsTokenType.tkMINUS_EQ: return CsTokenType.tkMINUS; case CsTokenType.tkDIV_EQ: return CsTokenType.tkDIV; case CsTokenType.tkMUL_EQ: return CsTokenType.tkSTAR; case CsTokenType.tkMOD_EQ: return CsTokenType.tkMOD; default: throw new Exception(); } }
public AllTokensByTypeFollowedByBannedElementIssueLocator(Func <CsElement, IEnumerable <CsToken> > getTokens, CsTokenType tokenTypeToInspect, IEnumerable <CsTokenType> bannedFollowers) : base(getTokens, (token, violation) => token.CsTokenType == tokenTypeToInspect, bannedFollowers) { }
public AllTokensByTypeNotFollowedByRequiredElementIssueLocator(Func <CsElement, IEnumerable <CsToken> > getTokens, CsTokenType tokenTypeToInspect, params CsTokenType[] requiredFollowers) : base(getTokens, (token, violation) => token.CsTokenType == tokenTypeToInspect, requiredFollowers) { }
public AllTokensByTypeNotFollowedByRequiredElementIssueLocator(Func<CsElement, IEnumerable<CsToken>> getTokens, CsTokenType tokenTypeToInspect, Func<CsToken, bool> isRequiredFollower) : base(getTokens, (token, violation) => token.CsTokenType == tokenTypeToInspect, isRequiredFollower) { }