<commit_before>﻿using System;
using System.Linq;
using Loyc.Syntax;
using Flame.Compiler;
using Loyc.Collections;
using Flame.Compiler.Expressions;
using Flame.Attributes;
using System.Collections.Generic;

namespace Flame.Ecs
{
    public static class AttributeConverters
    {
        private const string AttributeSuffix = "Attribute";

        private static LNode AppendAttributeSuffix(LNode Node)
        {
            if (Node.IsId)
                return LNode.Id(Node.Name + AttributeSuffix, Node);
            else if (Node.Calls(CodeSymbols.Dot, 2))
                return LNode.Call(
                    CodeSymbols.Dot, 
                    new VList<LNode>() 
                    { 
                        Node.Args[0], 
                        AppendAttributeSuffix(Node.Args[1]) 
                    }, 
                    Node);
            else
                return Node;
        }

        /// <summary>
        /// Converts a type node for a custom attribute. This
        /// includes appending an 'Attribute' suffix if initial
        /// lookup fails.
        /// </summary>
        /// <returns>The custom attribute type.</returns>
        /// <param name="Node">The custom attribute type node.</param>
        /// <param name="Scope">The global scope.</param>
        /// <param name="Converter">The node converter.</param>
        public static IType ConvertCustomAttributeType(
            LNode Node, GlobalScope Scope, NodeConverter Converter)
        {
            var simpleType = Converter.ConvertType(Node, Scope);
            var suffixedType = Converter.ConvertType(
                AppendAttributeSuffix(Node), Scope);

            if (simpleType == null)
            {
                if (suffixedType == null)
                {
                    Scope.Log.LogError(new LogEntry(
                            "type resolution",
                            NodeHelpers.HighlightEven(
                                "could not resolve attribute type '", Node.ToString(), "'."),
                            NodeHelpers.ToSourceLocation(Node.Range)));
                    return null;
                }

                return suffixedType;
            }
            else
            {
                if (suffixedType != null)
                {
                    Scope.Log.LogError(new LogEntry(
                        "type resolution",
                        NodeHelpers.HighlightEven(
                            "attribute type '", Node.ToString(), "' is ambiguous."),
                        NodeHelpers.ToSourceLocation(Node.Range)));
                }

                return simpleType;
            }
        }

        /// <summary>
        /// Converts the given custom attribute node.
        /// </summary>
        /// <returns>The analyzed custom attribute.</returns>
        /// <param name="Node">The custom attribute node.</param>
        /// <param name="Scope">The global scope.</param>
        /// <param name="Converter">The node converter.</param>
        public static IAttribute ConvertCustomAttribute(
            LNode Node, GlobalScope Scope, NodeConverter Converter)
        {
            var typeNode = Node.IsCall ? Node.Target : Node;
            var argNodes = Node.Args;<commit_msg>Improve custom attribute analysis logic<commit_after>﻿using System;
using System.Linq;
using Loyc.Syntax;
using Flame.Compiler;
using Loyc.Collections;
using Flame.Compiler.Expressions;
using Flame.Attributes;
using System.Collections.Generic;

namespace Flame.Ecs
{
    public static class AttributeConverters
    {
        private const string AttributeSuffix = "Attribute";

        private static LNode AppendAttributeSuffix(LNode Node)
        {
            if (Node.IsId)
                return LNode.Id(Node.Name + AttributeSuffix, Node);
            else if (Node.Calls(CodeSymbols.Dot, 2))
                return LNode.Call(
                    CodeSymbols.Dot, 
                    new VList<LNode>() 
                    { 
                        Node.Args[0], 
                        AppendAttributeSuffix(Node.Args[1]) 
                    }, 
                    Node);
            else
                return Node;
        }

        private static bool IsAttributeClass(IType Type)
        {
            return Type != null
            && Type.GetIsReferenceType()
            && !Type.GetIsDelegate()
            && !Type.GetIsArray()
            && !Type.GetIsInterface();
        }

        /// <summary>
        /// Converts a type node for a custom attribute. This
        /// includes appending an 'Attribute' suffix if initial
        /// lookup fails.
        /// </summary>
        /// <returns>The custom attribute type.</returns>
        /// <param name="Node">The custom attribute type node.</param>
        /// <param name="Scope">The global scope.</param>
        /// <param name="Converter">The node converter.</param>
        public static IType ConvertCustomAttributeType(
            LNode Node, GlobalScope Scope, NodeConverter Converter)
        {
            var simpleType = Converter.ConvertType(Node, Scope);
            var suffixedType = Converter.ConvertType(
                AppendAttributeSuffix(Node), Scope);

            if (!IsAttributeClass(simpleType))
            {
                if (!IsAttributeClass(suffixedType))
                {
                    Scope.Log.LogError(new LogEntry(
                            "type resolution",
                            NodeHelpers.HighlightEven(
                                "could not resolve attribute type '", Node.ToString(), "'."),
                            NodeHelpers.ToSourceLocation(Node.Range)));
                    return null;
                }

                return suffixedType;
            }
            else
            {
                if (IsAttributeClass(suffixedType))
                {
                    Scope.Log.LogError(new LogEntry(
                        "type resolution",
                        NodeHelpers.HighlightEven(
                            "attribute type '", Node.ToString(), "' is ambiguous " +
                            "between classes '", Scope.TypeNamer.Convert(simpleType), 
                            "' and '", Scope.TypeNamer.Convert(suffixedType), "'."),
                        NodeHelpers.ToSourceLocation(Node.Range)));
                }

                return simpleType;
            }
        }

        /// <summary>
        /// Converts the given custom attribute node.
        /// </summary>
        /// <returns>The analyzed custom attribute.</returns>
        /// <param name="Node">The custom attribute node.</param>
        /// <param name="Scope">The global scope.</param>
        /// <param name="Converter">The node converter.</param>
        public static IAttribute ConvertCustomAttribute(
            LNode Node, GlobalScope Scope, NodeConverter Converter)
        {
            var typeNode = Node.IsCall ? Node.Target : Node;
            var argNodes = Node.Args;
{'fraction_non_alphanumeric': 0.09861567004424147, 'fraction_numerical': 0.0008562865705722849, 'mean_word_length': 1.4123924268502581, 'pattern_counts': {'":': 0, '<': 45, '<?xml version=': 0, '>': 45, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7470019', 'n_tokens_mistral': 1700, 'n_tokens_neox': 1615, 'n_words': 401}