コード例 #1
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);
        }