示例#1
0
        private static void HandleAlternative(StringBuilder codeStringBuilder, Rule rule, Alternative alt, List<string> vars, List<string> items)
        {
            // TODO: to solve the infinite recursion problem i am seeing i need to come up with
            // TODO: a general version of the code seen in GrammarParser:Metas() or GrammarParser:Rules() 
            // TODO: and also determine when to apply that form vs. the form i already have.

            // detect a right recursive alternative
            if (alt.Items[alt.Items.Count - 1] == rule.Name)
            {
                HandleRightRecursiveAlternative(codeStringBuilder, rule, alt, items, vars);
            }
            else
            {
                HandleStandardAlternative(codeStringBuilder, rule, alt, items, vars);
            }
        }
示例#2
0
        private static void HandleRightRecursiveAlternative(StringBuilder codeStringBuilder, Rule rule, Alternative alt, List<string> items, List<string> vars)
        {
            System.Console.WriteLine("Right recursive rule alternative detected.");

            string _var = CreateVar(codeStringBuilder, alt.Items[0], items, vars);

            codeStringBuilder.Append("if(");
            codeStringBuilder.Append($"{_var} != null");
            codeStringBuilder.AppendLine(") {");
            // further condition matching here
            foreach (string _item in alt.Items.GetRange(1, alt.Items.Count - 1))
            {
                if (_item != alt.Items[alt.Items.Count - 1])
                {
                    CreateVar(codeStringBuilder, _item, items, vars);
                }
                else
                {
                    codeStringBuilder.AppendLine($"List<Object> _{_item} = new List<Object>();");
                    codeStringBuilder.AppendLine($"_{_item}.Add({_var});");
                    codeStringBuilder.AppendLine($"List<Object> _new{_item} = (List<Object>)Memoize({_item});");
                    codeStringBuilder.Append($"if(_new{_item} != null");
                    codeStringBuilder.AppendLine(") {");
                    codeStringBuilder.AppendLine($"_{_item}.AddRange(_new{_item});");
                    codeStringBuilder.AppendLine($"Console.WriteLine(\"Recognized [ {rule.Name} ]\");");
                    codeStringBuilder.AppendLine("return true;");
                    codeStringBuilder.AppendLine("}");
                }
                AddComment(codeStringBuilder, $"return _{_item}");
            }

            codeStringBuilder.AppendLine("}");
        }
示例#3
0
        private static void HandleStandardAlternative(StringBuilder codeStringBuilder, Rule rule, Alternative alt, List<string> items, List<string> vars)
        {
            foreach (string _item in alt.Items)
            {
                CreateVar(codeStringBuilder, _item, items, vars);
            }
            codeStringBuilder.Append("if(");
            foreach (string var in items)
            {
                if (var != items[items.Count - 1])
                {
                    codeStringBuilder.Append($"{var} != null && ");

                }
                else
                {
                    codeStringBuilder.Append($"{var} != null");
                }
            }
            codeStringBuilder.AppendLine(") {");
            codeStringBuilder.AppendLine($"Console.WriteLine(\"Recognized [ {rule.Name} ]\");");
            /*
                TODO: write node returning code here
            */
            codeStringBuilder.AppendLine("return true;");
            codeStringBuilder.AppendLine("}");
        }