コード例 #1
0
        private void AddXml(LSObject parent, XmlNode doc)
        {
            var obj = new LSObject();

            if (doc.Attributes != null)
            {
                foreach (XmlAttribute attr in doc.Attributes)
                {
                    obj.Add(attr.Name, attr.Value.Parse());
                }
            }
            foreach (XmlNode node in doc.ChildNodes)
            {
                if (node.NodeType == XmlNodeType.Element)
                {
                    AddXml(obj, node);
                }
            }
            obj.Name = doc.Name;
            if (obj.Count == 0)
            {
                obj.Value = doc.InnerText.Trim().Parse();
            }
            parent.Add(obj);
        }
コード例 #2
0
        public LSObject ReadProperties(string properties)
        {
            var obj   = new LSObject();
            var props = properties.Split("\r\n");

            foreach (var p in props)
            {
                var i  = p.IndexOf("//");//过滤注释部分
                var tp = i > -1 ? p.Substring(0, i) : p;
                if (tp.Length > 0)
                {
                    var entry = tp.Split("=");
                    var key   = entry[0];
                    var value = entry[1];
                    obj.Add(key, value.Parse());
                }
            }
            return(obj);
        }
コード例 #3
0
ファイル: LSParser.cs プロジェクト: zihunyeyu/ExtractorSharp
        private LSObject ParseObject()
        {
            Consume();
            var obj  = new LSObject();
            var name = new StringBuilder();

            while (true)
            {
                switch (LookAhead())
                {
                case LSToken.Comma:
                    Consume();
                    //分隔符,清除name
                    name.Clear();
                    break;

                case LSToken.RBracket:     //]} 对象终止
                case LSToken.RBrace:
                    Consume();
                    //处理未能识别的表达式
                    foreach (var t in dictionary.Keys.ToArray())
                    {
                        var t2 = obj.Find(dictionary[t]);
                        if (t2 != null)
                        {
                            if (t.ValueType == LSType.Object)
                            {
                                t2.CopyTo(t);
                            }
                            else if (t.ValueType == LSType.String && t2.ValueType != LSType.Object)
                            {
                                var s = t.Value as string;
                                t.Value = s.Replace("${" + dictionary[t] + "}", t2.Value?.ToString());
                            }
                            dictionary.Remove(t);
                        }
                    }
                    return(obj);

                case LSToken.Colon:     //:
                    Consume();
                    if (LookAhead() != LSToken.Expression)
                    {
                        var value = ParseValue();
                        var t     = obj.Add(name.ToString(), value);
                        if (value is string s)
                        {
                            var match = Regex.Match(s, @"\$\{.*\}");
                            var exp   = match.Value;
                            if (exp != string.Empty && (match.Index < 1 || s[match.Index - 1] != '\\'))
                            {
                                dictionary.Add(t, exp.Substring(2, exp.Length - 3));
                            }
                        }
                        name.Clear();
                    }
                    break;

                case LSToken.Dot:
                    Consume();
                    //refrence
                    name.Append(".");
                    break;

                case LSToken.Expression:
                    var key     = ParseExpression();
                    var current = obj.Find(key);
                    var tp      = obj.Add(name.ToString(), current);
                    if (current == null)
                    {
                        dictionary.Add(tp, key);
                    }
                    current = null;
                    name.Clear();
                    break;

                case LSToken.Identifier:
                    name.Append(ParseIdentifier());
                    break;

                case LSToken.Charcter:     // 字符/字符串
                case LSToken.String:
                    var val = ParseValue();
                    if (LookAhead() == LSToken.Colon)
                    {
                        //兼容字符串命名
                        name.Clear();
                        name.Append(val + "");
                    }
                    else
                    {
                        obj.Add(val);
                    }
                    break;

                case LSToken.LBrace:
                case LSToken.LBracket:
                case LSToken.True:
                case LSToken.False:
                case LSToken.Number:
                case LSToken.Null:
                    obj.Add(ParseValue());
                    break;

                default:
                    throw new Exception($"无法识别的字符!'{source[index]}'");
                }
            }
        }