public override IEnumerable <INode> VisitSourceFiles(TypescriptParser.SourceFilesContext context) { var root = new Root(base.VisitSourceFiles(context)); var sourceFilesByPath = root.SourceFiles.ToDictionary(x => x.Path); foreach (var rawSourceFile in context.sourceFile()) { var sourceFile = sourceFilesByPath[rawSourceFile.FilePath]; foreach (var importDeclaration in sourceFile.ImportedSourceFiles) { var importContext = (TypescriptParser.ImportDeclarationContext)importDeclaration.Context; var targetPath = importContext.TargetPath.ToLower(); if (!targetPath.EndsWith(".ts")) { targetPath += ".ts"; } importDeclaration.TargetSourceFile = sourceFilesByPath[targetPath]; } } yield return(root); }
public static Root Simplify(TypescriptParser.SourceFilesContext rootContext) { var simplifier = new AstSimplifier(); return(simplifier.Visit(rootContext).OfType <Root>().SingleOrDefault()); }
public static TypescriptParser.SourceFilesContext ParseAntlrAst(string[] originalFilenames) { var queue = new Queue <string>(); foreach (var originalFilename in originalFilenames) { queue.Enqueue(Path.GetFullPath(originalFilename)); } var errors = new List <string>(); var loadedFiles = new Dictionary <string, TypescriptParser.SourceFileContext>(); while (queue.Count > 0) { var filename = queue.Dequeue().ToLower(); if (loadedFiles.ContainsKey(filename)) { continue; } var sourceFile = LoadFile(filename, ref errors); if (sourceFile != null) { loadedFiles.Add(filename, sourceFile); foreach (var importDeclaration in sourceFile.importDeclaration()) { var targetPath = importDeclaration.TargetPath.ToLower(); if (!targetPath.EndsWith(".ts")) { targetPath += ".ts"; } queue.Enqueue(targetPath); } } } foreach (var error in errors) { Console.WriteLine(error); } if (errors.Any()) { return(null); } var sourceFiles = new TypescriptParser.SourceFilesContext(null, 0); foreach (var sourceFile in loadedFiles.Values) { foreach (var importDeclaration in sourceFile.importDeclaration()) { var targetPath = importDeclaration.TargetPath.ToLower(); if (!targetPath.EndsWith(".ts")) { targetPath += ".ts"; } importDeclaration.TargetFile = loadedFiles[targetPath]; } sourceFile.Parent = sourceFiles; sourceFiles.AddChild(sourceFile); } return(sourceFiles); }