public virtual string GetVisitorFileName(bool header) { AbstractTarget target = GetTarget(); if (target == null) { throw new NotSupportedException("Cannot generate code without a target."); } return(target.GetVisitorFileName(header)); }
public virtual void WriteBaseVisitor(Template outputFileST, bool header) { AbstractTarget target = GetTarget(); if (target == null) { throw new NotSupportedException("Cannot generate code without a target."); } target.GenFile(g, outputFileST, GetBaseVisitorFileName(header)); }
private Template Walk(OutputModelObject outputModel, bool header) { AbstractTarget target = GetTarget(); if (target == null) { throw new NotSupportedException("Cannot generate code without a target."); } OutputModelWalker walker = new OutputModelWalker(tool, target.GetTemplates()); return(walker.Walk(outputModel, header)); }
public virtual string GetHeaderFileName() { AbstractTarget target = GetTarget(); if (target == null) { throw new NotSupportedException("Cannot generate code without a target."); } Template extST = target.GetTemplates().GetInstanceOf("headerFileExtension"); if (extST == null) { return(null); } string recognizerName = g.GetRecognizerName(); return(recognizerName + extST.Render()); }
public virtual void WriteVocabFile() { AbstractTarget target = GetTarget(); if (target == null) { throw new NotSupportedException("Cannot generate code without a target."); } // write out the vocab interchange file; used by ANTLR, // does not change per target Template tokenVocabSerialization = GetTokenVocabOutput(); string fileName = GetVocabFileName(); if (fileName != null) { target.GenFile(g, tokenVocabSerialization, fileName); } }
public ErrorListener(AbstractTarget target) { this.target = target; }
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); } }
protected virtual void LoadLanguageTarget(string language) { string targetName = "Antlr4.Codegen.Target." + language + "Target"; try { Type c = Type.GetType(targetName, true); target = (AbstractTarget)Activator.CreateInstance(c, this); } catch (TargetInvocationException e) { tool.errMgr.ToolError(ErrorType.CANNOT_CREATE_TARGET_GENERATOR, e, targetName); } catch (TypeLoadException e) { tool.errMgr.ToolError(ErrorType.CANNOT_CREATE_TARGET_GENERATOR, e, targetName); } catch (ArgumentException e) { tool.errMgr.ToolError(ErrorType.CANNOT_CREATE_TARGET_GENERATOR, e, targetName); } catch (InvalidCastException e) { tool.errMgr.ToolError(ErrorType.CANNOT_CREATE_TARGET_GENERATOR, e, targetName); } }
public virtual void Process() { CodeGenerator gen = new CodeGenerator(g); AbstractTarget target = gen.GetTarget(); if (target == null) { return; } IntervalSet idTypes = new IntervalSet(); idTypes.Add(ANTLRParser.ID); idTypes.Add(ANTLRParser.RULE_REF); idTypes.Add(ANTLRParser.TOKEN_REF); IList <GrammarAST> idNodes = g.ast.GetNodesWithType(idTypes); foreach (GrammarAST idNode in idNodes) { if (target.GrammarSymbolCausesIssueInGeneratedCode(idNode)) { g.tool.errMgr.GrammarError(ErrorType.USE_OF_BAD_WORD, g.fileName, idNode.Token, idNode.Text); } } // all templates are generated in memory to report the most complete // error information possible, but actually writing output files stops // after the first error is reported int errorCount = g.tool.errMgr.GetNumErrors(); if (g.IsLexer()) { if (target.NeedsHeader()) { Template lexerHeader = gen.GenerateLexer(true); // Header file if needed. if (g.tool.errMgr.GetNumErrors() == errorCount) { WriteRecognizer(lexerHeader, gen, true); } } Template lexer = gen.GenerateLexer(false); if (g.tool.errMgr.GetNumErrors() == errorCount) { WriteRecognizer(lexer, gen, false); } } else { if (target.NeedsHeader()) { Template parserHeader = gen.GenerateParser(true); if (g.tool.errMgr.GetNumErrors() == errorCount) { WriteRecognizer(parserHeader, gen, true); } } Template parser = gen.GenerateParser(false); if (g.tool.errMgr.GetNumErrors() == errorCount) { WriteRecognizer(parser, gen, false); } if (g.tool.gen_listener) { if (target.NeedsHeader()) { Template listenerHeader = gen.GenerateListener(true); if (g.tool.errMgr.GetNumErrors() == errorCount) { gen.WriteListener(listenerHeader, true); } } Template listener = gen.GenerateListener(false); if (g.tool.errMgr.GetNumErrors() == errorCount) { gen.WriteListener(listener, false); } if (target.NeedsHeader()) { Template baseListener = gen.GenerateBaseListener(true); if (g.tool.errMgr.GetNumErrors() == errorCount) { gen.WriteBaseListener(baseListener, true); } } if (target.WantsBaseListener()) { Template baseListener = gen.GenerateBaseListener(false); if (g.tool.errMgr.GetNumErrors() == errorCount) { gen.WriteBaseListener(baseListener, false); } } } if (g.tool.gen_visitor) { if (target.NeedsHeader()) { Template visitorHeader = gen.GenerateVisitor(true); if (g.tool.errMgr.GetNumErrors() == errorCount) { gen.WriteVisitor(visitorHeader, true); } } Template visitor = gen.GenerateVisitor(false); if (g.tool.errMgr.GetNumErrors() == errorCount) { gen.WriteVisitor(visitor, false); } if (target.NeedsHeader()) { Template baseVisitor = gen.GenerateBaseVisitor(true); if (g.tool.errMgr.GetNumErrors() == errorCount) { gen.WriteBaseVisitor(baseVisitor, true); } } if (target.WantsBaseVisitor()) { Template baseVisitor = gen.GenerateBaseVisitor(false); if (g.tool.errMgr.GetNumErrors() == errorCount) { gen.WriteBaseVisitor(baseVisitor, false); } } } } gen.WriteVocabFile(); }