Ejemplo n.º 1
0
 private void DrawErrors()
 {
     TextBox.Range.ClearStyle(ColorScheme.Colors["error"]);
     if (_errors != null && _errors.Any())
     {
         foreach (var errorRange in _errors.Select(x => new { x.Line, x.Column, x.Length }))
         {
             var startLine = errorRange.Line - 1;
             var startChar = errorRange.Column == 0? errorRange.Column: errorRange.Column - 1;
             var endChar   = errorRange.Column + errorRange.Length == 0? 1 : errorRange.Length;
             var range     = new Range(TextBox, startChar, startLine, endChar, startLine);
             ColorScheme.Colors.Values.ToList().ForEach(x => range.ClearStyle(x));
             range.SetStyle(ColorScheme.Colors["error"]);
         }
     }
 }
Ejemplo n.º 2
0
        public void Imported(IModuleContext context)
        {
            if (_scraped)
            {
                return;
            }
            _scraped = true;

            var interp = context as AstPythonInterpreter;
            var fact   = interp?.Factory as AstPythonInterpreterFactory;

            if (fact == null)
            {
                return;
            }

            var  code      = LoadCachedCode(interp);
            bool needCache = code == null;

            if (needCache)
            {
                if (!File.Exists(fact.Configuration.InterpreterPath))
                {
                    return;
                }

                var args = GetScrapeArguments(fact);
                if (args == null)
                {
                    return;
                }

                var ms = new MemoryStream();
                code = ms;

                using (var sw = new StreamWriter(ms, Encoding.UTF8, 4096, true))
                    using (var proc = new ProcessHelper(
                               fact.Configuration.InterpreterPath,
                               args,
                               fact.Configuration.PrefixPath
                               )) {
                        proc.StartInfo.StandardOutputEncoding = Encoding.UTF8;
                        proc.OnOutputLine = sw.WriteLine;
                        proc.OnErrorLine  = s => fact.Log(TraceLevel.Error, "Scrape", s);

                        fact.Log(TraceLevel.Info, "Scrape", proc.FileName, proc.Arguments);

                        proc.Start();
                        var exitCode = proc.Wait(60000);

                        if (exitCode == null)
                        {
                            proc.Kill();
                            fact.Log(TraceLevel.Error, "ScrapeTimeout", proc.FileName, proc.Arguments);
                            return;
                        }
                        else if (exitCode != 0)
                        {
                            fact.Log(TraceLevel.Error, "Scrape", "ExitCode", exitCode);
                            return;
                        }
                    }

                code?.Seek(0, SeekOrigin.Begin);
            }

            if (code == null)
            {
                return;
            }

            PythonAst ast;

            using (code) {
                var sink = new CollectingErrorSink();
                using (var sr = new StreamReader(code, Encoding.UTF8, true, 4096, true)) {
                    var parser = Parser.CreateParser(sr, fact.GetLanguageVersion(), new ParserOptions {
                        ErrorSink = sink, StubFile = true
                    });
                    ast = parser.ParseFile();
                }

                ParseErrors = sink.Errors.Select(e => "{0} ({1}): {2}".FormatUI(_filePath ?? "(builtins)", e.Span, e.Message)).ToArray();
                if (ParseErrors.Any())
                {
                    fact.Log(TraceLevel.Error, "Parse", _filePath ?? "(builtins)");
                    foreach (var e in ParseErrors)
                    {
                        fact.Log(TraceLevel.Error, "Parse", e);
                    }
                }

                if (needCache)
                {
                    // We know we created the stream, so it's safe to seek here
                    code.Seek(0, SeekOrigin.Begin);
                    SaveCachedCode(interp, code);
                }
            }

            if (KeepAst)
            {
                Ast = ast;
            }

#if DEBUG
            if (!string.IsNullOrEmpty(_filePath))
            {
                var cachePath = fact.GetCacheFilePath(_filePath);
                if (!string.IsNullOrEmpty(cachePath))
                {
                    Locations = new[] { new LocationInfo(cachePath, null, 1, 1) };
                }
            }
#endif

            var walker = PrepareWalker(interp, ast);
            lock (_members) {
                ast.Walk(walker);
                PostWalk(walker);
            }
        }
Ejemplo n.º 3
0
        public void Imported(IModuleContext context)
        {
            if (_scraped)
            {
                return;
            }
            _scraped = true;

            var interp = context as AstPythonInterpreter;
            var fact   = interp?.Factory as AstPythonInterpreterFactory;

            if (fact == null)
            {
                return;
            }

            var  code      = LoadCachedCode(interp);
            bool needCache = code == null;

            if (needCache)
            {
                if (!File.Exists(fact.Configuration.InterpreterPath))
                {
                    return;
                }

                var args = GetScrapeArguments(fact);
                if (args == null)
                {
                    return;
                }

                using (var p = ProcessOutput.Run(
                           fact.Configuration.InterpreterPath,
                           args.ToArray(),
                           fact.Configuration.PrefixPath,
                           null,
                           visible: false,
                           redirector: null,
                           outputEncoding: Encoding.UTF8,
                           errorEncoding: Encoding.UTF8
                           )) {
                    p.Wait();
                    if (p.ExitCode == 0)
                    {
                        var ms = new MemoryStream();
                        code = ms;
                        using (var sw = new StreamWriter(ms, Encoding.UTF8, 4096, true)) {
                            foreach (var line in p.StandardOutputLines)
                            {
                                sw.WriteLine(line);
                            }
                        }
                        code.Seek(0, SeekOrigin.Begin);
                    }
                    else
                    {
                        fact.Log(TraceLevel.Error, "Scrape", p.Arguments);
                        foreach (var e in p.StandardErrorLines)
                        {
                            fact.Log(TraceLevel.Error, "Scrape", Name, e);
                        }

                        var err = new List <string> {
                            $"Error scraping {Name}", p.Arguments
                        };
                        err.AddRange(p.StandardErrorLines);
                        Debug.Fail(string.Join(Environment.NewLine, err));
                        ParseErrors = err;
                    }
                }
            }

            if (code == null)
            {
                return;
            }

            PythonAst ast;

            using (code) {
                var sink = new CollectingErrorSink();
                using (var sr = new StreamReader(code, Encoding.UTF8, true, 4096, true))
                    using (var parser = Parser.CreateParser(sr, fact.GetLanguageVersion(), new ParserOptions {
                        ErrorSink = sink
                    })) {
                        ast = parser.ParseFile();
                    }

                ParseErrors = sink.Errors.Select(e => $"{_filePath ?? "(builtins)"} ({e.Span}): {e.Message}").ToArray();
                if (ParseErrors.Any())
                {
                    fact.Log(TraceLevel.Error, "Parse", _filePath ?? "(builtins)");
                    foreach (var e in ParseErrors)
                    {
                        fact.Log(TraceLevel.Error, "Parse", e);
                    }
                }

                if (needCache)
                {
                    // We know we created the stream, so it's safe to seek here
                    code.Seek(0, SeekOrigin.Begin);
                    SaveCachedCode(interp, code);
                }
            }

#if DEBUG
            Locations = new[] { new LocationInfo(fact.GetCacheFilePath(_filePath), 1, 1) };
#endif

            var walker = PrepareWalker(interp, ast);
            lock (_members) {
                ast.Walk(walker);
                PostWalk(walker);
            }
        }