/// <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)); }
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(); } }
/// <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()); }
/// <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); }
/// <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(); }