string MakeString(IEnumerable <Token> tokens)
        {
            var sb = new IndentingStringBuilder(configuration);

            foreach (var token in tokens)
            {
                MakeTokenString(token, sb);
            }

            sb.TrimLast();

            return(sb.ToString());
        }
        string MakeString(IEnumerable<Token> tokens)
        {
            var sb = new IndentingStringBuilder(configuration);

            foreach (var token in tokens)
            {
                MakeTokenString(token, sb);
            }

            sb.TrimLast();

            return sb.ToString();
        }
Beispiel #3
0
        string MakeString(List <Token> tokens, Dictionary <Reference, string> referencePaths)
        {
            var sb = new IndentingStringBuilder(configuration);

            for (int i = 0; i < tokens.Count; i++)
            {
                int skip = MakeStringFromToken(tokens, i, referencePaths, sb);
                i += skip;
            }

            sb.TrimLast();

            return(sb.ToString());
        }
Beispiel #4
0
        string MakeString(IList <Token> tokens)
        {
            var sb = new IndentingStringBuilder(configuration);

            for (var i = 0; i < tokens.Count; i++)
            {
                var token = tokens[i];
                MakeTokenString(token, sb);
            }

            sb.TrimLast();
            var result = sb.ToString();

            // remove final superfluous trailing comma - very hacky, could probably be done better
            return(result.Remove(result.Length - 1));
        }
Beispiel #5
0
        void MakeTokenString(Token token, IndentingStringBuilder sb, Stack <string> endTags)
        {
            string tagName;
            string keyAttr;

            switch (token.Tokenkind)
            {
            case TokenType.StartScope:
            case TokenType.StartList:
            case TokenType.StartDict:
                sb.Indent();
                break;

            case TokenType.EndScope:
            case TokenType.EndList:
            case TokenType.EndDict:
                sb.DeIndent();
                sb.AppendFormatLine("</{0}>", endTags.Pop());
                break;

            case TokenType.SimpleFieldValue:
                tagName = TagName(token, out keyAttr);
                sb.AppendFormatLine("<{0}{1}>{2}</{0}>", tagName, keyAttr, Unquote(token.Value));
                break;

            case TokenType.SeenBeforeWithReference:
                tagName = TagName(token, out keyAttr);
                sb.AppendFormatLine("<{0}{1} ref='{2}'/>", tagName, keyAttr, token.ReferenceNo.Number);
                break;

            case TokenType.FieldnameWithTypeAndReference:
                var optionReferenceInfo = token.ReferenceNo != null
                      ? string.Format(" ref='{0}'", token.ReferenceNo.Number)
                      : "";

                var fieldType = OutputFormatterHelpers.MakeReadable(token.FieldType)
                                .Replace('<', '(')
                                .Replace('>', ')');
                tagName = TagName(token, out keyAttr);
                endTags.Push(tagName);
                sb.AppendFormatLine("<{0}{1} type='{2}'{3}>", tagName, keyAttr, fieldType, optionReferenceInfo);
                break;

            default:
                throw new ArgumentOutOfRangeException();
            }
        }
Beispiel #6
0
        string MakeString(List<Token> tokens)
        {
            var sb = new IndentingStringBuilder(configuration);

            var endTags = new Stack<string>();
            int pos = 0;
            foreach (Token token in tokens)
            {
                MakeTokenString(token, sb, endTags);
            }

            if (endTags.Any())
                throw new Exception("Internal logic error");

            sb.TrimLast();
            return sb.ToString();
        }
Beispiel #7
0
        void MakeTokenString(Token token, IndentingStringBuilder sb, Stack<string> endTags)
        {
            string tagName;
            string keyAttr;
            switch (token.Tokenkind)
            {
                case TokenType.StartScope:
                case TokenType.StartList:
                case TokenType.StartDict:
                    sb.Indent();
                    break;

                case TokenType.EndScope:
                case TokenType.EndList:
                case TokenType.EndDict:
                    sb.DeIndent();
                    sb.AppendFormatLine("</{0}>", endTags.Pop());
                    break;

                case TokenType.SimpleFieldValue:
                    tagName = TagName(token, out keyAttr);
                    sb.AppendFormatLine("<{0}{1}>{2}</{0}>", tagName, keyAttr, Unquote(token.Value));
                    break;

                case TokenType.SeenBeforeWithReference:
                    tagName = TagName(token, out keyAttr);
                    sb.AppendFormatLine("<{0}{1} ref='{2}'/>", tagName, keyAttr, token.ReferenceNo.Number);
                    break;

                case TokenType.FieldnameWithTypeAndReference:
                    var optionReferenceInfo = token.ReferenceNo != null
                      ? string.Format(" ref='{0}'", token.ReferenceNo.Number)
                      : "";
                    var fieldType = OutputFormatterHelpers.MakeReadable(token.FieldType)
                        .Replace('<', '(')
                        .Replace('>', ')');
                    tagName = TagName(token, out keyAttr);
                    endTags.Push(tagName);
                    sb.AppendFormatLine("<{0}{1} type='{2}'{3}>", tagName, keyAttr, fieldType, optionReferenceInfo);
                    break;

                default:
                    throw new ArgumentOutOfRangeException();
            }
        }
Beispiel #8
0
        string MakeString(List <Token> tokens)
        {
            var sb = new IndentingStringBuilder(configuration);

            var endTags = new Stack <string>();

            foreach (Token token in tokens)
            {
                MakeTokenString(token, sb, endTags);
            }

            if (endTags.Any())
            {
                throw new Exception("Internal logic error");
            }

            sb.TrimLast();
            return(sb.ToString());
        }
        void MakeTokenString(Token token, IndentingStringBuilder sb)
        {
            switch (token.Tokenkind)
            {
            case TokenType.StartScope:
            case TokenType.StartList:
            case TokenType.StartDict:
                sb.AppendFormatLine("{{");
                sb.Indent();
                break;

            case TokenType.EndScope:
            case TokenType.EndList:
            case TokenType.EndDict:
                sb.DeIndent();
                sb.AppendFormatLine("}}");
                break;

            case TokenType.SimpleFieldValue:
                sb.AppendFormatLine("{0}", MakeFieldValue(token, token.Value));
                break;

            case TokenType.SeenBeforeWithReference:
                sb.AppendFormatLine("{0}", MakeFieldValue(token, "-> " + token.ReferenceNo.Number));
                break;

            case TokenType.FieldnameWithTypeAndReference:
                var optionReferenceInfo = token.ReferenceNo == null
                      ? ""
                      : string.Format(", ref: {0}", token.ReferenceNo.Number);

                var fieldType = OutputFormatterHelpers.MakeReadable(token.FieldType);

                string value = string.Format("new {0}(){1}", fieldType, optionReferenceInfo);
                sb.AppendFormatLine("{0}", MakeFieldValue(token, value));
                break;

            default:
                throw new ArgumentOutOfRangeException();
            }
        }
Beispiel #10
0
        void MakeTokenString(Token token, IndentingStringBuilder sb)
        {
            switch (token.Tokenkind)
            {
            case TokenType.StartScope:
            case TokenType.StartList:
            case TokenType.StartDict:
                sb.AppendFormatLine("{{");
                sb.Indent();
                break;

            case TokenType.EndScope:
            case TokenType.EndList:
            case TokenType.EndDict:
                sb.DeIndent();
                sb.AppendFormatLine("}},");
                break;

            case TokenType.SimpleFieldValue:
                sb.AppendFormatLine("{0}", MakeFieldValue(token, token.Value));
                break;

            case TokenType.SeenBeforeWithReference:
                throw CreateCyclicalObjectGraphException();

            case TokenType.FieldnameWithTypeAndReference:
                if (token.ReferenceNo != null)
                {
                    throw CreateCyclicalObjectGraphException();
                }

                var fieldType = OutputFormatterHelpers.MakeReadable(token.FieldType);

                string value = string.Format("new {0}{1}", fieldType, token.FieldType.IsArray ? "" : "()");
                sb.AppendFormatLine("{0}", MakeFieldValue(token, value));
                break;

            default:
                throw new ArgumentOutOfRangeException();
            }
        }
        void MakeTokenString(Token token, IndentingStringBuilder sb)
        {
            switch (token.Tokenkind)
            {
                case TokenType.StartScope:
                case TokenType.StartList:
                case TokenType.StartDict:
                    sb.AppendFormatLine("{{");
                    sb.Indent();
                    break;

                case TokenType.EndScope:
                case TokenType.EndList:
                case TokenType.EndDict:
                    sb.DeIndent();
                    sb.AppendFormatLine("}}");
                    break;

                case TokenType.SimpleFieldValue:
                    sb.AppendFormatLine("{0}", MakeFieldValue(token, token.Value));
                    break;

                case TokenType.SeenBeforeWithReference:
                    sb.AppendFormatLine("{0}", MakeFieldValue(token, "-> " + token.ReferenceNo.Number));
                    break;

                case TokenType.FieldnameWithTypeAndReference:
                    var optionReferenceInfo = token.ReferenceNo == null
                      ? ""
                      : string.Format(", ref: {0}", token.ReferenceNo.Number);

                    var fieldType = OutputFormatterHelpers.MakeReadable(token.FieldType);

                    string value = string.Format("new {0}(){1}", fieldType, optionReferenceInfo);
                    sb.AppendFormatLine("{0}", MakeFieldValue(token, value));
                    break;

                default:
                    throw new ArgumentOutOfRangeException();
            }
        }
Beispiel #12
0
        int MakeStringFromToken(List<Token> tokens, int pos, Dictionary<Reference, string> referencePaths, IndentingStringBuilder sb)
        {
            var token = tokens[pos];
            TokenType? next = pos + 1 < tokens.Count ? tokens[pos + 1].Tokenkind : new TokenType?();
            int skip = 0;

            switch (token.Tokenkind)
            {
                case TokenType.StartScope:
                case TokenType.StartDict:
                    if (next == TokenType.EndScope || next == TokenType.EndDict)
                    {
                        sb.AppendFormatLine("{{}}{0}", OptionalComma(tokens, pos + 1));
                        ++skip;
                    }
                    else
                    {
                        sb.AppendFormatLine("{{");
                        sb.Indent();
                    }
                    break;

                case TokenType.EndScope:
                case TokenType.EndDict:
                    sb.DeIndent();
                    sb.AppendFormatLine("}}{0}", OptionalComma(tokens, pos));
                    break;

                case TokenType.StartList:
                    if (next == TokenType.EndList)
                    {
                        sb.AppendFormatLine("[]{0}", OptionalComma(tokens, pos + 1));
                        ++skip;
                    }
                    else
                    {
                        sb.AppendFormatLine("[");
                        sb.Indent();
                    }
                    break;

                case TokenType.EndList:
                    sb.DeIndent();
                    sb.AppendFormatLine("]{0}", OptionalComma(tokens, pos));
                    break;

                case TokenType.SimpleFieldValue:
                    sb.AppendFormatLine("{0}{1}", MakeFieldValue(token, token.Value),
                                        OptionalComma(tokens, pos));
                    break;

                case TokenType.SeenBeforeWithReference:
                    sb.AppendFormatLine("{0}{1}", MakeFieldValue(token, referencePaths[token.ReferenceNo]),
                                        OptionalComma(tokens, pos));
                    break;

                case TokenType.FieldnameWithTypeAndReference:
                    string value = MakeFieldValue(token, "");
                    sb.AppendFormat("{0}", value);
                    break;

                default:
                    throw new ArgumentOutOfRangeException();
            }

            return skip;
        }
Beispiel #13
0
        string MakeString(List<Token> tokens, Dictionary<Reference, string> referencePaths)
        {
            var sb = new IndentingStringBuilder(configuration);

            for (int i = 0; i < tokens.Count; i++)
            {
                int skip = MakeStringFromToken(tokens, i, referencePaths, sb);
                i += skip;
            }

            sb.TrimLast();

            return sb.ToString();
        }
Beispiel #14
0
        int MakeStringFromToken(List <Token> tokens, int pos, Dictionary <Reference, string> referencePaths, IndentingStringBuilder sb)
        {
            var       token = tokens[pos];
            TokenType?next  = pos + 1 < tokens.Count ? tokens[pos + 1].Tokenkind : new TokenType?();
            int       skip  = 0;

            switch (token.Tokenkind)
            {
            case TokenType.StartScope:
            case TokenType.StartDict:
                if (next == TokenType.EndScope || next == TokenType.EndDict)
                {
                    sb.AppendFormatLine("{{}}{0}", OptionalComma(tokens, pos + 1));
                    ++skip;
                }
                else
                {
                    sb.AppendFormatLine("{{");
                    sb.Indent();
                }
                break;

            case TokenType.EndScope:
            case TokenType.EndDict:
                sb.DeIndent();
                sb.AppendFormatLine("}}{0}", OptionalComma(tokens, pos));
                break;

            case TokenType.StartList:
                if (next == TokenType.EndList)
                {
                    sb.AppendFormatLine("[]{0}", OptionalComma(tokens, pos + 1));
                    ++skip;
                }
                else
                {
                    sb.AppendFormatLine("[");
                    sb.Indent();
                }
                break;

            case TokenType.EndList:
                sb.DeIndent();
                sb.AppendFormatLine("]{0}", OptionalComma(tokens, pos));
                break;

            case TokenType.SimpleFieldValue:
                sb.AppendFormatLine("{0}{1}", MakeFieldValue(token, token.Value),
                                    OptionalComma(tokens, pos));
                break;

            case TokenType.SeenBeforeWithReference:
                sb.AppendFormatLine("{0}{1}", MakeFieldValue(token, referencePaths[token.ReferenceNo]),
                                    OptionalComma(tokens, pos));
                break;

            case TokenType.FieldnameWithTypeAndReference:
                string value = MakeFieldValue(token, "");
                sb.AppendFormat("{0}", value);
                break;

            default:
                throw new ArgumentOutOfRangeException();
            }

            return(skip);
        }