private void formatQueryBody(QueryBodyNode body, StringBuilder sb) {
			foreach (var c in body.Clauses) {
				switch (c.QueryBodyClauseKind) {
				case From:
					var from = (FromQueryBodyClauseNode)c;
					sb.append("from ");
					if (from.Type != null) {
						sb.append(typeReferenceToString(from.Type));
						sb.append(" ");
					}
					sb.append(new String(text, from.NameOffset, from.NameLength));
					sb.append(" in ");
					sb.append(expressionToString(from.Origin));
					sb.append("\r\n");
					break;

				case Join:
					var join = (JoinQueryBodyClauseNode)c;
					sb.append("join ");
					if (join.Type != null) {
						sb.append(typeReferenceToString(join.Type));
						sb.append(" ");
					}
					sb.append(new String(text, join.NameOffset, join.NameLength));
					sb.append(" in ");
					sb.append(expressionToString(join.Origin));
					sb.append(" on ");
					sb.append(expressionToString(join.EqualsLeftOperand));
					sb.append(" equals ");
					sb.append(expressionToString(join.EqualsRightOperand));
					if (join.ResultLength > 0) {
						sb.append(" into ");
						sb.append(new String(text, join.ResultOffset, join.ResultLength));
					}
					sb.append("\r\n");
					break;

				case Let:
					var let = (LetQueryBodyClauseNode)c;
					sb.append("let ");
					sb.append(new String(text, let.NameOffset, let.NameLength));
					sb.append(" = ");
					sb.append(expressionToString(let.Value));
					sb.append("\r\n");
					break;

				case Orderby:
					var orderby = (OrderbyQueryBodyClauseNode)c;
					sb.append("orderby ");
					bool first = true;
					foreach (var o in orderby.Orderings) {
						if (first) {
							first = false;
						} else {
							sb.append(", ");
						}
						sb.append(expressionToString(o.KeySelector));
						if (o.Descending) {
							sb.append(" descending");
						}
					}
					sb.append("\r\n");
					break;

				case Where:
					var where = (WhereQueryBodyClauseNode)c;
					sb.append("where ");
					sb.append(expressionToString(where.Predicat));
					sb.append("\r\n");
					break;

				default:
					throw new RuntimeException("Unhandled query clause: " + c.QueryBodyClauseKind);
				}
			}
			if (body.By != null) {
				sb.append("group ");
			} else {
				sb.append("select ");
			}
			sb.append(expressionToString(body.SelectOrGroup));
			if (body.By != null) {
				sb.append(" by ");
				sb.append(expressionToString(body.By));
			}
			if (body.Continuation != null) {
				sb.append("\r\ninto ");
				sb.append(new String(text, body.Continuation.NameOffset, body.Continuation.NameLength));
				sb.append("\r\n");
				formatQueryBody(body.Continuation.Body, sb);
			}
		}
        private void parseQueryBody(QueryBodyNode body, int endPosition) {
            var done = false;
            do {
                switch (lexicalUnit) {
                case ContextualKeyword:
                case Keyword:
                    switch (scanner.Keyword) {
                    case From:
                        var from = new FromQueryBodyClauseNode();
                        parseFromClause(from);
                        body.Clauses.add(from);
                        endPosition = from.EndPosition;
                        break;

                    case Join:
                        var join = new JoinQueryBodyClauseNode();
                        parseJoinClause(join);
                        body.Clauses.add(join);
                        endPosition = join.EndPosition;
                        break;

                    case Let:
                        var let = new LetQueryBodyClauseNode();
                        parseLetClause(let);
                        body.Clauses.add(let);
                        endPosition = let.EndPosition;
                        break;

                    case Orderby:
                        var orderby = new OrderbyQueryBodyClauseNode();
                        parseOrderbyClause(orderby);
                        body.Clauses.add(orderby);
                        endPosition = orderby.EndPosition;
                        break;

                    case Where:
                        var where = new WhereQueryBodyClauseNode();
                        parseWhereClause(where);
                        body.Clauses.add(where);
                        endPosition = where.EndPosition;
                        break;

                    default:
                        done = true;
                        break;
                    }
                    break;

                default:
                    done = true;
                    break;
                }
            } while (!done);
            switch (lexicalUnit) {
            case ContextualKeyword:
            case Keyword:
                switch (scanner.Keyword) {
                case Select:
                    nextLexicalUnit(true);
                    body.SelectOrGroup = parseExpression();
                    endPosition = body.SelectOrGroup.EndPosition;
                    break;

                case Group:
                    nextLexicalUnit(true);
                    body.SelectOrGroup = parseExpression();
                    if (lexicalUnit != LexicalUnit.ContextualKeyword || scanner.Keyword != Keyword.By) {
                        throw error(ParseErrorId.ByExpected);
                    }
                    nextLexicalUnit(true);
                    body.By = parseExpression();
                    endPosition = body.By.EndPosition;
                    break;
                default:
                    throw error(ParseErrorId.SelectOrGroupExpected);
                }
                break;
            default:
                throw error(ParseErrorId.SelectOrGroupExpected);
            }
            if (lexicalUnit == LexicalUnit.ContextualKeyword && scanner.Keyword == Keyword.Into) {
                if (!isIdentifier(nextLexicalUnit(true))) {
                    throw error(ParseErrorId.IdentifierExpected);
                }
                body.Continuation = new QueryContinuationNode { NameOffset = scanner.StartPosition, NameLength = getLexicalUnitLength() };
                endPosition = scanner.EndPosition;
                nextLexicalUnit(true);
                parseQueryBody(body.Continuation.Body, endPosition);
                endPosition = body.Continuation.Body.EndPosition;
            }
            body.EndPosition = endPosition;
        }
Beispiel #3
0
        private void formatQueryBody(QueryBodyNode body, StringBuilder sb)
        {
            foreach (var c in body.Clauses)
            {
                switch (c.QueryBodyClauseKind)
                {
                case From:
                    var from = (FromQueryBodyClauseNode)c;
                    sb.append("from ");
                    if (from.Type != null)
                    {
                        sb.append(typeReferenceToString(from.Type));
                        sb.append(" ");
                    }
                    sb.append(new String(text, from.NameOffset, from.NameLength));
                    sb.append(" in ");
                    sb.append(expressionToString(from.Origin));
                    sb.append("\r\n");
                    break;

                case Join:
                    var join = (JoinQueryBodyClauseNode)c;
                    sb.append("join ");
                    if (join.Type != null)
                    {
                        sb.append(typeReferenceToString(join.Type));
                        sb.append(" ");
                    }
                    sb.append(new String(text, join.NameOffset, join.NameLength));
                    sb.append(" in ");
                    sb.append(expressionToString(join.Origin));
                    sb.append(" on ");
                    sb.append(expressionToString(join.EqualsLeftOperand));
                    sb.append(" equals ");
                    sb.append(expressionToString(join.EqualsRightOperand));
                    if (join.ResultLength > 0)
                    {
                        sb.append(" into ");
                        sb.append(new String(text, join.ResultOffset, join.ResultLength));
                    }
                    sb.append("\r\n");
                    break;

                case Let:
                    var let = (LetQueryBodyClauseNode)c;
                    sb.append("let ");
                    sb.append(new String(text, let.NameOffset, let.NameLength));
                    sb.append(" = ");
                    sb.append(expressionToString(let.Value));
                    sb.append("\r\n");
                    break;

                case Orderby:
                    var orderby = (OrderbyQueryBodyClauseNode)c;
                    sb.append("orderby ");
                    bool first = true;
                    foreach (var o in orderby.Orderings)
                    {
                        if (first)
                        {
                            first = false;
                        }
                        else
                        {
                            sb.append(", ");
                        }
                        sb.append(expressionToString(o.KeySelector));
                        if (o.Descending)
                        {
                            sb.append(" descending");
                        }
                    }
                    sb.append("\r\n");
                    break;

                case Where:
                    var where = (WhereQueryBodyClauseNode)c;
                    sb.append("where ");
                    sb.append(expressionToString(where.Predicat));
                    sb.append("\r\n");
                    break;

                default:
                    throw new RuntimeException("Unhandled query clause: " + c.QueryBodyClauseKind);
                }
            }
            if (body.By != null)
            {
                sb.append("group ");
            }
            else
            {
                sb.append("select ");
            }
            sb.append(expressionToString(body.SelectOrGroup));
            if (body.By != null)
            {
                sb.append(" by ");
                sb.append(expressionToString(body.By));
            }
            if (body.Continuation != null)
            {
                sb.append("\r\ninto ");
                sb.append(new String(text, body.Continuation.NameOffset, body.Continuation.NameLength));
                sb.append("\r\n");
                formatQueryBody(body.Continuation.Body, sb);
            }
        }