private string Generate(string file) { var f = GetTestFile(file); using (var stream = File.OpenRead(f)) { var lexer = new GrammarLexer(new AntlrInputStream(stream)); var parser = new GrammarParser(new CommonTokenStream(lexer)); var visitor = new TarsGrammarVisitor(f); AdhocWorkspace cw = new AdhocWorkspace(); var formattedNode = Formatter.Format(visitor.Visit(parser.tarsDefinition()), cw); return(formattedNode.ToFullString()); } }
public void Generate(string[] files, string dest) { foreach (var file in files) { using (var stream = File.OpenRead(file)) { var lexer = new GrammarLexer(new AntlrInputStream(stream)); var parser = new GrammarParser(new CommonTokenStream(lexer)); var visitor = new TarsGrammarVisitor(file); var syntax = Formatter.Format(visitor.Visit(parser.tarsDefinition()), workspace); File.WriteAllText(Path.Combine(dest, Path.GetFileNameWithoutExtension(file) + ".cs"), syntax.ToFullString()); } } }
private IEnumerable <UsingDirectiveSyntax> GetUsingDirective(GrammarParser.IncludeDefinitionContext context) { var newfile = context.String().GetText(); var newPath = file.Replace(Path.GetFileName(file), newfile.Replace("\"", "")); if (File.Exists(newPath)) { using (var stream = File.OpenRead(newPath)) { var lexer = new GrammarLexer(new AntlrInputStream(stream)); var parser = new GrammarParser(new CommonTokenStream(lexer)); var syntax = Visit(parser.tarsDefinition()) as CompilationUnitSyntax; return(syntax.Members.Select(i => i as NamespaceDeclarationSyntax) .Where(i => i != null) .Select(i => SyntaxFactory.UsingDirective(i.Name))); } } else { return(new UsingDirectiveSyntax[0]); } }