Ejemplo n.º 1
0
        /// <summary>
        /// Generates code that matches a rule
        /// </summary>
        /// <param name="exp"></param>
        /// <returns></returns>
        private string GenerateRuleMatch(GrammarInfo grammar, string name)
        {
            var g = grammar.FindProductionGrammar(name);

            //return String.Format("Match(parser, new {0}.{1}())", ns, name);
            return(String.Format(
                       "Match(parser, new {0}.{1}())",
                       g.Namespace,
                       name));
        }
Ejemplo n.º 2
0
 public GrammarInfo FindProductionGrammar(string name)
 {
     if (allProductions.ContainsKey(name) || inheritedRules.Contains(name))
     {
         return(this);
     }
     else if (inheritedGrammar != null)
     {
         return(inheritedGrammar.FindProductionGrammar(name));
     }
     else
     {
         throw new InvalidOperationException();
     }
 }
Ejemplo n.º 3
0
        /// <summary>
        /// Generate a single rule class
        /// </summary>
        /// <param name="f"></param>
        /// <returns></returns>
        private string GenerateRuleClass(GrammarInfo grammar, string name)
        {
            int tabs   = 3;
            var resstr = "res";

            var g   = grammar.FindProductionGrammar(name, false);
            var exp = g.Rules[name].GetValue(null) as LambdaExpression;

            if (exp == null)
            {
                // **** TODO
                throw new ParserGeneratorException(String.Format("Rule '{0}' must be a lambda expression.", name));
            }

            CodeStringBuilder code = new CodeStringBuilder();

            code.AppendLine(tabs, "bool res = true;");
            code.AppendLine();

            code.Append(GenerateProductionMatch(tabs, grammar, exp.Body, resstr));

            code.AppendLine(tabs, "return res;");

            // Figure out whether this rule is inherited or not
            string inherited;

            g = grammar.FindProductionBaseGrammar(name);

            if (g == grammar)
            {
                inherited = String.Format("{0}.Node", typeof(Token).Namespace);
            }
            else
            {
                inherited = String.Format("{0}.{1}", g.Attributes.Namespace, name);
                //inheritedRules.Add(name);
            }

            var template = new StringBuilder(Templates.Rule);

            template.Replace("[$InheritedType]", inherited);
            template.Replace("[$LibNamespace]", typeof(Token).Namespace);
            template.Replace("[$Name]", name);
            template.Replace("[$Code]", code.ToString());

            return(template.ToString());
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Generates code that matches a rule
        /// </summary>
        /// <param name="exp"></param>
        /// <returns></returns>
        private string GenerateRuleMatch(GrammarInfo grammar, string name)
        {
            var g = grammar.FindProductionGrammar(name);

            //return String.Format("Match(parser, new {0}.{1}())", ns, name);
            return String.Format(
                "Match(parser, new {0}.{1}())",
                g.Namespace,
                name);
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Generate a single rule class
        /// </summary>
        /// <param name="f"></param>
        /// <returns></returns>
        private string GenerateRuleClass(GrammarInfo grammar, string name)
        {
            int tabs = 3;
            var resstr = "res";

            var g = grammar.FindProductionGrammar(name, false);
            var exp = g.Rules[name].GetValue(null) as LambdaExpression;
            if (exp == null)
            {
                // **** TODO
                throw new ParserGeneratorException(String.Format("Rule '{0}' must be a lambda expression.", name));
            }

            CodeStringBuilder code = new CodeStringBuilder();

            code.AppendLine(tabs, "bool res = true;");
            code.AppendLine();

            code.Append(GenerateProductionMatch(tabs, grammar, exp.Body, resstr));

            code.AppendLine(tabs, "return res;");

            // Figure out whether this rule is inherited or not
            string inherited;
            g = grammar.FindProductionBaseGrammar(name);

            if (g == grammar)
            {
                inherited = String.Format("{0}.Node", typeof(Token).Namespace);
            }
            else
            {
                inherited = String.Format("{0}.{1}", g.Attributes.Namespace, name);
                //inheritedRules.Add(name);
            }

            var template = new StringBuilder(Templates.Rule);

            template.Replace("[$InheritedType]", inherited);
            template.Replace("[$LibNamespace]", typeof(Token).Namespace);
            template.Replace("[$Name]", name);
            template.Replace("[$Code]", code.ToString());

            return template.ToString();
        }