Пример #1
0
        private PayloadList PayloadList()
        {
            PayloadList payload = new PayloadList();

            pos++;             // '['

            SkipWhiteSpacesAndComments();

            // If there are no payloads inside of the list, return an empty list.
            if (currentChar == Syntax.LIST_CLOSING)
            {
                pos++;                 // ']'

                SkipWhiteSpacesAndComments();

                return(payload);
            }


funcPayload_AnotherPayload:

            Payload p = Payload();

            // If the list has a type assigned and the payload is not of this type.
            if (payload.listType != DataType.EmptyList && p.type != payload.listType)
            {
                throw new KFFParseException("Found mismatched payload '" + p.type.ToString() + "' (" + TextFileData.Calculate(this.fileName, this.s, this.pos) + ").");
            }
            payload.Add(p);

            if (currentChar == Syntax.LIST_ELEMENT_SEPARATOR)
            {
                pos++;                 // ','

                SkipWhiteSpacesAndComments();

                goto funcPayload_AnotherPayload;
            }

            if (currentChar == Syntax.LIST_CLOSING)
            {
                pos++;                 // ']'

                SkipWhiteSpacesAndComments();

                return(payload);
            }

            throw new KFFParseException("Expected to find '" + Syntax.LIST_ELEMENT_SEPARATOR + "' or '" + Syntax.LIST_CLOSING + "', but found '" + currentChar + "' (" + TextFileData.Calculate(this.fileName, this.s, pos) + ").");
        }
Пример #2
0
        private static PayloadList ParsePayloads(string text, Model.System system)
        {
            if (text == null)
            {
                throw new ArgumentException("Text cannot be null.", nameof(text));
            }

            var list = new PayloadList();
            var currentPayloadText = string.Empty;

            // Let's iterate through the message and parse out all payloads, one by one.
            for (var i = 0; i < text.Length; i++)
            {
                currentPayloadText += text[i];

                if (TryGetPayload(currentPayloadText, out var parsedPayload, system))
                {
                    if (!(parsedPayload is TextPayload))
                    {
                        list.Add(parsedPayload);
                    }
                    else if (!string.IsNullOrEmpty(parsedPayload.RawText))
                    {
                        list.Add(parsedPayload);
                    }

                    currentPayloadText = string.Empty;
                }

                if (i < text.Length - 1 && text[i + 1] == '\\')
                {
                    list.Add(new TextPayload(currentPayloadText));

                    currentPayloadText = string.Empty;
                }
            }

            if (!string.IsNullOrEmpty(currentPayloadText))
            {
                list.Add(new TextPayload(currentPayloadText));
            }

            return(list);
        }
Пример #3
0
        private string StringFromPayloadList(PayloadList t)
        {
            StringBuilder sb = new StringBuilder();

            if (((int)listOpeningNL & 1) == 1)              // before
            {
                sb.Append(Environment.NewLine);
            }

            sb.Append(Syntax.LIST_OPENING);

            if (((int)listOpeningNL & 2) == 2)              // after
            {
                sb.Append(Environment.NewLine);
            }


            for (int i = 0; i < t.count; i++)
            {
                DataType listType = t.listType;
                string   payload  = "";

                if (listType == DataType.Boolean)
                {
                    payload = StringFromPayloadBoolean((PayloadBoolean)t.Get(i));
                }

                if (listType == DataType.Integer)
                {
                    payload = StringFromPayloadInteger((PayloadInteger)t.Get(i));
                }
                if (listType == DataType.Decimal)
                {
                    payload = StringFromPayloadDecimal((PayloadDecimal)t.Get(i));
                }

                if (listType == DataType.String)
                {
                    payload = StringFromPayloadString((PayloadString)t.Get(i));
                }

                if (listType == DataType.Class)
                {
                    payload = StringFromPayloadClass((PayloadClass)t.Get(i));
                }
                if (listType == DataType.List)
                {
                    payload = StringFromPayloadList((PayloadList)t.Get(i));
                }

                if (tabInsideList)
                {
                    // insert tab at the front, right before the first payload in the list.
                    sb.Append("\t");
                    // insert tab after every newline char (skips the front of the first payload, as there's no newline char before it).
                    payload = payload.Replace(Environment.NewLine, Environment.NewLine + "\t");
                }

                sb.Append(payload);

                if (i != t.count - 1)
                {
                    if (((int)tagListElementSeparatorNL & 1) == 1)                      // before
                    {
                        sb.Append(Environment.NewLine);
                    }

                    sb.Append(Syntax.LIST_ELEMENT_SEPARATOR);

                    if (((int)tagListElementSeparatorNL & 2) == 2)                      // after
                    {
                        sb.Append(Environment.NewLine);
                    }
                }
            }


            if (((int)listClosingNL & 1) == 1)              // before
            {
                sb.Append(Environment.NewLine);
            }

            sb.Append(Syntax.LIST_CLOSING);

            if (((int)listClosingNL & 2) == 2)              // after
            {
                sb.Append(Environment.NewLine);
            }

            return(sb.ToString());
        }