예제 #1
0
        /// <inheritdoc/>
        public void Execute(GeneratorExecutionContext context)
        {
            foreach (var file in GetCdeltaFiles(context))
            {
                var input = file.GetText()?.ToString();
                if (input == null)
                {
                    continue;
                }

                var inputStream = new AntlrInputStream(input);

                var lexer       = new CdeltaLexer(inputStream);
                var tokenStream = new CommonTokenStream(lexer);
                var parser      = new CdeltaParser(tokenStream);

                CdeltaParser.CodeFileContext parsed = parser.codeFile();
                var visitor = new CdeltaGrammarVisitor();
                var result  = visitor.Visit(parsed);

                var code = result.First().ToCode();
                context.AddSource($"{Path.GetFileName(file.Path)}.cs", code);
            }

            using var stream = typeof(CdeltaGenerator).Assembly.GetManifestResourceStream("Cdelta.IAutomaton.cs");
            using var reader = new StreamReader(stream);
            context.AddSource("IAutomaton.cs", reader.ReadToEnd());
        }
예제 #2
0
        private static CodeFile BuildStructure(AntlrInputStream inputStream)
        {
            if (inputStream == null)
            {
                throw new ArgumentNullException(nameof(inputStream));
            }

            CdeltaLexer       lexer       = new CdeltaLexer(inputStream);
            CommonTokenStream tokenStream = new CommonTokenStream(lexer);
            CdeltaParser      parser      = new CdeltaParser(tokenStream);

            CdeltaParser.CodeFileContext result = parser.codeFile();

            CodeFile             codeFile = new CodeFile();
            CdeltaGrammarVisitor visitor  = new CdeltaGrammarVisitor(inputStream.ToString(), ref codeFile);

            visitor.Visit(result);
            codeFile.Finish();
            codeFile.Verify();

            return(codeFile);
        }
예제 #3
0
 /// <summary>
 /// Exit a parse tree produced by <see cref="CdeltaParser.codeFile"/>.
 /// <para>The default implementation does nothing.</para>
 /// </summary>
 /// <param name="context">The parse tree.</param>
 public virtual void ExitCodeFile([NotNull] CdeltaParser.CodeFileContext context)
 {
 }
예제 #4
0
 /// <summary>
 /// Visit a parse tree produced by <see cref="CdeltaParser.codeFile"/>.
 /// <para>
 /// The default implementation returns the result of calling <see cref="AbstractParseTreeVisitor{Result}.VisitChildren(IRuleNode)"/>
 /// on <paramref name="context"/>.
 /// </para>
 /// </summary>
 /// <param name="context">The parse tree.</param>
 /// <return>The visitor result.</return>
 public virtual Result VisitCodeFile([NotNull] CdeltaParser.CodeFileContext context)
 {
     return(VisitChildren(context));
 }
예제 #5
0
        public override object VisitCodeFile(CdeltaParser.CodeFileContext context)
        {
            contextWithStructure.Add(context, codeFile);

            return(base.VisitCodeFile(context));
        }