// // // // // $Revision$ // using Microsoft.Scripting; using Microsoft.Scripting.Hosting; using Microsoft.Scripting.Runtime; using System; using System.IO; using ICSharpCode.SharpDevelop.Dom; using ICSharpCode.SharpDevelop.Project; using IronPython; using IronPython.Compiler; using IronPython.Compiler.Ast; using IronPython.Runtime; using IronPython.Hosting; using IronPython.Runtime.Exceptions; namespace ICSharpCode.PythonBinding { /// /// Python parser. /// public class PythonParser : IParser { string[] lexerTags = new string[0]; ScriptEngine scriptEngine; public PythonParser() { } public string[] LexerTags { get { return lexerTags; } set { lexerTags = value; } } public LanguageProperties Language { get { return LanguageProperties.None; } } /// /// Creates a PythonExpressionFinder. /// public IExpressionFinder CreateExpressionFinder(string fileName) { return new PythonExpressionFinder(); } /// /// Returns true if the filename has a .py extension. /// public bool CanParse(string fileName) { string extension = Path.GetExtension(fileName); if (extension != null) { return extension.ToLower() == ".py"; } return false; } /// /// Returns true if the project's language is python. /// public bool CanParse(IProject project) { if (project != null) { return project.Language == PythonLanguageBinding.LanguageName; } return false; } /// /// Parses the python code and returns an ICompilationUnit. /// public ICompilationUnit Parse(IProjectContent projectContent, string fileName, string fileContent) { if (fileContent != null) { // try { if (scriptEngine == null) { scriptEngine = IronPython.Hosting.Python.CreateEngine(); } PythonCompilerSink sink = new PythonCompilerSink(); SourceUnit source = DefaultContext.DefaultPythonContext.CreateFileUnit(fileName, fileContent); CompilerContext context = new CompilerContext(source, new PythonCompilerOptions(), sink); Parser parser = Parser.CreateParser(context, new PythonOptions()); PythonAst ast = parser.ParseFile(false); PythonAstWalker walker = new PythonAstWalker(projectContent, fileName); walker.Walk(ast); return walker.CompilationUnit; // } catch (PythonSyntaxErrorException) { // Ignore parsing errors // } } DefaultCompilationUnit compilationUnit = new DefaultCompilationUnit(projectContent); compilationUnit.FileName = fileName; return compilationUnit; } /// /// Creates a new PythonResolver. /// public IResolver CreateResolver() { return new PythonResolver(); } } }