コード例 #1
0
        string MakeString(IEnumerable <Token> tokens)
        {
            var sb = new IndentingStringBuilder(configuration);

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

            sb.TrimLast();

            return(sb.ToString());
        }
コード例 #2
0
        string MakeString(IEnumerable<Token> tokens)
        {
            var sb = new IndentingStringBuilder(configuration);

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

            sb.TrimLast();

            return sb.ToString();
        }
コード例 #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());
        }
コード例 #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));
        }
コード例 #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();
            }
        }
コード例 #6
0
ファイル: XmlStyle.cs プロジェクト: kbilsted/StatePrinter
        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();
        }
コード例 #7
0
ファイル: XmlStyle.cs プロジェクト: kbilsted/StatePrinter
        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();
            }
        }
コード例 #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());
        }
コード例 #9
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:
                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();
            }
        }
コード例 #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();
            }
        }
コード例 #11
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:
                    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();
            }
        }
コード例 #12
0
ファイル: JsonStyle.cs プロジェクト: kbilsted/StatePrinter
        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;
        }
コード例 #13
0
ファイル: JsonStyle.cs プロジェクト: kbilsted/StatePrinter
        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();
        }
コード例 #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);
        }