public void EmitRewriting(SourceBuilder source, SequenceGenerator seqGen, String matchListName, String enumeratorName, bool fireDebugEvents) { source.AppendFrontFormat("case \"{0}\":\n", plainRuleName); source.AppendFront("{\n"); source.Indent(); source.AppendFront(matchType + " " + matchName + " = (" + matchType + ")" + enumeratorName + ".Current;\n"); String returnParameterDeclarations; String returnArguments; String returnAssignments; String returnParameterDeclarationsAllCall; String intermediateReturnAssignmentsAllCall; String returnAssignmentsAllCall; seqHelper.BuildReturnParameters(seqRule, ReturnVars, out returnParameterDeclarations, out returnArguments, out returnAssignments, out returnParameterDeclarationsAllCall, out intermediateReturnAssignmentsAllCall, out returnAssignmentsAllCall); // start a transaction if (fireDebugEvents) { source.AppendFront("procEnv.Matched(" + matchesName + ", " + matchName + ", " + specialStr + ");\n"); } if (fireDebugEvents) { source.AppendFront("procEnv.Finishing(" + matchesName + ", " + specialStr + ");\n"); } if (returnParameterDeclarations.Length != 0) { source.AppendFront(returnParameterDeclarations + "\n"); } source.AppendFront(ruleName + ".Modify(procEnv, " + matchName + returnArguments + ");\n"); if (returnAssignments.Length != 0) { source.AppendFront(returnAssignments + "\n"); } source.AppendFront("++procEnv.PerformanceInfo.RewritesPerformed;\n"); if (fireDebugEvents) { source.AppendFront("procEnv.Finished(" + matchesName + ", " + specialStr + ");\n"); } // rule applied, now execute the sequence seqGen.EmitSequence(seqSeq, source); source.AppendFront(COMP_HELPER.SetResultVar(seqMulti, COMP_HELPER.GetResultVar(seqSeq))); source.AppendFront("break;\n"); source.Unindent(); source.AppendFront("}\n"); }
public void Emit(SourceBuilder source, SequenceGenerator seqGen, bool fireDebugEvents) { source.AppendFront(COMP_HELPER.SetResultVar(seqFor, "true")); String parameters = seqHelper.BuildParameters(seqRule, ArgumentExpressions, source); source.AppendFront(matchesType + " " + matchesName + " = " + ruleName + ".Match(procEnv, procEnv.MaxMatches" + parameters + ");\n"); source.AppendFront("procEnv.PerformanceInfo.MatchesFound += " + matchesName + ".Count;\n"); for (int i = 0; i < seqRule.Filters.Count; ++i) { seqExprGen.EmitFilterCall(source, (SequenceFilterCallCompiled)seqRule.Filters[i], patternName, matchesName, seqRule.PackagePrefixedName, false); } source.AppendFront("if(" + matchesName + ".Count != 0) {\n"); source.Indent(); source.AppendFront(matchesName + " = (" + matchesType + ")" + matchesName + ".Clone();\n"); if (fireDebugEvents) { source.AppendFront("procEnv.Finishing(" + matchesName + ", " + specialStr + ");\n"); } String returnParameterDeclarations; String returnArguments; String returnAssignments; String returnParameterDeclarationsAllCall; String intermediateReturnAssignmentsAllCall; String returnAssignmentsAllCall; seqHelper.BuildReturnParameters(seqRule, ReturnVars, out returnParameterDeclarations, out returnArguments, out returnAssignments, out returnParameterDeclarationsAllCall, out intermediateReturnAssignmentsAllCall, out returnAssignmentsAllCall); // apply the sequence for every match found String enumeratorName = "enum_" + seqFor.Id; source.AppendFront("IEnumerator<" + matchType + "> " + enumeratorName + " = " + matchesName + ".GetEnumeratorExact();\n"); source.AppendFront("while(" + enumeratorName + ".MoveNext())\n"); source.AppendFront("{\n"); source.Indent(); source.AppendFront(matchType + " " + matchName + " = " + enumeratorName + ".Current;\n"); source.AppendFront(seqHelper.SetVar(seqFor.Var, matchName)); seqGen.EmitSequence(seqFor.Seq, source); source.AppendFront(COMP_HELPER.SetResultVar(seqFor, COMP_HELPER.GetResultVar(seqFor) + " & " + COMP_HELPER.GetResultVar(seqFor.Seq))); source.Unindent(); source.AppendFront("}\n"); source.Unindent(); source.AppendFront("}\n"); }
public void Emit(SourceBuilder source, SequenceGenerator seqGen, bool fireDebugEvents) { source.AppendFront(COMP_HELPER.SetResultVar(seqFor, "true")); seqMatcherGen.EmitMatchingAndCloning(source, "procEnv.MaxMatches"); SequenceRuleCallMatcherGenerator.EmitPreMatchEventFiring(source, matchesName); seqMatcherGen.EmitFiltering(source); source.AppendFront("if(" + matchesName + ".Count != 0) {\n"); source.Indent(); if (fireDebugEvents) { source.AppendFront("procEnv.Finishing(" + matchesName + ", " + specialStr + ");\n"); } String returnParameterDeclarations; String returnArguments; String returnAssignments; String returnParameterDeclarationsAllCall; String intermediateReturnAssignmentsAllCall; String returnAssignmentsAllCall; seqHelper.BuildReturnParameters(seqRule, ReturnVars, out returnParameterDeclarations, out returnArguments, out returnAssignments, out returnParameterDeclarationsAllCall, out intermediateReturnAssignmentsAllCall, out returnAssignmentsAllCall); // apply the sequence for every match found String enumeratorName = "enum_" + seqFor.Id; source.AppendFront("IEnumerator<" + matchType + "> " + enumeratorName + " = " + matchesName + ".GetEnumeratorExact();\n"); source.AppendFront("while(" + enumeratorName + ".MoveNext())\n"); source.AppendFront("{\n"); source.Indent(); source.AppendFront(matchType + " " + matchName + " = " + enumeratorName + ".Current;\n"); source.AppendFront(seqHelper.SetVar(seqFor.Var, matchName)); seqGen.EmitSequence(seqFor.Seq, source); source.AppendFront(COMP_HELPER.SetResultVar(seqFor, COMP_HELPER.GetResultVar(seqFor) + " & " + COMP_HELPER.GetResultVar(seqFor.Seq))); source.Unindent(); source.AppendFront("}\n"); source.Unindent(); source.AppendFront("}\n"); }
public void EmitRewriting(SourceBuilder source, SequenceGenerator seqGen, String matchListName, String enumeratorName, String firstRewrite, bool fireDebugEvents) { source.AppendFrontFormat("case \"{0}\":\n", plainRuleName); source.AppendFront("{\n"); source.Indent(); source.AppendFront(matchType + " " + matchName + " = (" + matchType + ")" + enumeratorName + ".Current;\n"); if (fireDebugEvents) { source.AppendFront("procEnv.Matched(" + matchesName + ", null, " + specialStr + ");\n"); } if (fireDebugEvents) { source.AppendFront("procEnv.Finishing(" + matchesName + ", " + specialStr + ");\n"); } source.AppendFront("if(!" + firstRewrite + ") procEnv.RewritingNextMatch();\n"); if (returnParameterDeclarations.Length != 0) { source.AppendFront(returnParameterDeclarations + "\n"); } source.AppendFront(ruleName + ".Modify(procEnv, " + matchName + returnArguments + ");\n"); if (returnAssignments.Length != 0) { source.AppendFront(returnAssignments + "\n"); } source.AppendFront("++procEnv.PerformanceInfo.RewritesPerformed;\n"); source.AppendFront(firstRewrite + " = false;\n"); if (fireDebugEvents) { source.AppendFront("procEnv.Finished(" + matchesName + ", " + specialStr + ");\n"); } seqGen.EmitSequence(seqRulePrefixedSequence.Sequence, source); source.AppendFront("break;\n"); source.Unindent(); source.AppendFront("}\n"); }
public void Emit(SourceBuilder source, SequenceGenerator seqGen, bool fireDebugEvents) { String parameters = seqHelper.BuildParameters(seqRule, ArgumentExpressions, source); seqMatcherGen.EmitMatching(source, parameters, "procEnv.MaxMatches"); SequenceRuleCallMatcherGenerator.EmitPreMatchEventFiring(source, matchesName); seqMatcherGen.EmitFiltering(source); seqMatcherGen.EmitCloning(source); source.AppendFront("if(" + matchesName + ".Count == 0) {\n"); source.Indent(); source.AppendFront(COMP_HELPER.SetResultVar(seq, "false")); source.Unindent(); source.AppendFront("} else {\n"); source.Indent(); source.AppendFront(COMP_HELPER.SetResultVar(seq, "false")); if (fireDebugEvents) { source.AppendFront("procEnv.Finishing(" + matchesName + ", " + specialStr + ");\n"); } String returnParameterDeclarations; String returnArguments; String returnAssignments; String returnParameterDeclarationsAllCall; String intermediateReturnAssignmentsAllCall; String returnAssignmentsAllCall; seqHelper.BuildReturnParameters(seqRule, ReturnVars, out returnParameterDeclarations, out returnArguments, out returnAssignments, out returnParameterDeclarationsAllCall, out intermediateReturnAssignmentsAllCall, out returnAssignmentsAllCall); // apply the rule and the following sequence for every match found String enumeratorName = "enum_" + seq.Id; source.AppendFront("IEnumerator<" + matchType + "> " + enumeratorName + " = " + matchesName + ".GetEnumeratorExact();\n"); source.AppendFront("while(" + enumeratorName + ".MoveNext())\n"); source.AppendFront("{\n"); source.Indent(); source.AppendFront(matchType + " " + matchName + " = " + enumeratorName + ".Current;\n"); source.AppendFront("procEnv.Matched(" + matchesName + ", " + matchName + ", " + specialStr + ");\n"); if (returnParameterDeclarations.Length != 0) { source.AppendFront(returnParameterDeclarations + "\n"); } source.AppendFront(ruleName + ".Modify(procEnv, " + matchName + returnArguments + ");\n"); if (returnAssignments.Length != 0) { source.AppendFront(returnAssignments + "\n"); } source.AppendFront("++procEnv.PerformanceInfo.RewritesPerformed;\n"); if (fireDebugEvents) { source.AppendFront("procEnv.Finished(" + matchesName + ", " + specialStr + ");\n"); } // rule applied, now execute the sequence seqGen.EmitSequence(seq.Sequence, source); source.AppendFront(COMP_HELPER.SetResultVar(seq, COMP_HELPER.GetResultVar(seq) + "|" + COMP_HELPER.GetResultVar(seq.Sequence))); source.Unindent(); source.AppendFront("}\n"); source.Unindent(); source.AppendFront("}\n"); }
public void Emit(SourceBuilder source, SequenceGenerator seqGen, bool fireDebugEvents) { String parameters = seqHelper.BuildParameters(seqRule, ArgumentExpressions, source); seqMatcherGen.EmitMatching(source, parameters, "procEnv.MaxMatches"); SequenceRuleCallMatcherGenerator.EmitPreMatchEventFiring(source, matchesName); seqMatcherGen.EmitFiltering(source); seqMatcherGen.EmitCloning(source); source.AppendFront("if(" + matchesName + ".Count == 0) {\n"); source.Indent(); source.AppendFront(COMP_HELPER.SetResultVar(seq, "false")); source.Unindent(); source.AppendFront("} else {\n"); source.Indent(); source.AppendFront(COMP_HELPER.SetResultVar(seq, "true")); // shut up compiler if (fireDebugEvents) { source.AppendFront("procEnv.Finishing(" + matchesName + ", " + specialStr + ");\n"); } String returnParameterDeclarations; String returnArguments; String returnAssignments; String returnParameterDeclarationsAllCall; String intermediateReturnAssignmentsAllCall; String returnAssignmentsAllCall; seqHelper.BuildReturnParameters(seqRule, ReturnVars, out returnParameterDeclarations, out returnArguments, out returnAssignments, out returnParameterDeclarationsAllCall, out intermediateReturnAssignmentsAllCall, out returnAssignmentsAllCall); // apply the rule and the following sequence for every match found, // until the first rule and sequence execution succeeded // rolling back the changes of failing executions until then String enumeratorName = "enum_" + seq.Id; String matchesTriedName = "matchesTried_" + seq.Id; source.AppendFront("int " + matchesTriedName + " = 0;\n"); source.AppendFront("IEnumerator<" + matchType + "> " + enumeratorName + " = " + matchesName + ".GetEnumeratorExact();\n"); source.AppendFront("while(" + enumeratorName + ".MoveNext())\n"); source.AppendFront("{\n"); source.Indent(); source.AppendFront(matchType + " " + matchName + " = " + enumeratorName + ".Current;\n"); source.AppendFront("++" + matchesTriedName + ";\n"); // start a transaction String transactionIdName = "transID_" + seq.Id; source.AppendFront("int " + transactionIdName + " = procEnv.TransactionManager.Start();\n"); String oldRewritesPerformedName = "oldRewritesPerformed_" + seq.Id; source.AppendFront("int " + oldRewritesPerformedName + " = procEnv.PerformanceInfo.RewritesPerformed;\n"); if (fireDebugEvents) { source.AppendFront("procEnv.Matched(" + matchesName + ", " + matchName + ", " + specialStr + ");\n"); } if (returnParameterDeclarations.Length != 0) { source.AppendFront(returnParameterDeclarations + "\n"); } source.AppendFront(ruleName + ".Modify(procEnv, " + matchName + returnArguments + ");\n"); if (returnAssignments.Length != 0) { source.AppendFront(returnAssignments + "\n"); } source.AppendFront("++procEnv.PerformanceInfo.RewritesPerformed;\n"); if (fireDebugEvents) { source.AppendFront("procEnv.Finished(" + matchesName + ", " + specialStr + ");\n"); } // rule applied, now execute the sequence seqGen.EmitSequence(seq.Seq, source); // if sequence execution failed, roll the changes back and try the next match of the rule source.AppendFront("if(!" + COMP_HELPER.GetResultVar(seq.Seq) + ") {\n"); source.Indent(); source.AppendFront("procEnv.TransactionManager.Rollback(" + transactionIdName + ");\n"); source.AppendFront("procEnv.PerformanceInfo.RewritesPerformed = " + oldRewritesPerformedName + ";\n"); source.AppendFront("if(" + matchesTriedName + " < " + matchesName + ".Count) {\n"); // further match available -> try it source.Indent(); source.AppendFront("continue;\n"); source.Unindent(); source.AppendFront("} else {\n"); // all matches tried, all failed later on -> end in fail source.Indent(); source.AppendFront(COMP_HELPER.SetResultVar(seq, "false")); source.AppendFront("break;\n"); source.Unindent(); source.AppendFront("}\n"); source.Unindent(); source.AppendFront("}\n"); // if sequence execution succeeded, commit the changes so far and succeed source.AppendFront("procEnv.TransactionManager.Commit(" + transactionIdName + ");\n"); source.AppendFront(COMP_HELPER.SetResultVar(seq, "true")); source.AppendFront("break;\n"); source.Unindent(); source.AppendFront("}\n"); source.Unindent(); source.AppendFront("}\n"); }
public void Emit(SourceBuilder source, SequenceGenerator seqGen, bool fireDebugEvents) { String matchListName = "MatchList_" + seqMulti.Id; source.AppendFrontFormat("List<GRGEN_LIBGR.IMatch> {0} = new List<GRGEN_LIBGR.IMatch>();\n", matchListName); // emit code for matching all the contained rules SequenceRuleCallMatcherGenerator[] ruleMatcherGenerators = new SequenceRuleCallMatcherGenerator[seqMulti.Rules.Sequences.Count]; for (int i = 0; i < seqMulti.Rules.Sequences.Count; ++i) { ruleMatcherGenerators[i] = new SequenceRuleCallMatcherGenerator((SequenceRuleCall)seqMulti.Rules.Sequences[i], seqExprGen, seqHelper); ruleMatcherGenerators[i].EmitMatchingAndCloning(source, "procEnv.MaxMatches"); } SequenceRuleCallMatcherGenerator.EmitPreMatchEventFiring(source, ruleMatcherGenerators); // emit code for rule-based filtering for (int i = 0; i < seqMulti.Rules.Sequences.Count; ++i) { ruleMatcherGenerators[i].EmitFiltering(source); ruleMatcherGenerators[i].EmitAddRange(source, matchListName); } // emit code for match class (non-rule-based) filtering foreach (SequenceFilterCallBase sequenceFilterCall in seqMulti.Rules.Filters) { seqExprGen.EmitMatchClassFilterCall(source, sequenceFilterCall, matchListName, false); } source.AppendFront("if(" + matchListName + ".Count == 0) {\n"); source.Indent(); source.AppendFront(COMP_HELPER.SetResultVar(seqMulti, "false")); source.Unindent(); source.AppendFront("} else {\n"); source.Indent(); source.AppendFront(COMP_HELPER.SetResultVar(seqMulti, "true")); // shut up compiler // apply the rules and the following sequence for every match found, // until the first rule and sequence execution succeeded // rolling back the changes of failing executions until then String enumeratorName = "enum_" + seqMulti.Id; String matchesTriedName = "matchesTried_" + seqMulti.Id; source.AppendFront("int " + matchesTriedName + " = 0;\n"); source.AppendFront("IEnumerator<GRGEN_LIBGR.IMatch> " + enumeratorName + " = " + matchListName + ".GetEnumerator();\n"); source.AppendFront("while(" + enumeratorName + ".MoveNext())\n"); source.AppendFront("{\n"); source.Indent(); source.AppendFront("++" + matchesTriedName + ";\n"); String transactionIdName = "transID_" + seqMulti.Id; source.AppendFront("int " + transactionIdName + " = procEnv.TransactionManager.Start();\n"); String oldRewritesPerformedName = "oldRewritesPerformed_" + seqMulti.Id; source.AppendFront("int " + oldRewritesPerformedName + " = procEnv.PerformanceInfo.RewritesPerformed;\n"); source.AppendFront("switch(" + enumeratorName + ".Current.Pattern.PackagePrefixedName)\n"); source.AppendFront("{\n"); source.Indent(); // emit code for rewriting the current match (for each rule, rule fitting to the match is selected by rule name) for (int i = 0; i < seqMulti.Rules.Sequences.Count; ++i) { SequenceMultiBacktrackRuleRewritingGenerator ruleRewritingGenerator = new SequenceMultiBacktrackRuleRewritingGenerator( seqMulti, (SequenceRuleCall)seqMulti.Rules.Sequences[i], seqExprGen, seqHelper); ruleRewritingGenerator.EmitRewriting(source, seqGen, matchListName, enumeratorName, fireDebugEvents); } source.AppendFrontFormat("default: throw new Exception(\"Unknown pattern \" + {0}.Current.Pattern.PackagePrefixedName + \" in match!\");", enumeratorName); source.Unindent(); source.AppendFront("}\n"); // rule applied, now execute the sequence seqGen.EmitSequence(seqMulti.Seq, source); // if sequence execution failed, roll the changes back and try the next match source.AppendFront("if(!" + COMP_HELPER.GetResultVar(seqMulti.Seq) + ") {\n"); source.Indent(); source.AppendFront("procEnv.TransactionManager.Rollback(" + transactionIdName + ");\n"); source.AppendFront("procEnv.PerformanceInfo.RewritesPerformed = " + oldRewritesPerformedName + ";\n"); source.AppendFront("if(" + matchesTriedName + " < " + matchListName + ".Count) {\n"); // further match available -> try it source.Indent(); source.AppendFront("continue;\n"); source.Unindent(); source.AppendFront("} else {\n"); // all matches tried, all failed later on -> end in fail source.Indent(); source.AppendFront(COMP_HELPER.SetResultVar(seqMulti, "false")); source.AppendFront("break;\n"); source.Unindent(); source.AppendFront("}\n"); source.Unindent(); source.AppendFront("}\n"); // if sequence execution succeeded, commit the changes so far and succeed source.AppendFront("procEnv.TransactionManager.Commit(" + transactionIdName + ");\n"); source.AppendFront(COMP_HELPER.SetResultVar(seqMulti, "true")); source.AppendFront("break;\n"); source.Unindent(); source.AppendFront("}\n"); source.Unindent(); source.AppendFront("}\n"); }
public bool GenerateXGRSCode(string xgrsName, String package, String xgrsStr, String[] paramNames, GrGenType[] paramTypes, String[] defToBeYieldedToNames, GrGenType[] defToBeYieldedToTypes, SourceBuilder source, int lineNr) { Dictionary <String, String> varDecls = new Dictionary <String, String>(); for (int i = 0; i < paramNames.Length; ++i) { varDecls.Add(paramNames[i], TypesHelper.DotNetTypeToXgrsType(paramTypes[i])); } for (int i = 0; i < defToBeYieldedToNames.Length; ++i) { varDecls.Add(defToBeYieldedToNames[i], TypesHelper.DotNetTypeToXgrsType(defToBeYieldedToTypes[i])); } Sequence seq; try { SequenceParserEnvironmentCompiled parserEnv = new SequenceParserEnvironmentCompiled(package, actionNames, model); List <string> warnings = new List <string>(); seq = SequenceParser.ParseSequence(xgrsStr, parserEnv, varDecls, warnings); foreach (string warning in warnings) { Console.Error.WriteLine("The exec statement \"" + xgrsStr + "\" given on line " + lineNr + " reported back:\n" + warning); } seq.Check(env); seq.SetNeedForProfilingRecursive(emitProfiling); } catch (ParseException ex) { Console.Error.WriteLine("The exec statement \"" + xgrsStr + "\" given on line " + lineNr + " caused the following error:\n" + ex.Message); return(false); } catch (SequenceParserException ex) { Console.Error.WriteLine("The exec statement \"" + xgrsStr + "\" given on line " + lineNr + " caused the following error:\n"); HandleSequenceParserException(ex); return(false); } source.Append("\n"); source.AppendFront("public static bool ApplyXGRS_" + xgrsName + "(GRGEN_LGSP.LGSPGraphProcessingEnvironment procEnv"); for (int i = 0; i < paramNames.Length; ++i) { source.Append(", " + TypesHelper.XgrsTypeToCSharpType(TypesHelper.DotNetTypeToXgrsType(paramTypes[i]), model) + " var_"); source.Append(paramNames[i]); } for (int i = 0; i < defToBeYieldedToTypes.Length; ++i) { source.Append(", ref " + TypesHelper.XgrsTypeToCSharpType(TypesHelper.DotNetTypeToXgrsType(defToBeYieldedToTypes[i]), model) + " var_"); source.Append(defToBeYieldedToNames[i]); } source.Append(")\n"); source.AppendFront("{\n"); source.Indent(); source.AppendFront("GRGEN_LGSP.LGSPGraph graph = procEnv.graph;\n"); source.AppendFront("GRGEN_LGSP.LGSPActions actions = procEnv.curActions;\n"); neededEntitiesEmitter.Reset(); if (fireDebugEvents) { source.AppendFrontFormat("procEnv.DebugEntering(\"{0}\", \"{1}\");\n", InjectExec(xgrsName), xgrsStr.Replace("\\", "\\\\").Replace("\"", "\\\"")); } neededEntitiesEmitter.EmitNeededVarAndRuleEntities(seq, source); seqGen.EmitSequence(seq, source); if (fireDebugEvents) { source.AppendFrontFormat("procEnv.DebugExiting(\"{0}\");\n", InjectExec(xgrsName)); } source.AppendFront("return " + seqGen.GetSequenceResult(seq) + ";\n"); source.Unindent(); source.AppendFront("}\n"); List <SequenceExpressionContainerConstructor> containerConstructors = new List <SequenceExpressionContainerConstructor>(); Dictionary <SequenceVariable, SetValueType> variables = new Dictionary <SequenceVariable, SetValueType>(); seq.GetLocalVariables(variables, containerConstructors, null); foreach (SequenceExpressionContainerConstructor cc in containerConstructors) { SequenceContainerConstructorEmitter.GenerateContainerConstructor(model, cc, source); } return(true); }