private void InitializeVerbs()
        {
            foreach (var prop in typeof(T).GetTypeInfo().GetProperties()
                     .Where(p => p.GetCustomAttributes <VerbAttribute>().Any()))
            {
                foreach (var attr in prop.GetCustomAttributes <VerbAttribute>())
                {
                    // TODO deduplicate this Verb Name generation logic
                    var verbName = attr.Name ?? prop.Name.ToLowerInvariant();
                    if (verbName.StartsWith(
                            ArgumentPrefix.ToString()))
                    {
                        throw new ArgumentIntegrityException(String.Format(
                                                                 "Verb {0} cannot begin with {1}",
                                                                 verbName, ArgumentPrefix));
                    }
                    if (Verbs.ContainsKey(verbName))
                    {
                        throw new DuplicateArgumentException(verbName);
                    }

                    // TODO allow verb to use other configuration types?
                    var parserConfigType = typeof(AttributeParserConfig <>)
                                           .GetTypeInfo()
                                           .MakeGenericType(new[] { prop.PropertyType });
                    var verbParserConfig = Activator.CreateInstance(
                        type: parserConfigType,
                        args: new object[] { Options, null /* TODO add triggers inside verb configs */, VerbFactory });

                    var verbConfigWrapperType = typeof(VerbParserConfig <>)
                                                .GetTypeInfo()
                                                .MakeGenericType(new[] { prop.PropertyType });
                    var config = (IVerbParserConfig)Activator.CreateInstance(
                        type: verbConfigWrapperType,
                        args: new[] { verbParserConfig, new PropertyValueStore(prop), Options, VerbFactory, new[] { verbName } });

                    config.Name        = verbName;
                    config.Description = attr.Description;
                    if (Options.IncludeHelpTriggerInVerbs)
                    {
                        var helpWrapperType = typeof(AutomaticHelpGenerator <>)
                                              .GetTypeInfo()
                                              .MakeGenericType(new[] { prop.PropertyType });
                        var triggers = new[] { (IHelpGenerator)Activator.CreateInstance(
                                                   type: helpWrapperType,
                                                   args: new object[] { }) };
                        config.AppendTriggers(triggers);
                    }

                    // TODO optimize this a bit
                    foreach (var innerVerb in config.Verbs.Values)
                    {
                        innerVerb.PrecursorVerbs = Enumerable.Repeat(verbName, 1).Concat(innerVerb.PrecursorVerbs).ToArray();
                    }

                    Verbs.Add(verbName, config);
                }
            }
        }
Exemple #2
0
 public string QueryVerbPresent(string word)
 {
     if (Verbs.ContainsKey(word))
     {
         var verb = Verbs[word];
         return(verb.Progressive);
     }
     return(null);
 }
Exemple #3
0
 public string QueryVerbPast(string word)
 {
     if (Verbs.ContainsKey(word))
     {
         var verb = Verbs[word];
         return(verb.Past);
     }
     return(null);
 }
        private void InitializeVerbs()
        {
            foreach (var prop in typeof(T).GetProperties()
                     .Where(p => p.GetCustomAttributes <VerbAttribute>().Any()))
            {
                foreach (var attr in prop.GetCustomAttributes <VerbAttribute>())
                {
                    var verbName = attr.Name ?? prop.Name;
                    if (verbName.StartsWith(
                            ArgumentPrefix.ToString(CultureInfo.InvariantCulture)))
                    {
                        throw new ArgumentIntegrityException(String.Format(
                                                                 "Verb {0} cannot begin with {1}",
                                                                 verbName, ArgumentPrefix));
                    }
                    if (Verbs.ContainsKey(verbName))
                    {
                        throw new DuplicateArgumentException(verbName);
                    }

                    var parserConfigType = typeof(AttributeParserConfig <>)
                                           .MakeGenericType(new[] { prop.PropertyType });
                    var parserConfig = Activator.CreateInstance(
                        parserConfigType,
                        new object[] { Options, null /* TODO add triggers inside verb configs */ },
                        null);

                    // Verb's settings object
                    // Object must have default constructor
                    var obj = Activator.CreateInstance(prop.PropertyType);

                    var contextType = typeof(ParsingContext <>)
                                      .MakeGenericType(new[] { prop.PropertyType });
                    var context = (IParsingContext)Activator.CreateInstance(
                        contextType,
                        new [] { obj, parserConfig },
                        null);

                    var verbConfig = new VerbConfig
                    {
                        Context = context,
                        Object  = obj,
                        Store   = new PropertyValueStore(prop)
                    };

                    Verbs.Add(verbName, verbConfig);
                }
            }
        }
Exemple #5
0
        private void InitializeVerbs()
        {
            foreach (var prop in typeof(T).GetTypeInfo().GetProperties()
                     .Where(p => p.GetCustomAttributes <VerbAttribute>().Any()))
            {
                foreach (var attr in prop.GetCustomAttributes <VerbAttribute>())
                {
                    // TODO deduplicate this Verb Name generation logic
                    var verbName = attr.Name ?? prop.Name.ToLowerInvariant();
                    if (verbName.StartsWith(
                            ArgumentPrefix.ToString()))
                    {
                        throw new ArgumentIntegrityException(String.Format(
                                                                 "Verb {0} cannot begin with {1}",
                                                                 verbName, ArgumentPrefix));
                    }
                    if (Verbs.ContainsKey(verbName))
                    {
                        throw new DuplicateArgumentException(verbName);
                    }

                    // TODO allow verb to use other configuration types?
                    var parserConfigType = typeof(AttributeParserConfig <>)
                                           .GetTypeInfo()
                                           .MakeGenericType(new[] { prop.PropertyType });
                    var verbParserConfig = Activator.CreateInstance(
                        type: parserConfigType,
                        args: new object[] { Options, null /* TODO add triggers inside verb configs */, VerbFactory });

                    var verbConfigWrapperType = typeof(VerbParserConfig <>)
                                                .GetTypeInfo()
                                                .MakeGenericType(new[] { prop.PropertyType });
                    var config = (IVerbParserConfig)Activator.CreateInstance(
                        type: verbConfigWrapperType,
                        args: new[] { verbParserConfig, new PropertyValueStore(prop), Options, VerbFactory });

                    Verbs.Add(verbName, config);
                }
            }
        }