Ejemplo n.º 1
0
        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;
        }
Ejemplo n.º 2
0
        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;
        }
Ejemplo n.º 3
0
        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();
                    }
                }
            }
        }
Ejemplo n.º 4
0
 public CompilerOptions GetCompilerOptions()
 {
     return(_language.GetCompilerOptions());
 }
 public CompilerOptions GetCompilerOptions(ScriptScope scope)
 {
     return(LanguageContext.GetCompilerOptions(scope.Scope));
 }
 public CompilerOptions GetCompilerOptions()
 {
     return(LanguageContext.GetCompilerOptions());
 }