Esempio n. 1
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;
        }
        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);
        }
Esempio n. 3
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;
        }
Esempio n. 4
0
        public static PythonAst ParseFile(string path)
        {
            var py  = Python.CreateEngine();
            var src = HostingHelpers.GetSourceUnit(py.CreateScriptSourceFromFile(path));

            return(Parse(src, py));
        }
Esempio n. 5
0
        public static PythonAst ParseContent(string content)
        {
            var py  = Python.CreateEngine();
            var src = HostingHelpers.GetSourceUnit(py.CreateScriptSourceFromString(content));

            return(Parse(src, py));
        }
Esempio n. 6
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();
                    }
                }
            }
        }
Esempio n. 7
0
        private PythonAst ParsePythonFile(string path)
        {
            var pythonEngine     = Python.CreateEngine();
            var pythonSource     = pythonEngine.CreateScriptSourceFromFile(path);
            var pythonSourceUnit = HostingHelpers.GetSourceUnit(pythonSource);
            var context          = new CompilerContext(pythonSourceUnit, pythonEngine.GetCompilerOptions(), ErrorSink.Default);
            var options          = new PythonOptions();
            var parser           = Parser.CreateParser(context, options);

            return(parser.ParseFile(false));
        }
Esempio n. 8
0
        public void Parse()
        {
            var srcunit = HostingHelpers.GetSourceUnit(source);
            var parser  = new IronRuby.Compiler.Parser(errorSink);

            ast = parser.Parse(srcunit, new RubyCompilerOptions(), errorSink);

            var transformer = new Transformer.Transformer(ast);

            transformations = transformer.Transform();
        }
        protected override bool ShouldEvaluateForCompletion(string source)
        {
            var scriptSrc = _localEngine.CreateScriptSource(new StringTextContentProvider(source), "", SourceCodeKind.Expression);
            var context   = new CompilerContext(HostingHelpers.GetSourceUnit(scriptSrc), HostingHelpers.GetLanguageContext(_localEngine).GetCompilerOptions(), ErrorSink.Null);
            var parser    = Parser.CreateParser(context, new PythonOptions());

            var stmt       = parser.ParseSingleStatement();
            var exprWalker = new ExprWalker();

            stmt.Walk(exprWalker);
            return(exprWalker.ShouldExecute);
        }
Esempio n. 10
0
        //spawns the python compiler and analyzes a script
        // outputs: ParseInfo class containing:
        //          errors
        //          python ast
        public ParseInfo Parse(string source)
        {
            var parseErrorSink = new ScriptErrorSink();

            ScriptSource script_source = scriptEngine.CreateScriptSourceFromString(source, SourceCodeKind.File);
            var          source_unit   = HostingHelpers.GetSourceUnit(script_source);

            var    language_context = HostingHelpers.GetLanguageContext(scriptEngine);
            Parser parser           = Parser.CreateParser(
                new CompilerContext(source_unit, language_context.GetCompilerOptions(), parseErrorSink),
                (PythonOptions)language_context.Options);

            var python_ast = parser.ParseFile(false);

            var info = new ParseInfo(parseErrorSink.Errors, python_ast);

            //no need for that
            //var walker = new TurtleScriptAstWalker(info);
            //python_ast.Walk(walker);

            return(info);
        }
Esempio n. 11
0
        private void ParsePythonCode(TextContentProvider content, out PythonAst ast, out CollectingErrorSink errorSink)
        {
            ast       = null;
            errorSink = new CollectingErrorSink();

            // parse the tree
            var source      = _engine.CreateScriptSource(content, "", SourceCodeKind.File);
            var compOptions = (PythonCompilerOptions)HostingHelpers.GetLanguageContext(_engine).GetCompilerOptions();
            var context     = new CompilerContext(HostingHelpers.GetSourceUnit(source), compOptions, errorSink);

            //compOptions.Verbatim = true;
            using (var parser = MakeParser(context)) {
                if (parser != null)
                {
                    try {
                        ast = parser.ParseFile(false);
                    } catch (Exception e) {
                        Debug.Assert(false, String.Format("Failure in IronPython parser: {0}", e.ToString()));
                    }
                }
            }
        }
    internal PythonScript ParseScript(string filename, string content)
    {
        var sourceUnit =
            HostingHelpers.GetSourceUnit(
                _engine.CreateScriptSourceFromString(content, filename, SourceCodeKind.File));
        var compilerContext = new CompilerContext(sourceUnit, _compilerOptions, ErrorSink.Default);
        var options         = new PythonOptions();

        var parser = Parser.CreateParser(compilerContext, options);

        PythonAst ast;

        try
        {
            ast = parser.ParseFile(false);
        }
        catch (SyntaxErrorException e)
        {
            throw new ArgumentException($"Failed to parse file '{filename}' at line {e.Line}.", e);
        }

        return(new PythonScript(filename, content, ast));
    }
    private PythonAst ParseSnippet(string snippet)
    {
        var sourceUnit =
            HostingHelpers.GetSourceUnit(
                _engine.CreateScriptSourceFromString(snippet, SourceCodeKind.SingleStatement));
        var compilerContext = new CompilerContext(sourceUnit, _compilerOptions, ErrorSink.Default);
        var options         = new PythonOptions();

        var parser = Parser.CreateParser(compilerContext, options);

        PythonAst ast;

        try
        {
            ast = parser.ParseSingleStatement();
        }
        catch (SyntaxErrorException e)
        {
            throw new ArgumentException($"Failed to parse snippet '{snippet}'.", e);
        }

        return(ast);
    }
Esempio n. 14
0
        private Parser CreatePythonParser(string script)
        {
            var src = HostingHelpers.GetSourceUnit(Engine.CreateScriptSourceFromString(script));

            return(Parser.CreateParser(new CompilerContext(src, Engine.GetCompilerOptions(), ErrorSink.Default), new PythonOptions()));
        }
Esempio n. 15
0
 public override void ErrorReported(ScriptSource source, string message, SourceSpan span, int errorCode, Severity severity)
 {
     throw new SyntaxErrorException(message, HostingHelpers.GetSourceUnit(source), span, errorCode, severity);
 }