Esempio n. 1
0
        private void CustomSelector(string name, bool hasArguments)
        {
            Delegate deleg;

            if (!CustomSelectors.TryGetValue(name, out deleg))
            {
                throw new FormatException(string.Format("Unknown pseudo-selector '{0}'.", name));
            }
            var formalParameters = deleg.Method.GetParameters();
            var actualParameters = new object[formalParameters.Length];

            for (int i = 0; i < formalParameters.Length; i++)
            {
                if (i != 0)
                {
                    Read(ToTokenSpec(Token.Semicolon()));
                    Read(ToTokenSpec(TokenKind.WhiteSpace));
                }
                var type     = formalParameters[i].ParameterType;
                var typeCode = Type.GetTypeCode(type);
                if (typeCode == TypeCode.String)
                {
                    actualParameters[i] = Read(ToTokenSpec(TokenKind.String)).Text;
                }
                else if (
                    typeCode == TypeCode.Byte ||
                    typeCode == TypeCode.UInt16 ||
                    typeCode == TypeCode.UInt32 ||
                    typeCode == TypeCode.UInt64 ||
                    typeCode == TypeCode.SByte ||
                    typeCode == TypeCode.Int16 ||
                    typeCode == TypeCode.Int32 ||
                    typeCode == TypeCode.Int64
                    )
                {
                    actualParameters[i] = int.Parse(Read(ToTokenSpec(TokenKind.Integer)).Text, CultureInfo.InvariantCulture);
                }

                else if (type.GetGenericTypeDefinition() == typeof(Selector <>))
                {
                    actualParameters[i] = ParseSubGenerator().Selector;
                }
                else
                {
                    throw new ArgumentException(string.Format("Unsupported parameter type for custom selector '{0}'", name));
                }
            }
            var selector = deleg.DynamicInvoke(actualParameters);

            _generator.CustomSelector(selector);
        }