コード例 #1
0
ファイル: MainForm.cs プロジェクト: windcatcher/SharpDevelop
 void RunAstWalkerButtonClick(object sender, EventArgs e)
 {
     try {
         IronPython.Hosting.Python.CreateEngine();
         Clear();
         PythonCompilerSink sink    = new PythonCompilerSink();
         SourceUnit         source  = DefaultContext.DefaultPythonContext.CreateFileUnit(@"D:\Temp.py", codeTextBox.Text);
         CompilerContext    context = new CompilerContext(source, new PythonCompilerOptions(), sink);
         Parser             parser  = Parser.CreateParser(context, new PythonOptions());
         PythonAst          ast     = parser.ParseFile(false);
         if (sink.Errors.Count == 0)
         {
             ResolveWalker walker = new ResolveWalker(this);
             ast.Walk(walker);
         }
         else
         {
             walkerOutputTextBox.Text += "\r\n";
             foreach (PythonCompilerError error in sink.Errors)
             {
                 walkerOutputTextBox.Text += error.ToString() + "\r\n";
             }
         }
     } catch (Exception ex) {
         walkerOutputTextBox.Text = ex.ToString();
     }
 }
コード例 #2
0
        public void AddInvalidSourceSpan()
        {
            IronPython.Hosting.Python.CreateEngine();
            PythonCompilerSink sink   = new PythonCompilerSink();
            SourceUnit         source = DefaultContext.DefaultPythonContext.CreateSourceUnit(NullTextContentProvider.Null, @"D:\test.py", SourceCodeKind.InteractiveCode);

            sink.Add(source, "Test", SourceSpan.Invalid, 1000, Severity.FatalError);
        }
コード例 #3
0
ファイル: PythonParser.cs プロジェクト: hpsa/SharpDevelop
		/// <summary>
		/// Parses a python file and creates a PythonAst.
		/// </summary>
		public PythonAst CreateAst(string fileName, string fileContent)
		{
			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);
			using (Parser parser = Parser.CreateParser(context, new PythonOptions())) {
				return parser.ParseFile(false);	
			}
		}
コード例 #4
0
		/// <summary>
		/// Resolves the type of the variable name specified.
		/// </summary>
		/// <param name="variableName">Name of the variable.</param>
		/// <param name="code">The python code containing the variable.</param>
		public string Resolve(string variableName, string fileName, string code)
		{
			if (code != null) {
				ScriptEngine scriptEngine = IronPython.Hosting.Python.CreateEngine();
				PythonCompilerSink sink = new PythonCompilerSink();
				SourceUnit source = DefaultContext.DefaultPythonContext.CreateFileUnit(fileName, code);
				CompilerContext context = new CompilerContext(source, new PythonCompilerOptions(), sink);
				Parser parser = Parser.CreateParser(context, new PythonOptions());
				PythonAst ast = parser.ParseFile(false);

				return Resolve(variableName, ast);
			}
			return null;
		}
		public IPyCompiler.Tokenizer CreateTokenizer(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();
			IPyCompiler.PythonCompilerOptions options = new IPyCompiler.PythonCompilerOptions();

			tokenizer = new IPyCompiler.Tokenizer(sink, options);
			tokenizer.Initialize(source);
			return tokenizer;
		}
コード例 #6
0
        public IPyCompiler.Tokenizer CreateTokenizer(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();

            IPyCompiler.PythonCompilerOptions options = new IPyCompiler.PythonCompilerOptions();

            tokenizer = new IPyCompiler.Tokenizer(sink, options);
            tokenizer.Initialize(source);
            return(tokenizer);
        }
		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 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);
            }
        }