<commit_before>using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using Avalonia.Markup.Parsers;
using XamlX;
using XamlX.Ast;
using XamlX.Emit;
using XamlX.IL;
using XamlX.Transform;
using XamlX.Transform.Transformers;
using XamlX.TypeSystem;

namespace Avalonia.Markup.Xaml.XamlIl.CompilerExtensions.Transformers
{
    using XamlParseException = XamlX.XamlParseException;
    using XamlLoadException = XamlX.XamlLoadException;
    class AvaloniaXamlIlSelectorTransformer : IXamlAstTransformer
    {
        public IXamlAstNode Transform(AstTransformationContext context, IXamlAstNode node)
        {
            if (!(node is XamlAstObjectNode on && on.Type.GetClrType().FullName == "Avalonia.Styling.Style"))
                return node;

            var pn = on.Children.OfType<XamlAstXamlPropertyValueNode>()
                .FirstOrDefault(p => p.Property.GetClrProperty().Name == "Selector");

            if (pn == null)
                return node;

            if (pn.Values.Count != 1)
                throw new XamlParseException("Selector property should should have exactly one value", node);
            
            if (pn.Values[0] is XamlIlSelectorNode)
                //Deja vu. I've just been in this place before
                return node;
            
            if (!(pn.Values[0] is XamlAstTextNode tn))
                throw new XamlParseException("Selector property should be a text node", node);

            var selectorType = pn.Property.GetClrProperty().Getter.ReturnType;
            var initialNode = new XamlIlSelectorInitialNode(node, selectorType);
            var avaloniaAttachedPropertyT = context.GetAvaloniaTypes().AvaloniaAttachedPropertyT;
            XamlIlSelectorNode Create(IEnumerable<SelectorGrammar.ISyntax> syntax,
                Func<string, string, XamlAstClrTypeReference> typeResolver)
            {
                XamlIlSelectorNode result = initialNode;
                XamlIlOrSelectorNode results = null;
                foreach (var i in syntax)
                {
                    switch (i)
                    {

                        case SelectorGrammar.OfTypeSyntax ofType:
                            result = new XamlIlTypeSelector(result, typeResolver(ofType.Xmlns, ofType.TypeName).Type, true);
                            break;
                        case SelectorGrammar.IsSyntax @is:
                            result = new XamlIlTypeSelector(result, typeResolver(@is.Xmlns, @is.TypeName).Type, false);
                            break;
                        case SelectorGrammar.ClassSyntax @class:
                            result = new XamlIlStringSelector(result, XamlIlStringSelector.SelectorType.Class, @class.Class);
                            break;
                        case SelectorGrammar.NameSyntax name:
                            result = new XamlIlStringSelector(result, XamlIlStringSelector.SelectorType.Name, name.Name);
                            break;
                        case SelectorGrammar.PropertySyntax property:
                        {
                            var type = result?.TargetType;

                            if (type == null)
                                throw new XamlParseException("Property selectors must be applied to a type.", node);

                            var targetProperty =
                                type.GetAllProperties().FirstOrDefault(p => p.Name == property.Property);

                            if (targetProperty == null)
                                throw new XamlParseException($"Cannot find '{property.Property}' on '{type}", node);

                            if (!XamlTransformHelpers.TryGetCorrectlyTypedValue(context,
                                new XamlAstTextNode(node, property.Value, type: context.Configuration.WellKnownTypes.String),
                                targetProperty.PropertyType, out var typedValue))
                                throw new XamlParseException(
                                    $"Cannot convert '{property.Value}' to '{targetProperty.PropertyType.GetFqn()}",
                                    node);

                            result = new XamlIlPropertyEqualsSelector(result, targetProperty, typedValue);
                            break;
                        }
                        case SelectorGrammar.AttachedPropertySyntax attachedProperty:
                            {
                                var targetType = result?.TargetType;
                                if (targetType == null)
                                {
                                    throw new XamlParseException("Attached Property selectors must be applied to a type.",node);
                                }
                                var attachedPropertyOwnerType = typeResolver(attachedProperty.Xmlns, attachedProperty.TypeName).Type;

                                if (attachedPropertyOwnerType is null)
                                {
                                    throw new XamlParseException($"Cannot find '{attachedProperty.Xmlns}:{attachedProperty.TypeName}",node);
                                }

                                var attachedPropertyName = attachedProperty.Property + "Property";

                                var targetPropertyField = attachedPropertyOwnerType.GetAllFields()
                                    .FirstOrDefault(f => f.IsStatic
                                        && f.IsPublic
                                        && f.Name == attachedPropertyName
                                        && f.FieldType.GenericTypeDefinition == avaloniaAttachedPropertyT
                                        );

                                if (targetPropertyField is null)
                                {
                                    throw new XamlParseException($"Cannot find '{attachedProperty.Property}' on '{attachedPropertyOwnerType.GetFqn()}", node);
                                }

                                var targetPropertyType = XamlIlAvaloniaPropertyHelper
                                    .GetAvaloniaPropertyType(targetPropertyField, context.GetAvaloniaTypes(), node);

                                if (!XamlTransformHelpers.TryGetCorrectlyTypedValue(context,
                                    new XamlAstTextNode(node, attachedProperty.Value, context.Configuration.WellKnownTypes.String),
                                    targetPropertyType, out var typedValue))
                                        throw new XamlParseException(
                                            $"Cannot convert '{attachedProperty.Value}' to '{targetPropertyType.GetFqn()}",
                                            node);

                                result = new XamlIlAttacchedPropertyEqualsSelector(result, targetPropertyField, typedValue);
                                break;
                            }
                        case SelectorGrammar.ChildSyntax child:
                            result = new XamlIlCombinatorSelector(result, XamlIlCombinatorSelector.SelectorType.Child);
                            break;<commit_msg>Fix compilation error from merge from master<commit_after>using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using Avalonia.Markup.Parsers;
using XamlX;
using XamlX.Ast;
using XamlX.Emit;
using XamlX.IL;
using XamlX.Transform;
using XamlX.Transform.Transformers;
using XamlX.TypeSystem;

namespace Avalonia.Markup.Xaml.XamlIl.CompilerExtensions.Transformers
{
    using XamlParseException = XamlX.XamlParseException;
    using XamlLoadException = XamlX.XamlLoadException;
    class AvaloniaXamlIlSelectorTransformer : IXamlAstTransformer
    {
        public IXamlAstNode Transform(AstTransformationContext context, IXamlAstNode node)
        {
            if (!(node is XamlAstObjectNode on && on.Type.GetClrType().FullName == "Avalonia.Styling.Style"))
                return node;

            var pn = on.Children.OfType<XamlAstXamlPropertyValueNode>()
                .FirstOrDefault(p => p.Property.GetClrProperty().Name == "Selector");

            if (pn == null)
                return node;

            if (pn.Values.Count != 1)
                throw new XamlParseException("Selector property should should have exactly one value", node);
            
            if (pn.Values[0] is XamlIlSelectorNode)
                //Deja vu. I've just been in this place before
                return node;
            
            if (!(pn.Values[0] is XamlAstTextNode tn))
                throw new XamlParseException("Selector property should be a text node", node);

            var selectorType = pn.Property.GetClrProperty().Getter.ReturnType;
            var initialNode = new XamlIlSelectorInitialNode(node, selectorType);
            var avaloniaAttachedPropertyT = context.GetAvaloniaTypes().AvaloniaAttachedPropertyT;
            XamlIlSelectorNode Create(IEnumerable<SelectorGrammar.ISyntax> syntax,
                Func<string, string, XamlAstClrTypeReference> typeResolver)
            {
                XamlIlSelectorNode result = initialNode;
                XamlIlOrSelectorNode results = null;
                foreach (var i in syntax)
                {
                    switch (i)
                    {

                        case SelectorGrammar.OfTypeSyntax ofType:
                            result = new XamlIlTypeSelector(result, typeResolver(ofType.Xmlns, ofType.TypeName).Type, true);
                            break;
                        case SelectorGrammar.IsSyntax @is:
                            result = new XamlIlTypeSelector(result, typeResolver(@is.Xmlns, @is.TypeName).Type, false);
                            break;
                        case SelectorGrammar.ClassSyntax @class:
                            result = new XamlIlStringSelector(result, XamlIlStringSelector.SelectorType.Class, @class.Class);
                            break;
                        case SelectorGrammar.NameSyntax name:
                            result = new XamlIlStringSelector(result, XamlIlStringSelector.SelectorType.Name, name.Name);
                            break;
                        case SelectorGrammar.PropertySyntax property:
                        {
                            var type = result?.TargetType;

                            if (type == null)
                                throw new XamlParseException("Property selectors must be applied to a type.", node);

                            var targetProperty =
                                type.GetAllProperties().FirstOrDefault(p => p.Name == property.Property);

                            if (targetProperty == null)
                                throw new XamlParseException($"Cannot find '{property.Property}' on '{type}", node);

                            if (!XamlTransformHelpers.TryGetCorrectlyTypedValue(context,
                                new XamlAstTextNode(node, property.Value, type: context.Configuration.WellKnownTypes.String),
                                targetProperty.PropertyType, out var typedValue))
                                throw new XamlParseException(
                                    $"Cannot convert '{property.Value}' to '{targetProperty.PropertyType.GetFqn()}",
                                    node);

                            result = new XamlIlPropertyEqualsSelector(result, targetProperty, typedValue);
                            break;
                        }
                        case SelectorGrammar.AttachedPropertySyntax attachedProperty:
                            {
                                var targetType = result?.TargetType;
                                if (targetType == null)
                                {
                                    throw new XamlParseException("Attached Property selectors must be applied to a type.",node);
                                }
                                var attachedPropertyOwnerType = typeResolver(attachedProperty.Xmlns, attachedProperty.TypeName).Type;

                                if (attachedPropertyOwnerType is null)
                                {
                                    throw new XamlParseException($"Cannot find '{attachedProperty.Xmlns}:{attachedProperty.TypeName}",node);
                                }

                                var attachedPropertyName = attachedProperty.Property + "Property";

                                var targetPropertyField = attachedPropertyOwnerType.GetAllFields()
                                    .FirstOrDefault(f => f.IsStatic
                                        && f.IsPublic
                                        && f.Name == attachedPropertyName
                                        && f.FieldType.GenericTypeDefinition == avaloniaAttachedPropertyT
                                        );

                                if (targetPropertyField is null)
                                {
                                    throw new XamlParseException($"Cannot find '{attachedProperty.Property}' on '{attachedPropertyOwnerType.GetFqn()}", node);
                                }

                                var targetPropertyType = XamlIlAvaloniaPropertyHelper
                                    .GetAvaloniaPropertyType(targetPropertyField, context.GetAvaloniaTypes(), node);

                                if (!XamlTransformHelpers.TryGetCorrectlyTypedValue(context,
                                    new XamlAstTextNode(node, attachedProperty.Value, type: context.Configuration.WellKnownTypes.String),
                                    targetPropertyType, out var typedValue))
                                        throw new XamlParseException(
                                            $"Cannot convert '{attachedProperty.Value}' to '{targetPropertyType.GetFqn()}",
                                            node);

                                result = new XamlIlAttacchedPropertyEqualsSelector(result, targetPropertyField, typedValue);
                                break;
                            }
                        case SelectorGrammar.ChildSyntax child:
                            result = new XamlIlCombinatorSelector(result, XamlIlCombinatorSelector.SelectorType.Child);
                            break;
{'fraction_non_alphanumeric': 0.06736682097699163, 'fraction_numerical': 0.0004145650521661024, 'mean_word_length': 1.3813754524514643, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 62, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6822150', 'n_tokens_mistral': 3251, 'n_tokens_neox': 3069, 'n_words': 678}