コード例 #1
0
ファイル: Python.cs プロジェクト: venkateshpotluri/CodeTalk
        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;
        }
コード例 #2
0
ファイル: Python.cs プロジェクト: venkateshpotluri/CodeTalk
        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;
        }
コード例 #3
0
ファイル: MainWindow.xaml.cs プロジェクト: valdisz/PyToJs
        private static void Transformator(object context)
        {
            ThreadContext p = (ThreadContext)context;

            ScriptRuntime   runtime         = Python.CreateRuntime();
            ScriptEngine    engine          = runtime.GetEngine("py");
            LanguageContext languageContext = HostingHelpers.GetLanguageContext(engine);

            while (p.Running)
            {
                string pySrc;
                if (p.Queue.TryDequeue(out pySrc))
                {
                    LocalSink sink = new LocalSink();

                    try
                    {
                        SourceUnit src = HostingHelpers.GetSourceUnit(engine.CreateScriptSourceFromString(pySrc));

                        CompilerContext ctx    = new CompilerContext(src, languageContext.GetCompilerOptions(), sink);
                        Parser          parser = Parser.CreateParser(ctx, (PythonOptions)languageContext.Options);

                        PythonAst           ast       = parser.ParseFile(true);
                        JavascriptGenerator generator = new JavascriptGenerator(src, sink);

                        if (sink.IsError)
                        {
                            p.SetOutput(null);
                        }
                        else
                        {
                            p.SetOutput(generator.ToJavaScript(ast));
                        }
                    }
                    catch (Exception e)
                    {
                        if (!sink.IsError)
                        {
                            p.SetOutput(e.ToString());
                        }
                    }
                    finally
                    {
                        p.SetErrors(sink.Messages);
                    }
                }
                else
                {
                    Thread.Sleep(50);
                    if (p.Queue.Count == 0)
                    {
                        p.Suspend();
                    }
                }
            }
        }
コード例 #4
0
 public CompilerOptions GetCompilerOptions()
 {
     return(_language.GetCompilerOptions());
 }
コード例 #5
0
 public CompilerOptions GetCompilerOptions(ScriptScope scope)
 {
     return(LanguageContext.GetCompilerOptions(scope.Scope));
 }
コード例 #6
0
 public CompilerOptions GetCompilerOptions()
 {
     return(LanguageContext.GetCompilerOptions());
 }