public DocumentationComment Parse(ProjectDom dom, string fileName, string content) { var document = new DocumentationComment(fileName); var compilationUnit = new PythonCompilationUnit(fileName); document.CompilationUnit = compilationUnit; if (String.IsNullOrEmpty(content)) { return(document); } var scriptSource = pythonEngine.CreateScriptSourceFromString(content, SourceCodeKind.File); var context = new CompilerContext(HostingHelpers.GetSourceUnit(scriptSource), compilerOptions, ErrorSink.Default); var parser = IronPythonParserEngine.CreateParser(context, langOptions); IronPythonAst ast = null; try { ast = parser.ParseFile(false); } catch (SyntaxErrorException exc) { // We could likely improve the error message document.Errors.Add(new Error(exc.Line, exc.Column, exc.Message)); return(document); } walker.Reset(); ast.Walk(walker); compilationUnit.Module = walker.Module; compilationUnit.Build(); return(document); }
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; }
private static Parser CreateParserWorker(CompilerContext context, PythonOptions options, bool verbatim) { ContractUtils.RequiresNotNull(context, "context"); ContractUtils.RequiresNotNull(options, "options"); PythonCompilerOptions compilerOptions = context.Options as PythonCompilerOptions; if (options == null) { throw new ValueErrorException(Resources.PythonContextRequired); } SourceCodeReader reader; try { reader = context.SourceUnit.GetReader(); if (compilerOptions.SkipFirstLine) { reader.ReadLine(); } } catch (IOException e) { context.Errors.Add(context.SourceUnit, e.Message, SourceSpan.Invalid, 0, Severity.Error); throw; } Tokenizer tokenizer = new Tokenizer(context.Errors, compilerOptions, verbatim); tokenizer.Initialize(null, reader, context.SourceUnit, SourceLocation.MinValue); tokenizer.IndentationInconsistencySeverity = options.IndentationInconsistencySeverity; Parser result = new Parser(context, tokenizer, context.Errors, context.ParserSink, compilerOptions.Module); result._sourceReader = reader; return result; }
public TokenizerErrorSink(Parser parser) { Assert.NotNull(parser); _parser = parser; }
/// <summary> /// Private helper function for all parsing. Takes in the IronPython Parser /// object and a filename that's used for error reports /// </summary> /// <param name="p"></param> private CodeCompileUnit Parse(Parser p, string filename) { Statement s = p.ParseFileInput(); CodeCompileUnit res = new CodeCompileUnit(); CodeNamespace defaultNamespace = new CodeNamespace(); defaultNamespace.UserData["Line"] = 1; defaultNamespace.UserData["Column"] = 1; defaultNamespace.UserData["EndLine"] = 1; defaultNamespace.UserData["EndColumn"] = 1; //!!! enable AD usage when we're strong named. //AppDomainSetup ads = new AppDomainSetup(); //ads.ApplicationBase = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); //AppDomain ad = AppDomain.CreateDomain("ParserReferenceDomain",null,ads); try { RemoteReferences rc = new RemoteReferences(); /*(RemoteReferences)ad.CreateInstanceAndUnwrap( Assembly.GetExecutingAssembly().FullName, "IronPython.CodeDom.RemoteReferences");*/ rc.Initialize(references); CodeWalker cw = new CodeWalker(filename, rc); s.Walk(cw); CodeObject co = cw.LastObject; CodeObjectSuite cos = co as CodeObjectSuite; if (cos == null) cos = new CodeObjectSuite(new CodeObject[] { co }); // walk the top-level and see if we need to create a fake top-type // or if we can just stick everything directly into the namespace. CodeTypeDeclaration topType = null; for (int i = 0; i < cos.Count; i++) { topType = CheckTopLevel(cos[i], res, defaultNamespace, topType); } // if no namespaces we're added then everything's in our default namespace. if (res.Namespaces.Count == 0 || defaultNamespace.Types.Count > 0) { res.Namespaces.Add(defaultNamespace); } UpdateTopType(topType, defaultNamespace); } finally { //AppDomain.Unload(ad); } return res; }
private static PythonModule GenerateAndInitializeModule(ICallerContext context, CompilerContext cc, Parser parser, string name, string filename) { Statement stmt = parser.ParseFileInput(); PythonModule module = OutputGenerator.GenerateModule(context.SystemState, cc, stmt, name); module.Filename = filename; module.ModuleName = name; return Importer.InitializeModule(filename, module); }