public ExceptionClause(OutputModelFactory factory, ActionAST catchArg, ActionAST catchAction) : base(factory, catchArg) { this.catchArg = new Action(factory, catchArg); this.catchAction = new Action(factory, catchAction); }
public virtual IDictionary<string, Action> BuildNamedActions(Grammar g) { IDictionary<string, Action> namedActions = new Dictionary<string, Action>(); foreach (string name in g.namedActions.Keys) { ActionAST ast = g.namedActions[name]; namedActions[name] = new Action(factory, ast); } return namedActions; }
public ListenerFile(OutputModelFactory factory, string fileName) : base(factory, fileName) { Grammar g = factory.GetGrammar(); parserName = g.GetRecognizerName(); grammarName = g.name; namedActions = BuildNamedActions(factory.GetGrammar()); foreach (KeyValuePair<string, IList<RuleAST>> entry in g.contextASTs) { foreach (RuleAST ruleAST in entry.Value) { try { IDictionary<string, IList<System.Tuple<int, AltAST>>> labeledAlternatives = g.GetLabeledAlternatives(ruleAST); listenerNames.UnionWith(labeledAlternatives.Keys); } catch (RecognitionException) { } } } foreach (Rule r in g.rules.Values) { listenerNames.Add(r.GetBaseContext()); } foreach (Rule r in g.rules.Values) { IDictionary<string, IList<System.Tuple<int, AltAST>>> labels = r.GetAltLabels(); if (labels != null) { foreach (KeyValuePair<string, IList<System.Tuple<int, AltAST>>> pair in labels) { listenerLabelRuleNames[pair.Key] = r.name; } } } ActionAST ast; if (g.namedActions.TryGetValue("header", out ast) && ast != null) header = new Action(factory, ast); genPackage = factory.GetGrammar().tool.genPackage; exportMacro = factory.GetGrammar().GetOptionString("exportMacro"); }
public override IList<SrcOp> RulePostamble(RuleFunction function, Rule r) { if (r.namedActions.ContainsKey("after") || r.namedActions.ContainsKey("finally")) { // See OutputModelController.buildLeftRecursiveRuleFunction // and Parser.exitRule for other places which set stop. CodeGenerator gen = GetGenerator(); TemplateGroup codegenTemplates = gen.GetTemplates(); Template setStopTokenAST = codegenTemplates.GetInstanceOf("recRuleSetStopToken"); Action setStopTokenAction = new Action(this, function.ruleCtx, setStopTokenAST); IList<SrcOp> ops = new List<SrcOp>(1); ops.Add(setStopTokenAction); return ops; } return base.RulePostamble(function, r); }
public virtual void FillNamedActions(OutputModelFactory factory, Rule r) { if (r.finallyAction != null) { finallyAction = new Action(factory, r.finallyAction); } namedActions = new Dictionary<string, Action>(); foreach (string name in r.namedActions.Keys) { ActionAST ast; r.namedActions.TryGetValue(name, out ast); namedActions[name] = new Action(factory, ast); } }
public virtual void BuildLeftRecursiveRuleFunction(LeftRecursiveRule r, LeftRecursiveRuleFunction function) { BuildNormalRuleFunction(r, function); // now inject code to start alts AbstractTarget target = @delegate.GetTarget(); TemplateGroup codegenTemplates = target.GetTemplates(); // pick out alt(s) for primaries CodeBlockForOuterMostAlt outerAlt = (CodeBlockForOuterMostAlt)function.code[0]; IList<CodeBlockForAlt> primaryAltsCode = new List<CodeBlockForAlt>(); SrcOp primaryStuff = outerAlt.ops[0]; if (primaryStuff is Choice) { Choice primaryAltBlock = (Choice)primaryStuff; foreach (var alt in primaryAltBlock.alts) primaryAltsCode.Add(alt); } else { // just a single alt I guess; no block primaryAltsCode.Add((CodeBlockForAlt)primaryStuff); } // pick out alt(s) for op alts StarBlock opAltStarBlock = (StarBlock)outerAlt.ops[1]; CodeBlockForAlt altForOpAltBlock = opAltStarBlock.alts[0]; IList<CodeBlockForAlt> opAltsCode = new List<CodeBlockForAlt>(); SrcOp opStuff = altForOpAltBlock.ops[0]; if (opStuff is AltBlock) { AltBlock opAltBlock = (AltBlock)opStuff; foreach (var alt in opAltBlock.alts) opAltsCode.Add(alt); } else { // just a single alt I guess; no block opAltsCode.Add((CodeBlockForAlt)opStuff); } // Insert code in front of each primary alt to create specialized context if there was a label for (int i = 0; i < primaryAltsCode.Count; i++) { LeftRecursiveRuleAltInfo altInfo = r.recPrimaryAlts[i]; if (altInfo.altLabel == null) continue; Template altActionST = codegenTemplates.GetInstanceOf("recRuleReplaceContext"); altActionST.Add("ctxName", Utils.Capitalize(altInfo.altLabel)); AltLabelStructDecl ctx = null; if (altInfo.altLabel != null) function.altLabelCtxs.TryGetValue(altInfo.altLabel, out ctx); Action altAction = new Action(@delegate, ctx, altActionST); CodeBlockForAlt alt = primaryAltsCode[i]; alt.InsertOp(0, altAction); } // Insert code to set ctx.stop after primary block and before op * loop Template setStopTokenAST = codegenTemplates.GetInstanceOf("recRuleSetStopToken"); Action setStopTokenAction = new Action(@delegate, function.ruleCtx, setStopTokenAST); outerAlt.InsertOp(1, setStopTokenAction); // Insert code to set _prevctx at start of * loop Template setPrevCtx = codegenTemplates.GetInstanceOf("recRuleSetPrevCtx"); Action setPrevCtxAction = new Action(@delegate, function.ruleCtx, setPrevCtx); opAltStarBlock.AddIterationOp(setPrevCtxAction); // Insert code in front of each op alt to create specialized context if there was an alt label for (int i = 0; i < opAltsCode.Count; i++) { Template altActionST; LeftRecursiveRuleAltInfo altInfo = r.recOpAlts.GetElement(i); string templateName; if (altInfo.altLabel != null) { templateName = "recRuleLabeledAltStartAction"; altActionST = codegenTemplates.GetInstanceOf(templateName); altActionST.Add("currentAltLabel", altInfo.altLabel); } else { templateName = "recRuleAltStartAction"; altActionST = codegenTemplates.GetInstanceOf(templateName); altActionST.Add("ctxName", Utils.Capitalize(r.name)); } altActionST.Add("ruleName", r.name); // add label of any LR ref we deleted altActionST.Add("label", altInfo.leftRecursiveRuleRefLabel); if (altActionST.impl.FormalArguments.Any(x => x.Name == "isListLabel")) { altActionST.Add("isListLabel", altInfo.isListLabel); } else if (altInfo.isListLabel) { @delegate.GetGenerator().tool.errMgr.ToolError(ErrorType.CODE_TEMPLATE_ARG_ISSUE, templateName, "isListLabel"); } AltLabelStructDecl ctx = null; if (altInfo.altLabel != null) function.altLabelCtxs.TryGetValue(altInfo.altLabel, out ctx); Action altAction = new Action(@delegate, ctx, altActionST); CodeBlockForAlt alt = opAltsCode[i]; alt.InsertOp(0, altAction); } }