private void GenerateSingleSubRule(Rule R, string ReturnType) { string fName = GetFName(R.Name); string body = t(2) + "private " + ReturnType + " " + fName + "()\r\n"; body += t(2) + "{\r\n"; body += t(3) + "//" + R.Name + " => " + R.RuleGrammer + "\r\n"; body += t(3) + "int ti = TokenIndex;\r\n"; body += t(3) + "object[] matches = new object[" + R.RuleParts.Length + "];\r\n"; body += t(3) + "if("; bool first = true; int matchIndex = 0; foreach (string part in R.RuleParts) { if (!first) { body += " && "; } first = false; if (RuleMap.ContainsKey(part)) { body += "(matches[" + matchIndex++ + "] = " + GetFName(part) + "()) != null"; RuleReferenceCheck[part].Referenced = true; } else { string tokenVal = part; tokenVal = tokenVal.Replace("\"", "\\\""); body += "(matches[" + matchIndex++ + "] = TokenMatches(\"" + tokenVal + "\")) != null"; } } body += ")\r\n"; body += t(3) + "{\r\n"; //body += t(4) + "return GetResult(x => " + R.Action + ", matches);\r\n"; body += t(4) + "Func<object[], " + ReturnType + "> f = x => " + R.Action + ";\r\n"; body += t(4) + "return f(matches);\r\n"; body += t(3) + "}\r\n"; body += t(3) + "TokenIndex = ti;\r\n"; body += t(3) + "return null;\r\n"; body += t(2) + "}\r\n"; RuleFunction rf = new RuleFunction(fName, body, R.Name); Functions.Add(rf); }
private void GenerateRule(List <Rule> SubRules, string ReturnType) { string fName = GetFName(SubRules[0].Name); string body = t(2) + "private " + ReturnType + " " + fName + "(int RuleIndex=0)\r\n"; body += t(2) + "{\r\n"; body += t(3) + ReturnType + " matches = null;\r\n"; for (int i = 0; i < SubRules.Count; ++i) { Rule r = SubRules[i]; int ruleIndex = i + 1; string subName = fName + "_" + ruleIndex; body += t(3) + "//" + r.Name + " => " + r.RuleGrammer + "\r\n"; if (r.RuleParts.Length == 1 && RuleMap.ContainsKey(r.RuleParts[0])) { body += t(3) + "if(RuleIndex != " + ruleIndex + " && (matches = " + GetFName(r.RuleParts[0]) + "()) != null)\r\n"; body += t(3) + "{\r\n"; body += t(4) + "Func<object[], " + ReturnType + "> f = x => " + r.Action + ";\r\n"; body += t(4) + "return f(new object[]{ matches });\r\n"; RuleReferenceCheck[r.RuleParts[0]].Referenced = true; } else { string lhCode = GenerateLHCode(r); body += t(3) + "if(RuleIndex != " + ruleIndex + (lhCode != null ? " " + lhCode : "") + " && (matches = " + subName + "()) != null)\r\n"; body += t(3) + "{\r\n"; body += t(4) + "return matches;\r\n"; GenerateSubRule(subName, ruleIndex, r, ReturnType, SubRules[0].Name); } body += t(3) + "}\r\n"; } body += t(3) + "return null;\r\n"; body += t(2) + "}\r\n"; RuleFunction func = new RuleFunction(fName, body, SubRules[0].Name); Functions.Add(func); }