Пример #1
0
        /// <summary>
        /// Parses an attribute.
        /// </summary>
        /// <param name="parentReference">The parent code unit.</param>
        /// <param name="unsafeCode">Indicates whether the attribute lies within a block of unsafe code.</param>
        /// <param name="masterDocument">The master document object.</param>
        /// <returns>Returns the attribute.</returns>
        internal Attribute ParseAttribute(Reference<ICodePart> parentReference, bool unsafeCode, CsDocument masterDocument)
        {
            Param.AssertNotNull(parentReference, "parentReference");
            Param.Ignore(unsafeCode);
            Param.AssertNotNull(masterDocument, "masterDocument");

            Debug.Assert(this.document == null, "A CodeParser instance may only be used once.");
            this.document = masterDocument;

            var attributeReference = new Reference<ICodePart>();

            // Get the first symbol and make sure it is the right type.
            Symbol firstSymbol = this.symbols.Peek(1);
            Debug.Assert(firstSymbol != null && firstSymbol.SymbolType == SymbolType.OpenSquareBracket, "Expected an opening square bracket");

            // The list of attribute expressions in the attribute.
            List<AttributeExpression> attributeExpressions = new List<AttributeExpression>();

            // Move past the opening square bracket.
            Bracket openingBracket = new Bracket(
                firstSymbol.Text, CsTokenType.OpenAttributeBracket, firstSymbol.Location, attributeReference, this.symbols.Generated);
            Node<CsToken> openingBracketNode = this.tokens.InsertLast(openingBracket);
            this.symbols.Advance();

            // Get each of the child attribute expressions within this attribute.
            while (true)
            {
                // Move to the next symbol.
                this.AdvanceToNextCodeSymbol(attributeReference);
                Symbol symbol = this.symbols.Peek(1);
                if (symbol == null)
                {
                    throw new SyntaxException(this.document.SourceCode, firstSymbol.LineNumber);
                }

                // Check the type. If this is the closing bracket then we are done.
                if (symbol.SymbolType == SymbolType.CloseSquareBracket)
                {
                    Bracket closingBracket = new Bracket(
                        symbol.Text, CsTokenType.CloseAttributeBracket, symbol.Location, attributeReference, this.symbols.Generated);
                    Node<CsToken> closingBracketNode = this.tokens.InsertLast(closingBracket);
                    this.symbols.Advance();

                    openingBracket.MatchingBracketNode = closingBracketNode;
                    closingBracket.MatchingBracketNode = openingBracketNode;

                    break;
                }

                // Check to see if there is a target specified.
                LiteralExpression target = null;

                this.AdvanceToNextCodeSymbol(attributeReference);
                symbol = this.symbols.Peek(1);
                if (symbol == null)
                {
                    throw new SyntaxException(this.document.SourceCode, firstSymbol.LineNumber);
                }

                Node<CsToken> previousTokenNode = this.tokens.Last;
                var attributeExpressionReference = new Reference<ICodePart>();

                if (symbol.SymbolType == SymbolType.Other || symbol.SymbolType == SymbolType.Return)
                {
                    // Peek ahead to the next symbol and check if it's a colon.
                    int index = this.GetNextCodeSymbolIndex(2);
                    if (index != -1)
                    {
                        Symbol colon = this.symbols.Peek(index);

                        if (colon.SymbolType == SymbolType.Colon)
                        {
                            // This is a target. Get the literal target expression and move past the colon.
                            // Change the type of the target symbol to OTHER so that it will be parsed
                            // correctly by the literal expression parser.
                            symbol.SymbolType = SymbolType.Other;

                            // Get the literal expression.
                            var targetReference = new Reference<ICodePart>();
                            target = this.GetLiteralExpression(targetReference, unsafeCode);
                            Debug.Assert(target != null, "We should always succeed getting a Literal here since we have already seen a type of Other.");

                            targetReference.Target = target;

                            // Add the colon.
                            this.AdvanceToNextCodeSymbol(attributeExpressionReference);
                            this.tokens.Add(new CsToken(colon.Text, CsTokenType.AttributeColon, symbol.Location, attributeExpressionReference, this.symbols.Generated));
                            this.symbols.Advance();

                            this.AdvanceToNextCodeSymbol(attributeExpressionReference);
                        }
                    }
                }

                // Now get the attribute call expression.
                Expression initialization = this.GetNextExpression(ExpressionPrecedence.None, attributeExpressionReference, unsafeCode);
                if (initialization == null)
                {
                    throw new SyntaxException(this.document.SourceCode, firstSymbol.LineNumber);
                }

                // Create and add the attribute expression.
                Debug.Assert(previousTokenNode.Next != null, "Nothing was added to the token list!");

                var attributeExpression = new AttributeExpression(
                    new CsTokenList(this.tokens, previousTokenNode.Next, this.tokens.Last),
                    target,
                    initialization);

                attributeExpressions.Add(attributeExpression);
                attributeExpressionReference.Target = attributeExpression;

                // Get the next item, which must either be a comma or the closing attribute bracket.
                this.AdvanceToNextCodeSymbol(attributeReference);
                symbol = this.symbols.Peek(1);
                if (symbol == null)
                {
                    throw new SyntaxException(this.document.SourceCode, firstSymbol.LineNumber);
                }

                if (symbol.SymbolType == SymbolType.Comma)
                {
                    // Add the comma and continue.
                    this.tokens.Add(this.GetToken(CsTokenType.Comma, SymbolType.Comma, attributeReference));
                }
                else if (symbol.SymbolType != SymbolType.CloseSquareBracket)
                {
                    // This type of symbol is unexpected.
                    throw new SyntaxException(this.document.SourceCode, firstSymbol.LineNumber);
                }
            }

            // Get the location of the attribute.
            CodeLocation location = CsToken.JoinLocations(this.tokens.First, this.tokens.Last);

            // Create and return the attribute.
            var attribute = new Attribute(
                this.tokens,
                location,
                parentReference,
                attributeExpressions.ToArray(),
                this.tokens.First.Value.Generated || this.tokens.Last.Value.Generated);

            attributeReference.Target = attribute;
            return attribute;
        }
        /// <summary>
        /// Parses the iterators from a for-statement.
        /// </summary>
        /// <param name="statementReference">A reference to the statement being created.</param>
        /// <param name="unsafeCode">Indicates whether the code is located within an unsafe block.</param>
        /// <param name="openParenthesis">The opening parentheis.</param>
        /// <param name="openParenthesisNode">The opening parenthesis node.</param>
        /// <returns>Returns the list of iterators.</returns>
        private List<Expression> ParseForStatementIterators(
            Reference<ICodePart> statementReference, bool unsafeCode, Bracket openParenthesis, Node<CsToken> openParenthesisNode)
        {
            Param.AssertNotNull(statementReference, "statementReference");
            Param.Ignore(unsafeCode);
            Param.AssertNotNull(openParenthesis, "openParenthesis");
            Param.AssertNotNull(openParenthesisNode, "openParenthesisNode");

            // Get the iterators.
            List<Expression> iterators = new List<Expression>();

            while (true)
            {
                // Check the type of the next symbol.
                Symbol symbol = this.GetNextSymbol(statementReference);

                if (symbol.SymbolType == SymbolType.CloseParenthesis)
                {
                    // This is the end of the iterator list. Add the parenthesis and break.
                    Bracket closeParenthesis = this.GetBracketToken(CsTokenType.CloseParenthesis, SymbolType.CloseParenthesis, statementReference);
                    Node<CsToken> closeParenthesisNode = this.tokens.InsertLast(closeParenthesis);

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

                // Get the next iterator expression.
                Expression iterator = this.GetNextExpression(ExpressionPrecedence.None, statementReference, unsafeCode);
                if (iterator == null || iterator.Tokens.First == null)
                {
                    throw new SyntaxException(this.document.SourceCode, symbol.LineNumber);
                }

                // Add the initializer to the list.
                iterators.Add(iterator);

                // If the next symbol is a comma, save it.
                symbol = this.GetNextSymbol(statementReference);

                if (symbol.SymbolType == SymbolType.Comma)
                {
                    this.tokens.Add(this.GetToken(CsTokenType.Comma, SymbolType.Comma, statementReference));
                }
                else if (symbol.SymbolType != SymbolType.CloseParenthesis)
                {
                    // If it's not a comma it must be a closing parenthesis.
                    throw new SyntaxException(this.document.SourceCode, symbol.LineNumber);
                }
            }

            return iterators;
        }
Пример #3
0
        private MasterList<CsToken> GetGenericArgumentList(
            Reference<ICodePart> genericTypeReference, bool unsafeCode, CsToken name, int startIndex, out int endIndex)
        {
            Param.AssertNotNull(genericTypeReference, "genericTypeReference");
            Param.Ignore(unsafeCode);
            Param.Ignore(name);
            Param.AssertGreaterThanOrEqualToZero(startIndex, "startIndex");

            endIndex = -1;
            MasterList<CsToken> genericArgumentListTokens = null;

            // Move past whitespace and comments.
            int index = startIndex;
            while (true)
            {
                Symbol next = this.symbols.Peek(index);
                if (next == null ||
                    (next.SymbolType != SymbolType.WhiteSpace &&
                     next.SymbolType != SymbolType.EndOfLine &&
                     next.SymbolType != SymbolType.SingleLineComment &&
                     next.SymbolType != SymbolType.MultiLineComment &&
                     next.SymbolType != SymbolType.PreprocessorDirective))
                {
                    break;
                }

                ++index;
            }

            // The next symbol should be an opening bracket, if this is a generic.
            Symbol symbol = this.symbols.Peek(index);
            if (symbol != null && symbol.SymbolType == SymbolType.LessThan)
            {
                // This might be a generic. Assume that it is and start creating tokens.
                genericArgumentListTokens = new MasterList<CsToken>();

                // Add the name if one was provided.
                if (name != null)
                {
                    genericArgumentListTokens.Add(name);
                }

                Node<CsToken> openingGenericBracketNode = null;

                // Add everything up to the opening bracket into the token list.
                for (int i = startIndex; i <= index; ++i)
                {
                    symbol = this.symbols.Peek(i);
                    Debug.Assert(symbol != null, "The next symbol should not be null");

                    if (symbol.SymbolType == SymbolType.LessThan)
                    {
                        if (openingGenericBracketNode != null)
                        {
                            // This is not a generic statement.
                            return null;
                        }

                        Bracket openingGenericBracket = new Bracket(
                            symbol.Text, CsTokenType.OpenGenericBracket, symbol.Location, genericTypeReference, this.symbols.Generated);
                        openingGenericBracketNode = genericArgumentListTokens.InsertLast(openingGenericBracket);
                    }
                    else
                    {
                        genericArgumentListTokens.Add(this.ConvertSymbol(symbol, TokenTypeFromSymbolType(symbol.SymbolType), genericTypeReference));
                    }
                }

                // Loop through the rest of the symbols.
                while (true)
                {
                    symbol = this.symbols.Peek(++index);
                    if (symbol == null)
                    {
                        // The code ran out before we found the end of the generic.
                        throw new SyntaxException(this.document.SourceCode, name.LineNumber);
                    }
                    else if (symbol.SymbolType == SymbolType.GreaterThan)
                    {
                        if (openingGenericBracketNode == null)
                        {
                            // This is not a generic statement.
                            return null;
                        }

                        // This is the end of the generic statement. Add the closing bracket to the token list.
                        Bracket closingGenericBracket = new Bracket(
                            symbol.Text, CsTokenType.CloseGenericBracket, symbol.Location, genericTypeReference, this.symbols.Generated);
                        Node<CsToken> closingGenericBracketNode = genericArgumentListTokens.InsertLast(closingGenericBracket);

                        ((Bracket)openingGenericBracketNode.Value).MatchingBracketNode = closingGenericBracketNode;
                        closingGenericBracket.MatchingBracketNode = openingGenericBracketNode;

                        endIndex = index;
                        break;
                    }
                    else if (symbol.SymbolType == SymbolType.Out || symbol.SymbolType == SymbolType.In)
                    {
                        // Get the in or out keyword.
                        genericArgumentListTokens.Add(this.ConvertSymbol(
                            symbol, symbol.SymbolType == SymbolType.In ? CsTokenType.In : CsTokenType.Out, genericTypeReference));
                    }
                    else if (symbol.SymbolType == SymbolType.Other)
                    {
                        int lastIndex = 0;
                        var wordReference = new Reference<ICodePart>();
                        CsToken word = this.GetTypeTokenAux(wordReference, genericTypeReference, unsafeCode, true, false, index, out lastIndex);
                        if (word == null)
                        {
                            throw new SyntaxException(this.document.SourceCode, symbol.LineNumber);
                        }

                        // Advance the index to the end of the token.
                        index = lastIndex;

                        // Add the token.
                        genericArgumentListTokens.Add(word);
                    }
                    else if (symbol.SymbolType == SymbolType.WhiteSpace ||
                        symbol.SymbolType == SymbolType.EndOfLine ||
                        symbol.SymbolType == SymbolType.SingleLineComment ||
                        symbol.SymbolType == SymbolType.MultiLineComment ||
                        symbol.SymbolType == SymbolType.PreprocessorDirective)
                    {
                        // Add these to the token list.
                        genericArgumentListTokens.Add(this.ConvertSymbol(symbol, TokenTypeFromSymbolType(symbol.SymbolType), genericTypeReference));
                    }
                    else if (symbol.SymbolType == SymbolType.Comma)
                    {
                        genericArgumentListTokens.Add(this.ConvertSymbol(symbol, CsTokenType.Comma, genericTypeReference));
                    }
                    else if (symbol.SymbolType == SymbolType.OpenSquareBracket)
                    {
                        // An attribute on the generic type.
                        genericArgumentListTokens.Add(this.GetAttribute(genericTypeReference, unsafeCode));
                    }
                    else
                    {
                        // Any other symbol signifies that this is not a generic statement.
                        genericArgumentListTokens = null;
                        break;
                    }
                }
            }

            return genericArgumentListTokens;
        }
Пример #4
0
        /// <summary>
        /// Gets array brackets symbol for a type token, if they exist.
        /// </summary>
        /// <param name="typeTokenReference">A reference to the type token.</param>
        /// <param name="typeTokens">The tokens within the type token.</param>
        /// <param name="startIndex">The start index within the symbols.</param>
        private void GetTypeTokenArrayBrackets(Reference<ICodePart> typeTokenReference, MasterList<CsToken> typeTokens, ref int startIndex)
        {
            Param.AssertNotNull(typeTokenReference, "typeTokenReference");
            Param.AssertNotNull(typeTokens, "typeTokens");
            Param.AssertGreaterThanOrEqualToZero(startIndex, "startIndex");

            int index = this.GetNextCodeSymbolIndex(startIndex);
            if (index != -1)
            {
                Symbol symbol = this.symbols.Peek(index);
                if (symbol.SymbolType == SymbolType.OpenSquareBracket)
                {
                    // Add the tokens up to this point.
                    for (int i = startIndex; i <= index - 1; ++i)
                    {
                        Symbol symbolToConvert = this.symbols.Peek(startIndex);
                        typeTokens.Add(this.ConvertSymbol(symbolToConvert, TokenTypeFromSymbolType(symbolToConvert.SymbolType), typeTokenReference));

                        ++startIndex;
                    }

                    // Now collect the brackets.
                    Node<CsToken> openingBracketNode = null;
                    while (true)
                    {
                        symbol = this.symbols.Peek(startIndex);
                        if (symbol.SymbolType == SymbolType.WhiteSpace ||
                            symbol.SymbolType == SymbolType.EndOfLine ||
                            symbol.SymbolType == SymbolType.SingleLineComment ||
                            symbol.SymbolType == SymbolType.MultiLineComment ||
                            symbol.SymbolType == SymbolType.PreprocessorDirective)
                        {
                            typeTokens.Add(this.ConvertSymbol(symbol, TokenTypeFromSymbolType(symbol.SymbolType), typeTokenReference));
                            ++startIndex;
                        }
                        else if (symbol.SymbolType == SymbolType.Number)
                        {
                            typeTokens.Add(this.ConvertSymbol(symbol, CsTokenType.Number, typeTokenReference));
                            ++startIndex;
                        }
                        else if (symbol.SymbolType == SymbolType.Other)
                        {
                            // A const used in place of a number.
                            typeTokens.Add(this.ConvertSymbol(symbol, CsTokenType.Other, typeTokenReference));
                            ++startIndex;
                        }
                        else if (symbol.SymbolType == SymbolType.Comma)
                        {
                            typeTokens.Add(this.ConvertSymbol(symbol, CsTokenType.Comma, typeTokenReference));
                            ++startIndex;
                        }
                        else if (symbol.SymbolType == SymbolType.OpenSquareBracket)
                        {
                            if (openingBracketNode != null)
                            {
                                throw new SyntaxException(this.document.SourceCode, symbol.LineNumber);
                            }

                            Bracket openingBracket = new Bracket(
                                symbol.Text, CsTokenType.OpenSquareBracket, symbol.Location, typeTokenReference, this.symbols.Generated);
                            openingBracketNode = typeTokens.InsertLast(openingBracket);
                            ++startIndex;
                        }
                        else if (symbol.SymbolType == SymbolType.CloseSquareBracket)
                        {
                            if (openingBracketNode == null)
                            {
                                throw new SyntaxException(this.document.SourceCode, symbol.LineNumber);
                            }

                            Bracket closingBracket = new Bracket(
                                symbol.Text, CsTokenType.CloseSquareBracket, symbol.Location, typeTokenReference, this.symbols.Generated);
                            Node<CsToken> closingBracketNode = typeTokens.InsertLast(closingBracket);
                            ++startIndex;

                            ((Bracket)openingBracketNode.Value).MatchingBracketNode = closingBracketNode;
                            closingBracket.MatchingBracketNode = openingBracketNode;

                            openingBracketNode = null;

                            // Check whether the next character is another opening bracket.
                            int temp = this.GetNextCodeSymbolIndex(startIndex);
                            if (temp != -1 && this.symbols.Peek(temp).SymbolType != SymbolType.OpenSquareBracket)
                            {
                                break;
                            }
                        }
                        else
                        {
                            if (openingBracketNode != null)
                            {
                                throw new SyntaxException(this.document.SourceCode, symbol.LineNumber);
                            }

                            break;
                        }
                    }
                }
            }
        }
Пример #5
0
        private static string GetOpeningOrClosingBracketText(Bracket bracket)
        {
            switch (bracket.CsTokenType)
            {
                case CsTokenType.OpenParenthesis:
                case CsTokenType.OpenCurlyBracket:
                case CsTokenType.OpenSquareBracket:
                case CsTokenType.OpenGenericBracket:
                case CsTokenType.OpenAttributeBracket:
                    return Microsoft.StyleCop.CSharp.Strings.Opening;

                case CsTokenType.CloseParenthesis:
                case CsTokenType.CloseCurlyBracket:
                case CsTokenType.CloseSquareBracket:
                case CsTokenType.CloseGenericBracket:
                case CsTokenType.CloseAttributeBracket:
                    return Microsoft.StyleCop.CSharp.Strings.Closing;
            }
            return string.Empty;
        }
Пример #6
0
 private void GetTypeTokenArrayBrackets(MasterList<CsToken> typeTokens, ref int startIndex)
 {
     Symbol symbol;
     int nextCodeSymbolIndex = this.GetNextCodeSymbolIndex(startIndex);
     if ((nextCodeSymbolIndex == -1) || (this.symbols.Peek(nextCodeSymbolIndex).SymbolType != SymbolType.OpenSquareBracket))
     {
         return;
     }
     for (int i = startIndex; i <= (nextCodeSymbolIndex - 1); i++)
     {
         Symbol symbol2 = this.symbols.Peek(startIndex);
         typeTokens.Add(this.ConvertSymbol(symbol2, TokenTypeFromSymbolType(symbol2.SymbolType)));
         startIndex++;
     }
     Microsoft.StyleCop.Node<CsToken> node = null;
     Label_0067:
     symbol = this.symbols.Peek(startIndex);
     if (((symbol.SymbolType == SymbolType.WhiteSpace) || (symbol.SymbolType == SymbolType.EndOfLine)) || (((symbol.SymbolType == SymbolType.SingleLineComment) || (symbol.SymbolType == SymbolType.MultiLineComment)) || (symbol.SymbolType == SymbolType.PreprocessorDirective)))
     {
         typeTokens.Add(this.ConvertSymbol(symbol, TokenTypeFromSymbolType(symbol.SymbolType)));
         startIndex++;
         goto Label_0067;
     }
     if (symbol.SymbolType == SymbolType.Number)
     {
         typeTokens.Add(this.ConvertSymbol(symbol, CsTokenType.Number));
         startIndex++;
         goto Label_0067;
     }
     if (symbol.SymbolType == SymbolType.Other)
     {
         typeTokens.Add(this.ConvertSymbol(symbol, CsTokenType.Other));
         startIndex++;
         goto Label_0067;
     }
     if (symbol.SymbolType == SymbolType.Comma)
     {
         typeTokens.Add(this.ConvertSymbol(symbol, CsTokenType.Comma));
         startIndex++;
         goto Label_0067;
     }
     if (symbol.SymbolType == SymbolType.OpenSquareBracket)
     {
         if (node != null)
         {
             throw new SyntaxException(this.document.SourceCode, symbol.LineNumber);
         }
         Bracket item = new Bracket(symbol.Text, CsTokenType.OpenSquareBracket, symbol.Location, this.symbols.Generated);
         node = typeTokens.InsertLast(item);
         startIndex++;
         goto Label_0067;
     }
     if (symbol.SymbolType == SymbolType.CloseSquareBracket)
     {
         if (node == null)
         {
             throw new SyntaxException(this.document.SourceCode, symbol.LineNumber);
         }
         Bracket bracket2 = new Bracket(symbol.Text, CsTokenType.CloseSquareBracket, symbol.Location, this.symbols.Generated);
         Microsoft.StyleCop.Node<CsToken> node2 = typeTokens.InsertLast(bracket2);
         startIndex++;
         ((Bracket) node.Value).MatchingBracketNode = node2;
         bracket2.MatchingBracketNode = node;
         node = null;
         int count = this.GetNextCodeSymbolIndex(startIndex);
         if ((count != -1) && (this.symbols.Peek(count).SymbolType != SymbolType.OpenSquareBracket))
         {
             return;
         }
         goto Label_0067;
     }
     if (node != null)
     {
         throw new SyntaxException(this.document.SourceCode, symbol.LineNumber);
     }
 }
Пример #7
0
 private List<Expression> ParseForStatementIterators(bool unsafeCode, Bracket openParenthesis, Microsoft.StyleCop.Node<CsToken> openParenthesisNode)
 {
     List<Expression> list = new List<Expression>();
     while (true)
     {
         Symbol nextSymbol = this.GetNextSymbol();
         if (nextSymbol.SymbolType == SymbolType.CloseParenthesis)
         {
             Bracket bracketToken = this.GetBracketToken(CsTokenType.CloseParenthesis, SymbolType.CloseParenthesis);
             Microsoft.StyleCop.Node<CsToken> node = this.tokens.InsertLast(bracketToken);
             openParenthesis.MatchingBracketNode = node;
             bracketToken.MatchingBracketNode = openParenthesisNode;
             return list;
         }
         Expression nextExpression = this.GetNextExpression(ExpressionPrecedence.None, unsafeCode);
         if ((nextExpression == null) || (nextExpression.Tokens.First == null))
         {
             throw new SyntaxException(this.document.SourceCode, nextSymbol.LineNumber);
         }
         list.Add(nextExpression);
         nextSymbol = this.GetNextSymbol();
         if (nextSymbol.SymbolType != SymbolType.Comma)
         {
             if (nextSymbol.SymbolType != SymbolType.CloseParenthesis)
             {
                 throw new SyntaxException(this.document.SourceCode, nextSymbol.LineNumber);
             }
         }
         else
         {
             this.tokens.Add(this.GetToken(CsTokenType.Comma, SymbolType.Comma));
         }
     }
 }
Пример #8
0
 internal Microsoft.StyleCop.CSharp.Attribute ParseAttribute(bool unsafeCode)
 {
     Symbol symbol = this.symbols.Peek(1);
     List<AttributeExpression> list = new List<AttributeExpression>();
     Bracket item = new Bracket(symbol.Text, CsTokenType.OpenAttributeBracket, symbol.Location, this.symbols.Generated);
     Microsoft.StyleCop.Node<CsToken> node = this.tokens.InsertLast(item);
     this.symbols.Advance();
     Label_004A:
     this.AdvanceToNextCodeSymbol();
     Symbol symbol2 = this.symbols.Peek(1);
     if (symbol2 == null)
     {
         throw new SyntaxException(this.document.SourceCode, symbol.LineNumber);
     }
     if (symbol2.SymbolType == SymbolType.CloseSquareBracket)
     {
         Bracket bracket2 = new Bracket(symbol2.Text, CsTokenType.CloseAttributeBracket, symbol2.Location, this.symbols.Generated);
         Microsoft.StyleCop.Node<CsToken> node2 = this.tokens.InsertLast(bracket2);
         this.symbols.Advance();
         item.MatchingBracketNode = node2;
         bracket2.MatchingBracketNode = node;
     }
     else
     {
         LiteralExpression target = null;
         this.AdvanceToNextCodeSymbol();
         symbol2 = this.symbols.Peek(1);
         if (symbol2 == null)
         {
             throw new SyntaxException(this.document.SourceCode, symbol.LineNumber);
         }
         Microsoft.StyleCop.Node<CsToken> last = this.tokens.Last;
         if ((symbol2.SymbolType == SymbolType.Other) || (symbol2.SymbolType == SymbolType.Return))
         {
             int nextCodeSymbolIndex = this.GetNextCodeSymbolIndex(2);
             if (nextCodeSymbolIndex != -1)
             {
                 Symbol symbol3 = this.symbols.Peek(nextCodeSymbolIndex);
                 if (symbol3.SymbolType == SymbolType.Colon)
                 {
                     symbol2.SymbolType = SymbolType.Other;
                     target = this.GetLiteralExpression(unsafeCode);
                     this.AdvanceToNextCodeSymbol();
                     this.tokens.Add(new CsToken(symbol3.Text, CsTokenType.AttributeColon, symbol2.Location, this.symbols.Generated));
                     this.symbols.Advance();
                     this.AdvanceToNextCodeSymbol();
                 }
             }
         }
         Expression nextExpression = this.GetNextExpression(ExpressionPrecedence.None, unsafeCode);
         if (nextExpression == null)
         {
             throw new SyntaxException(this.document.SourceCode, symbol.LineNumber);
         }
         list.Add(new AttributeExpression(new CsTokenList(this.tokens, last.Next, this.tokens.Last), target, nextExpression));
         this.AdvanceToNextCodeSymbol();
         symbol2 = this.symbols.Peek(1);
         if (symbol2 == null)
         {
             throw new SyntaxException(this.document.SourceCode, symbol.LineNumber);
         }
         if (symbol2.SymbolType != SymbolType.Comma)
         {
             if (symbol2.SymbolType != SymbolType.CloseSquareBracket)
             {
                 throw new SyntaxException(this.document.SourceCode, symbol.LineNumber);
             }
         }
         else
         {
             this.tokens.Add(this.GetToken(CsTokenType.Comma, SymbolType.Comma));
         }
         goto Label_004A;
     }
     return new Microsoft.StyleCop.CSharp.Attribute(this.tokens, CodeLocation.Join<CsToken>(this.tokens.First, this.tokens.Last), list.ToArray(), this.tokens.First.Value.Generated || this.tokens.Last.Value.Generated);
 }
Пример #9
0
 private MasterList<CsToken> GetGenericArgumentList(bool unsafeCode, CsToken name, int startIndex, out int endIndex)
 {
     endIndex = -1;
     MasterList<CsToken> list = null;
     int count = startIndex;
     while (true)
     {
         Symbol symbol = this.symbols.Peek(count);
         if ((symbol == null) || (((symbol.SymbolType != SymbolType.WhiteSpace) && (symbol.SymbolType != SymbolType.EndOfLine)) && (((symbol.SymbolType != SymbolType.SingleLineComment) && (symbol.SymbolType != SymbolType.MultiLineComment)) && (symbol.SymbolType != SymbolType.PreprocessorDirective))))
         {
             break;
         }
         count++;
     }
     Symbol symbol2 = this.symbols.Peek(count);
     if ((symbol2 == null) || (symbol2.SymbolType != SymbolType.LessThan))
     {
         return list;
     }
     list = new MasterList<CsToken>();
     if (name != null)
     {
         list.Add(name);
     }
     Microsoft.StyleCop.Node<CsToken> node = null;
     for (int i = startIndex; i <= count; i++)
     {
         symbol2 = this.symbols.Peek(i);
         if (symbol2.SymbolType == SymbolType.LessThan)
         {
             if (node != null)
             {
                 return null;
             }
             Bracket item = new Bracket(symbol2.Text, CsTokenType.OpenGenericBracket, symbol2.Location, this.symbols.Generated);
             node = list.InsertLast(item);
         }
         else
         {
             list.Add(this.ConvertSymbol(symbol2, TokenTypeFromSymbolType(symbol2.SymbolType)));
         }
     }
     Label_00F4:
     symbol2 = this.symbols.Peek(++count);
     if (symbol2 == null)
     {
         throw new SyntaxException(this.document.SourceCode, name.LineNumber);
     }
     if (symbol2.SymbolType == SymbolType.GreaterThan)
     {
         if (node == null)
         {
             return null;
         }
         Bracket bracket2 = new Bracket(symbol2.Text, CsTokenType.CloseGenericBracket, symbol2.Location, this.symbols.Generated);
         Microsoft.StyleCop.Node<CsToken> node2 = list.InsertLast(bracket2);
         ((Bracket) node.Value).MatchingBracketNode = node2;
         bracket2.MatchingBracketNode = node;
         endIndex = count;
         return list;
     }
     if (symbol2.SymbolType == SymbolType.Other)
     {
         int num3 = 0;
         CsToken token = this.GetTypeTokenAux(unsafeCode, true, false, count, out num3);
         if (token == null)
         {
             throw new SyntaxException(this.document.SourceCode, symbol2.LineNumber);
         }
         count = num3;
         list.Add(token);
         goto Label_00F4;
     }
     if (((symbol2.SymbolType == SymbolType.WhiteSpace) || (symbol2.SymbolType == SymbolType.EndOfLine)) || (((symbol2.SymbolType == SymbolType.SingleLineComment) || (symbol2.SymbolType == SymbolType.MultiLineComment)) || (symbol2.SymbolType == SymbolType.PreprocessorDirective)))
     {
         list.Add(this.ConvertSymbol(symbol2, TokenTypeFromSymbolType(symbol2.SymbolType)));
         goto Label_00F4;
     }
     if (symbol2.SymbolType == SymbolType.Comma)
     {
         list.Add(this.ConvertSymbol(symbol2, CsTokenType.Comma));
         goto Label_00F4;
     }
     return null;
 }
Пример #10
0
 private ParenthesizedExpression GetConditionalPreprocessorParenthesizedExpression(SourceCode sourceCode)
 {
     this.AdvanceToNextConditionalDirectiveCodeSymbol();
     Symbol symbol = this.symbols.Peek(1);
     if ((symbol == null) || (symbol.SymbolType != SymbolType.OpenParenthesis))
     {
         throw new SyntaxException(sourceCode, symbol.LineNumber);
     }
     this.symbols.Advance();
     Bracket item = new Bracket(symbol.Text, CsTokenType.OpenParenthesis, symbol.Location, this.symbols.Generated);
     Microsoft.StyleCop.Node<CsToken> firstItemNode = this.tokens.InsertLast(item);
     Expression nextConditionalPreprocessorExpression = this.GetNextConditionalPreprocessorExpression(sourceCode, ExpressionPrecedence.None);
     if (nextConditionalPreprocessorExpression == null)
     {
         throw new SyntaxException(sourceCode, symbol.LineNumber);
     }
     this.AdvanceToNextConditionalDirectiveCodeSymbol();
     Symbol symbol2 = this.symbols.Peek(1);
     if ((symbol2 == null) || (symbol2.SymbolType != SymbolType.CloseParenthesis))
     {
         throw new SyntaxException(sourceCode, symbol.LineNumber);
     }
     this.symbols.Advance();
     Bracket bracket2 = new Bracket(symbol2.Text, CsTokenType.CloseParenthesis, symbol2.Location, this.symbols.Generated);
     Microsoft.StyleCop.Node<CsToken> node2 = this.tokens.InsertLast(bracket2);
     item.MatchingBracketNode = node2;
     bracket2.MatchingBracketNode = firstItemNode;
     return new ParenthesizedExpression(new CsTokenList(this.tokens, firstItemNode, this.tokens.Last), nextConditionalPreprocessorExpression);
 }
Пример #11
0
        /// <summary>
        /// Gets friendly output text for "opening" or "closing", depending on the type of the bracket.
        /// </summary>
        /// <param name="bracket">The bracket.</param>
        /// <returns>Returns the opening or closing text.</returns>
        private static string GetOpeningOrClosingBracketText(Bracket bracket)
        {
            Param.AssertNotNull(bracket, "bracket");

            switch (bracket.CsTokenType)
            {
                case CsTokenType.OpenAttributeBracket:
                case CsTokenType.OpenCurlyBracket:
                case CsTokenType.OpenGenericBracket:
                case CsTokenType.OpenParenthesis:
                case CsTokenType.OpenSquareBracket:
                    return Strings.Opening;

                case CsTokenType.CloseAttributeBracket:
                case CsTokenType.CloseCurlyBracket:
                case CsTokenType.CloseGenericBracket:
                case CsTokenType.CloseParenthesis:
                case CsTokenType.CloseSquareBracket:
                    return Strings.Closing;

                default:
                    Debug.Fail("Invalid bracket type.");
                    return string.Empty;
            }
        }
        /// <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);
            }

            var 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.
            var expression = new ParenthesizedExpression(partialTokens, innerExpression);
            expressionReference.Target = expression;

            return expression;
        }