コード例 #1
0
ファイル: Plugins.cs プロジェクト: vokenet/CommonLibrary.NET
        /// <summary>
        /// Register all of the custom extensions
        /// </summary>
        public void RegisterCustomByType(Type pluginType)
        {
            var         name   = pluginType.Name.Replace("Plugin", "");
            ILangPlugin plugin = _extMap.ContainsKey(name)
                                 ? _extMap[name]
                                 : (ILangPlugin)CreatePluginInstance(pluginType);

            Register(plugin);
        }
コード例 #2
0
        protected void ExpectError(ILangPlugin plugin, string script, string messageErrorPart)
        {
            // Check errors.
            var it = new Interpreter();

            it.Context.Plugins.Register(plugin);
            it.Execute(script);
            Assert.IsFalse(it.Result.Success);
            Assert.IsTrue(it.Result.Message.Contains(messageErrorPart));
        }
コード例 #3
0
ファイル: Plugins.cs プロジェクト: vokenet/CommonLibrary.NET
 private void RegisterPlugin(IDictionary <string, List <ILangPlugin> > map, ILangPlugin plugin, string[] tokens, bool sort)
 {
     if (tokens.Length > 0)
     {
         foreach (var token in tokens)
         {
             AddPlugin(map, plugin, token, sort);
         }
     }
 }
コード例 #4
0
ファイル: Plugins.cs プロジェクト: vokenet/CommonLibrary.NET
        private void RegisterExtensionsByNames(ICollection <string> pluginKeys, IDictionary <string, ILangPlugin> map)
        {
            var plugins = new ILangPlugin[pluginKeys.Count];
            int ndx     = 0;

            foreach (var key in pluginKeys)
            {
                if (!map.ContainsKey(key))
                {
                    throw new ArgumentException("Unknown plugin : " + key);
                }

                plugins[ndx] = map[key];
                ndx++;
            }
            Register(plugins);
        }
コード例 #5
0
ファイル: Plugins.cs プロジェクト: vokenet/CommonLibrary.NET
        private void AddPlugin(IDictionary <string, List <ILangPlugin> > map, ILangPlugin plugin, string token, bool sort)
        {
            List <ILangPlugin> list;

            if (!map.ContainsKey(token))
            {
                list       = new List <ILangPlugin>();
                map[token] = list;
            }
            else
            {
                list = map[token];
            }

            list.Add(plugin);

            if (sort)
            {
                SortPlugins(list);
            }
        }
コード例 #6
0
ファイル: Plugins.cs プロジェクト: vokenet/CommonLibrary.NET
 /// <summary>
 /// Registers a custom function callback.
 /// </summary>
 /// <param name="pluginToRegister">The function</param>
 /// <param name="sort">Whether or not to sort the plugin by precedence after adding it to the system.</param>
 public void Register(ILangPlugin pluginToRegister, bool sort = true)
 {
     if (pluginToRegister is IDisposable)
     {
         if (_disposablePlugins == null)
         {
             _disposablePlugins = new List <IDisposable>();
         }
         _disposablePlugins.Add(pluginToRegister as IDisposable);
     }
     if (pluginToRegister is IExprPlugin)
     {
         RegisterExprPlugin(pluginToRegister as IExprPlugin, sort);
     }
     else if (pluginToRegister is ITokenPlugin)
     {
         RegisterTokenPlugin(pluginToRegister as ITokenPlugin, sort);
     }
     else if (pluginToRegister is ILexPlugin)
     {
         RegisterLexPlugin(pluginToRegister as ILexPlugin, sort);
     }
 }
コード例 #7
0
ファイル: Plugins.cs プロジェクト: vokenet/CommonLibrary.NET
        /// <summary>
        /// Gets a plugin that matches the token supplied.
        /// </summary>
        /// <param name="map">The map of plugins to match the tokens against</param>
        /// <param name="token">The token to match a plugin against</param>
        /// <param name="key">Optional key to use to look for plugin instead of using the token.Text property</param>
        /// <param name="tokenPos">The position of the token supplied. e.g. 0 indicates it's the current token. 1 indicates its the next token</param>
        /// <returns></returns>
        private ILangPlugin GetPlugin(IDictionary <string, List <ILangPlugin> > map, Token token, string key = null, int tokenPos = 0)
        {
            // Check for end token.
            if (token == Tokens.EndToken)
            {
                return(null);
            }
            if (map == null || map.Count == 0)
            {
                return(null);
            }

            string             name           = key == null ? token.Text : key;
            bool               isCurrentToken = tokenPos == 0;
            List <ILangPlugin> plugins        = null;

            if (name == null)
            {
                return(null);
            }

            if (map.ContainsKey(name))
            {
                plugins = map[name];
            }
            else
            {
                var kind = "IdToken";
                if (token.Kind == TokenKind.Ident)
                {
                    kind = "IdToken";
                }
                else if (token.Kind == TokenKind.LiteralDate)
                {
                    kind = "DateToken";
                }
                else if (token.Kind == TokenKind.LiteralNumber)
                {
                    kind = "NumberToken";
                }
                else
                {
                    kind = token.GetType().Name;
                }

                name = "$" + kind;
                if (map.ContainsKey(name))
                {
                    plugins = map[name];
                }
            }
            if (plugins != null)
            {
                // Either a specific word like "select" or a general IdToken.
                ILangPlugin matchedPlugin = null;
                foreach (var plugin in plugins)
                {
                    bool isTokenTypePlugin = plugin is ITokenPlugin;
                    if (isTokenTypePlugin)
                    {
                        if (((ITokenPlugin)plugin).CanHandle(token, isCurrentToken))
                        {
                            matchedPlugin = plugin;
                            break;
                        }
                    }
                    else if (plugin.CanHandle(token))
                    {
                        matchedPlugin = plugin;
                        break;
                    }
                }
                return(matchedPlugin);
            }
            return(null);
        }