Exemple #1
0
        internal static IProperty GetPropertyKey(XamlParserContext parserContext, ITextLocation lineInformation, XmlNamespace xmlNamespace, string typeAndPropertyName, XmlNamespace targetTypeNamespace, IType targetTypeId, MemberType memberTypes, MemberType defaultType, bool allowProtectedPropertiesOnTargetType)
        {
            string typeName;
            string memberName;

            if (XamlTypeHelper.SplitMemberName(parserContext, lineInformation, typeAndPropertyName, out typeName, out memberName))
            {
                if (string.IsNullOrEmpty(memberName))
                {
                    parserContext.ReportError(XamlParseErrors.MissingArgumentName(lineInformation));
                    return((IProperty)null);
                }
                if (xmlNamespace == XmlNamespace.DesignTimeXmlNamespace)
                {
                    return(parserContext.PlatformMetadata.GetDesignTimeProperty(memberName, targetTypeId));
                }
                if (xmlNamespace != null && typeName == null)
                {
                    if (xmlNamespace == XmlNamespace.XamlXmlNamespace)
                    {
                        IProperty property = XamlProcessingAttributes.GetProperty(memberName, parserContext.TypeResolver.PlatformMetadata);
                        if (property != null)
                        {
                            return(property);
                        }
                        parserContext.ReportError(XamlParseErrors.UnrecognizedAttribute(lineInformation, xmlNamespace, memberName));
                        return((IProperty)null);
                    }
                    if (xmlNamespace == XmlNamespace.PresentationOptionsXmlNamespace)
                    {
                        if (memberName == parserContext.PlatformMetadata.KnownProperties.DesignTimeFreeze.Name)
                        {
                            return(parserContext.TypeResolver.ResolveProperty(parserContext.PlatformMetadata.KnownProperties.DesignTimeFreeze));
                        }
                        parserContext.ReportError(XamlParseErrors.UnrecognizedAttribute(lineInformation, xmlNamespace, memberName));
                        return((IProperty)null);
                    }
                    if (targetTypeNamespace == null || !xmlNamespace.Equals((object)targetTypeNamespace))
                    {
                        parserContext.ReportError(XamlParseErrors.UnrecognizedAttribute(lineInformation, xmlNamespace, memberName));
                        return((IProperty)null);
                    }
                }
                MemberAccessTypes access = MemberAccessTypes.None;
                if (typeName != null)
                {
                    if (xmlNamespace != null)
                    {
                        targetTypeId = XamlTypeHelper.GetTypeId(parserContext, lineInformation, xmlNamespace, typeName);
                        if (parserContext.PlatformMetadata.IsNullType((ITypeId)targetTypeId))
                        {
                            return((IProperty)null);
                        }
                        access = TypeHelper.GetAllowableMemberAccess(parserContext.TypeResolver, targetTypeId);
                    }
                }
                else if (!parserContext.PlatformMetadata.IsNullType((ITypeId)targetTypeId))
                {
                    access = TypeHelper.GetAllowableMemberAccess(parserContext.TypeResolver, targetTypeId);
                    if (allowProtectedPropertiesOnTargetType)
                    {
                        access |= MemberAccessTypes.Protected;
                    }
                }
                if (!parserContext.PlatformMetadata.IsNullType((ITypeId)targetTypeId))
                {
                    IProperty property1 = (IProperty)targetTypeId.GetMember(defaultType, memberName, access);
                    if (property1 == null)
                    {
                        MemberType memberTypes1 = memberTypes & ~defaultType;
                        if (memberTypes1 != MemberType.None)
                        {
                            property1 = (IProperty)targetTypeId.GetMember(memberTypes1, memberName, access);
                        }
                    }
                    if (property1 == null && targetTypeId.PlatformMetadata.GetProxyProperties(parserContext.TypeResolver) != null)
                    {
                        foreach (IProperty property2 in targetTypeId.PlatformMetadata.GetProxyProperties(parserContext.TypeResolver))
                        {
                            if (memberName == property2.Name && property2.DeclaringType.IsAssignableFrom((ITypeId)targetTypeId) && TypeHelper.IsSet(memberTypes, property2.MemberType))
                            {
                                property1 = property2;
                                break;
                            }
                        }
                    }
                    if (property1 == null)
                    {
                        property1 = (IProperty)XamlTypeHelper.AddMemberIfPossible(parserContext.PlatformMetadata, targetTypeId, defaultType, memberName);
                    }
                    if (property1 == null)
                    {
                        parserContext.ReportError(XamlParseErrors.UnrecognizedOrInaccessibleMember(lineInformation, memberName));
                    }
                    return(property1);
                }
                parserContext.ReportError(XamlParseErrors.CannotDetermineMemberTargetType(lineInformation, memberName));
            }
            return((IProperty)null);
        }
Exemple #2
0
        public static MarkupExtensionDescription Tokenize(XamlParserContext parserContext, ITextLocation lineInformation, string text)
        {
            MarkupExtensionDescription extensionDescription = new MarkupExtensionDescription();

            MarkupExtensionParser.ScannerState scannerState = MarkupExtensionParser.ScannerState.Begin;
            int    startIndex = 0;
            int    index      = 0;
            string key        = (string)null;

            while (index < text.Length)
            {
                char c = text[index];
                ++index;
                switch (scannerState)
                {
                case MarkupExtensionParser.ScannerState.Begin:
                    if ((int)c == 123)
                    {
                        scannerState = MarkupExtensionParser.ScannerState.ScanningMarkupExtensionName;
                        while (index < text.Length && Scanner.IsXmlWhitespace(text[index]))
                        {
                            ++index;
                        }
                        startIndex = index;
                        continue;
                    }
                    if (!Scanner.IsXmlWhitespace(c))
                    {
                        parserContext.ReportError(XamlParseErrors.UnexpectedCharacter(lineInformation, c));
                        return((MarkupExtensionDescription)null);
                    }
                    continue;

                case MarkupExtensionParser.ScannerState.ScanningMarkupExtensionName:
                    if ((int)c == 125 || Scanner.IsXmlWhitespace(c))
                    {
                        int    length = index - startIndex - 1;
                        string str    = MarkupExtensionParser.ReplaceEscapedCharacters(MarkupExtensionParser.TrimOffWhitespace(text.Substring(startIndex, length)));
                        if (str.Length > 0)
                        {
                            extensionDescription.Name = str;
                            scannerState = (int)c == 125 ? MarkupExtensionParser.ScannerState.End : MarkupExtensionParser.ScannerState.ScanningNameOrValue;
                            startIndex   = index;
                            continue;
                        }
                        parserContext.ReportError(XamlParseErrors.MissingMarkupExtensionName(lineInformation));
                        return((MarkupExtensionDescription)null);
                    }
                    if ((int)c == 123 || (int)c == 44 || (int)c == 61)
                    {
                        parserContext.ReportError(XamlParseErrors.UnexpectedCharacter(lineInformation, c));
                        return((MarkupExtensionDescription)null);
                    }
                    continue;

                case MarkupExtensionParser.ScannerState.ScanningNameOrValue:
                case MarkupExtensionParser.ScannerState.ScanningValue:
                    switch (c)
                    {
                    case '=':
                        int length1 = index - startIndex - 1;
                        key        = MarkupExtensionParser.ReplaceEscapedCharacters(MarkupExtensionParser.TrimOffQuotes(MarkupExtensionParser.TrimOffWhitespace(text.Substring(startIndex, length1))));
                        startIndex = index;
                        if (key.Length > 0)
                        {
                            scannerState = MarkupExtensionParser.ScannerState.ScanningValue;
                            continue;
                        }
                        parserContext.ReportError(XamlParseErrors.MissingArgumentName(lineInformation));
                        return((MarkupExtensionDescription)null);

                    case '\\':
                        if (index < text.Length)
                        {
                            ++index;
                            continue;
                        }
                        continue;

                    case '{':
                        int num = 1;
                        while (index < text.Length)
                        {
                            char ch = text[index];
                            ++index;
                            switch (ch)
                            {
                            case '\\':
                                if (index < text.Length)
                                {
                                    ++index;
                                    break;
                                }
                                break;

                            case '{':
                                ++num;
                                break;

                            case '}':
                                --num;
                                break;
                            }
                            if (num == 0)
                            {
                                break;
                            }
                        }
                        if (num != 0)
                        {
                            parserContext.ReportError(XamlParseErrors.UnexpectedEnd(lineInformation));
                            return((MarkupExtensionDescription)null);
                        }
                        continue;

                    case '}':
                    case ',':
                        int    length2      = index - startIndex - 1;
                        string argumentName = MarkupExtensionParser.TrimOffQuotes(MarkupExtensionParser.TrimOffWhitespace(text.Substring(startIndex, length2)));
                        startIndex = index;
                        if (scannerState == MarkupExtensionParser.ScannerState.ScanningNameOrValue)
                        {
                            if (extensionDescription.NamedArguments.Count > 0)
                            {
                                parserContext.ReportError(XamlParseErrors.UnexpectedPositionalArgument(lineInformation, argumentName));
                                return((MarkupExtensionDescription)null);
                            }
                            extensionDescription.PositionalArguments.Add(argumentName);
                        }
                        else
                        {
                            if (string.IsNullOrEmpty(key))
                            {
                                parserContext.ReportError(XamlParseErrors.UnexpectedPositionalArgument(lineInformation, argumentName));
                                return((MarkupExtensionDescription)null);
                            }
                            extensionDescription.NamedArguments.Add(new KeyValuePair <string, string>(key, argumentName));
                        }
                        scannerState = (int)c != 125 ? MarkupExtensionParser.ScannerState.ScanningNameOrValue : MarkupExtensionParser.ScannerState.End;
                        continue;

                    case '"':
                    case '\'':
                        char ch1 = c;
                        while (index < text.Length)
                        {
                            char ch2 = text[index];
                            ++index;
                            if ((int)ch1 == (int)ch2)
                            {
                                break;
                            }
                        }
                        continue;

                    default:
                        continue;
                    }

                default:
                    if (!Scanner.IsXmlWhitespace(c))
                    {
                        parserContext.ReportError(XamlParseErrors.UnexpectedCharacter(lineInformation, c));
                        return((MarkupExtensionDescription)null);
                    }
                    continue;
                }
            }
            if (scannerState == MarkupExtensionParser.ScannerState.End)
            {
                return(extensionDescription);
            }
            parserContext.ReportError(XamlParseErrors.UnexpectedEnd(lineInformation));
            return((MarkupExtensionDescription)null);
        }