/// <summary>
        /// Gets a query order-by clause.
        /// </summary>
        /// <param name="parentReference">The parent code unit.</param>
        /// <param name="unsafeCode">Indicates whether the code being parsed resides 
        /// in an unsafe code block.</param>
        /// <returns>Returns the query order-by clause.</returns>
        private QueryOrderByClause GetQueryOrderByClause(Reference<ICodePart> parentReference, bool unsafeCode)
        {
            Param.AssertNotNull(parentReference, "parentReference");
            Param.Ignore(unsafeCode);

            // Get and add the 'orderby' symbol.
            Symbol symbol = this.GetNextSymbol(SymbolType.Other, parentReference);
            Debug.Assert(symbol.Text == "orderby", "Expected an orderby keyword");

            var clauseReference = new Reference<ICodePart>();
            Node<CsToken> firstTokenNode = this.tokens.InsertLast(this.GetToken(CsTokenType.OrderBy, SymbolType.Other, clauseReference));

            // Get each of the orderings in the clause.
            List<QueryOrderByOrdering> orderings = new List<QueryOrderByOrdering>();

            while (true)
            {
                QueryOrderByOrdering ordering = new QueryOrderByOrdering();

                // Get the ordering expression.
                ordering.Expression = this.GetNextExpression(ExpressionPrecedence.Query, clauseReference, unsafeCode);
                if (ordering.Expression == null)
                {
                    throw this.CreateSyntaxException();
                }

                // Get the order direction if it exists.
                symbol = this.GetNextSymbol(clauseReference);

                ordering.Direction = QueryOrderByDirection.Undefined;

                if (symbol.Text == "ascending")
                {
                    ordering.Direction = QueryOrderByDirection.Ascending;
                    this.tokens.Add(this.GetToken(CsTokenType.Ascending, SymbolType.Other, clauseReference));
                }
                else if (symbol.Text == "descending")
                {
                    ordering.Direction = QueryOrderByDirection.Descending;
                    this.tokens.Add(this.GetToken(CsTokenType.Descending, SymbolType.Other, clauseReference));
                }

                // Add the ordering to the list.
                orderings.Add(ordering);

                // If the next symbol is a comma, then we should continue and get the next ordering expression.
                symbol = this.GetNextSymbol(clauseReference);

                if (symbol.SymbolType == SymbolType.Comma)
                {
                    this.tokens.Add(this.GetToken(CsTokenType.Comma, SymbolType.Comma, clauseReference));
                }
                else
                {
                    // This was the last ordering expression.
                    break;
                }
            }

            // Create and return the clause.
            var clause = new QueryOrderByClause(new CsTokenList(this.tokens, firstTokenNode, this.tokens.Last), orderings);
            clauseReference.Target = clause;

            return clause;
        }
Example #2
0
 private QueryOrderByClause GetQueryOrderByClause(bool unsafeCode)
 {
     QueryOrderByOrdering ordering;
     Symbol nextSymbol = this.GetNextSymbol(SymbolType.Other);
     Microsoft.StyleCop.Node<CsToken> firstItemNode = this.tokens.InsertLast(this.GetToken(CsTokenType.OrderBy, SymbolType.Other));
     List<QueryOrderByOrdering> orderings = new List<QueryOrderByOrdering>();
     Label_0025:
     ordering = new QueryOrderByOrdering();
     ordering.Expression = this.GetNextExpression(ExpressionPrecedence.Query, unsafeCode);
     if (ordering.Expression == null)
     {
         throw this.CreateSyntaxException();
     }
     nextSymbol = this.GetNextSymbol();
     ordering.Direction = QueryOrderByDirection.Undefined;
     if (nextSymbol.Text == "ascending")
     {
         ordering.Direction = QueryOrderByDirection.Ascending;
         this.tokens.Add(this.GetToken(CsTokenType.Ascending, SymbolType.Other));
     }
     else if (nextSymbol.Text == "descending")
     {
         ordering.Direction = QueryOrderByDirection.Descending;
         this.tokens.Add(this.GetToken(CsTokenType.Descending, SymbolType.Other));
     }
     orderings.Add(ordering);
     if (this.GetNextSymbol().SymbolType == SymbolType.Comma)
     {
         this.tokens.Add(this.GetToken(CsTokenType.Comma, SymbolType.Comma));
         goto Label_0025;
     }
     return new QueryOrderByClause(new CsTokenList(this.tokens, firstItemNode, this.tokens.Last), orderings);
 }