Exemplo n.º 1
0
        public static PdxSublist FromList(List <string> strs, PdxSublist parent = null)
        {
            var data = new PdxSublist(parent);

            strs.ForEach((s) =>
            {
                data.AddValue(s);
            }
                         );

            return(data);
        }
Exemplo n.º 2
0
        private static void Terminate(PdxSublist currentList, StringBuilder key, StringBuilder value, char?ch = null)
        {
            switch (State)
            {
            //determine what to do about current thing
            case ReadState.key:
            case ReadState.postKey:
                currentList.AddValue(string.Empty, key.ToString());
                break;

            case ReadState.value:
                currentList.AddValue(key.ToString(), value.ToString());
                break;

            case ReadState.preValue:
            case ReadState.preKey:
            case ReadState.comment:
                break;

            default:
                var unexpected = ch.HasValue ? ch.Value.ToString() : "EoF";
                throw new Exception($"Syntax error:  Unexcepted '{unexpected}'");
            }
        }
Exemplo n.º 3
0
        private static void SingleLineArray(string key, string value, PdxSublist currentList)
        {
            var numValues = new List <string>();
            var inQuotes  = false;
            var nextVal   = new StringBuilder();

            foreach (var ch in value)
            {
                if (ch == '}')
                {
                    break;
                }
                if (!inQuotes && char.IsWhiteSpace(ch))
                {
                    if (nextVal.Length > 0)
                    {
                        numValues.Add(nextVal.ToString());
                    }
                    nextVal = new StringBuilder();
                    continue;
                }
                if (ch == '"')
                {
                    inQuotes = !inQuotes;
                    continue;
                }

                nextVal.Append(ch);
            }
            if (nextVal.Length > 0)
            {
                numValues.Add(nextVal.ToString());
            }
            foreach (var val in numValues)
            {
                currentList.AddValue(null, val);
            }
        }
Exemplo n.º 4
0
        public static PdxSublist RunLine(string line, PdxSublist currentList)
        {
            if (line.Contains('#'))
            {
                //filter out comment
                line = line.Substring(0, line.IndexOf('#'));
            }
            if (string.IsNullOrWhiteSpace(line))
            {
                return(currentList);
            }
            string key = null;

            if (State == ReadState.value)
            {
                key = ReadKey;
            }
            var value = line.Substring(line.IndexOf('=') + 1).Trim();

            if (line.Contains('='))
            {
                key = RemoveWhitespace(line.Substring(0, line.IndexOf('=')));
            }
            else if (value == "}")
            {
                return(currentList.Parent);
            }
            if (string.IsNullOrWhiteSpace(value))
            {
                State   = ReadState.value;
                ReadKey = key;
            }
            var parent = 0;

            if (value.Contains('}'))
            {
                parent = value.Count(c => c == '}');


                value = value.Substring(0, value.IndexOf('}')).Trim();
            }

            if (value.FirstOrDefault() == '{')
            {
                var list = new PdxSublist(currentList, key);

                if (line.Contains('}'))
                {
                    if (line.IndexOf('}') < line.IndexOf('{'))
                    {
                        currentList = currentList.Parent;
                        key         = key.Substring(key.IndexOf('}') + 1);
                        list.Key    = key;
                        list.Parent = currentList;
                    }
                    else
                    {
                        parent = 1;
                        var open = line.IndexOf('{');
                        value = line.Substring(open + 1, line.IndexOf('}') - open - 1);
                        if (value.Contains('='))
                        {
                            SingleLineKeyValuePairs(key, value, list);
                        }
                        else
                        {
                            SingleLineArray(key, value, list);
                        }
                    }
                }
                currentList.AddSublist(key, list);
                currentList = list;
            }
            else if (key == null)
            {
                // awkward single line array of numbers
                value = line.Substring(line.IndexOf('=') + 1).Trim();
                SingleLineArray(key, value, currentList);
            }
            else
            {
                currentList.AddValue(key, value);
            }
            for (var i = 0; i < parent; i++)
            {
                currentList = currentList.Parent;
            }
            return(currentList);
        }