コード例 #1
0
 public BuildDependencyGenerator(AntlrTool tool, Grammar g)
 {
     this.tool = tool;
     this.g = g;
     string language = g.GetOptionString("language");
     generator = new CodeGenerator(tool, g, language);
 }
コード例 #2
0
ファイル: LexerATNFactory.cs プロジェクト: sharwell/antlr4cs
 public LexerATNFactory(LexerGrammar g)
     : base(g)
 {
     // use codegen to get correct language templates for lexer commands
     string language = g.GetOptionString("language");
     CodeGenerator gen = new CodeGenerator(g.tool, null, language);
     AbstractTarget target = gen.GetTarget();
     codegenTemplates = target != null ? target.GetTemplates() : null;
 }
コード例 #3
0
        protected DefaultOutputModelFactory([NotNull] CodeGenerator gen)
        {
            this.gen = gen;
            this.g = gen.g;

            if (gen.GetTarget() == null)
            {
                throw new NotSupportedException("Cannot build an output model without a target.");
            }
        }
コード例 #4
0
ファイル: Target.cs プロジェクト: sharwell/antlr4cs
 protected AbstractTarget(CodeGenerator gen, string language)
 {
     targetCharValueEscape['\n'] = "\\n";
     targetCharValueEscape['\r'] = "\\r";
     targetCharValueEscape['\t'] = "\\t";
     targetCharValueEscape['\b'] = "\\b";
     targetCharValueEscape['\f'] = "\\f";
     targetCharValueEscape['\\'] = "\\\\";
     targetCharValueEscape['\''] = "\\'";
     targetCharValueEscape['"'] = "\\\"";
     this.gen = gen;
     this.language = language;
 }
コード例 #5
0
ファイル: Target.cs プロジェクト: sharwell/antlr4cs
 /**
  * Convert from an ANTLR string literal found in a grammar file to an
  * equivalent string literal in the target language.
  */
 public abstract string GetTargetStringLiteralFromANTLRStringLiteral(
     CodeGenerator generator,
     string literal, bool addQuotes);
コード例 #6
0
ファイル: ActionTranslator.cs プロジェクト: sharwell/antlr4cs
 public ActionTranslator(OutputModelFactory factory, ActionAST node)
 {
     this.factory = factory;
     this.node = node;
     this.gen = factory.GetGenerator();
 }
コード例 #7
0
        public virtual void LoadPrecRuleTemplates()
        {
            string templateGroupFile = Path.Combine("Tool", "Templates", "LeftRecursiveRules.stg");
            recRuleTemplates = new TemplateGroupFile(
                Path.Combine(
                    Path.GetDirectoryName(typeof(AntlrTool).GetTypeInfo().Assembly.Location),
                    templateGroupFile),
                Encoding.UTF8);
            if (!recRuleTemplates.IsDefined("recRule"))
            {
                tool.errMgr.ToolError(ErrorType.MISSING_CODE_GEN_TEMPLATES, "LeftRecursiveRules");
            }

            // use codegen to get correct language templates; that's it though
            CodeGenerator gen = new CodeGenerator(tool, null, language);
            TemplateGroup templates = gen.GetTemplates();
            if (templates == null)
            {
                // this class will still operate using Java templates
                templates = new CodeGenerator(tool, null, "Java").GetTemplates();
                Debug.Assert(templates != null);
            }

            codegenTemplates = templates;
        }
コード例 #8
0
ファイル: CodeGenPipeline.cs プロジェクト: sharwell/antlr4cs
        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();
        }
コード例 #9
0
ファイル: CodeGenPipeline.cs プロジェクト: sharwell/antlr4cs
        protected virtual void WriteRecognizer(Template template, CodeGenerator gen, bool header)
        {
#if false
            if (g.tool.launch_ST_inspector)
            {
                STViz viz = template.inspect();
                if (g.tool.ST_inspector_wait_for_close)
                {
                    try
                    {
                        viz.waitForClose();
                    }
                    catch (InterruptedException ex)
                    {
                        g.tool.errMgr.toolError(ErrorType.INTERNAL_ERROR, ex);
                    }
                }
            }
#endif

            gen.WriteRecognizer(template, header);
        }
コード例 #10
0
ファイル: ParserFactory.cs プロジェクト: sharwell/antlr4cs
 public ParserFactory(CodeGenerator gen)
     : base(gen)
 {
 }