Пример #1
0
 public SyntaxTree(IEnumerable<string> namespaces, ObjectNode rootObject, IEnumerable<ObjectNode> objects)
 {
     if (namespaces != null)
         this.namespaces = new HashSet<string>(namespaces);
     this.rootObject = rootObject;
     this.objects = new List<ObjectNode>(objects);
 }
Пример #2
0
        private ReferenceNode Object()
        {
            var token = GetToken();
            if (!(token is TypeToken))
                // todo: error message
                throw new EdgeParserException();

            var type = ((TypeToken)token).Type;
            var id = ObjectId(type);
            objects[id] = null;
            var ctor = CtorArgs();
            var properties = Properties();

            var obj = new ObjectNode(type, id, ctor, properties);
            objects[id] = obj;

            return new ReferenceNode(id, type);
        }
Пример #3
0
 private void CheckObject(SyntaxTree tree, ObjectNode obj)
 {
     var type = CheckType(obj.Type);
     CheckCtor(tree, type, obj);
     CheckProperties(type, obj.Properties);
 }
Пример #4
0
        private void CheckCtor(SyntaxTree tree, Type objType, ObjectNode objNode)
        {
            ConstructorInfo ctor;
            List<IValueNode> args = null;
            if (objNode.ConstructorArguments != null)
                args = objNode.ConstructorArguments.ToList();

            if (objNode.ConstructorArguments == null || args.Count == 0)
            {
                ctor = objType.GetConstructor(Type.EmptyTypes);
                if (ctor != null)
                    return;
            }
            else
            {
                var types = new Type[args.Count];

                var strType = typeof(string);
                var doubleType = typeof(double);

                for (int i = 0; i < types.Length; i++)
                {
                    var t = args[0];

                    var refType = t as ReferenceNode;
                    if (refType != null)
                    {
                        types[i] = CheckType(refType.Type);
                    }
                    else if (t is StringNode)
                    {
                        types[i] = strType;
                    }
                    else if (t is NumberNode)
                    {
                        types[i] = doubleType;
                    }
                    else if (t is EnumNode)
                    {
                        types[i] = CheckType(((EnumNode)t).Type);
                    }
                    else
                    {
                        // todo: error message
                        throw new EdgeParserException();
                    }
                }

                ctor = objType.GetConstructor(types);
                if (ctor != null)
                    return;

                // ctor type inference
                // todo: refactor!!!
                var avaliableCtors = objType.GetConstructors()
                                            .Select(t => t.GetParameters())
                                            .Where(t => t.Length == types.Length)
                                            .ToArray();
                var uriType = typeof(Uri);

                for (int i = 0; i < avaliableCtors.Length && ctor == null; i++)
                {
                    var currentCtor = avaliableCtors[i];

                    for (int j = 0; j < currentCtor.Length && ctor == null; j++)
                    {
                        if (currentCtor[j].ParameterType == uriType && types[j] == strType)
                        {
                            types[j] = uriType;

                            // id
                            var urlStrType = char.ToLowerInvariant(uriType.Name[0]) + uriType.Name.Substring(1);
                            string id = null;
                            for (int k = 1; k < int.MaxValue; k++)
                            {
                                id = urlStrType + k;
                                if (!tree.Objects.Any(obj => obj.Id == id))
                                    break;
                            }
                            if (id == null)
                                // todo: message
                                throw new EdgeAnalyzerException();

                            tree.AddObject(new ObjectNode(uriType.Name, id, new[] { args[j] }));
                            args[j] = new ReferenceNode(id, uriType.Name);

                            ctor = objType.GetConstructor(types);
                        }
                    }
                }

                // todo: fix
                if (ctor != null)
                {
                    objNode.ConstructorArguments = args;

                    return;
                }
            }

            // todo: error message
            throw new EdgeAnalyzerException();
        }
Пример #5
0
 public void AddObject(ObjectNode node)
 {
     objects.Add(node);
 }
Пример #6
0
 public SyntaxTree(ObjectNode rootObject, IEnumerable<ObjectNode> objects)
     : this(null, rootObject, objects)
 {
 }
Пример #7
0
 public void RemoveObject(ObjectNode node)
 {
     objects.Remove(node);
 }
Пример #8
0
 public void RemoveObject(ObjectNode node)
 {
     objects.Remove(node);
 }
Пример #9
0
 public void AddObject(ObjectNode node)
 {
     objects.Add(node);
 }
Пример #10
0
 public SyntaxTree(ObjectNode rootObject, IEnumerable <ObjectNode> objects)
     : this(null, rootObject, objects)
 {
 }
Пример #11
0
        private string CreateConstructor(ObjectNode node)
        {
            if (node.ConstructorArguments == null)
                return $"new {node.Type}()";

            return $"new {node.Type}({string.Join("m", node.ConstructorArguments.Select(CreateValue))})";
        }