public CodeFile Parse(string programText, string fileName) { ScriptEngine py = null; SourceUnit src = null; LanguageContext pythonLanguageContext = null; CompilerContext cc = null; IronPython.PythonOptions pyOptions; IronPython.Compiler.Parser pyParser = null; PythonAst ast = null; try { py = IronPython.Hosting.Python.CreateEngine(); src = HostingHelpers.GetSourceUnit(py.CreateScriptSourceFromString(programText)); pythonLanguageContext = HostingHelpers.GetLanguageContext(py); cc = new CompilerContext(src, pythonLanguageContext.GetCompilerOptions(), ErrorSink.Default); pyOptions = pythonLanguageContext.Options as IronPython.PythonOptions; pyParser = Parser.CreateParser(cc, pyOptions); ast = pyParser.ParseFile(true); } finally { pyParser?.Dispose(); } PythonEntityCollector collector = new PythonEntityCollector(); ast.Walk(collector); var cf = collector.getCodeFile(); cf.Name = String.IsNullOrWhiteSpace(fileName) ? $"{rm.GetString("PythonString", CultureInfo.CurrentCulture)}" : fileName; return cf; }
public IEnumerable<CompileError> GetDiagnostics(string programText) { ScriptEngine py = null; SourceUnit src = null; LanguageContext pythonLanguageContext = null; CompilerContext cc = null; IronPython.PythonOptions pyOptions; IronPython.Compiler.Parser pyParser = null; IEnumerable<CompileError> errorList = Enumerable.Empty<CompileError>(); try { py = IronPython.Hosting.Python.CreateEngine(); src = HostingHelpers.GetSourceUnit(py.CreateScriptSourceFromString(programText)); pythonLanguageContext = HostingHelpers.GetLanguageContext(py); cc = new CompilerContext(src, pythonLanguageContext.GetCompilerOptions(), ErrorSink.Default); pyOptions = pythonLanguageContext.Options as IronPython.PythonOptions; pyParser = Parser.CreateParser(cc, pyOptions); pyParser.ParseFile(true); } catch (Microsoft.Scripting.SyntaxErrorException e) { CompileError syntaxError = new CompileError(e.Message, new FileSpan(e.RawSpan.Start.Line, e.RawSpan.Start.Column, e.RawSpan.End.Line, e.RawSpan.End.Column)); errorList = errorList.Concat(new[] { syntaxError }); } finally { pyParser?.Dispose(); } return errorList; }