public PythonParser() { pythonEngine = Python.CreateEngine (); var langContext = HostingHelpers.GetLanguageContext (pythonEngine); compilerOptions = langContext.GetCompilerOptions (); langOptions = (PythonOptions) langContext.Options; walker = new CustomPythonWalker (); }
public FromImportStatement ParseStatement(string text) { ScriptEngine engine = Python.CreateEngine(); PythonContext context = HostingHelpers.GetLanguageContext(engine) as PythonContext; StringTextContentProvider textProvider = new StringTextContentProvider(text); SourceUnit source = context.CreateSourceUnit(textProvider, String.Empty, SourceCodeKind.SingleStatement); PythonCompilerSink sink = new PythonCompilerSink(); CompilerContext compilerContext = new CompilerContext(source, new PythonCompilerOptions(), sink); PythonOptions options = new PythonOptions(); using (Parser parser = Parser.CreateParser(compilerContext, options)) { return parser.ParseSingleStatement().Body as FromImportStatement; } }
public void TestAnalyzeStdLib() { //string dir = Path.Combine(Environment.GetEnvironmentVariable("ProgramFiles"), "IronPython 2.6 for .NET 4.0 RC\\Lib"); string dir = Path.Combine("C:\\Python27\\Lib"); List<string> files = new List<string>(); CollectFiles(dir, files); List<SourceUnit> sourceUnits = new List<SourceUnit>(); foreach (string file in files) { sourceUnits.Add( new SourceUnit( HostingHelpers.GetLanguageContext(_engine), new FileTextContentProvider(new FileStreamContentProvider(file)), Path.GetFileNameWithoutExtension(file), SourceCodeKind.File ) ); } Stopwatch sw = new Stopwatch(); sw.Start(); long start0 = sw.ElapsedMilliseconds; var projectState = new ProjectState(_engine); List<ProjectEntry> modules = new List<ProjectEntry>(); PythonOptions EmptyOptions = new PythonOptions(); foreach (var sourceUnit in sourceUnits) { modules.Add(projectState.AddModule(Path.GetFileNameWithoutExtension(sourceUnit.Path), sourceUnit.Path, null)); } long start1 = sw.ElapsedMilliseconds; Console.WriteLine("AddSourceUnit: {0} ms", start1 - start0); List<PythonAst> nodes = new List<PythonAst>(); for (int i = 0; i < modules.Count; i++) { PythonAst ast = null; try { var sourceUnit = sourceUnits[i]; var context = new CompilerContext(sourceUnit, HostingHelpers.GetLanguageContext(_engine).GetCompilerOptions(), ErrorSink.Null); ast = Parser.CreateParser(context, EmptyOptions).ParseFile(false); } catch (Exception) { } nodes.Add(ast); } long start2 = sw.ElapsedMilliseconds; Console.WriteLine("Parse: {0} ms", start2 - start1); for (int i = 0; i < modules.Count; i++) { var ast = nodes[i]; if (ast != null) { modules[i].UpdateTree(ast, null); } } long start3 = sw.ElapsedMilliseconds; for (int i = 0; i < modules.Count; i++) { Console.WriteLine("Analyzing {1}: {0} ms", sw.ElapsedMilliseconds - start3, sourceUnits[i].Path); var ast = nodes[i]; if (ast != null) { modules[i].Analyze(); } } long start4 = sw.ElapsedMilliseconds; Console.WriteLine("Analyze: {0} ms", start4 - start3); Console.ReadLine(); }