Example #1
0
 private void LoadDefaultProperties(CompilerPlugin plugin)
 {
     // 2. Assign all plugin properties
     plugin.Desc = this.GetOrDefaultString("desc", string.Empty);
     plugin.PluginType = this.GetOrDefaultString("type", "expr");
     plugin.Author = this.GetOrDefaultString("author", "Kishore Reddy");
     plugin.Company = this.GetOrDefaultString("company", "CodeHelix Solutions Inc.");
     plugin.Url = this.GetOrDefaultString("url", "http://www.codehelixsolutions.com");
     plugin.Url2 = this.GetOrDefaultString("url2", "http://fluentscript.codeplex.com");
     plugin.Doc = this.GetOrDefaultString("doc", "http://fluentscript.codeplex.com/documentation");
     plugin.Version = this.GetOrDefaultString("version", "0.9.8.10");
     plugin.IsStatement = this.GetOrDefaultBool("isStatement", true);
     plugin.IsEndOfStatementRequired = this.GetOrDefaultBool("isEOSRequired", true);
     plugin.IsEnabled = this.GetOrDefaultBool("isEnabled", true);
     plugin.IsSystemLevel = this.GetOrDefaultBool("isSystemLevel", false);
     plugin.IsAutoMatched = this.GetOrDefaultBool("isAutoMatched", false);
     plugin.IsAssignmentSupported = this.GetOrDefaultBool("isAssignmentSupported", false);
 }
Example #2
0
        /// <summary>
        /// After parsing is complete, register the alias.
        /// </summary>
        /// <param name="node"></param>
        public override void OnParseComplete(AstNode node)
        {
            var stmt = node as AliasStmt;
            //var plugin = new AliasTokenPlugin(stmt.Alias, stmt.Actual);
            //plugin.Init(_parser, _tokenIt);
            var plugin = new CompilerPlugin();
            plugin.PluginType = "token";
            plugin.TokenReplacements = new List<string[]>();
            plugin.TokenReplacements.Add(new string[] { stmt.Alias, stmt.Actual.Text });
            plugin.Precedence = 1;

            Ctx.PluginsMeta.Register(plugin);
        }
Example #3
0
 private void LoadExamples(CompilerPlugin plugin)
 {
     // 3. Examples
     var examplesList = this.Ctx.Memory.Get<object>("examples") as LArray;
     if (examplesList != null && examplesList.Value != null && examplesList.Value.Count > 0)
     {
         var examples = new List<string>();
         foreach (var lobj in examplesList.Value)
         {
             var example = lobj as LObject;
             var exampleText = example.GetValue().ToString();
             examples.Add(exampleText);
         }
         plugin.Examples = examples.ToArray();
     }
 }
Example #4
0
        private void SetupPlugin()
        {
            // 1. Create the meta plugin
            var plugin = new CompilerPlugin();
            plugin.Name = this.Name;

            // 2. Load default properties such as desc, company, etc.
            this.LoadDefaultProperties(plugin);

            // 3. Load the examples
            this.LoadExamples(plugin);

            // 4. token replacements ? or expression plugin?
            if(plugin.PluginType == "expr")
            {
                plugin.ParseExpr = this.GetFunc("parse");
                this.LoadStartTokensAsMap(plugin);
                this.LoadGrammar(plugin);
            }
            else if(plugin.PluginType == "token")
            {
                this.LoadGrammar(plugin);
                this.LoadStartTokensAsList(plugin);
                this.LoadTokenReplacements(plugin);
                if(this.Ctx.Memory.Contains("parse"))
                    plugin.ParseExpr = this.GetFunc("parse");
            }

            // 5. Finally register the plugin.
            this.Ctx.PluginsMeta.Register(plugin);
        }
Example #5
0
        private void LoadStartTokensAsMap(CompilerPlugin plugin)
        {
            // 4. Setup the start tokens.
            var tokens = new List<string>();
            var map = this.Ctx.Memory.Get<object>("start_tokens") as LMap;

            // 5. Not start tokens supplied ?
            if (map != null && map.Value.Count != 0)
            {
                plugin.StartTokenMap = map.Value;
                foreach (var keyval in map.Value)
                {
                    tokens.Add(keyval.Key);
                }
                plugin.StartTokens = tokens.ToArray();
            }
        }
Example #6
0
        private void LoadTokenReplacements(CompilerPlugin plugin)
        {
            if (!this.Ctx.Memory.Contains("tokens"))
                return;

            var array = this.Ctx.Memory.Get<object>("tokens") as LArray;
            var records = array.Value;
            var replacements = new List<string[]>();
            foreach (var record in records)
            {
                var list = record as LArray;
                if (list != null)
                {
                    var columns = list.Value;
                    var alias = columns[0] as LString;
                    var replacement = columns[1] as LString;
                    if (alias != null && replacement != null)
                    {
                        replacements.Add(new string[2] { alias.Value, replacement.Value });
                    }
                }
            }
            plugin.TokenReplacements = replacements;
        }
Example #7
0
        private void LoadGrammar(CompilerPlugin plugin)
        {
            // 6. Parse the grammar
            plugin.Grammar = this.GetOrDefaultString("grammar_parse", "");

            // 7a. check for empty
            if (!string.IsNullOrEmpty(plugin.Grammar))
            {
                var parser = new GrammerParser();
                plugin.Matches = parser.Parse(plugin.Grammar);
            }
        }
Example #8
0
        private void LoadStartTokensAsList(CompilerPlugin plugin)
        {
            if (!this.Ctx.Memory.Contains("start_tokens"))
                return;

            var list = this.Ctx.Memory.Get<object>("start_tokens") as LArray;
            List<string> tokens = null;

            // 5. Not start tokens supplied ?
            if (list != null && list.Value.Count != 0)
            {
                tokens = new List<string>();
                for (var ndx = 0; ndx < list.Value.Count; ndx++)
                {
                    var val = list.Value[ndx] as LString;
                    tokens.Add(val.Value);
                }
            }
            if(tokens != null && tokens.Count > 0)
                plugin.StartTokens = tokens.ToArray();
        }
Example #9
0
 private MatchResult IsGrammarMatch(CompilerPlugin plugin)
 {
     // 5. Check Grammer.
     var args = new Dictionary<string, object>();
     var peekCount = 0;
     var result = CheckMatches(plugin, plugin.Matches, args, peekCount);
     result.Plugin = plugin;
     return result;
 }
Example #10
0
        private MatchResult CheckMatches(CompilerPlugin plugin, List<TokenMatch> matches, Dictionary<string, object> args, int peekCount)
        {
            var isMatch = true;
            var token = peekCount == 0 ? this.TokenIt.NextToken : this.TokenIt.Peek(peekCount);
            foreach (var match in matches)
            {
                var incrementPeek = false;

                // Check 1: Group tokens ?
                if(match.IsGroup)
                {
                    var submatches = ((TokenGroup) match).Matches;
                    var result = CheckMatches(plugin, submatches, args, peekCount);
                    if(match.IsRequired && !result.Success)
                    {
                        isMatch = false;
                        break;
                    }
                }
                // Check 2: starttoken?
                else if (match.TokenType == "@starttoken")
                {
                    incrementPeek = true;
                }
                // Check 3a: Optional words with text
                else if (!match.IsRequired && match.Text != null && match.Text != token.Token.Text)
                {
                    incrementPeek = false;
                }
                // Check 3b: Optional words matched
                else if (!match.IsRequired && match.IsMatchingValue(token.Token))
                {
                    incrementPeek = true;
                }
                // Check 4: Optional word not matched
                else if (!match.IsRequired && !match.IsMatchingValue(token.Token))
                {
                    incrementPeek = false;
                }
                // Check 5: Expected word
                else if (match.IsRequired && match.TokenType == null && match.Text == token.Token.Text)
                {
                    incrementPeek = true;
                }
                // Check 6: check the type of n1
                else if (match.IsMatchingType(token.Token))
                {
                    incrementPeek = true;
                }
                else
                {
                    isMatch = false;
                    break;
                }
                if (incrementPeek)
                {
                    if (!string.IsNullOrEmpty(match.Name))
                    {
                        args[match.Name] = token;
                        if(match.TokenPropEnabled && match.TokenPropValue == "value")
                        {
                            var startToken = token.Token.Text;
                            args[match.Name + "-value"] = plugin.StartTokenMap[startToken];
                        }
                    }
                    // Matched: increment.
                    peekCount++;
                    token = this.TokenIt.Peek(peekCount);
                }
            }
            var res = new MatchResult(isMatch, null, args);
            res.TokenCount = peekCount;
            return res;
        }
Example #11
0
        /// <summary>
        /// Validates the compiler plugin.
        /// </summary>
        /// <param name="plugin"></param>
        /// <returns></returns>
        public BoolMsgObj Validate(CompilerPlugin plugin)
        {
            var errors = new List<string>();
            if(string.IsNullOrEmpty(plugin.Grammar))
                errors.Add("Grammar not supplied");

            if(string.IsNullOrEmpty(plugin.Name))
                errors.Add("Compiler plugin name not supplied");

            if(string.IsNullOrEmpty(plugin.FullName))
                errors.Add("Compiler plugin full name not supplied");

            if(plugin.StartTokens.Length == 0)
                errors.Add("Start tokens not supplied");

            if(string.IsNullOrEmpty(plugin.PluginType))
                errors.Add("Plugin type not supplied");

            if(plugin.ParseExpr == null)
                errors.Add("Plugin parse function not supplied");

            var success = errors.Count == 0;
            var message = "";
            if (!success)
            {
                foreach (var msg in errors)
                    message += msg + "\r\n";
            }
            var result = new BoolMsgObj(errors, success, message);
            return result;
        }
Example #12
0
        /// <summary>
        /// Register the compiler plugin.
        /// </summary>
        /// <param name="plugin"></param>
        public void Register(CompilerPlugin plugin)
        {
            _allPlugins.Add(plugin);
            if (plugin.PluginType == "expr" && plugin.StartTokens.Length > 0)
            {
                foreach (var startToken in plugin.StartTokens)
                {
                    var tokenPlugins = _pluginExprs.ContainsKey(startToken)
                                           ? _pluginExprs[startToken]
                                           : new List<CompilerPlugin>();
                    tokenPlugins.Add(plugin);
                    _pluginExprs[startToken] = tokenPlugins;
                }
            }
            else if(plugin.PluginType == "token" )
            {
                var tplugin = new TokenReplacePlugin();

                var hasStartTokens = plugin.StartTokens != null && plugin.StartTokens.Length > 0;
                var list = new List<CompilerPlugin>();
                plugin.Handler = tplugin;
                list.Add(plugin);

                if( hasStartTokens )
                {
                    foreach(var startToken in plugin.StartTokens)
                    {
                        _pluginTokens[startToken] = list;
                    }
                }
                if (plugin.TokenReplacements != null && plugin.TokenReplacements.Count > 0)
                {
                    foreach (var replacements in plugin.TokenReplacements)
                    {
                        var alias = replacements[0];
                        var replaceWith = replacements[1];
                        tplugin.SetupReplacement(alias, replaceWith);
                        if(!hasStartTokens)
                        {
                            _pluginTokens[alias] = list;
                        }
                    }
                }
            }
        }
Example #13
0
 public MatchResult(bool success, CompilerPlugin plugin, IDictionary<string, object> args)
 {
     this.Args = args;
     this.Plugin = plugin;
     this.Success = success;
 }