Exemple #1
0
        private void Parse(CancellationToken cancellationToken)
        {
            CollectingErrorSink sink = null;
            int    version;
            Parser parser;

            // Log?.Log(TraceEventType.Verbose, $"Parse begins: {Name} ({ModuleType})");

            lock (_syncObj) {
                version = _buffer.Version;
                var options = new ParserOptions {
                    StubFile = FilePath != null && Path.GetExtension(FilePath).Equals(".pyi", FileSystem.StringComparison)
                };
                if (ModuleType == ModuleType.User)
                {
                    sink = new CollectingErrorSink();
                    options.ErrorSink = sink;
                }
                parser = Parser.CreateParser(new StringReader(_buffer.Text), Interpreter.LanguageVersion, options);
            }

            var ast = parser.ParseFile(Uri);

            // Log?.Log(TraceEventType.Verbose, $"Parse complete: {Name} ({ModuleType})");

            lock (_syncObj) {
                cancellationToken.ThrowIfCancellationRequested();
                if (version != _buffer.Version)
                {
                    throw new OperationCanceledException();
                }

                // Stored nodes are no longer valid.
                _astMap.Clear();
                _astMap[this] = ast;

                _parseErrors = sink?.Diagnostics ?? Array.Empty <DiagnosticsEntry>();

                // Do not report issues with libraries or stubs
                if (sink != null)
                {
                    _diagnosticsService?.Replace(Uri, _parseErrors, DiagnosticSource.Parser);
                }

                ContentState = State.Parsed;
                Analysis     = new EmptyAnalysis(Services, this);
            }

            NewAst?.Invoke(this, EventArgs.Empty);
            Analyze(ast, version);

            lock (_syncObj) {
                _parsingTask = null;
            }
        }
Exemple #2
0
        protected PythonModule(string name, ModuleType moduleType, IServiceContainer services)
        {
            Name       = name ?? throw new ArgumentNullException(nameof(name));
            Services   = services ?? throw new ArgumentNullException(nameof(services));
            ModuleType = moduleType;

            Log         = services.GetService <ILogger>();
            Interpreter = services.GetService <IPythonInterpreter>();
            Analysis    = new EmptyAnalysis(services, this);

            _diagnosticsService = services.GetService <IDiagnosticsService>();
        }