@zzzen/pyright-internal 1.1.255 → 1.2.0-dev.20220703
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +1 -0
- package/dist/analyzer/aliasDeclarationUtils.js +2 -2
- package/dist/analyzer/analyzerFileInfo.d.ts +1 -1
- package/dist/analyzer/analyzerNodeInfo.d.ts +4 -1
- package/dist/analyzer/analyzerNodeInfo.js +12 -1
- package/dist/analyzer/analyzerNodeInfo.js.map +1 -1
- package/dist/analyzer/backgroundAnalysisProgram.d.ts +1 -0
- package/dist/analyzer/backgroundAnalysisProgram.js +3 -0
- package/dist/analyzer/backgroundAnalysisProgram.js.map +1 -1
- package/dist/analyzer/binder.d.ts +6 -1
- package/dist/analyzer/binder.js +142 -32
- package/dist/analyzer/binder.js.map +1 -1
- package/dist/analyzer/checker.d.ts +4 -1
- package/dist/analyzer/checker.js +144 -73
- package/dist/analyzer/checker.js.map +1 -1
- package/dist/analyzer/codeFlowUtils.js +33 -0
- package/dist/analyzer/codeFlowUtils.js.map +1 -1
- package/dist/analyzer/constraintSolver.js +6 -3
- package/dist/analyzer/constraintSolver.js.map +1 -1
- package/dist/analyzer/dataClasses.js +6 -4
- package/dist/analyzer/dataClasses.js.map +1 -1
- package/dist/analyzer/declaration.d.ts +18 -6
- package/dist/analyzer/declaration.js +25 -5
- package/dist/analyzer/declaration.js.map +1 -1
- package/dist/analyzer/declarationUtils.d.ts +1 -1
- package/dist/analyzer/declarationUtils.js +19 -16
- package/dist/analyzer/declarationUtils.js.map +1 -1
- package/dist/analyzer/importResult.js +7 -0
- package/dist/analyzer/importResult.js.map +1 -1
- package/dist/analyzer/importStatementUtils.js +10 -1
- package/dist/analyzer/importStatementUtils.js.map +1 -1
- package/dist/analyzer/namedTuples.js +1 -1
- package/dist/analyzer/namedTuples.js.map +1 -1
- package/dist/analyzer/packageTypeReport.js +10 -1
- package/dist/analyzer/packageTypeReport.js.map +1 -1
- package/dist/analyzer/packageTypeVerifier.js +6 -6
- package/dist/analyzer/parseTreeUtils.d.ts +3 -3
- package/dist/analyzer/parseTreeUtils.js +27 -13
- package/dist/analyzer/parseTreeUtils.js.map +1 -1
- package/dist/analyzer/parseTreeWalker.d.ts +4 -1
- package/dist/analyzer/parseTreeWalker.js +19 -1
- package/dist/analyzer/parseTreeWalker.js.map +1 -1
- package/dist/analyzer/program.d.ts +2 -1
- package/dist/analyzer/program.js +27 -23
- package/dist/analyzer/program.js.map +1 -1
- package/dist/analyzer/properties.js +2 -0
- package/dist/analyzer/properties.js.map +1 -1
- package/dist/analyzer/protocols.d.ts +0 -1
- package/dist/analyzer/protocols.js +4 -64
- package/dist/analyzer/protocols.js.map +1 -1
- package/dist/analyzer/scope.js +22 -1
- package/dist/analyzer/scope.js.map +1 -1
- package/dist/analyzer/service.d.ts +20 -8
- package/dist/analyzer/service.js +39 -19
- package/dist/analyzer/service.js.map +1 -1
- package/dist/analyzer/sourceFile.js +1 -1
- package/dist/analyzer/sourceFile.js.map +1 -1
- package/dist/analyzer/sourceFileInfoUtils.d.ts +2 -0
- package/dist/analyzer/sourceFileInfoUtils.js +15 -0
- package/dist/analyzer/sourceFileInfoUtils.js.map +1 -0
- package/dist/analyzer/symbol.d.ts +3 -1
- package/dist/analyzer/symbol.js +39 -1
- package/dist/analyzer/symbol.js.map +1 -1
- package/dist/analyzer/tracePrinter.js +8 -4
- package/dist/analyzer/tracePrinter.js.map +1 -1
- package/dist/analyzer/typeDocStringUtils.js +11 -3
- package/dist/analyzer/typeDocStringUtils.js.map +1 -1
- package/dist/analyzer/typeEvaluator.js +610 -208
- package/dist/analyzer/typeEvaluator.js.map +1 -1
- package/dist/analyzer/typeEvaluatorTypes.d.ts +2 -1
- package/dist/analyzer/typeEvaluatorTypes.js +65 -1
- package/dist/analyzer/typeEvaluatorTypes.js.map +1 -1
- package/dist/analyzer/typeEvaluatorWithTracker.js +2 -0
- package/dist/analyzer/typeEvaluatorWithTracker.js.map +1 -1
- package/dist/analyzer/typeGuards.d.ts +2 -0
- package/dist/analyzer/typeGuards.js +113 -69
- package/dist/analyzer/typeGuards.js.map +1 -1
- package/dist/analyzer/typePrinter.js +27 -2
- package/dist/analyzer/typePrinter.js.map +1 -1
- package/dist/analyzer/typeStubWriter.d.ts +4 -1
- package/dist/analyzer/typeStubWriter.js +36 -0
- package/dist/analyzer/typeStubWriter.js.map +1 -1
- package/dist/analyzer/typeUtils.js +129 -33
- package/dist/analyzer/typeUtils.js.map +1 -1
- package/dist/analyzer/typedDicts.js +2 -1
- package/dist/analyzer/typedDicts.js.map +1 -1
- package/dist/analyzer/types.d.ts +12 -4
- package/dist/analyzer/types.js +215 -6
- package/dist/analyzer/types.js.map +1 -1
- package/dist/backgroundAnalysisBase.d.ts +1 -1
- package/dist/commands/commands.js +9 -0
- package/dist/commands/commands.js.map +1 -1
- package/dist/common/commandLineOptions.js +8 -1
- package/dist/common/commandLineOptions.js.map +1 -1
- package/dist/common/core.js +7 -1
- package/dist/common/core.js.map +1 -1
- package/dist/common/diagnostic.js +9 -1
- package/dist/common/diagnostic.js.map +1 -1
- package/dist/common/host.js +7 -1
- package/dist/common/host.js.map +1 -1
- package/dist/common/pathUtils.d.ts +1 -0
- package/dist/common/pathUtils.js +34 -1
- package/dist/common/pathUtils.js.map +1 -1
- package/dist/common/stringUtils.d.ts +1 -0
- package/dist/common/stringUtils.js +8 -1
- package/dist/common/stringUtils.js.map +1 -1
- package/dist/languageServerBase.d.ts +10 -3
- package/dist/languageServerBase.js +40 -14
- package/dist/languageServerBase.js.map +1 -1
- package/dist/languageService/analyzerServiceExecutor.js +3 -0
- package/dist/languageService/analyzerServiceExecutor.js.map +1 -1
- package/dist/languageService/autoImporter.js +2 -1
- package/dist/languageService/autoImporter.js.map +1 -1
- package/dist/languageService/callHierarchyProvider.js +9 -9
- package/dist/languageService/completionProvider.d.ts +2 -2
- package/dist/languageService/completionProvider.js +22 -18
- package/dist/languageService/completionProvider.js.map +1 -1
- package/dist/languageService/definitionProvider.js +3 -3
- package/dist/languageService/documentSymbolCollector.js +1 -1
- package/dist/languageService/documentSymbolProvider.js +10 -7
- package/dist/languageService/documentSymbolProvider.js.map +1 -1
- package/dist/languageService/hoverProvider.js +19 -5
- package/dist/languageService/hoverProvider.js.map +1 -1
- package/dist/languageService/referencesProvider.js +8 -5
- package/dist/languageService/referencesProvider.js.map +1 -1
- package/dist/languageService/tooltipUtils.js +1 -1
- package/dist/localization/localize.d.ts +27 -0
- package/dist/localization/localize.js +16 -0
- package/dist/localization/localize.js.map +1 -1
- package/dist/localization/package.nls.en-us.json +18 -2
- package/dist/parser/parseNodes.d.ts +44 -5
- package/dist/parser/parseNodes.js +191 -4
- package/dist/parser/parseNodes.js.map +1 -1
- package/dist/parser/parser.d.ts +3 -1
- package/dist/parser/parser.js +155 -24
- package/dist/parser/parser.js.map +1 -1
- package/dist/parser/stringTokenUtils.js +8 -1
- package/dist/parser/stringTokenUtils.js.map +1 -1
- package/dist/parser/tokenizer.js +4 -3
- package/dist/parser/tokenizer.js.map +1 -1
- package/dist/parser/tokenizerTypes.d.ts +5 -3
- package/dist/parser/tokenizerTypes.js +157 -1
- package/dist/parser/tokenizerTypes.js.map +1 -1
- package/dist/pyright.js +6 -1
- package/dist/pyright.js.map +1 -1
- package/dist/tests/chainedSourceFiles.test.js +12 -9
- package/dist/tests/chainedSourceFiles.test.js.map +1 -1
- package/dist/tests/checker.test.js +4 -0
- package/dist/tests/checker.test.js.map +1 -1
- package/dist/tests/config.test.js +14 -13
- package/dist/tests/config.test.js.map +1 -1
- package/dist/tests/debug.test.js +6 -0
- package/dist/tests/debug.test.js.map +1 -1
- package/dist/tests/fourslash/completions.autoimport.lib.alias.fourslash.d.ts +1 -0
- package/dist/tests/fourslash/completions.autoimport.lib.alias.fourslash.js +53 -0
- package/dist/tests/fourslash/completions.autoimport.lib.alias.fourslash.js.map +1 -0
- package/dist/tests/fourslash/fourslash.d.ts +16 -0
- package/dist/tests/fourslash/hover.class.docString.fourslash.d.ts +1 -0
- package/dist/tests/fourslash/hover.class.docString.fourslash.js +24 -0
- package/dist/tests/fourslash/hover.class.docString.fourslash.js.map +1 -0
- package/dist/tests/harness/fourslash/fourSlashParser.js +6 -0
- package/dist/tests/harness/fourslash/fourSlashParser.js.map +1 -1
- package/dist/tests/harness/fourslash/fourSlashTypes.js +19 -1
- package/dist/tests/harness/fourslash/fourSlashTypes.js.map +1 -1
- package/dist/tests/harness/fourslash/testLanguageService.js +9 -1
- package/dist/tests/harness/fourslash/testLanguageService.js.map +1 -1
- package/dist/tests/harness/fourslash/testState.Consts.d.ts +1 -0
- package/dist/tests/harness/fourslash/testState.Consts.js +1 -0
- package/dist/tests/harness/fourslash/testState.Consts.js.map +1 -1
- package/dist/tests/harness/fourslash/testState.d.ts +4 -3
- package/dist/tests/harness/fourslash/testState.js +27 -11
- package/dist/tests/harness/fourslash/testState.js.map +1 -1
- package/dist/tests/harness/vfs/pathValidation.js +25 -1
- package/dist/tests/harness/vfs/pathValidation.js.map +1 -1
- package/dist/tests/pathUtils.test.js +17 -0
- package/dist/tests/pathUtils.test.js.map +1 -1
- package/dist/tests/testUtils.js +1 -1
- package/dist/tests/testUtils.js.map +1 -1
- package/dist/tests/typeEvaluator1.test.js +9 -1
- package/dist/tests/typeEvaluator1.test.js.map +1 -1
- package/dist/tests/typeEvaluator2.test.js +9 -1
- package/dist/tests/typeEvaluator2.test.js.map +1 -1
- package/dist/tests/typeEvaluator3.test.js +4 -0
- package/dist/tests/typeEvaluator3.test.js.map +1 -1
- package/dist/tests/typeEvaluator5.test.d.ts +1 -0
- package/dist/tests/typeEvaluator5.test.js +112 -0
- package/dist/tests/typeEvaluator5.test.js.map +1 -0
- package/dist/workspaceMap.d.ts +2 -1
- package/dist/workspaceMap.js +25 -3
- package/dist/workspaceMap.js.map +1 -1
- package/package.json +45 -45
- package/src/analyzer/aliasDeclarationUtils.ts +0 -169
- package/src/analyzer/analysis.ts +0 -92
- package/src/analyzer/analyzerFileInfo.ts +0 -56
- package/src/analyzer/analyzerNodeInfo.ts +0 -198
- package/src/analyzer/backgroundAnalysisProgram.ts +0 -311
- package/src/analyzer/binder.ts +0 -4081
- package/src/analyzer/checker.ts +0 -5507
- package/src/analyzer/circularDependency.ts +0 -52
- package/src/analyzer/codeFlowEngine.ts +0 -1548
- package/src/analyzer/codeFlowTypes.ts +0 -266
- package/src/analyzer/codeFlowUtils.ts +0 -436
- package/src/analyzer/commentUtils.ts +0 -174
- package/src/analyzer/constraintSolver.ts +0 -926
- package/src/analyzer/constructorTransform.ts +0 -317
- package/src/analyzer/dataClasses.ts +0 -999
- package/src/analyzer/declaration.ts +0 -252
- package/src/analyzer/declarationUtils.ts +0 -238
- package/src/analyzer/docStringConversion.ts +0 -850
- package/src/analyzer/docStringUtils.ts +0 -112
- package/src/analyzer/functionTransform.ts +0 -137
- package/src/analyzer/importResolver.ts +0 -2308
- package/src/analyzer/importResult.ts +0 -98
- package/src/analyzer/importStatementUtils.ts +0 -834
- package/src/analyzer/namedTuples.ts +0 -422
- package/src/analyzer/packageTypeReport.ts +0 -108
- package/src/analyzer/packageTypeVerifier.ts +0 -1466
- package/src/analyzer/parentDirectoryCache.ts +0 -88
- package/src/analyzer/parseTreeCleaner.ts +0 -35
- package/src/analyzer/parseTreeUtils.ts +0 -2165
- package/src/analyzer/parseTreeWalker.ts +0 -663
- package/src/analyzer/patternMatching.ts +0 -1356
- package/src/analyzer/program.ts +0 -2778
- package/src/analyzer/properties.ts +0 -534
- package/src/analyzer/protocols.ts +0 -660
- package/src/analyzer/pyTypedUtils.ts +0 -52
- package/src/analyzer/pythonPathUtils.ts +0 -201
- package/src/analyzer/scope.ts +0 -202
- package/src/analyzer/scopeUtils.ts +0 -81
- package/src/analyzer/service.ts +0 -1679
- package/src/analyzer/sourceFile.ts +0 -1401
- package/src/analyzer/sourceMapper.ts +0 -717
- package/src/analyzer/staticExpressions.ts +0 -308
- package/src/analyzer/symbol.ts +0 -249
- package/src/analyzer/symbolNameUtils.ts +0 -50
- package/src/analyzer/symbolUtils.ts +0 -49
- package/src/analyzer/testWalker.ts +0 -101
- package/src/analyzer/tracePrinter.ts +0 -264
- package/src/analyzer/typeCache.ts +0 -181
- package/src/analyzer/typeDocStringUtils.ts +0 -371
- package/src/analyzer/typeEvaluator.ts +0 -23395
- package/src/analyzer/typeEvaluatorTypes.ts +0 -452
- package/src/analyzer/typeEvaluatorWithTracker.ts +0 -161
- package/src/analyzer/typeGuards.ts +0 -1828
- package/src/analyzer/typePrinter.ts +0 -807
- package/src/analyzer/typeStubWriter.ts +0 -727
- package/src/analyzer/typeUtils.ts +0 -3126
- package/src/analyzer/typeVarContext.ts +0 -330
- package/src/analyzer/typedDicts.ts +0 -950
- package/src/analyzer/types.ts +0 -2857
- package/src/backgroundAnalysis.ts +0 -49
- package/src/backgroundAnalysisBase.ts +0 -606
- package/src/backgroundThreadBase.ts +0 -143
- package/src/commands/commandController.ts +0 -62
- package/src/commands/commandResult.ts +0 -21
- package/src/commands/commands.ts +0 -16
- package/src/commands/createTypeStub.ts +0 -56
- package/src/commands/quickActionCommand.ts +0 -40
- package/src/commands/restartServer.ts +0 -20
- package/src/common/cancellationUtils.ts +0 -71
- package/src/common/chokidarFileWatcherProvider.ts +0 -74
- package/src/common/collectionUtils.ts +0 -379
- package/src/common/commandLineOptions.ts +0 -132
- package/src/common/configOptions.ts +0 -1291
- package/src/common/console.ts +0 -201
- package/src/common/core.ts +0 -152
- package/src/common/crypto.ts +0 -60
- package/src/common/debug.ts +0 -144
- package/src/common/deferred.ts +0 -78
- package/src/common/diagnostic.ts +0 -191
- package/src/common/diagnosticRules.ts +0 -80
- package/src/common/diagnosticSink.ts +0 -150
- package/src/common/editAction.ts +0 -46
- package/src/common/extensibility.ts +0 -35
- package/src/common/extensions.ts +0 -21
- package/src/common/fileBasedCancellationUtils.ts +0 -274
- package/src/common/fileSystem.ts +0 -152
- package/src/common/fullAccessHost.ts +0 -203
- package/src/common/host.ts +0 -49
- package/src/common/logTracker.ts +0 -157
- package/src/common/lspUtils.ts +0 -18
- package/src/common/memUtils.ts +0 -36
- package/src/common/pathConsts.ts +0 -16
- package/src/common/pathUtils.ts +0 -987
- package/src/common/positionUtils.ts +0 -78
- package/src/common/progressReporter.ts +0 -53
- package/src/common/pythonVersion.ts +0 -74
- package/src/common/realFileSystem.ts +0 -433
- package/src/common/stringUtils.ts +0 -161
- package/src/common/textEditUtils.ts +0 -31
- package/src/common/textRange.ts +0 -225
- package/src/common/textRangeCollection.ts +0 -122
- package/src/common/timing.ts +0 -105
- package/src/common/uriParser.ts +0 -26
- package/src/common/workspaceEditUtils.ts +0 -100
- package/src/languageServerBase.ts +0 -1556
- package/src/languageService/analyzerServiceExecutor.ts +0 -144
- package/src/languageService/autoImporter.ts +0 -926
- package/src/languageService/callHierarchyProvider.ts +0 -476
- package/src/languageService/codeActionProvider.ts +0 -85
- package/src/languageService/completionProvider.ts +0 -3103
- package/src/languageService/definitionProvider.ts +0 -235
- package/src/languageService/documentHighlightProvider.ts +0 -61
- package/src/languageService/documentSymbolCollector.ts +0 -466
- package/src/languageService/documentSymbolProvider.ts +0 -466
- package/src/languageService/hoverProvider.ts +0 -499
- package/src/languageService/importAdder.ts +0 -437
- package/src/languageService/importSorter.ts +0 -196
- package/src/languageService/indentationUtils.ts +0 -813
- package/src/languageService/quickActions.ts +0 -113
- package/src/languageService/referencesProvider.ts +0 -316
- package/src/languageService/renameModuleProvider.ts +0 -1400
- package/src/languageService/signatureHelpProvider.ts +0 -227
- package/src/languageService/tooltipUtils.ts +0 -147
- package/src/localization/localize.ts +0 -1272
- package/src/localization/package.nls.de.json +0 -3
- package/src/localization/package.nls.en-us.json +0 -636
- package/src/localization/package.nls.es.json +0 -3
- package/src/localization/package.nls.fr.json +0 -3
- package/src/localization/package.nls.ja.json +0 -3
- package/src/localization/package.nls.ru.json +0 -3
- package/src/localization/package.nls.zh-cn.json +0 -3
- package/src/localization/package.nls.zh-tw.json +0 -3
- package/src/nodeMain.ts +0 -21
- package/src/nodeServer.ts +0 -30
- package/src/parser/characterStream.ts +0 -145
- package/src/parser/characters.ts +0 -285
- package/src/parser/parseNodes.ts +0 -2338
- package/src/parser/parser.ts +0 -4859
- package/src/parser/stringTokenUtils.ts +0 -564
- package/src/parser/tokenizer.ts +0 -1379
- package/src/parser/tokenizerTypes.ts +0 -420
- package/src/parser/unicode.ts +0 -2917
- package/src/pyright.ts +0 -824
- package/src/pyrightFileSystem.ts +0 -237
- package/src/readonlyAugmentedFileSystem.ts +0 -204
- package/src/server.ts +0 -298
- package/src/tests/chainedSourceFiles.test.ts +0 -183
- package/src/tests/checker.test.ts +0 -445
- package/src/tests/collectionUtils.test.ts +0 -178
- package/src/tests/common.test.ts +0 -117
- package/src/tests/config.test.ts +0 -291
- package/src/tests/debug.test.ts +0 -120
- package/src/tests/deferred.test.ts +0 -54
- package/src/tests/diagnosticOverrides.test.ts +0 -104
- package/src/tests/docStringConversion.test.ts +0 -823
- package/src/tests/docStringUtils.test.ts +0 -57
- package/src/tests/documentSymbolCollector.test.ts +0 -735
- package/src/tests/filesystem.test.ts +0 -215
- package/src/tests/fourSlashParser.test.ts +0 -330
- package/src/tests/fourSlashRunner.test.ts +0 -36
- package/src/tests/fourslash/completions.autoimport.Lib.Found.Type.fourslash.ts +0 -33
- package/src/tests/fourslash/completions.autoimport.Lib.Found.duplication.fourslash.ts +0 -47
- package/src/tests/fourslash/completions.autoimport.Lib.NotFound.fourslash.ts +0 -14
- package/src/tests/fourslash/completions.autoimport.disabled.fourslash.ts +0 -25
- package/src/tests/fourslash/completions.autoimport.duplicates.fourslash.ts +0 -36
- package/src/tests/fourslash/completions.autoimport.fourslash.ts +0 -29
- package/src/tests/fourslash/completions.autoimport.fromImport.fourslash.ts +0 -130
- package/src/tests/fourslash/completions.autoimport.plainText.fourslash.ts +0 -29
- package/src/tests/fourslash/completions.autoimport.shadow.fourslash.ts +0 -48
- package/src/tests/fourslash/completions.autoimport.submodule.fourslash.ts +0 -31
- package/src/tests/fourslash/completions.autoimport.topLevel.fourslash.ts +0 -57
- package/src/tests/fourslash/completions.builtinDocstrings.fourslash.ts +0 -117
- package/src/tests/fourslash/completions.builtinOverride.fourslash.ts +0 -22
- package/src/tests/fourslash/completions.call.stringLiteral.fourslash.ts +0 -55
- package/src/tests/fourslash/completions.call.typedDict.fourslash.ts +0 -247
- package/src/tests/fourslash/completions.call.typedDict.list.fourslash.ts +0 -163
- package/src/tests/fourslash/completions.call.typedDict.states.fourslash.ts +0 -135
- package/src/tests/fourslash/completions.classVariable.fourslash.ts +0 -37
- package/src/tests/fourslash/completions.comment.fourslash.ts +0 -28
- package/src/tests/fourslash/completions.declNames.class.fourslash.ts +0 -35
- package/src/tests/fourslash/completions.declNames.exception.fourslash.ts +0 -18
- package/src/tests/fourslash/completions.declNames.for.fourslash.ts +0 -30
- package/src/tests/fourslash/completions.declNames.importAlias.fourslash.ts +0 -15
- package/src/tests/fourslash/completions.declNames.lambda.fourslash.ts +0 -45
- package/src/tests/fourslash/completions.declNames.method.fourslash.ts +0 -71
- package/src/tests/fourslash/completions.declNames.overload.fourslash.ts +0 -156
- package/src/tests/fourslash/completions.declNames.topLevelOverload.fourslash.ts +0 -141
- package/src/tests/fourslash/completions.dictionary.keys.complex.fourslash.ts +0 -76
- package/src/tests/fourslash/completions.dictionary.keys.expression.fourslash.ts +0 -63
- package/src/tests/fourslash/completions.dictionary.keys.literalTypes.fourslash.ts +0 -161
- package/src/tests/fourslash/completions.dictionary.keys.simple.fourslash.ts +0 -151
- package/src/tests/fourslash/completions.dictionary.keys.stringLiterals.fourslash.ts +0 -116
- package/src/tests/fourslash/completions.dictionary.keys.symbols.fourslash.ts +0 -53
- package/src/tests/fourslash/completions.enums.members.fourslash.ts +0 -44
- package/src/tests/fourslash/completions.excluded.fourslash.ts +0 -12
- package/src/tests/fourslash/completions.fourslash.ts +0 -51
- package/src/tests/fourslash/completions.fstring.fourslash.ts +0 -33
- package/src/tests/fourslash/completions.fstring.stringLiteral.fourslash.ts +0 -96
- package/src/tests/fourslash/completions.fuzzyMatching.fourslash.ts +0 -30
- package/src/tests/fourslash/completions.importDunderNames.fourslash.ts +0 -31
- package/src/tests/fourslash/completions.importSubmodule.fourslash.ts +0 -26
- package/src/tests/fourslash/completions.importsDuplicates.fourslash.ts +0 -23
- package/src/tests/fourslash/completions.inList.fourslash.ts +0 -33
- package/src/tests/fourslash/completions.included.fourslash.ts +0 -12
- package/src/tests/fourslash/completions.indexer.keys.getitem.fourslash.ts +0 -57
- package/src/tests/fourslash/completions.inherited.function.docFromStub.fourslash.ts +0 -28
- package/src/tests/fourslash/completions.inherited.overload.docFromScrWithStub.fourslash.ts +0 -52
- package/src/tests/fourslash/completions.inherited.overload.docFromStub.fourslash.ts +0 -53
- package/src/tests/fourslash/completions.inherited.property.docFromSrc.fourslash.ts +0 -104
- package/src/tests/fourslash/completions.inherited.property.docFromStub.fourslash.ts +0 -108
- package/src/tests/fourslash/completions.keywords.pythonVersion.fourslash.ts +0 -72
- package/src/tests/fourslash/completions.libCodeAndStub.fourslash.ts +0 -79
- package/src/tests/fourslash/completions.libCodeNoStub.fourslash.ts +0 -69
- package/src/tests/fourslash/completions.libStub.fourslash.ts +0 -69
- package/src/tests/fourslash/completions.literals.fourslash.ts +0 -33
- package/src/tests/fourslash/completions.localCode.fourslash.ts +0 -74
- package/src/tests/fourslash/completions.moduleContext.UnknownMemberOnInstance.fourslash.ts +0 -17
- package/src/tests/fourslash/completions.moduleContext.UnknownStaticFunctionOnClass.fourslash.ts +0 -57
- package/src/tests/fourslash/completions.moduleContext.libCodeNoStub.fourslash.ts +0 -30
- package/src/tests/fourslash/completions.overloads.fourslash.ts +0 -40
- package/src/tests/fourslash/completions.override.default.fourslash.ts +0 -47
- package/src/tests/fourslash/completions.override.default.importStub.ts +0 -50
- package/src/tests/fourslash/completions.override.default.imported.fourslash.ts +0 -61
- package/src/tests/fourslash/completions.override.default.stub.fourslash.ts +0 -47
- package/src/tests/fourslash/completions.override.fourslash.ts +0 -47
- package/src/tests/fourslash/completions.override.property.fourslash.ts +0 -31
- package/src/tests/fourslash/completions.override.property.stub.fourslash.ts +0 -31
- package/src/tests/fourslash/completions.override.staticAndClassmethod.fourslash.ts +0 -64
- package/src/tests/fourslash/completions.override.stub.fourslash.ts +0 -47
- package/src/tests/fourslash/completions.override2.fourslash.ts +0 -71
- package/src/tests/fourslash/completions.parameters.fourslash.ts +0 -23
- package/src/tests/fourslash/completions.params.fourslash.ts +0 -24
- package/src/tests/fourslash/completions.parentFolder.fourslash.ts +0 -17
- package/src/tests/fourslash/completions.parentFolders.fourslash.ts +0 -77
- package/src/tests/fourslash/completions.plainText.fourslash.ts +0 -34
- package/src/tests/fourslash/completions.private.fourslash.ts +0 -64
- package/src/tests/fourslash/completions.property.fourslash.ts +0 -28
- package/src/tests/fourslash/completions.propertyDocStrings.fourslash.ts +0 -64
- package/src/tests/fourslash/completions.self.fourslash.ts +0 -43
- package/src/tests/fourslash/completions.stringLiteral.escape.fourslash.ts +0 -71
- package/src/tests/fourslash/completions.stringLiteral.fourslash.ts +0 -79
- package/src/tests/fourslash/completions.typeAlias.fourslash.ts +0 -29
- package/src/tests/fourslash/completions.typeshed.fourslash.ts +0 -9
- package/src/tests/fourslash/completions.vardecls.fourslash.ts +0 -40
- package/src/tests/fourslash/completions.variableDocStrings.fourslash.ts +0 -69
- package/src/tests/fourslash/completions.wildcardimports.fourslash.ts +0 -116
- package/src/tests/fourslash/completions.with.fourslash.ts +0 -26
- package/src/tests/fourslash/diagnostics.missingModuleSource.fourslash.ts +0 -69
- package/src/tests/fourslash/findDefinitions.builtinClass.fourslash.ts +0 -29
- package/src/tests/fourslash/findDefinitions.classes.fourslash.ts +0 -101
- package/src/tests/fourslash/findDefinitions.definitionFilter.preferSource.fourslash.ts +0 -29
- package/src/tests/fourslash/findDefinitions.definitionFilter.preferSource.onlyStubs.ts +0 -24
- package/src/tests/fourslash/findDefinitions.definitionFilter.preferStub.fourslash.ts +0 -29
- package/src/tests/fourslash/findDefinitions.definitionFilter.preferStub.onlySource.fourslash.ts +0 -25
- package/src/tests/fourslash/findDefinitions.fields.fourslash.ts +0 -131
- package/src/tests/fourslash/findDefinitions.functions.fourslash.ts +0 -87
- package/src/tests/fourslash/findDefinitions.methods.fourslash.ts +0 -162
- package/src/tests/fourslash/findDefinitions.modules.fourslash.ts +0 -50
- package/src/tests/fourslash/findDefinitions.namespaceImport.fourslash.ts +0 -40
- package/src/tests/fourslash/findDefinitions.overloads.fourslash.ts +0 -223
- package/src/tests/fourslash/findDefinitions.parameters.fourslash.ts +0 -162
- package/src/tests/fourslash/findDefinitions.sourceAndStub.function.fourslash.ts +0 -26
- package/src/tests/fourslash/findDefinitions.sourceAndStub.innerClass.fourslash.ts +0 -32
- package/src/tests/fourslash/findDefinitions.sourceAndStub.innerClassMethod.fourslash.ts +0 -33
- package/src/tests/fourslash/findDefinitions.sourceAndStub.outerClass.fourslash.ts +0 -28
- package/src/tests/fourslash/findDefinitions.sourceAndStub.outerClassMethod.fourslash.ts +0 -29
- package/src/tests/fourslash/findDefinitions.sourceAndStub.outerClassPropertyReadOnly.fourslash.ts +0 -31
- package/src/tests/fourslash/findDefinitions.sourceAndStub.outerClassPropertyReadWrite.fourslash.ts +0 -36
- package/src/tests/fourslash/findDefinitions.sourceOnly.class.fourslash.ts +0 -29
- package/src/tests/fourslash/findDefinitions.sourceOnly.function1.fourslash.ts +0 -28
- package/src/tests/fourslash/findDefinitions.sourceOnly.function2.fourslash.ts +0 -28
- package/src/tests/fourslash/findDefinitions.sourceOnly.relativeImport1.fourslash.ts +0 -22
- package/src/tests/fourslash/findDefinitions.sourceOnly.relativeImport2.fourslash.ts +0 -22
- package/src/tests/fourslash/findDefinitions.stubOnly.fourslash.ts +0 -61
- package/src/tests/fourslash/findDefinitions.stubPackages.fourslash.ts +0 -71
- package/src/tests/fourslash/findDefinitions.typedDict.keys.fourslash.ts +0 -99
- package/src/tests/fourslash/findDefinitions.variables.fourslash.ts +0 -88
- package/src/tests/fourslash/findDefinitions.wildcardimports.fourslash.ts +0 -117
- package/src/tests/fourslash/findTypeDefinitions.builtinClass.fourslash.ts +0 -26
- package/src/tests/fourslash/findTypeDefinitions.classes.fourslash.ts +0 -72
- package/src/tests/fourslash/findTypeDefinitions.unions.fourslash.ts +0 -26
- package/src/tests/fourslash/findallreferences.classPropertyReadWrite.ts +0 -39
- package/src/tests/fourslash/findallreferences.fourslash.ts +0 -34
- package/src/tests/fourslash/findallreferences.importalias.fourslash.ts +0 -34
- package/src/tests/fourslash/findallreferences.invokedFromLibrary.fourslash.ts +0 -52
- package/src/tests/fourslash/findallreferences.module.nested.fourslash.ts +0 -67
- package/src/tests/fourslash/findallreferences.modules.duplicated.fourslash.ts +0 -59
- package/src/tests/fourslash/findallreferences.modules.fourslash.ts +0 -46
- package/src/tests/fourslash/findallreferences.modules.shadow.fourslash.ts +0 -95
- package/src/tests/fourslash/findallreferences.openFiles.fourslash.ts +0 -37
- package/src/tests/fourslash/findallreferences.parameter.fourslash.ts +0 -24
- package/src/tests/fourslash/findallreferences.sourceAndStub.class.fourslash.ts +0 -33
- package/src/tests/fourslash/findallreferences.sourceAndStub.classMethod.fourslash.ts +0 -37
- package/src/tests/fourslash/findallreferences.sourceAndStub.classPropertyReadOnly.fourslash.ts +0 -40
- package/src/tests/fourslash/findallreferences.sourceAndStub.classPropertyReadWrite.fourslash.skip.ts +0 -48
- package/src/tests/fourslash/findallreferences.sourceAndStub.function.fourslash.ts +0 -35
- package/src/tests/fourslash/findallreferences.variable.fourslash.ts +0 -24
- package/src/tests/fourslash/fourslash.ts +0 -411
- package/src/tests/fourslash/highlightreferences.attributes.fourslash.ts +0 -31
- package/src/tests/fourslash/hover.builtinDocstrings.fourslash.ts +0 -64
- package/src/tests/fourslash/hover.classNoInit.fourslash.ts +0 -14
- package/src/tests/fourslash/hover.docFromScr.stringFormat.fourslash.ts +0 -52
- package/src/tests/fourslash/hover.docFromSrc.fourslash.ts +0 -83
- package/src/tests/fourslash/hover.docFromSrc.pkg-vs-module1.fourslash.ts +0 -26
- package/src/tests/fourslash/hover.docFromSrc.pkg-vs-module2.fourslash.ts +0 -26
- package/src/tests/fourslash/hover.docFromSrc.relativeImport1.fourslash.ts +0 -23
- package/src/tests/fourslash/hover.docFromSrc.relativeImport2.fourslash.ts +0 -22
- package/src/tests/fourslash/hover.docFromSrc.relativeImport3.fourslash.ts +0 -46
- package/src/tests/fourslash/hover.docFromSrc.stubs-package.fourslash.ts +0 -29
- package/src/tests/fourslash/hover.docFromSrc.typeshed.fourslash.ts +0 -20
- package/src/tests/fourslash/hover.docstring.links.fourslash.ts +0 -12
- package/src/tests/fourslash/hover.docstring.overloads.fourslash.ts +0 -41
- package/src/tests/fourslash/hover.docstring.split.fourslash.ts +0 -24
- package/src/tests/fourslash/hover.fourslash.ts +0 -20
- package/src/tests/fourslash/hover.import.django.view.fourslash.ts +0 -27
- package/src/tests/fourslash/hover.import.fourslash.ts +0 -11
- package/src/tests/fourslash/hover.inherited.docFromSrc.fourslash.ts +0 -63
- package/src/tests/fourslash/hover.inherited.docFromSrcWithStub.fourslash.ts +0 -54
- package/src/tests/fourslash/hover.inherited.docFromStub.fourslash.ts +0 -42
- package/src/tests/fourslash/hover.inherited.overload.docFromSrcWithStub.fourslash.ts +0 -46
- package/src/tests/fourslash/hover.inherited.overload.docFromStub.fourslash.ts +0 -47
- package/src/tests/fourslash/hover.inherited.property.docFromSrcWithStub.fourslash.ts +0 -87
- package/src/tests/fourslash/hover.inherited.property.docFromStub.fourslash.ts +0 -91
- package/src/tests/fourslash/hover.init.fourslash.ts +0 -39
- package/src/tests/fourslash/hover.libCodeAndStub.fourslash.ts +0 -52
- package/src/tests/fourslash/hover.libCodeNoStub.fourslash.ts +0 -41
- package/src/tests/fourslash/hover.libStub.fourslash.ts +0 -41
- package/src/tests/fourslash/hover.optionalAliasParameter.fourslash.ts +0 -14
- package/src/tests/fourslash/hover.plainText.fourslash.ts +0 -21
- package/src/tests/fourslash/hover.typedDict.key.fourslash.ts +0 -51
- package/src/tests/fourslash/hover.variable.docString.fourslash.ts +0 -41
- package/src/tests/fourslash/hover.wildcardimports.fourslash.ts +0 -80
- package/src/tests/fourslash/import.publicSymbols.fourslash.ts +0 -65
- package/src/tests/fourslash/import.pytyped.dunderAll.fourslash.ts +0 -77
- package/src/tests/fourslash/import.pytyped.privateSymbols.fourslash.ts +0 -65
- package/src/tests/fourslash/import.pytyped.typeCheckingBasic.fourslash.ts +0 -38
- package/src/tests/fourslash/import.pytyped.typeCheckingOff.fourslash.ts +0 -38
- package/src/tests/fourslash/import.wildcard.fourslash.ts +0 -27
- package/src/tests/fourslash/importnotresolved.fourslash.ts +0 -13
- package/src/tests/fourslash/missingModuleSource.disablingInStrictMode.fourslash.ts +0 -20
- package/src/tests/fourslash/missingModuleSource.fourslash.ts +0 -14
- package/src/tests/fourslash/missingTypeStub.codeAction.fourslash.ts +0 -33
- package/src/tests/fourslash/missingTypeStub.command.multipart.fourslash.ts +0 -48
- package/src/tests/fourslash/missingTypeStub.command.singlefile.fourslash.ts +0 -38
- package/src/tests/fourslash/missingTypeStub.command.singlepart.fourslash.ts +0 -38
- package/src/tests/fourslash/missingTypeStub.fourslash.ts +0 -20
- package/src/tests/fourslash/missingTypeStub.invokeCodeAction.fourslash.ts +0 -36
- package/src/tests/fourslash/noerrors.fourslash.ts +0 -9
- package/src/tests/fourslash/orderImports1.command.fourslash.ts +0 -20
- package/src/tests/fourslash/orderImports2.command.fourslash.ts +0 -24
- package/src/tests/fourslash/rename.externallyHidden.fourslash.ts +0 -24
- package/src/tests/fourslash/rename.externallyHidden.params.fourslash.ts +0 -24
- package/src/tests/fourslash/rename.fourslash.ts +0 -29
- package/src/tests/fourslash/rename.library.fourslash.ts +0 -29
- package/src/tests/fourslash/rename.library.sourceAndStub.fourslash.ts +0 -33
- package/src/tests/fourslash/rename.multipleDecl.fourslash.ts +0 -21
- package/src/tests/fourslash/rename.sourceAndStub.fourslash.ts +0 -37
- package/src/tests/fourslash/rename.string.excluded.fourslash.ts +0 -43
- package/src/tests/fourslash/rename.string.fourslash.ts +0 -26
- package/src/tests/fourslash/signature.builtinDocstrings.fourslash.ts +0 -63
- package/src/tests/fourslash/signature.complicated.fourslash.ts +0 -100
- package/src/tests/fourslash/signature.cornercases.fourslash.ts +0 -23
- package/src/tests/fourslash/signature.docstrings.fourslash.ts +0 -43
- package/src/tests/fourslash/signature.docstrings.overloaded.fourslash.ts +0 -50
- package/src/tests/fourslash/signature.docstrings.wildcardimports.fourslash.ts +0 -122
- package/src/tests/fourslash/signature.overload.fourslash.ts +0 -64
- package/src/tests/fourslash/signature.simple.fourslash.ts +0 -104
- package/src/tests/harness/fourslash/fourSlashParser.ts +0 -441
- package/src/tests/harness/fourslash/fourSlashTypes.ts +0 -134
- package/src/tests/harness/fourslash/runner.ts +0 -99
- package/src/tests/harness/fourslash/testLanguageService.ts +0 -142
- package/src/tests/harness/fourslash/testState.Consts.ts +0 -29
- package/src/tests/harness/fourslash/testState.ts +0 -1993
- package/src/tests/harness/testAccessHost.ts +0 -23
- package/src/tests/harness/testHost.ts +0 -177
- package/src/tests/harness/utils.ts +0 -355
- package/src/tests/harness/vfs/factory.ts +0 -199
- package/src/tests/harness/vfs/filesystem.ts +0 -1893
- package/src/tests/harness/vfs/pathValidation.ts +0 -154
- package/src/tests/importAdder.test.ts +0 -1361
- package/src/tests/importResolver.test.ts +0 -534
- package/src/tests/importStatementUtils.test.ts +0 -526
- package/src/tests/indentationUtils.ptvs.test.ts +0 -388
- package/src/tests/indentationUtils.reindent.test.ts +0 -403
- package/src/tests/indentationUtils.test.ts +0 -455
- package/src/tests/ipythonMode.test.ts +0 -365
- package/src/tests/localizer.test.ts +0 -47
- package/src/tests/parseTreeUtils.test.ts +0 -281
- package/src/tests/parser.test.ts +0 -99
- package/src/tests/pathUtils.test.ts +0 -321
- package/src/tests/pyrightFileSystem.test.ts +0 -183
- package/src/tests/renameModule.folder.test.ts +0 -261
- package/src/tests/renameModule.fromImports.test.ts +0 -952
- package/src/tests/renameModule.imports.test.ts +0 -450
- package/src/tests/renameModule.misc.test.ts +0 -765
- package/src/tests/renameModule.relativePath.test.ts +0 -277
- package/src/tests/renameModuleTestUtils.ts +0 -210
- package/src/tests/samples/abstractClass1.py +0 -51
- package/src/tests/samples/abstractClass2.py +0 -25
- package/src/tests/samples/abstractClass3.py +0 -30
- package/src/tests/samples/abstractClass4.py +0 -30
- package/src/tests/samples/abstractClass5.py +0 -42
- package/src/tests/samples/abstractClass6.py +0 -25
- package/src/tests/samples/abstractClass7.py +0 -22
- package/src/tests/samples/abstractClass8.py +0 -27
- package/src/tests/samples/annotated1.py +0 -72
- package/src/tests/samples/annotatedVar1.py +0 -22
- package/src/tests/samples/annotatedVar2.py +0 -28
- package/src/tests/samples/annotatedVar3.py +0 -54
- package/src/tests/samples/annotatedVar4.py +0 -24
- package/src/tests/samples/annotatedVar5.py +0 -37
- package/src/tests/samples/annotatedVar6.py +0 -22
- package/src/tests/samples/annotatedVar7.py +0 -13
- package/src/tests/samples/annotatedVar8.py +0 -29
- package/src/tests/samples/annotations1.py +0 -98
- package/src/tests/samples/annotations2.py +0 -33
- package/src/tests/samples/annotations3.py +0 -43
- package/src/tests/samples/annotations4.py +0 -54
- package/src/tests/samples/annotations5.py +0 -9
- package/src/tests/samples/annotations6.py +0 -29
- package/src/tests/samples/assert1.py +0 -22
- package/src/tests/samples/assertType1.py +0 -46
- package/src/tests/samples/assignment1.py +0 -58
- package/src/tests/samples/assignment10.py +0 -14
- package/src/tests/samples/assignment2.py +0 -40
- package/src/tests/samples/assignment3.py +0 -46
- package/src/tests/samples/assignment4.py +0 -20
- package/src/tests/samples/assignment5.py +0 -16
- package/src/tests/samples/assignment6.py +0 -25
- package/src/tests/samples/assignment7.py +0 -13
- package/src/tests/samples/assignment8.py +0 -40
- package/src/tests/samples/assignment9.py +0 -25
- package/src/tests/samples/assignmentExpr1.py +0 -23
- package/src/tests/samples/assignmentExpr2.py +0 -49
- package/src/tests/samples/assignmentExpr3.py +0 -27
- package/src/tests/samples/assignmentExpr4.py +0 -49
- package/src/tests/samples/assignmentExpr5.py +0 -15
- package/src/tests/samples/assignmentExpr6.py +0 -10
- package/src/tests/samples/assignmentExpr7.py +0 -13
- package/src/tests/samples/assignmentExpr8.py +0 -11
- package/src/tests/samples/assignmentExpr9.py +0 -28
- package/src/tests/samples/async1.py +0 -49
- package/src/tests/samples/augmentedAssignment1.py +0 -62
- package/src/tests/samples/augmentedAssignment2.py +0 -23
- package/src/tests/samples/augmentedAssignment3.py +0 -8
- package/src/tests/samples/await1.py +0 -23
- package/src/tests/samples/await2.py +0 -25
- package/src/tests/samples/badToken1.py +0 -6
- package/src/tests/samples/builtins1.py +0 -2
- package/src/tests/samples/call1.py +0 -93
- package/src/tests/samples/call2.py +0 -118
- package/src/tests/samples/call3.py +0 -168
- package/src/tests/samples/call4.py +0 -13
- package/src/tests/samples/call5.py +0 -95
- package/src/tests/samples/call6.py +0 -35
- package/src/tests/samples/call7.py +0 -57
- package/src/tests/samples/callSite1.py +0 -6
- package/src/tests/samples/callSite2.py +0 -12
- package/src/tests/samples/callable1.py +0 -53
- package/src/tests/samples/callable2.py +0 -58
- package/src/tests/samples/callable3.py +0 -28
- package/src/tests/samples/callable4.py +0 -32
- package/src/tests/samples/callable5.py +0 -40
- package/src/tests/samples/callable6.py +0 -83
- package/src/tests/samples/callbackProtocol1.py +0 -119
- package/src/tests/samples/callbackProtocol2.py +0 -29
- package/src/tests/samples/callbackProtocol3.py +0 -23
- package/src/tests/samples/callbackProtocol4.py +0 -73
- package/src/tests/samples/callbackProtocol5.py +0 -66
- package/src/tests/samples/callbackProtocol6.py +0 -66
- package/src/tests/samples/callbackProtocol7.py +0 -16
- package/src/tests/samples/callbackProtocol8.py +0 -16
- package/src/tests/samples/capturedVariable1.py +0 -106
- package/src/tests/samples/circular1.py +0 -16
- package/src/tests/samples/circular2.py +0 -38
- package/src/tests/samples/circularBaseClass.py +0 -17
- package/src/tests/samples/classGetItem1.py +0 -30
- package/src/tests/samples/classVar1.py +0 -41
- package/src/tests/samples/classVar2.py +0 -29
- package/src/tests/samples/classVar3.py +0 -46
- package/src/tests/samples/classVar4.py +0 -36
- package/src/tests/samples/classes1.py +0 -50
- package/src/tests/samples/classes3.py +0 -48
- package/src/tests/samples/classes4.py +0 -15
- package/src/tests/samples/classes5.py +0 -260
- package/src/tests/samples/classes6.py +0 -16
- package/src/tests/samples/classes7.py +0 -18
- package/src/tests/samples/classes8.py +0 -47
- package/src/tests/samples/codeFlow1.py +0 -65
- package/src/tests/samples/codeFlow2.py +0 -26
- package/src/tests/samples/codeFlow3.py +0 -8
- package/src/tests/samples/codeFlow4.py +0 -133
- package/src/tests/samples/codeFlow5.py +0 -10
- package/src/tests/samples/codeFlow6.py +0 -35
- package/src/tests/samples/codeFlow7.py +0 -35
- package/src/tests/samples/comparison1.py +0 -77
- package/src/tests/samples/comparison2.py +0 -37
- package/src/tests/samples/complex1.py +0 -17
- package/src/tests/samples/constant1.py +0 -40
- package/src/tests/samples/constants1.py +0 -64
- package/src/tests/samples/constructor1.py +0 -30
- package/src/tests/samples/constructor10.py +0 -19
- package/src/tests/samples/constructor11.py +0 -32
- package/src/tests/samples/constructor12.py +0 -20
- package/src/tests/samples/constructor13.py +0 -15
- package/src/tests/samples/constructor2.py +0 -180
- package/src/tests/samples/constructor3.py +0 -18
- package/src/tests/samples/constructor4.py +0 -25
- package/src/tests/samples/constructor5.py +0 -25
- package/src/tests/samples/constructor6.py +0 -70
- package/src/tests/samples/constructor7.py +0 -11
- package/src/tests/samples/constructor8.py +0 -99
- package/src/tests/samples/constructor9.py +0 -19
- package/src/tests/samples/coroutines1.py +0 -60
- package/src/tests/samples/coroutines2.py +0 -20
- package/src/tests/samples/coroutines3.py +0 -37
- package/src/tests/samples/dataclass1.py +0 -44
- package/src/tests/samples/dataclass10.py +0 -45
- package/src/tests/samples/dataclass11.py +0 -53
- package/src/tests/samples/dataclass12.py +0 -32
- package/src/tests/samples/dataclass13.py +0 -42
- package/src/tests/samples/dataclass14.py +0 -38
- package/src/tests/samples/dataclass15.py +0 -58
- package/src/tests/samples/dataclass16.py +0 -21
- package/src/tests/samples/dataclass17.py +0 -46
- package/src/tests/samples/dataclass18.py +0 -57
- package/src/tests/samples/dataclass19.py +0 -44
- package/src/tests/samples/dataclass2.py +0 -30
- package/src/tests/samples/dataclass20.py +0 -71
- package/src/tests/samples/dataclass21.py +0 -13
- package/src/tests/samples/dataclass3.py +0 -18
- package/src/tests/samples/dataclass4.py +0 -64
- package/src/tests/samples/dataclass5.py +0 -81
- package/src/tests/samples/dataclass6.py +0 -29
- package/src/tests/samples/dataclass7.py +0 -93
- package/src/tests/samples/dataclass8.py +0 -22
- package/src/tests/samples/dataclass9.py +0 -24
- package/src/tests/samples/dataclassPostInit1.py +0 -61
- package/src/tests/samples/dataclassTransform1.py +0 -71
- package/src/tests/samples/dataclassTransform2.py +0 -78
- package/src/tests/samples/dataclassTransform3.py +0 -83
- package/src/tests/samples/dataclassTransform4.py +0 -87
- package/src/tests/samples/decorator1.py +0 -20
- package/src/tests/samples/decorator2.py +0 -31
- package/src/tests/samples/decorator3.py +0 -27
- package/src/tests/samples/decorator4.py +0 -37
- package/src/tests/samples/decorator5.py +0 -29
- package/src/tests/samples/decorator6.py +0 -34
- package/src/tests/samples/defaultInitializer1.py +0 -26
- package/src/tests/samples/deprecated1.py +0 -37
- package/src/tests/samples/descriptor1.py +0 -134
- package/src/tests/samples/descriptor2.py +0 -29
- package/src/tests/samples/dictionary1.py +0 -42
- package/src/tests/samples/dictionary2.py +0 -14
- package/src/tests/samples/dictionary3.py +0 -30
- package/src/tests/samples/dictionary4.py +0 -106
- package/src/tests/samples/dunderAll1.py +0 -32
- package/src/tests/samples/dunderAll2.py +0 -16
- package/src/tests/samples/dunderAll3.pyi +0 -16
- package/src/tests/samples/duplicateDeclaration1.py +0 -115
- package/src/tests/samples/duplicateDeclaration2.py +0 -45
- package/src/tests/samples/duplicateImports1.py +0 -13
- package/src/tests/samples/ellipsis1.pyi +0 -48
- package/src/tests/samples/emptyContainers1.py +0 -89
- package/src/tests/samples/enums1.py +0 -52
- package/src/tests/samples/enums2.py +0 -15
- package/src/tests/samples/enums3.py +0 -24
- package/src/tests/samples/enums4.py +0 -23
- package/src/tests/samples/enums5.py +0 -16
- package/src/tests/samples/enums6.py +0 -33
- package/src/tests/samples/enums7.py +0 -54
- package/src/tests/samples/enums8.py +0 -14
- package/src/tests/samples/expressions1.py +0 -54
- package/src/tests/samples/expressions2.py +0 -29
- package/src/tests/samples/expressions3.py +0 -26
- package/src/tests/samples/expressions4.py +0 -23
- package/src/tests/samples/expressions5.py +0 -70
- package/src/tests/samples/expressions6.py +0 -14
- package/src/tests/samples/expressions7.py +0 -40
- package/src/tests/samples/expressions8.py +0 -26
- package/src/tests/samples/expressions9.py +0 -14
- package/src/tests/samples/final1.py +0 -14
- package/src/tests/samples/final2.py +0 -86
- package/src/tests/samples/final3.py +0 -179
- package/src/tests/samples/final4.pyi +0 -21
- package/src/tests/samples/final5.py +0 -18
- package/src/tests/samples/forLoop1.py +0 -88
- package/src/tests/samples/forLoop2.py +0 -48
- package/src/tests/samples/fstring1.py +0 -66
- package/src/tests/samples/fstring2.py +0 -12
- package/src/tests/samples/fstring3.py +0 -35
- package/src/tests/samples/fstring4.py +0 -17
- package/src/tests/samples/fstring5.py +0 -17
- package/src/tests/samples/fstring6.py +0 -14
- package/src/tests/samples/function1.py +0 -12
- package/src/tests/samples/function10.py +0 -20
- package/src/tests/samples/function11.py +0 -36
- package/src/tests/samples/function2.py +0 -7
- package/src/tests/samples/function3.py +0 -82
- package/src/tests/samples/function4.py +0 -13
- package/src/tests/samples/function6.py +0 -24
- package/src/tests/samples/function7.py +0 -13
- package/src/tests/samples/function8.py +0 -38
- package/src/tests/samples/function9.py +0 -75
- package/src/tests/samples/functionAnnotation1.py +0 -61
- package/src/tests/samples/functionAnnotation2.py +0 -26
- package/src/tests/samples/functionAnnotation3.py +0 -37
- package/src/tests/samples/functionAnnotation4.py +0 -22
- package/src/tests/samples/functionMember1.py +0 -18
- package/src/tests/samples/functionMember2.py +0 -45
- package/src/tests/samples/generators1.py +0 -122
- package/src/tests/samples/generators10.py +0 -18
- package/src/tests/samples/generators11.py +0 -27
- package/src/tests/samples/generators12.py +0 -25
- package/src/tests/samples/generators13.py +0 -65
- package/src/tests/samples/generators14.py +0 -19
- package/src/tests/samples/generators15.py +0 -26
- package/src/tests/samples/generators2.py +0 -31
- package/src/tests/samples/generators3.py +0 -42
- package/src/tests/samples/generators4.py +0 -33
- package/src/tests/samples/generators5.py +0 -24
- package/src/tests/samples/generators6.py +0 -14
- package/src/tests/samples/generators7.py +0 -18
- package/src/tests/samples/generators8.py +0 -12
- package/src/tests/samples/generators9.py +0 -19
- package/src/tests/samples/generic1.py +0 -41
- package/src/tests/samples/genericTypes1.py +0 -12
- package/src/tests/samples/genericTypes10.py +0 -38
- package/src/tests/samples/genericTypes11.py +0 -26
- package/src/tests/samples/genericTypes12.py +0 -14
- package/src/tests/samples/genericTypes13.py +0 -27
- package/src/tests/samples/genericTypes14.py +0 -22
- package/src/tests/samples/genericTypes15.py +0 -13
- package/src/tests/samples/genericTypes16.py +0 -15
- package/src/tests/samples/genericTypes17.py +0 -24
- package/src/tests/samples/genericTypes18.py +0 -135
- package/src/tests/samples/genericTypes19.py +0 -32
- package/src/tests/samples/genericTypes2.py +0 -40
- package/src/tests/samples/genericTypes20.py +0 -44
- package/src/tests/samples/genericTypes21.py +0 -29
- package/src/tests/samples/genericTypes22.py +0 -23
- package/src/tests/samples/genericTypes23.py +0 -27
- package/src/tests/samples/genericTypes24.py +0 -32
- package/src/tests/samples/genericTypes25.py +0 -37
- package/src/tests/samples/genericTypes26.py +0 -31
- package/src/tests/samples/genericTypes27.py +0 -14
- package/src/tests/samples/genericTypes28.py +0 -51
- package/src/tests/samples/genericTypes29.py +0 -24
- package/src/tests/samples/genericTypes3.py +0 -13
- package/src/tests/samples/genericTypes30.py +0 -32
- package/src/tests/samples/genericTypes31.py +0 -30
- package/src/tests/samples/genericTypes32.py +0 -25
- package/src/tests/samples/genericTypes33.py +0 -46
- package/src/tests/samples/genericTypes34.py +0 -54
- package/src/tests/samples/genericTypes35.py +0 -45
- package/src/tests/samples/genericTypes36.py +0 -35
- package/src/tests/samples/genericTypes37.py +0 -26
- package/src/tests/samples/genericTypes38.py +0 -15
- package/src/tests/samples/genericTypes39.py +0 -82
- package/src/tests/samples/genericTypes4.py +0 -37
- package/src/tests/samples/genericTypes40.py +0 -38
- package/src/tests/samples/genericTypes41.py +0 -16
- package/src/tests/samples/genericTypes42.py +0 -32
- package/src/tests/samples/genericTypes43.py +0 -24
- package/src/tests/samples/genericTypes44.py +0 -33
- package/src/tests/samples/genericTypes45.py +0 -43
- package/src/tests/samples/genericTypes46.py +0 -66
- package/src/tests/samples/genericTypes47.py +0 -39
- package/src/tests/samples/genericTypes48.py +0 -18
- package/src/tests/samples/genericTypes49.py +0 -41
- package/src/tests/samples/genericTypes5.py +0 -42
- package/src/tests/samples/genericTypes50.py +0 -62
- package/src/tests/samples/genericTypes51.py +0 -55
- package/src/tests/samples/genericTypes52.py +0 -34
- package/src/tests/samples/genericTypes53.py +0 -42
- package/src/tests/samples/genericTypes54.py +0 -31
- package/src/tests/samples/genericTypes55.py +0 -61
- package/src/tests/samples/genericTypes56.py +0 -19
- package/src/tests/samples/genericTypes57.py +0 -30
- package/src/tests/samples/genericTypes58.py +0 -52
- package/src/tests/samples/genericTypes59.py +0 -71
- package/src/tests/samples/genericTypes6.py +0 -49
- package/src/tests/samples/genericTypes60.py +0 -27
- package/src/tests/samples/genericTypes61.py +0 -34
- package/src/tests/samples/genericTypes62.py +0 -26
- package/src/tests/samples/genericTypes63.py +0 -32
- package/src/tests/samples/genericTypes64.py +0 -16
- package/src/tests/samples/genericTypes65.py +0 -15
- package/src/tests/samples/genericTypes66.py +0 -30
- package/src/tests/samples/genericTypes67.py +0 -57
- package/src/tests/samples/genericTypes68.py +0 -14
- package/src/tests/samples/genericTypes69.py +0 -45
- package/src/tests/samples/genericTypes7.py +0 -24
- package/src/tests/samples/genericTypes70.py +0 -31
- package/src/tests/samples/genericTypes71.py +0 -72
- package/src/tests/samples/genericTypes72.py +0 -24
- package/src/tests/samples/genericTypes73.py +0 -14
- package/src/tests/samples/genericTypes74.py +0 -23
- package/src/tests/samples/genericTypes75.py +0 -45
- package/src/tests/samples/genericTypes76.py +0 -107
- package/src/tests/samples/genericTypes77.py +0 -26
- package/src/tests/samples/genericTypes78.py +0 -23
- package/src/tests/samples/genericTypes79.py +0 -37
- package/src/tests/samples/genericTypes8.py +0 -26
- package/src/tests/samples/genericTypes80.py +0 -10
- package/src/tests/samples/genericTypes81.py +0 -32
- package/src/tests/samples/genericTypes82.py +0 -19
- package/src/tests/samples/genericTypes83.py +0 -28
- package/src/tests/samples/genericTypes84.py +0 -11
- package/src/tests/samples/genericTypes85.py +0 -20
- package/src/tests/samples/genericTypes86.py +0 -15
- package/src/tests/samples/genericTypes87.py +0 -21
- package/src/tests/samples/genericTypes88.py +0 -25
- package/src/tests/samples/genericTypes89.py +0 -17
- package/src/tests/samples/genericTypes9.py +0 -109
- package/src/tests/samples/genericTypes90.py +0 -12
- package/src/tests/samples/genericTypes91.py +0 -20
- package/src/tests/samples/import1.py +0 -7
- package/src/tests/samples/import10.py +0 -11
- package/src/tests/samples/import11.py +0 -11
- package/src/tests/samples/import12.py +0 -5
- package/src/tests/samples/import13.py +0 -6
- package/src/tests/samples/import14.py +0 -10
- package/src/tests/samples/import2.py +0 -27
- package/src/tests/samples/import3.py +0 -8
- package/src/tests/samples/import4.py +0 -15
- package/src/tests/samples/import5.py +0 -6
- package/src/tests/samples/import6.py +0 -17
- package/src/tests/samples/import7.py +0 -14
- package/src/tests/samples/import8.py +0 -7
- package/src/tests/samples/import9.py +0 -7
- package/src/tests/samples/inconsistentConstructor1.py +0 -23
- package/src/tests/samples/inconsistentSpaceTab1.py +0 -9
- package/src/tests/samples/inconsistentSpaceTab2.py +0 -10
- package/src/tests/samples/index1.py +0 -97
- package/src/tests/samples/inferredTypes1.py +0 -25
- package/src/tests/samples/initVar1.py +0 -23
- package/src/tests/samples/initsubclass1.py +0 -46
- package/src/tests/samples/initsubclass2.py +0 -16
- package/src/tests/samples/isinstance1.py +0 -57
- package/src/tests/samples/isinstance10.py +0 -15
- package/src/tests/samples/isinstance2.py +0 -28
- package/src/tests/samples/isinstance3.py +0 -59
- package/src/tests/samples/isinstance4.py +0 -57
- package/src/tests/samples/isinstance5.py +0 -8
- package/src/tests/samples/isinstance6.py +0 -19
- package/src/tests/samples/isinstance7.py +0 -42
- package/src/tests/samples/isinstance9.py +0 -26
- package/src/tests/samples/kwargsUnpack1.py +0 -116
- package/src/tests/samples/lambda1.py +0 -77
- package/src/tests/samples/lambda2.py +0 -26
- package/src/tests/samples/lambda3.py +0 -34
- package/src/tests/samples/lambda4.py +0 -70
- package/src/tests/samples/lambda5.py +0 -23
- package/src/tests/samples/lambda6.py +0 -15
- package/src/tests/samples/lines1.py +0 -14
- package/src/tests/samples/list1.py +0 -90
- package/src/tests/samples/listComprehension1.py +0 -45
- package/src/tests/samples/listComprehension2.py +0 -17
- package/src/tests/samples/listComprehension3.py +0 -12
- package/src/tests/samples/listComprehension4.py +0 -15
- package/src/tests/samples/listComprehension5.py +0 -20
- package/src/tests/samples/listComprehension6.py +0 -14
- package/src/tests/samples/listComprehension7.py +0 -15
- package/src/tests/samples/listComprehension8.py +0 -15
- package/src/tests/samples/literalString1.py +0 -43
- package/src/tests/samples/literals1.py +0 -48
- package/src/tests/samples/literals2.py +0 -30
- package/src/tests/samples/literals3.py +0 -40
- package/src/tests/samples/literals4.py +0 -10
- package/src/tests/samples/literals5.py +0 -28
- package/src/tests/samples/literals6.py +0 -90
- package/src/tests/samples/literals7.py +0 -38
- package/src/tests/samples/loops1.py +0 -45
- package/src/tests/samples/loops10.py +0 -11
- package/src/tests/samples/loops11.py +0 -22
- package/src/tests/samples/loops12.py +0 -18
- package/src/tests/samples/loops13.py +0 -13
- package/src/tests/samples/loops14.py +0 -22
- package/src/tests/samples/loops15.py +0 -22
- package/src/tests/samples/loops16.py +0 -322
- package/src/tests/samples/loops17.py +0 -9
- package/src/tests/samples/loops18.py +0 -23
- package/src/tests/samples/loops19.py +0 -14
- package/src/tests/samples/loops2.py +0 -25
- package/src/tests/samples/loops20.py +0 -41
- package/src/tests/samples/loops21.py +0 -18
- package/src/tests/samples/loops22.py +0 -21
- package/src/tests/samples/loops23.py +0 -16
- package/src/tests/samples/loops24.py +0 -12
- package/src/tests/samples/loops25.py +0 -45
- package/src/tests/samples/loops26.py +0 -12
- package/src/tests/samples/loops3.py +0 -11
- package/src/tests/samples/loops4.py +0 -17
- package/src/tests/samples/loops5.py +0 -14
- package/src/tests/samples/loops6.py +0 -25
- package/src/tests/samples/loops7.py +0 -14
- package/src/tests/samples/loops8.py +0 -17
- package/src/tests/samples/loops9.py +0 -25
- package/src/tests/samples/match1.py +0 -190
- package/src/tests/samples/match10.py +0 -67
- package/src/tests/samples/match2.py +0 -340
- package/src/tests/samples/match3.py +0 -364
- package/src/tests/samples/match4.py +0 -100
- package/src/tests/samples/match5.py +0 -70
- package/src/tests/samples/match6.py +0 -64
- package/src/tests/samples/match7.py +0 -65
- package/src/tests/samples/match8.py +0 -19
- package/src/tests/samples/match9.py +0 -53
- package/src/tests/samples/maxParseDepth1.py +0 -499
- package/src/tests/samples/maxParseDepth2.py +0 -16
- package/src/tests/samples/memberAccess1.py +0 -70
- package/src/tests/samples/memberAccess10.py +0 -65
- package/src/tests/samples/memberAccess11.py +0 -37
- package/src/tests/samples/memberAccess12.py +0 -36
- package/src/tests/samples/memberAccess13.py +0 -17
- package/src/tests/samples/memberAccess14.py +0 -63
- package/src/tests/samples/memberAccess15.py +0 -20
- package/src/tests/samples/memberAccess16.py +0 -12
- package/src/tests/samples/memberAccess17.py +0 -31
- package/src/tests/samples/memberAccess18.py +0 -31
- package/src/tests/samples/memberAccess2.py +0 -45
- package/src/tests/samples/memberAccess3.py +0 -64
- package/src/tests/samples/memberAccess4.py +0 -91
- package/src/tests/samples/memberAccess5.py +0 -26
- package/src/tests/samples/memberAccess6.py +0 -52
- package/src/tests/samples/memberAccess7.py +0 -36
- package/src/tests/samples/memberAccess8.py +0 -75
- package/src/tests/samples/memberAccess9.py +0 -11
- package/src/tests/samples/metaclass1.py +0 -7
- package/src/tests/samples/metaclass2.py +0 -35
- package/src/tests/samples/metaclass3.py +0 -43
- package/src/tests/samples/metaclass4.py +0 -26
- package/src/tests/samples/metaclass5.py +0 -33
- package/src/tests/samples/metaclass6.py +0 -15
- package/src/tests/samples/metaclass7.py +0 -38
- package/src/tests/samples/metaclass8.py +0 -20
- package/src/tests/samples/metaclass9.py +0 -77
- package/src/tests/samples/methodOverride1.py +0 -507
- package/src/tests/samples/methodOverride2.py +0 -74
- package/src/tests/samples/methodOverride3.py +0 -111
- package/src/tests/samples/methodOverride4.py +0 -36
- package/src/tests/samples/missingSuper1.py +0 -73
- package/src/tests/samples/module1.py +0 -13
- package/src/tests/samples/module2.py +0 -7
- package/src/tests/samples/mro1.py +0 -16
- package/src/tests/samples/mro2.py +0 -45
- package/src/tests/samples/mro3.py +0 -30
- package/src/tests/samples/mro4.py +0 -30
- package/src/tests/samples/nameBindings1.py +0 -48
- package/src/tests/samples/nameBindings2.py +0 -9
- package/src/tests/samples/nameBindings3.py +0 -36
- package/src/tests/samples/nameBindings4.py +0 -10
- package/src/tests/samples/nameBindings5.py +0 -53
- package/src/tests/samples/namedTuples1.py +0 -103
- package/src/tests/samples/namedTuples2.py +0 -56
- package/src/tests/samples/namedTuples3.py +0 -15
- package/src/tests/samples/namedTuples4.py +0 -24
- package/src/tests/samples/namedTuples5.py +0 -15
- package/src/tests/samples/namedTuples6.py +0 -31
- package/src/tests/samples/namedTuples7.py +0 -27
- package/src/tests/samples/never1.py +0 -12
- package/src/tests/samples/never2.py +0 -43
- package/src/tests/samples/newType1.py +0 -34
- package/src/tests/samples/newType2.py +0 -25
- package/src/tests/samples/newType3.py +0 -34
- package/src/tests/samples/newType4.py +0 -8
- package/src/tests/samples/none1.py +0 -29
- package/src/tests/samples/none2.py +0 -22
- package/src/tests/samples/noreturn1.py +0 -78
- package/src/tests/samples/noreturn2.py +0 -37
- package/src/tests/samples/noreturn3.py +0 -16
- package/src/tests/samples/noreturn4.py +0 -29
- package/src/tests/samples/operators1.py +0 -101
- package/src/tests/samples/operators2.py +0 -30
- package/src/tests/samples/operators3.py +0 -8
- package/src/tests/samples/operators4.py +0 -22
- package/src/tests/samples/operators5.py +0 -5
- package/src/tests/samples/operators6.py +0 -10
- package/src/tests/samples/operators7.py +0 -24
- package/src/tests/samples/operators8.py +0 -125
- package/src/tests/samples/optional1.py +0 -77
- package/src/tests/samples/optional2.py +0 -23
- package/src/tests/samples/overload1.py +0 -50
- package/src/tests/samples/overload10.py +0 -48
- package/src/tests/samples/overload2.py +0 -37
- package/src/tests/samples/overload3.py +0 -29
- package/src/tests/samples/overload4.py +0 -46
- package/src/tests/samples/overload5.py +0 -333
- package/src/tests/samples/overload6.py +0 -64
- package/src/tests/samples/overload7.py +0 -376
- package/src/tests/samples/overload8.py +0 -89
- package/src/tests/samples/overload9.py +0 -19
- package/src/tests/samples/package1/__init__.py +0 -3
- package/src/tests/samples/package1/psyche/pysche.py +0 -4
- package/src/tests/samples/package1/psyche.py +0 -4
- package/src/tests/samples/package1/sub/__init__.py +0 -5
- package/src/tests/samples/package1/sub.py +0 -4
- package/src/tests/samples/package1/sub.pyi +0 -2
- package/src/tests/samples/paramInference1.py +0 -27
- package/src/tests/samples/paramNames1.py +0 -39
- package/src/tests/samples/paramSpec1.py +0 -64
- package/src/tests/samples/paramSpec10.py +0 -57
- package/src/tests/samples/paramSpec11.py +0 -36
- package/src/tests/samples/paramSpec12.py +0 -88
- package/src/tests/samples/paramSpec13.py +0 -112
- package/src/tests/samples/paramSpec14.py +0 -30
- package/src/tests/samples/paramSpec15.py +0 -28
- package/src/tests/samples/paramSpec16.py +0 -28
- package/src/tests/samples/paramSpec17.py +0 -34
- package/src/tests/samples/paramSpec18.py +0 -43
- package/src/tests/samples/paramSpec19.py +0 -93
- package/src/tests/samples/paramSpec2.py +0 -29
- package/src/tests/samples/paramSpec20.py +0 -87
- package/src/tests/samples/paramSpec21.py +0 -58
- package/src/tests/samples/paramSpec22.py +0 -26
- package/src/tests/samples/paramSpec23.py +0 -21
- package/src/tests/samples/paramSpec24.py +0 -67
- package/src/tests/samples/paramSpec25.py +0 -36
- package/src/tests/samples/paramSpec26.py +0 -25
- package/src/tests/samples/paramSpec27.py +0 -57
- package/src/tests/samples/paramSpec28.py +0 -39
- package/src/tests/samples/paramSpec29.py +0 -37
- package/src/tests/samples/paramSpec3.py +0 -71
- package/src/tests/samples/paramSpec30.py +0 -49
- package/src/tests/samples/paramSpec31.py +0 -16
- package/src/tests/samples/paramSpec32.py +0 -57
- package/src/tests/samples/paramSpec33.py +0 -29
- package/src/tests/samples/paramSpec34.py +0 -29
- package/src/tests/samples/paramSpec35.py +0 -24
- package/src/tests/samples/paramSpec36.py +0 -43
- package/src/tests/samples/paramSpec37.py +0 -25
- package/src/tests/samples/paramSpec4.py +0 -109
- package/src/tests/samples/paramSpec5.py +0 -29
- package/src/tests/samples/paramSpec6.py +0 -17
- package/src/tests/samples/paramSpec7.py +0 -22
- package/src/tests/samples/paramSpec8.py +0 -45
- package/src/tests/samples/paramSpec9.py +0 -57
- package/src/tests/samples/paramType1.py +0 -71
- package/src/tests/samples/parameters1.py +0 -16
- package/src/tests/samples/partial1.py +0 -163
- package/src/tests/samples/partial2.py +0 -31
- package/src/tests/samples/private1.py +0 -55
- package/src/tests/samples/private2.py +0 -11
- package/src/tests/samples/project1/pyrightconfig.json +0 -5
- package/src/tests/samples/project1/sample1.py +0 -0
- package/src/tests/samples/project1/subfolder1/sample2.py +0 -0
- package/src/tests/samples/project1/subfolder1/subfolder1-1/sample3.py +0 -0
- package/src/tests/samples/project2/pyrightconfig.json +0 -3
- package/src/tests/samples/project3/pyrightconfig.json +0 -7
- package/src/tests/samples/project4/presentfile.py +0 -0
- package/src/tests/samples/project4/pyrightconfig.json +0 -11
- package/src/tests/samples/project4/subfolder/presentfile2.py +0 -0
- package/src/tests/samples/project4/subfolder/presentfile3.py +0 -0
- package/src/tests/samples/project5/pyrightconfig.json +0 -4
- package/src/tests/samples/project6/app1.py +0 -0
- package/src/tests/samples/project6/app2.py +0 -0
- package/src/tests/samples/project6/projectA/foo/__init__.py +0 -0
- package/src/tests/samples/project6/projectA/foo/bar/__init__.py +0 -0
- package/src/tests/samples/project6/projectB/foo/__init__.py +0 -0
- package/src/tests/samples/project6/projectB/foo/baz/__init__.py +0 -0
- package/src/tests/samples/project_src/src/module1.py +0 -0
- package/src/tests/samples/project_src_is_pkg/src/__init__.py +0 -0
- package/src/tests/samples/project_src_is_pkg/src/module1.py +0 -0
- package/src/tests/samples/project_src_with_config_extra_paths/pyrightconfig.json +0 -3
- package/src/tests/samples/project_src_with_config_extra_paths/src/module1.py +0 -0
- package/src/tests/samples/project_src_with_config_no_extra_paths/pyrightconfig.json +0 -2
- package/src/tests/samples/project_src_with_config_no_extra_paths/src/module1.py +0 -0
- package/src/tests/samples/project_src_with_extra_paths/src/_vendored/vendored1.py +0 -1
- package/src/tests/samples/project_src_with_extra_paths/src/module1.py +0 -2
- package/src/tests/samples/project_with_pyproject_toml/pyproject.toml +0 -7
- package/src/tests/samples/project_with_venv_auto_detect_exclude/myvenv/Lib/site-packages/library1.py +0 -0
- package/src/tests/samples/project_with_venv_auto_detect_exclude/myvenv/pyvenv.cfg +0 -0
- package/src/tests/samples/project_with_venv_auto_detect_exclude/pyrightconfig.json +0 -2
- package/src/tests/samples/project_with_venv_auto_detect_exclude/sample1.py +0 -0
- package/src/tests/samples/project_with_venv_auto_detect_exclude/subfolder1/sample2.py +0 -0
- package/src/tests/samples/project_with_venv_auto_detect_exclude/subfolder1/subfolder1-1/sample3.py +0 -0
- package/src/tests/samples/project_with_venv_auto_detect_include/excluded/excluded1.py +0 -0
- package/src/tests/samples/project_with_venv_auto_detect_include/myvenv/Lib/site-packages/library1.py +0 -0
- package/src/tests/samples/project_with_venv_auto_detect_include/myvenv/pyvenv.cfg +0 -0
- package/src/tests/samples/project_with_venv_auto_detect_include/pyrightconfig.json +0 -3
- package/src/tests/samples/project_with_venv_auto_detect_include/sample1.py +0 -0
- package/src/tests/samples/project_with_venv_auto_detect_include/subfolder1/sample2.py +0 -0
- package/src/tests/samples/project_with_venv_auto_detect_include/subfolder1/subfolder1-1/sample3.py +0 -0
- package/src/tests/samples/properties1.py +0 -64
- package/src/tests/samples/properties10.py +0 -19
- package/src/tests/samples/properties11.py +0 -45
- package/src/tests/samples/properties12.py +0 -23
- package/src/tests/samples/properties13.py +0 -15
- package/src/tests/samples/properties2.py +0 -34
- package/src/tests/samples/properties3.py +0 -93
- package/src/tests/samples/properties4.py +0 -17
- package/src/tests/samples/properties5.py +0 -29
- package/src/tests/samples/properties6.py +0 -35
- package/src/tests/samples/properties7.py +0 -30
- package/src/tests/samples/properties8.py +0 -26
- package/src/tests/samples/properties9.py +0 -24
- package/src/tests/samples/protocol1.py +0 -132
- package/src/tests/samples/protocol10.py +0 -36
- package/src/tests/samples/protocol11.py +0 -32
- package/src/tests/samples/protocol12.py +0 -12
- package/src/tests/samples/protocol13.py +0 -26
- package/src/tests/samples/protocol14.py +0 -27
- package/src/tests/samples/protocol15.py +0 -27
- package/src/tests/samples/protocol16.py +0 -22
- package/src/tests/samples/protocol17.py +0 -94
- package/src/tests/samples/protocol18.py +0 -27
- package/src/tests/samples/protocol19.py +0 -39
- package/src/tests/samples/protocol2.py +0 -38
- package/src/tests/samples/protocol20.py +0 -22
- package/src/tests/samples/protocol21.py +0 -26
- package/src/tests/samples/protocol22.py +0 -43
- package/src/tests/samples/protocol23.py +0 -41
- package/src/tests/samples/protocol24.py +0 -74
- package/src/tests/samples/protocol25.py +0 -31
- package/src/tests/samples/protocol26.py +0 -40
- package/src/tests/samples/protocol27.py +0 -76
- package/src/tests/samples/protocol28.py +0 -32
- package/src/tests/samples/protocol29.py +0 -26
- package/src/tests/samples/protocol3.py +0 -110
- package/src/tests/samples/protocol30.py +0 -30
- package/src/tests/samples/protocol31.py +0 -22
- package/src/tests/samples/protocol32.py +0 -67
- package/src/tests/samples/protocol33.py +0 -39
- package/src/tests/samples/protocol34.py +0 -21
- package/src/tests/samples/protocol4.py +0 -22
- package/src/tests/samples/protocol5.py +0 -25
- package/src/tests/samples/protocol6.py +0 -65
- package/src/tests/samples/protocol7.py +0 -24
- package/src/tests/samples/protocol8.py +0 -34
- package/src/tests/samples/protocol9.py +0 -35
- package/src/tests/samples/protocolModule1.py +0 -15
- package/src/tests/samples/protocolModule2.py +0 -83
- package/src/tests/samples/protocolModule3.py +0 -14
- package/src/tests/samples/protocolModule4.py +0 -26
- package/src/tests/samples/pseudoGeneric1.py +0 -20
- package/src/tests/samples/pyrightIgnore1.py +0 -11
- package/src/tests/samples/pyrightIgnore2.py +0 -24
- package/src/tests/samples/python2.py +0 -31
- package/src/tests/samples/recursiveTypeAlias1.py +0 -79
- package/src/tests/samples/recursiveTypeAlias2.py +0 -32
- package/src/tests/samples/recursiveTypeAlias3.py +0 -26
- package/src/tests/samples/recursiveTypeAlias4.py +0 -55
- package/src/tests/samples/recursiveTypeAlias5.pyi +0 -10
- package/src/tests/samples/recursiveTypeAlias6.py +0 -25
- package/src/tests/samples/recursiveTypeAlias7.py +0 -15
- package/src/tests/samples/recursiveTypeAlias8.py +0 -38
- package/src/tests/samples/recursiveTypeAlias9.py +0 -22
- package/src/tests/samples/required1.py +0 -47
- package/src/tests/samples/required2.py +0 -56
- package/src/tests/samples/required3.py +0 -22
- package/src/tests/samples/returnTypes1.py +0 -26
- package/src/tests/samples/revealedType1.py +0 -30
- package/src/tests/samples/sample1.py +0 -6
- package/src/tests/samples/self1.py +0 -93
- package/src/tests/samples/self2.py +0 -195
- package/src/tests/samples/self3.py +0 -9
- package/src/tests/samples/self4.py +0 -18
- package/src/tests/samples/self5.py +0 -26
- package/src/tests/samples/setComprehension1.py +0 -34
- package/src/tests/samples/slots1.py +0 -63
- package/src/tests/samples/slots2.py +0 -38
- package/src/tests/samples/slots3.py +0 -35
- package/src/tests/samples/specialization1.py +0 -78
- package/src/tests/samples/specialization2.py +0 -51
- package/src/tests/samples/staticExpressions1.py +0 -52
- package/src/tests/samples/strings1.py +0 -22
- package/src/tests/samples/subscript1.py +0 -74
- package/src/tests/samples/subscript2.py +0 -55
- package/src/tests/samples/subscript3.py +0 -135
- package/src/tests/samples/suiteExpectedColon1.py +0 -4
- package/src/tests/samples/suiteExpectedColon2.py +0 -2
- package/src/tests/samples/suiteExpectedColon3.py +0 -2
- package/src/tests/samples/super1.py +0 -57
- package/src/tests/samples/super2.py +0 -29
- package/src/tests/samples/super3.py +0 -10
- package/src/tests/samples/super4.py +0 -24
- package/src/tests/samples/super5.py +0 -28
- package/src/tests/samples/super6.py +0 -17
- package/src/tests/samples/super7.py +0 -52
- package/src/tests/samples/super8.py +0 -7
- package/src/tests/samples/super9.py +0 -36
- package/src/tests/samples/test_file1.py +0 -0
- package/src/tests/samples/threePartVersion1.py +0 -33
- package/src/tests/samples/totalOrdering1.py +0 -52
- package/src/tests/samples/tryExcept1.py +0 -39
- package/src/tests/samples/tryExcept10.py +0 -12
- package/src/tests/samples/tryExcept2.py +0 -16
- package/src/tests/samples/tryExcept3.py +0 -12
- package/src/tests/samples/tryExcept4.py +0 -28
- package/src/tests/samples/tryExcept5.py +0 -30
- package/src/tests/samples/tryExcept6.py +0 -27
- package/src/tests/samples/tryExcept7.py +0 -17
- package/src/tests/samples/tryExcept8.py +0 -69
- package/src/tests/samples/tryExcept9.py +0 -42
- package/src/tests/samples/tupleUnpack1.py +0 -63
- package/src/tests/samples/tupleUnpack2.py +0 -56
- package/src/tests/samples/tupleUnpack3.py +0 -28
- package/src/tests/samples/tuples1.py +0 -185
- package/src/tests/samples/tuples10.py +0 -30
- package/src/tests/samples/tuples11.py +0 -21
- package/src/tests/samples/tuples12.py +0 -31
- package/src/tests/samples/tuples13.py +0 -26
- package/src/tests/samples/tuples15.py +0 -16
- package/src/tests/samples/tuples16.py +0 -14
- package/src/tests/samples/tuples2.py +0 -23
- package/src/tests/samples/tuples3.py +0 -15
- package/src/tests/samples/tuples4.py +0 -21
- package/src/tests/samples/tuples5.py +0 -16
- package/src/tests/samples/tuples6.py +0 -56
- package/src/tests/samples/tuples7.py +0 -61
- package/src/tests/samples/tuples8.py +0 -98
- package/src/tests/samples/tuples9.py +0 -17
- package/src/tests/samples/typeAlias1.py +0 -37
- package/src/tests/samples/typeAlias10.py +0 -45
- package/src/tests/samples/typeAlias11.py +0 -37
- package/src/tests/samples/typeAlias12.py +0 -20
- package/src/tests/samples/typeAlias13.py +0 -53
- package/src/tests/samples/typeAlias14.py +0 -29
- package/src/tests/samples/typeAlias15.py +0 -24
- package/src/tests/samples/typeAlias16.py +0 -24
- package/src/tests/samples/typeAlias2.py +0 -26
- package/src/tests/samples/typeAlias3.py +0 -35
- package/src/tests/samples/typeAlias4.py +0 -68
- package/src/tests/samples/typeAlias5.py +0 -56
- package/src/tests/samples/typeAlias6.py +0 -51
- package/src/tests/samples/typeAlias7.py +0 -50
- package/src/tests/samples/typeAlias8.py +0 -24
- package/src/tests/samples/typeAlias9.py +0 -35
- package/src/tests/samples/typeGuard1.py +0 -68
- package/src/tests/samples/typeGuard2.py +0 -58
- package/src/tests/samples/typeGuard3.py +0 -98
- package/src/tests/samples/typeIgnore1.py +0 -10
- package/src/tests/samples/typeIgnore2.py +0 -20
- package/src/tests/samples/typeIgnore3.py +0 -22
- package/src/tests/samples/typeIgnore4.py +0 -10
- package/src/tests/samples/typeIgnore5.py +0 -7
- package/src/tests/samples/typeNarrowing1.py +0 -56
- package/src/tests/samples/typeNarrowing2.py +0 -14
- package/src/tests/samples/typeNarrowing3.py +0 -47
- package/src/tests/samples/typeNarrowing4.py +0 -24
- package/src/tests/samples/typeNarrowing5.py +0 -19
- package/src/tests/samples/typeNarrowing6.py +0 -27
- package/src/tests/samples/typeNarrowing7.py +0 -71
- package/src/tests/samples/typeNarrowingAssert1.py +0 -24
- package/src/tests/samples/typeNarrowingCallable1.py +0 -78
- package/src/tests/samples/typeNarrowingEnum1.py +0 -58
- package/src/tests/samples/typeNarrowingEnum2.py +0 -71
- package/src/tests/samples/typeNarrowingFalsy1.py +0 -50
- package/src/tests/samples/typeNarrowingIn1.py +0 -97
- package/src/tests/samples/typeNarrowingIsNone1.py +0 -56
- package/src/tests/samples/typeNarrowingIsNone2.py +0 -8
- package/src/tests/samples/typeNarrowingIsNoneTuple1.py +0 -42
- package/src/tests/samples/typeNarrowingIsNoneTuple2.py +0 -35
- package/src/tests/samples/typeNarrowingIsinstance1.py +0 -162
- package/src/tests/samples/typeNarrowingIsinstance2.py +0 -14
- package/src/tests/samples/typeNarrowingIsinstance3.py +0 -78
- package/src/tests/samples/typeNarrowingIsinstance4.py +0 -50
- package/src/tests/samples/typeNarrowingIsinstance5.py +0 -39
- package/src/tests/samples/typeNarrowingIsinstance6.py +0 -77
- package/src/tests/samples/typeNarrowingIsinstance7.py +0 -19
- package/src/tests/samples/typeNarrowingIsinstance8.py +0 -19
- package/src/tests/samples/typeNarrowingLiteral1.py +0 -35
- package/src/tests/samples/typeNarrowingLiteral2.py +0 -63
- package/src/tests/samples/typeNarrowingLiteralMember1.py +0 -140
- package/src/tests/samples/typeNarrowingLocalConst1.py +0 -136
- package/src/tests/samples/typeNarrowingNoneMember1.py +0 -102
- package/src/tests/samples/typeNarrowingTuple1.py +0 -23
- package/src/tests/samples/typeNarrowingTupleLength1.py +0 -47
- package/src/tests/samples/typeNarrowingTypeIs1.py +0 -95
- package/src/tests/samples/typeNarrowingTypedDict1.py +0 -99
- package/src/tests/samples/typeNarrowingTypedDict2.py +0 -35
- package/src/tests/samples/typeNarrowingTypedDict3.py +0 -45
- package/src/tests/samples/typePromotions1.py +0 -30
- package/src/tests/samples/typeVar1.py +0 -23
- package/src/tests/samples/typeVar10.py +0 -21
- package/src/tests/samples/typeVar11.py +0 -12
- package/src/tests/samples/typeVar2.py +0 -26
- package/src/tests/samples/typeVar3.py +0 -73
- package/src/tests/samples/typeVar4.py +0 -45
- package/src/tests/samples/typeVar5.py +0 -75
- package/src/tests/samples/typeVar6.py +0 -115
- package/src/tests/samples/typeVar7.py +0 -171
- package/src/tests/samples/typeVar8.py +0 -30
- package/src/tests/samples/typeVar9.py +0 -118
- package/src/tests/samples/typedDict1.py +0 -63
- package/src/tests/samples/typedDict10.py +0 -34
- package/src/tests/samples/typedDict11.py +0 -12
- package/src/tests/samples/typedDict12.py +0 -103
- package/src/tests/samples/typedDict13.py +0 -14
- package/src/tests/samples/typedDict14.py +0 -25
- package/src/tests/samples/typedDict15.py +0 -53
- package/src/tests/samples/typedDict16.py +0 -103
- package/src/tests/samples/typedDict17.py +0 -24
- package/src/tests/samples/typedDict18.py +0 -43
- package/src/tests/samples/typedDict2.py +0 -56
- package/src/tests/samples/typedDict3.py +0 -40
- package/src/tests/samples/typedDict4.py +0 -68
- package/src/tests/samples/typedDict5.py +0 -50
- package/src/tests/samples/typedDict6.py +0 -77
- package/src/tests/samples/typedDict7.py +0 -22
- package/src/tests/samples/typedDict8.py +0 -19
- package/src/tests/samples/typedDict9.py +0 -22
- package/src/tests/samples/unbound1.py +0 -18
- package/src/tests/samples/unbound2.py +0 -12
- package/src/tests/samples/unbound3.py +0 -13
- package/src/tests/samples/unbound4.py +0 -25
- package/src/tests/samples/unicode1.py +0 -39
- package/src/tests/samples/uninitializedVariable1.py +0 -27
- package/src/tests/samples/unions1.py +0 -79
- package/src/tests/samples/unions2.py +0 -15
- package/src/tests/samples/unions3.py +0 -48
- package/src/tests/samples/unions4.py +0 -20
- package/src/tests/samples/unions5.py +0 -43
- package/src/tests/samples/unnecessaryCast1.py +0 -13
- package/src/tests/samples/unnecessaryIsInstance1.py +0 -91
- package/src/tests/samples/unnecessaryIsSubclass1.py +0 -22
- package/src/tests/samples/unpack1.py +0 -56
- package/src/tests/samples/unpack2.py +0 -27
- package/src/tests/samples/unpack3.py +0 -16
- package/src/tests/samples/unpack4.py +0 -15
- package/src/tests/samples/unreachable1.py +0 -112
- package/src/tests/samples/unusedCallResult1.py +0 -67
- package/src/tests/samples/unusedCoroutine1.py +0 -23
- package/src/tests/samples/unusedExpression1.py +0 -34
- package/src/tests/samples/unusedVariable1.py +0 -21
- package/src/tests/samples/variadicTypeVar1.py +0 -51
- package/src/tests/samples/variadicTypeVar10.py +0 -65
- package/src/tests/samples/variadicTypeVar11.py +0 -72
- package/src/tests/samples/variadicTypeVar12.py +0 -26
- package/src/tests/samples/variadicTypeVar13.py +0 -35
- package/src/tests/samples/variadicTypeVar14.py +0 -72
- package/src/tests/samples/variadicTypeVar2.py +0 -63
- package/src/tests/samples/variadicTypeVar3.py +0 -86
- package/src/tests/samples/variadicTypeVar4.py +0 -71
- package/src/tests/samples/variadicTypeVar5.py +0 -131
- package/src/tests/samples/variadicTypeVar6.py +0 -78
- package/src/tests/samples/variadicTypeVar7.py +0 -59
- package/src/tests/samples/variadicTypeVar8.py +0 -116
- package/src/tests/samples/variadicTypeVar9.py +0 -38
- package/src/tests/samples/with1.py +0 -117
- package/src/tests/samples/with2.py +0 -63
- package/src/tests/samples/with3.py +0 -66
- package/src/tests/samples/with4.py +0 -24
- package/src/tests/samples/with5.py +0 -33
- package/src/tests/samples/zipfs/bad.egg +0 -1
- package/src/tests/samples/zipfs/bad.zip +0 -1
- package/src/tests/samples/zipfs/basic.egg +0 -0
- package/src/tests/samples/zipfs/basic.zip +0 -0
- package/src/tests/samples/zipfs/corrupt.egg +0 -0
- package/src/tests/samples/zipfs/corrupt.zip +0 -0
- package/src/tests/sourceFile.test.ts +0 -25
- package/src/tests/stringUtils.test.ts +0 -63
- package/src/tests/symbolNameUtils.test.ts +0 -77
- package/src/tests/testState.test.ts +0 -579
- package/src/tests/testUtils.ts +0 -251
- package/src/tests/tokenizer.test.ts +0 -1534
- package/src/tests/typeEvaluator1.test.ts +0 -1372
- package/src/tests/typeEvaluator2.test.ts +0 -1253
- package/src/tests/typeEvaluator3.test.ts +0 -1279
- package/src/tests/typeEvaluator4.test.ts +0 -1178
- package/src/tests/updateSymbolReference.test.ts +0 -1107
- package/src/tests/zipfs.test.ts +0 -116
- package/src/workspaceMap.ts +0 -76
package/src/parser/parser.ts
DELETED
@@ -1,4859 +0,0 @@
|
|
1
|
-
/*
|
2
|
-
* parser.ts
|
3
|
-
* Copyright (c) Microsoft Corporation.
|
4
|
-
* Licensed under the MIT license.
|
5
|
-
* Author: Eric Traut
|
6
|
-
*
|
7
|
-
* Based on code from python-language-server repository:
|
8
|
-
* https://github.com/Microsoft/python-language-server
|
9
|
-
*
|
10
|
-
* Parser for the Python language. Converts a stream of tokens
|
11
|
-
* into an abstract syntax tree (AST).
|
12
|
-
*/
|
13
|
-
|
14
|
-
import Char from 'typescript-char';
|
15
|
-
|
16
|
-
import { appendArray } from '../common/collectionUtils';
|
17
|
-
import { assert } from '../common/debug';
|
18
|
-
import { Diagnostic, DiagnosticAddendum } from '../common/diagnostic';
|
19
|
-
import { DiagnosticSink } from '../common/diagnosticSink';
|
20
|
-
import { convertOffsetsToRange, convertPositionToOffset } from '../common/positionUtils';
|
21
|
-
import { latestStablePythonVersion, PythonVersion } from '../common/pythonVersion';
|
22
|
-
import { TextRange } from '../common/textRange';
|
23
|
-
import { TextRangeCollection } from '../common/textRangeCollection';
|
24
|
-
import { timingStats } from '../common/timing';
|
25
|
-
import { Localizer } from '../localization/localize';
|
26
|
-
import {
|
27
|
-
ArgumentCategory,
|
28
|
-
ArgumentNode,
|
29
|
-
AssertNode,
|
30
|
-
AssignmentExpressionNode,
|
31
|
-
AssignmentNode,
|
32
|
-
AugmentedAssignmentNode,
|
33
|
-
AwaitNode,
|
34
|
-
BinaryOperationNode,
|
35
|
-
BreakNode,
|
36
|
-
CallNode,
|
37
|
-
CaseNode,
|
38
|
-
ClassNode,
|
39
|
-
ConstantNode,
|
40
|
-
ContinueNode,
|
41
|
-
DecoratorNode,
|
42
|
-
DelNode,
|
43
|
-
DictionaryEntryNode,
|
44
|
-
DictionaryExpandEntryNode,
|
45
|
-
DictionaryKeyEntryNode,
|
46
|
-
DictionaryNode,
|
47
|
-
EllipsisNode,
|
48
|
-
ErrorExpressionCategory,
|
49
|
-
ErrorNode,
|
50
|
-
ExceptNode,
|
51
|
-
ExpressionNode,
|
52
|
-
extendRange,
|
53
|
-
FormatStringNode,
|
54
|
-
ForNode,
|
55
|
-
FunctionAnnotationNode,
|
56
|
-
FunctionNode,
|
57
|
-
getNextNodeId,
|
58
|
-
GlobalNode,
|
59
|
-
IfNode,
|
60
|
-
ImportAsNode,
|
61
|
-
ImportFromAsNode,
|
62
|
-
ImportFromNode,
|
63
|
-
ImportNode,
|
64
|
-
IndexNode,
|
65
|
-
LambdaNode,
|
66
|
-
ListComprehensionForIfNode,
|
67
|
-
ListComprehensionForNode,
|
68
|
-
ListComprehensionIfNode,
|
69
|
-
ListComprehensionNode,
|
70
|
-
ListNode,
|
71
|
-
MatchNode,
|
72
|
-
MemberAccessNode,
|
73
|
-
ModuleNameNode,
|
74
|
-
ModuleNode,
|
75
|
-
NameNode,
|
76
|
-
NonlocalNode,
|
77
|
-
NumberNode,
|
78
|
-
ParameterCategory,
|
79
|
-
ParameterNode,
|
80
|
-
ParseNode,
|
81
|
-
ParseNodeType,
|
82
|
-
PassNode,
|
83
|
-
PatternAsNode,
|
84
|
-
PatternAtomNode,
|
85
|
-
PatternCaptureNode,
|
86
|
-
PatternClassArgumentNode,
|
87
|
-
PatternClassNode,
|
88
|
-
PatternLiteralNode,
|
89
|
-
PatternMappingEntryNode,
|
90
|
-
PatternMappingExpandEntryNode,
|
91
|
-
PatternMappingKeyEntryNode,
|
92
|
-
PatternMappingNode,
|
93
|
-
PatternSequenceNode,
|
94
|
-
PatternValueNode,
|
95
|
-
RaiseNode,
|
96
|
-
ReturnNode,
|
97
|
-
SetNode,
|
98
|
-
SliceNode,
|
99
|
-
StatementListNode,
|
100
|
-
StatementNode,
|
101
|
-
StringListNode,
|
102
|
-
StringNode,
|
103
|
-
SuiteNode,
|
104
|
-
TernaryNode,
|
105
|
-
TryNode,
|
106
|
-
TupleNode,
|
107
|
-
TypeAnnotationNode,
|
108
|
-
UnaryOperationNode,
|
109
|
-
UnpackNode,
|
110
|
-
WhileNode,
|
111
|
-
WithItemNode,
|
112
|
-
WithNode,
|
113
|
-
YieldFromNode,
|
114
|
-
YieldNode,
|
115
|
-
} from './parseNodes';
|
116
|
-
import * as StringTokenUtils from './stringTokenUtils';
|
117
|
-
import { Tokenizer, TokenizerOutput } from './tokenizer';
|
118
|
-
import {
|
119
|
-
DedentToken,
|
120
|
-
IdentifierToken,
|
121
|
-
IndentToken,
|
122
|
-
KeywordToken,
|
123
|
-
KeywordType,
|
124
|
-
NumberToken,
|
125
|
-
OperatorToken,
|
126
|
-
OperatorType,
|
127
|
-
StringToken,
|
128
|
-
StringTokenFlags,
|
129
|
-
Token,
|
130
|
-
TokenType,
|
131
|
-
} from './tokenizerTypes';
|
132
|
-
|
133
|
-
interface ListResult<T> {
|
134
|
-
list: T[];
|
135
|
-
trailingComma: boolean;
|
136
|
-
parseError?: ErrorNode | undefined;
|
137
|
-
}
|
138
|
-
|
139
|
-
interface SubscriptListResult {
|
140
|
-
list: ArgumentNode[];
|
141
|
-
trailingComma: boolean;
|
142
|
-
}
|
143
|
-
|
144
|
-
export class ParseOptions {
|
145
|
-
constructor() {
|
146
|
-
this.isStubFile = false;
|
147
|
-
this.pythonVersion = latestStablePythonVersion;
|
148
|
-
this.reportInvalidStringEscapeSequence = false;
|
149
|
-
this.skipFunctionAndClassBody = false;
|
150
|
-
this.ipythonMode = false;
|
151
|
-
this.reportErrorsForParsedStringContents = false;
|
152
|
-
}
|
153
|
-
|
154
|
-
isStubFile: boolean;
|
155
|
-
pythonVersion: PythonVersion;
|
156
|
-
reportInvalidStringEscapeSequence: boolean;
|
157
|
-
skipFunctionAndClassBody: boolean;
|
158
|
-
ipythonMode: boolean;
|
159
|
-
reportErrorsForParsedStringContents: boolean;
|
160
|
-
}
|
161
|
-
|
162
|
-
export interface ParseResults {
|
163
|
-
text: string;
|
164
|
-
parseTree: ModuleNode;
|
165
|
-
importedModules: ModuleImport[];
|
166
|
-
futureImports: Map<string, boolean>;
|
167
|
-
tokenizerOutput: TokenizerOutput;
|
168
|
-
containsWildcardImport: boolean;
|
169
|
-
typingSymbolAliases: Map<string, string>;
|
170
|
-
}
|
171
|
-
|
172
|
-
export interface ParseExpressionTextResults {
|
173
|
-
parseTree?: ExpressionNode | FunctionAnnotationNode | undefined;
|
174
|
-
lines: TextRangeCollection<TextRange>;
|
175
|
-
diagnostics: Diagnostic[];
|
176
|
-
}
|
177
|
-
|
178
|
-
export interface ModuleImport {
|
179
|
-
nameNode: ModuleNameNode;
|
180
|
-
leadingDots: number;
|
181
|
-
nameParts: string[];
|
182
|
-
|
183
|
-
// Used for "from X import Y" pattern. An empty
|
184
|
-
// array implies "from X import *".
|
185
|
-
importedSymbols: string[] | undefined;
|
186
|
-
}
|
187
|
-
|
188
|
-
export interface ArgListResult {
|
189
|
-
args: ArgumentNode[];
|
190
|
-
trailingComma: boolean;
|
191
|
-
}
|
192
|
-
|
193
|
-
const enum ParseTextMode {
|
194
|
-
Expression,
|
195
|
-
VariableAnnotation,
|
196
|
-
FunctionAnnotation,
|
197
|
-
}
|
198
|
-
|
199
|
-
// Limit the max child node depth to prevent stack overflows.
|
200
|
-
const maxChildNodeDepth = 256;
|
201
|
-
|
202
|
-
export class Parser {
|
203
|
-
private _fileContents?: string;
|
204
|
-
private _tokenizerOutput?: TokenizerOutput;
|
205
|
-
private _tokenIndex = 0;
|
206
|
-
private _areErrorsSuppressed = false;
|
207
|
-
private _parseOptions: ParseOptions = new ParseOptions();
|
208
|
-
private _diagSink: DiagnosticSink = new DiagnosticSink();
|
209
|
-
private _isInLoop = false;
|
210
|
-
private _isInFunction = false;
|
211
|
-
private _isInFinally = false;
|
212
|
-
private _isParsingTypeAnnotation = false;
|
213
|
-
private _isParsingIndexTrailer = false;
|
214
|
-
private _futureImportMap = new Map<string, boolean>();
|
215
|
-
private _importedModules: ModuleImport[] = [];
|
216
|
-
private _containsWildcardImport = false;
|
217
|
-
private _assignmentExpressionsAllowed = true;
|
218
|
-
private _typingImportAliases: string[] = [];
|
219
|
-
private _typingSymbolAliases: Map<string, string> = new Map<string, string>();
|
220
|
-
|
221
|
-
parseSourceFile(fileContents: string, parseOptions: ParseOptions, diagSink: DiagnosticSink): ParseResults {
|
222
|
-
timingStats.tokenizeFileTime.timeOperation(() => {
|
223
|
-
this._startNewParse(fileContents, 0, fileContents.length, parseOptions, diagSink);
|
224
|
-
});
|
225
|
-
|
226
|
-
const moduleNode = ModuleNode.create({ start: 0, length: fileContents.length });
|
227
|
-
|
228
|
-
timingStats.parseFileTime.timeOperation(() => {
|
229
|
-
while (!this._atEof()) {
|
230
|
-
if (!this._consumeTokenIfType(TokenType.NewLine)) {
|
231
|
-
// Handle a common error case and try to recover.
|
232
|
-
const nextToken = this._peekToken();
|
233
|
-
if (nextToken.type === TokenType.Indent) {
|
234
|
-
this._getNextToken();
|
235
|
-
const indentToken = nextToken as IndentToken;
|
236
|
-
if (indentToken.isIndentAmbiguous) {
|
237
|
-
this._addError(Localizer.Diagnostic.inconsistentTabs(), indentToken);
|
238
|
-
} else {
|
239
|
-
this._addError(Localizer.Diagnostic.unexpectedIndent(), nextToken);
|
240
|
-
}
|
241
|
-
}
|
242
|
-
|
243
|
-
const statement = this._parseStatement();
|
244
|
-
if (!statement) {
|
245
|
-
// Perform basic error recovery to get to the next line.
|
246
|
-
this._consumeTokensUntilType([TokenType.NewLine]);
|
247
|
-
} else {
|
248
|
-
statement.parent = moduleNode;
|
249
|
-
moduleNode.statements.push(statement);
|
250
|
-
}
|
251
|
-
}
|
252
|
-
}
|
253
|
-
});
|
254
|
-
|
255
|
-
assert(this._tokenizerOutput !== undefined);
|
256
|
-
return {
|
257
|
-
text: fileContents,
|
258
|
-
parseTree: moduleNode,
|
259
|
-
importedModules: this._importedModules,
|
260
|
-
futureImports: this._futureImportMap,
|
261
|
-
tokenizerOutput: this._tokenizerOutput!,
|
262
|
-
containsWildcardImport: this._containsWildcardImport,
|
263
|
-
typingSymbolAliases: this._typingSymbolAliases,
|
264
|
-
};
|
265
|
-
}
|
266
|
-
|
267
|
-
parseTextExpression(
|
268
|
-
fileContents: string,
|
269
|
-
textOffset: number,
|
270
|
-
textLength: number,
|
271
|
-
parseOptions: ParseOptions,
|
272
|
-
parseTextMode = ParseTextMode.Expression,
|
273
|
-
initialParenDepth = 0,
|
274
|
-
typingSymbolAliases?: Map<string, string>
|
275
|
-
): ParseExpressionTextResults {
|
276
|
-
const diagSink = new DiagnosticSink();
|
277
|
-
this._startNewParse(fileContents, textOffset, textLength, parseOptions, diagSink, initialParenDepth);
|
278
|
-
|
279
|
-
if (typingSymbolAliases) {
|
280
|
-
this._typingSymbolAliases = new Map<string, string>(typingSymbolAliases);
|
281
|
-
}
|
282
|
-
|
283
|
-
let parseTree: ExpressionNode | FunctionAnnotationNode | undefined;
|
284
|
-
if (parseTextMode === ParseTextMode.VariableAnnotation) {
|
285
|
-
parseTree = this._parseTypeAnnotation();
|
286
|
-
} else if (parseTextMode === ParseTextMode.FunctionAnnotation) {
|
287
|
-
parseTree = this._parseFunctionTypeAnnotation();
|
288
|
-
} else {
|
289
|
-
const exprListResult = this._parseTestOrStarExpressionList(
|
290
|
-
/* allowAssignmentExpression */ false,
|
291
|
-
/* allowMultipleUnpack */ true
|
292
|
-
);
|
293
|
-
if (exprListResult.parseError) {
|
294
|
-
parseTree = exprListResult.parseError;
|
295
|
-
} else {
|
296
|
-
if (exprListResult.list.length === 0) {
|
297
|
-
this._addError(Localizer.Diagnostic.expectedExpr(), this._peekToken());
|
298
|
-
}
|
299
|
-
parseTree = this._makeExpressionOrTuple(exprListResult, /* enclosedInParens */ false);
|
300
|
-
}
|
301
|
-
}
|
302
|
-
|
303
|
-
if (this._peekTokenType() === TokenType.NewLine) {
|
304
|
-
this._getNextToken();
|
305
|
-
}
|
306
|
-
|
307
|
-
if (!this._atEof()) {
|
308
|
-
this._addError(Localizer.Diagnostic.unexpectedExprToken(), this._peekToken());
|
309
|
-
}
|
310
|
-
|
311
|
-
return {
|
312
|
-
parseTree,
|
313
|
-
lines: this._tokenizerOutput!.lines,
|
314
|
-
diagnostics: diagSink.fetchAndClear(),
|
315
|
-
};
|
316
|
-
}
|
317
|
-
|
318
|
-
private _startNewParse(
|
319
|
-
fileContents: string,
|
320
|
-
textOffset: number,
|
321
|
-
textLength: number,
|
322
|
-
parseOptions: ParseOptions,
|
323
|
-
diagSink: DiagnosticSink,
|
324
|
-
initialParenDepth = 0
|
325
|
-
) {
|
326
|
-
this._fileContents = fileContents;
|
327
|
-
this._parseOptions = parseOptions;
|
328
|
-
this._diagSink = diagSink;
|
329
|
-
|
330
|
-
// Tokenize the file contents.
|
331
|
-
const tokenizer = new Tokenizer();
|
332
|
-
this._tokenizerOutput = tokenizer.tokenize(
|
333
|
-
fileContents,
|
334
|
-
textOffset,
|
335
|
-
textLength,
|
336
|
-
initialParenDepth,
|
337
|
-
this._parseOptions.ipythonMode
|
338
|
-
);
|
339
|
-
this._tokenIndex = 0;
|
340
|
-
}
|
341
|
-
|
342
|
-
// stmt: simple_stmt | compound_stmt
|
343
|
-
// compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt | with_stmt
|
344
|
-
// | funcdef | classdef | decorated | async_stmt
|
345
|
-
private _parseStatement(): StatementNode | ErrorNode | undefined {
|
346
|
-
// Handle the errant condition of a dedent token here to provide
|
347
|
-
// better recovery.
|
348
|
-
if (this._consumeTokenIfType(TokenType.Dedent)) {
|
349
|
-
this._addError(Localizer.Diagnostic.unexpectedUnindent(), this._peekToken());
|
350
|
-
}
|
351
|
-
|
352
|
-
switch (this._peekKeywordType()) {
|
353
|
-
case KeywordType.If:
|
354
|
-
return this._parseIfStatement();
|
355
|
-
|
356
|
-
case KeywordType.While:
|
357
|
-
return this._parseWhileStatement();
|
358
|
-
|
359
|
-
case KeywordType.For:
|
360
|
-
return this._parseForStatement();
|
361
|
-
|
362
|
-
case KeywordType.Try:
|
363
|
-
return this._parseTryStatement();
|
364
|
-
|
365
|
-
case KeywordType.With:
|
366
|
-
return this._parseWithStatement();
|
367
|
-
|
368
|
-
case KeywordType.Def:
|
369
|
-
return this._parseFunctionDef();
|
370
|
-
|
371
|
-
case KeywordType.Class:
|
372
|
-
return this._parseClassDef();
|
373
|
-
|
374
|
-
case KeywordType.Async:
|
375
|
-
return this._parseAsyncStatement();
|
376
|
-
|
377
|
-
case KeywordType.Match: {
|
378
|
-
// Match is considered a "soft" keyword, so we will treat
|
379
|
-
// it as an identifier if it is followed by an unexpected
|
380
|
-
// token.
|
381
|
-
const peekToken = this._peekToken(1);
|
382
|
-
let isInvalidMatchToken = false;
|
383
|
-
|
384
|
-
if (
|
385
|
-
peekToken.type === TokenType.Colon ||
|
386
|
-
peekToken.type === TokenType.Semicolon ||
|
387
|
-
peekToken.type === TokenType.Comma ||
|
388
|
-
peekToken.type === TokenType.Dot ||
|
389
|
-
peekToken.type === TokenType.NewLine ||
|
390
|
-
peekToken.type === TokenType.EndOfStream
|
391
|
-
) {
|
392
|
-
isInvalidMatchToken = true;
|
393
|
-
} else if (peekToken.type === TokenType.Operator) {
|
394
|
-
const operatorToken = peekToken as OperatorToken;
|
395
|
-
if (
|
396
|
-
operatorToken.operatorType !== OperatorType.Multiply &&
|
397
|
-
operatorToken.operatorType !== OperatorType.Subtract
|
398
|
-
) {
|
399
|
-
isInvalidMatchToken = true;
|
400
|
-
}
|
401
|
-
}
|
402
|
-
|
403
|
-
if (!isInvalidMatchToken) {
|
404
|
-
// Try to parse the match statement. If it doesn't appear to
|
405
|
-
// be a match statement, treat as a non-keyword and reparse.
|
406
|
-
const matchStatement = this._parseMatchStatement();
|
407
|
-
if (matchStatement) {
|
408
|
-
return matchStatement;
|
409
|
-
}
|
410
|
-
}
|
411
|
-
}
|
412
|
-
}
|
413
|
-
|
414
|
-
if (this._peekOperatorType() === OperatorType.MatrixMultiply) {
|
415
|
-
return this._parseDecorated();
|
416
|
-
}
|
417
|
-
|
418
|
-
return this._parseSimpleStatement();
|
419
|
-
}
|
420
|
-
|
421
|
-
// async_stmt: 'async' (funcdef | with_stmt | for_stmt)
|
422
|
-
private _parseAsyncStatement(): StatementNode | undefined {
|
423
|
-
const asyncToken = this._getKeywordToken(KeywordType.Async);
|
424
|
-
|
425
|
-
switch (this._peekKeywordType()) {
|
426
|
-
case KeywordType.Def:
|
427
|
-
return this._parseFunctionDef(asyncToken);
|
428
|
-
|
429
|
-
case KeywordType.With:
|
430
|
-
return this._parseWithStatement(asyncToken);
|
431
|
-
|
432
|
-
case KeywordType.For:
|
433
|
-
return this._parseForStatement(asyncToken);
|
434
|
-
}
|
435
|
-
|
436
|
-
this._addError(Localizer.Diagnostic.unexpectedAsyncToken(), asyncToken);
|
437
|
-
|
438
|
-
return undefined;
|
439
|
-
}
|
440
|
-
|
441
|
-
// match_stmt: "match" subject_expr ':' NEWLINE INDENT case_block+ DEDENT
|
442
|
-
// subject_expr:
|
443
|
-
// | star_named_expression ',' star_named_expressions?
|
444
|
-
// | named_expression
|
445
|
-
private _parseMatchStatement(): MatchNode | undefined {
|
446
|
-
// Parse the subject expression with errors suppressed. If it's not
|
447
|
-
// followed by a colon, we'll assume this is not a match statement.
|
448
|
-
// We need to do this because "match" is considered a soft keyword,
|
449
|
-
// and we need to distinguish between "match(2)" and "match (2):"
|
450
|
-
// and between "match[2]" and "match [2]:"
|
451
|
-
let smellsLikeMatchStatement = false;
|
452
|
-
this._suppressErrors(() => {
|
453
|
-
const curTokenIndex = this._tokenIndex;
|
454
|
-
|
455
|
-
this._getKeywordToken(KeywordType.Match);
|
456
|
-
const expression = this._parseTestOrStarListAsExpression(
|
457
|
-
/* allowAssignmentExpression */ true,
|
458
|
-
/* allowMultipleUnpack */ true,
|
459
|
-
ErrorExpressionCategory.MissingPatternSubject,
|
460
|
-
Localizer.Diagnostic.expectedReturnExpr()
|
461
|
-
);
|
462
|
-
smellsLikeMatchStatement =
|
463
|
-
expression.nodeType !== ParseNodeType.Error && this._peekToken().type === TokenType.Colon;
|
464
|
-
|
465
|
-
// Set the token index back to the start.
|
466
|
-
this._tokenIndex = curTokenIndex;
|
467
|
-
});
|
468
|
-
|
469
|
-
if (!smellsLikeMatchStatement) {
|
470
|
-
return undefined;
|
471
|
-
}
|
472
|
-
|
473
|
-
const matchToken = this._getKeywordToken(KeywordType.Match);
|
474
|
-
|
475
|
-
const subjectExpression = this._parseTestOrStarListAsExpression(
|
476
|
-
/* allowAssignmentExpression */ true,
|
477
|
-
/* allowMultipleUnpack */ true,
|
478
|
-
ErrorExpressionCategory.MissingPatternSubject,
|
479
|
-
Localizer.Diagnostic.expectedReturnExpr()
|
480
|
-
);
|
481
|
-
const matchNode = MatchNode.create(matchToken, subjectExpression);
|
482
|
-
|
483
|
-
const nextToken = this._peekToken();
|
484
|
-
|
485
|
-
if (!this._consumeTokenIfType(TokenType.Colon)) {
|
486
|
-
this._addError(Localizer.Diagnostic.expectedColon(), nextToken);
|
487
|
-
|
488
|
-
// Try to perform parse recovery by consuming tokens until
|
489
|
-
// we find the end of the line.
|
490
|
-
if (this._consumeTokensUntilType([TokenType.NewLine, TokenType.Colon])) {
|
491
|
-
this._getNextToken();
|
492
|
-
}
|
493
|
-
} else if (!this._consumeTokenIfType(TokenType.NewLine)) {
|
494
|
-
this._addError(Localizer.Diagnostic.expectedNewline(), nextToken);
|
495
|
-
} else {
|
496
|
-
const possibleIndent = this._peekToken();
|
497
|
-
if (!this._consumeTokenIfType(TokenType.Indent)) {
|
498
|
-
this._addError(Localizer.Diagnostic.expectedIndentedBlock(), this._peekToken());
|
499
|
-
} else {
|
500
|
-
const indentToken = possibleIndent as IndentToken;
|
501
|
-
if (indentToken.isIndentAmbiguous) {
|
502
|
-
this._addError(Localizer.Diagnostic.inconsistentTabs(), indentToken);
|
503
|
-
}
|
504
|
-
}
|
505
|
-
|
506
|
-
while (true) {
|
507
|
-
// Handle a common error here and see if we can recover.
|
508
|
-
const nextToken = this._peekToken();
|
509
|
-
if (nextToken.type === TokenType.Indent) {
|
510
|
-
this._getNextToken();
|
511
|
-
const indentToken = nextToken as IndentToken;
|
512
|
-
if (indentToken.isIndentAmbiguous) {
|
513
|
-
this._addError(Localizer.Diagnostic.inconsistentTabs(), indentToken);
|
514
|
-
} else {
|
515
|
-
this._addError(Localizer.Diagnostic.unexpectedIndent(), nextToken);
|
516
|
-
}
|
517
|
-
}
|
518
|
-
|
519
|
-
const caseStatement = this._parseCaseStatement();
|
520
|
-
if (!caseStatement) {
|
521
|
-
// Perform basic error recovery to get to the next line.
|
522
|
-
if (this._consumeTokensUntilType([TokenType.NewLine, TokenType.Colon])) {
|
523
|
-
this._getNextToken();
|
524
|
-
}
|
525
|
-
} else {
|
526
|
-
caseStatement.parent = matchNode;
|
527
|
-
matchNode.cases.push(caseStatement);
|
528
|
-
}
|
529
|
-
|
530
|
-
const dedentToken = this._peekToken() as DedentToken;
|
531
|
-
if (this._consumeTokenIfType(TokenType.Dedent)) {
|
532
|
-
if (!dedentToken.matchesIndent) {
|
533
|
-
this._addError(Localizer.Diagnostic.inconsistentIndent(), dedentToken);
|
534
|
-
}
|
535
|
-
if (dedentToken.isDedentAmbiguous) {
|
536
|
-
this._addError(Localizer.Diagnostic.inconsistentTabs(), dedentToken);
|
537
|
-
}
|
538
|
-
break;
|
539
|
-
}
|
540
|
-
|
541
|
-
if (this._peekTokenType() === TokenType.EndOfStream) {
|
542
|
-
break;
|
543
|
-
}
|
544
|
-
}
|
545
|
-
|
546
|
-
if (matchNode.cases.length > 0) {
|
547
|
-
extendRange(matchNode, matchNode.cases[matchNode.cases.length - 1]);
|
548
|
-
} else {
|
549
|
-
this._addError(Localizer.Diagnostic.zeroCaseStatementsFound(), matchToken);
|
550
|
-
}
|
551
|
-
}
|
552
|
-
|
553
|
-
// This feature requires Python 3.10.
|
554
|
-
if (this._getLanguageVersion() < PythonVersion.V3_10) {
|
555
|
-
this._addError(Localizer.Diagnostic.matchIncompatible(), matchToken);
|
556
|
-
}
|
557
|
-
|
558
|
-
// Validate that only the last entry uses an irrefutable pattern.
|
559
|
-
for (let i = 0; i < matchNode.cases.length - 1; i++) {
|
560
|
-
const caseNode = matchNode.cases[i];
|
561
|
-
if (!caseNode.guardExpression && caseNode.isIrrefutable) {
|
562
|
-
this._addError(Localizer.Diagnostic.casePatternIsIrrefutable(), caseNode.pattern);
|
563
|
-
}
|
564
|
-
}
|
565
|
-
|
566
|
-
return matchNode;
|
567
|
-
}
|
568
|
-
|
569
|
-
// case_block: "case" patterns [guard] ':' block
|
570
|
-
// patterns: sequence_pattern | as_pattern
|
571
|
-
// guard: 'if' named_expression
|
572
|
-
private _parseCaseStatement(): CaseNode | undefined {
|
573
|
-
const caseToken = this._peekToken();
|
574
|
-
|
575
|
-
if (!this._consumeTokenIfKeyword(KeywordType.Case)) {
|
576
|
-
this._addError(Localizer.Diagnostic.expectedCase(), caseToken);
|
577
|
-
return undefined;
|
578
|
-
}
|
579
|
-
|
580
|
-
const patternList = this._parsePatternSequence();
|
581
|
-
let casePattern: PatternAtomNode;
|
582
|
-
|
583
|
-
if (patternList.parseError) {
|
584
|
-
casePattern = patternList.parseError;
|
585
|
-
} else if (patternList.list.length === 0) {
|
586
|
-
this._addError(Localizer.Diagnostic.expectedPatternExpr(), this._peekToken());
|
587
|
-
casePattern = ErrorNode.create(caseToken, ErrorExpressionCategory.MissingPattern);
|
588
|
-
} else if (patternList.list.length === 1 && !patternList.trailingComma) {
|
589
|
-
const pattern = patternList.list[0].orPatterns[0];
|
590
|
-
|
591
|
-
if (pattern.nodeType === ParseNodeType.PatternCapture && pattern.isStar) {
|
592
|
-
casePattern = PatternSequenceNode.create(patternList.list[0], patternList.list);
|
593
|
-
} else {
|
594
|
-
casePattern = patternList.list[0];
|
595
|
-
}
|
596
|
-
} else {
|
597
|
-
casePattern = PatternSequenceNode.create(patternList.list[0], patternList.list);
|
598
|
-
}
|
599
|
-
|
600
|
-
let guardExpression: ExpressionNode | undefined;
|
601
|
-
if (this._consumeTokenIfKeyword(KeywordType.If)) {
|
602
|
-
guardExpression = this._parseTestExpression(/* allowAssignmentExpression */ true);
|
603
|
-
}
|
604
|
-
|
605
|
-
const suite = this._parseSuite(this._isInFunction);
|
606
|
-
return CaseNode.create(caseToken, casePattern, this._isPatternIrrefutable(casePattern), guardExpression, suite);
|
607
|
-
}
|
608
|
-
|
609
|
-
// PEP 634 defines the concept of an "irrefutable" pattern - a pattern that
|
610
|
-
// will always be matched.
|
611
|
-
private _isPatternIrrefutable(node: PatternAtomNode): boolean {
|
612
|
-
if (node.nodeType === ParseNodeType.PatternCapture) {
|
613
|
-
return true;
|
614
|
-
}
|
615
|
-
|
616
|
-
if (node.nodeType === ParseNodeType.PatternAs) {
|
617
|
-
return node.orPatterns.some((pattern) => this._isPatternIrrefutable(pattern));
|
618
|
-
}
|
619
|
-
|
620
|
-
return false;
|
621
|
-
}
|
622
|
-
|
623
|
-
private _getPatternTargetNames(node: PatternAtomNode, nameMap: Map<string, boolean>): void {
|
624
|
-
switch (node.nodeType) {
|
625
|
-
case ParseNodeType.PatternSequence: {
|
626
|
-
node.entries.forEach((subpattern) => {
|
627
|
-
this._getPatternTargetNames(subpattern, nameMap);
|
628
|
-
});
|
629
|
-
break;
|
630
|
-
}
|
631
|
-
|
632
|
-
case ParseNodeType.PatternClass: {
|
633
|
-
node.arguments.forEach((arg) => {
|
634
|
-
this._getPatternTargetNames(arg.pattern, nameMap);
|
635
|
-
});
|
636
|
-
break;
|
637
|
-
}
|
638
|
-
|
639
|
-
case ParseNodeType.PatternAs: {
|
640
|
-
if (node.target) {
|
641
|
-
nameMap.set(node.target.value, true);
|
642
|
-
}
|
643
|
-
node.orPatterns.forEach((subpattern) => {
|
644
|
-
this._getPatternTargetNames(subpattern, nameMap);
|
645
|
-
});
|
646
|
-
break;
|
647
|
-
}
|
648
|
-
|
649
|
-
case ParseNodeType.PatternCapture: {
|
650
|
-
if (!node.isWildcard) {
|
651
|
-
nameMap.set(node.target.value, true);
|
652
|
-
}
|
653
|
-
break;
|
654
|
-
}
|
655
|
-
|
656
|
-
case ParseNodeType.PatternMapping: {
|
657
|
-
node.entries.forEach((mapEntry) => {
|
658
|
-
if (mapEntry.nodeType === ParseNodeType.PatternMappingExpandEntry) {
|
659
|
-
nameMap.set(mapEntry.target.value, true);
|
660
|
-
} else {
|
661
|
-
this._getPatternTargetNames(mapEntry.keyPattern, nameMap);
|
662
|
-
this._getPatternTargetNames(mapEntry.valuePattern, nameMap);
|
663
|
-
}
|
664
|
-
});
|
665
|
-
break;
|
666
|
-
}
|
667
|
-
|
668
|
-
case ParseNodeType.PatternLiteral:
|
669
|
-
case ParseNodeType.PatternValue:
|
670
|
-
case ParseNodeType.Error: {
|
671
|
-
break;
|
672
|
-
}
|
673
|
-
}
|
674
|
-
}
|
675
|
-
|
676
|
-
private _parsePatternSequence() {
|
677
|
-
const patternList = this._parseExpressionListGeneric(() => this._parsePatternAs());
|
678
|
-
|
679
|
-
// Check for more than one star entry.
|
680
|
-
const starEntries = patternList.list.filter(
|
681
|
-
(entry) =>
|
682
|
-
entry.orPatterns.length === 1 &&
|
683
|
-
entry.orPatterns[0].nodeType === ParseNodeType.PatternCapture &&
|
684
|
-
entry.orPatterns[0].isStar
|
685
|
-
);
|
686
|
-
if (starEntries.length > 1) {
|
687
|
-
this._addError(Localizer.Diagnostic.duplicateStarPattern(), starEntries[1].orPatterns[0]);
|
688
|
-
}
|
689
|
-
|
690
|
-
// Look for redundant capture targets.
|
691
|
-
const captureTargetMap = new Map<string, PatternAtomNode>();
|
692
|
-
patternList.list.forEach((asPattern) => {
|
693
|
-
asPattern.orPatterns.forEach((patternAtom) => {
|
694
|
-
if (
|
695
|
-
patternAtom.nodeType === ParseNodeType.PatternCapture &&
|
696
|
-
!patternAtom.isStar &&
|
697
|
-
!patternAtom.isWildcard
|
698
|
-
) {
|
699
|
-
if (captureTargetMap.has(patternAtom.target.value)) {
|
700
|
-
this._addError(
|
701
|
-
Localizer.Diagnostic.duplicateCapturePatternTarget().format({
|
702
|
-
name: patternAtom.target.value,
|
703
|
-
}),
|
704
|
-
patternAtom
|
705
|
-
);
|
706
|
-
} else {
|
707
|
-
captureTargetMap.set(patternAtom.target.value, patternAtom);
|
708
|
-
}
|
709
|
-
}
|
710
|
-
});
|
711
|
-
});
|
712
|
-
|
713
|
-
return patternList;
|
714
|
-
}
|
715
|
-
|
716
|
-
// as_pattern: or_pattern ['as' NAME]
|
717
|
-
// or_pattern: '|'.pattern_atom+
|
718
|
-
private _parsePatternAs(): PatternAsNode {
|
719
|
-
const orPatterns: PatternAtomNode[] = [];
|
720
|
-
|
721
|
-
while (true) {
|
722
|
-
const patternAtom = this._parsePatternAtom();
|
723
|
-
orPatterns.push(patternAtom);
|
724
|
-
|
725
|
-
if (!this._consumeTokenIfOperator(OperatorType.BitwiseOr)) {
|
726
|
-
break;
|
727
|
-
}
|
728
|
-
}
|
729
|
-
|
730
|
-
if (orPatterns.length > 1) {
|
731
|
-
// Star patterns cannot be ORed with other patterns.
|
732
|
-
orPatterns.forEach((patternAtom) => {
|
733
|
-
if (patternAtom.nodeType === ParseNodeType.PatternCapture && patternAtom.isStar) {
|
734
|
-
this._addError(Localizer.Diagnostic.starPatternInOrPattern(), patternAtom);
|
735
|
-
}
|
736
|
-
});
|
737
|
-
}
|
738
|
-
|
739
|
-
let target: NameNode | undefined;
|
740
|
-
if (this._consumeTokenIfKeyword(KeywordType.As)) {
|
741
|
-
const nameToken = this._getTokenIfIdentifier();
|
742
|
-
if (nameToken) {
|
743
|
-
target = NameNode.create(nameToken);
|
744
|
-
} else {
|
745
|
-
this._addError(Localizer.Diagnostic.expectedNameAfterAs(), this._peekToken());
|
746
|
-
}
|
747
|
-
}
|
748
|
-
|
749
|
-
// Star patterns cannot be used with AS pattern.
|
750
|
-
if (
|
751
|
-
target &&
|
752
|
-
orPatterns.length === 1 &&
|
753
|
-
orPatterns[0].nodeType === ParseNodeType.PatternCapture &&
|
754
|
-
orPatterns[0].isStar
|
755
|
-
) {
|
756
|
-
this._addError(Localizer.Diagnostic.starPatternInAsPattern(), orPatterns[0]);
|
757
|
-
}
|
758
|
-
|
759
|
-
// Validate that irrefutable patterns are not in any entries other than the last.
|
760
|
-
orPatterns.forEach((orPattern, index) => {
|
761
|
-
if (index < orPatterns.length - 1 && this._isPatternIrrefutable(orPattern)) {
|
762
|
-
this._addError(Localizer.Diagnostic.orPatternIrrefutable(), orPattern);
|
763
|
-
}
|
764
|
-
});
|
765
|
-
|
766
|
-
// Validate that all bound variables are the same within all or patterns.
|
767
|
-
const fullNameMap = new Map<string, boolean>();
|
768
|
-
orPatterns.forEach((orPattern) => {
|
769
|
-
this._getPatternTargetNames(orPattern, fullNameMap);
|
770
|
-
});
|
771
|
-
|
772
|
-
orPatterns.forEach((orPattern) => {
|
773
|
-
const localNameMap = new Map<string, boolean>();
|
774
|
-
this._getPatternTargetNames(orPattern, localNameMap);
|
775
|
-
|
776
|
-
if (localNameMap.size < fullNameMap.size) {
|
777
|
-
const missingNames = Array.from(fullNameMap.keys()).filter((name) => !localNameMap.has(name));
|
778
|
-
const diag = new DiagnosticAddendum();
|
779
|
-
diag.addMessage(
|
780
|
-
Localizer.DiagnosticAddendum.orPatternMissingName().format({
|
781
|
-
name: missingNames.map((name) => `"${name}"`).join(', '),
|
782
|
-
})
|
783
|
-
);
|
784
|
-
this._addError(Localizer.Diagnostic.orPatternMissingName() + diag.getString(), orPattern);
|
785
|
-
}
|
786
|
-
});
|
787
|
-
|
788
|
-
return PatternAsNode.create(orPatterns, target);
|
789
|
-
}
|
790
|
-
|
791
|
-
// pattern_atom:
|
792
|
-
// | literal_pattern
|
793
|
-
// | name_or_attr
|
794
|
-
// | '(' as_pattern ')'
|
795
|
-
// | '[' [sequence_pattern] ']'
|
796
|
-
// | '(' [sequence_pattern] ')'
|
797
|
-
// | '{' [items_pattern] '}'
|
798
|
-
// | name_or_attr '(' [pattern_arguments ','?] ')'
|
799
|
-
// name_or_attr: attr | NAME
|
800
|
-
// attr: name_or_attr '.' NAME
|
801
|
-
// sequence_pattern: ','.maybe_star_pattern+ ','?
|
802
|
-
// maybe_star_pattern: '*' NAME | pattern
|
803
|
-
// items_pattern: ','.key_value_pattern+ ','?
|
804
|
-
private _parsePatternAtom(): PatternAtomNode {
|
805
|
-
const patternLiteral = this._parsePatternLiteral();
|
806
|
-
if (patternLiteral) {
|
807
|
-
return patternLiteral;
|
808
|
-
}
|
809
|
-
|
810
|
-
const patternCaptureOrValue = this._parsePatternCaptureOrValue();
|
811
|
-
if (patternCaptureOrValue) {
|
812
|
-
const openParenToken = this._peekToken();
|
813
|
-
if (
|
814
|
-
patternCaptureOrValue.nodeType === ParseNodeType.Error ||
|
815
|
-
!this._consumeTokenIfType(TokenType.OpenParenthesis)
|
816
|
-
) {
|
817
|
-
return patternCaptureOrValue;
|
818
|
-
}
|
819
|
-
|
820
|
-
const args = this._parseClassPatternArgList();
|
821
|
-
|
822
|
-
const classNameExpr =
|
823
|
-
patternCaptureOrValue.nodeType === ParseNodeType.PatternCapture
|
824
|
-
? patternCaptureOrValue.target
|
825
|
-
: patternCaptureOrValue.expression;
|
826
|
-
const classPattern = PatternClassNode.create(classNameExpr, args);
|
827
|
-
|
828
|
-
if (!this._consumeTokenIfType(TokenType.CloseParenthesis)) {
|
829
|
-
this._addError(Localizer.Diagnostic.expectedCloseParen(), openParenToken);
|
830
|
-
|
831
|
-
// Consume the remainder of tokens on the line for error
|
832
|
-
// recovery.
|
833
|
-
this._consumeTokensUntilType([TokenType.NewLine]);
|
834
|
-
|
835
|
-
// Extend the node's range to include the rest of the line.
|
836
|
-
// This helps the signatureHelpProvider.
|
837
|
-
extendRange(classPattern, this._peekToken());
|
838
|
-
}
|
839
|
-
|
840
|
-
return classPattern;
|
841
|
-
}
|
842
|
-
|
843
|
-
const nextToken = this._peekToken();
|
844
|
-
const nextOperator = this._peekOperatorType();
|
845
|
-
|
846
|
-
if (nextOperator === OperatorType.Multiply) {
|
847
|
-
const starToken = this._getNextToken();
|
848
|
-
const identifierToken = this._getTokenIfIdentifier();
|
849
|
-
if (!identifierToken) {
|
850
|
-
this._addError(Localizer.Diagnostic.expectedIdentifier(), this._peekToken());
|
851
|
-
return ErrorNode.create(starToken, ErrorExpressionCategory.MissingExpression);
|
852
|
-
} else {
|
853
|
-
return PatternCaptureNode.create(NameNode.create(identifierToken), starToken);
|
854
|
-
}
|
855
|
-
}
|
856
|
-
|
857
|
-
if (nextToken.type === TokenType.OpenParenthesis || nextToken.type === TokenType.OpenBracket) {
|
858
|
-
const startToken = this._getNextToken();
|
859
|
-
const patternList = this._parsePatternSequence();
|
860
|
-
let casePattern: PatternAtomNode;
|
861
|
-
|
862
|
-
if (patternList.parseError) {
|
863
|
-
casePattern = patternList.parseError;
|
864
|
-
} else if (
|
865
|
-
patternList.list.length === 1 &&
|
866
|
-
!patternList.trailingComma &&
|
867
|
-
startToken.type === TokenType.OpenParenthesis
|
868
|
-
) {
|
869
|
-
const pattern = patternList.list[0].orPatterns[0];
|
870
|
-
|
871
|
-
if (pattern.nodeType === ParseNodeType.PatternCapture && pattern.isStar) {
|
872
|
-
casePattern = PatternSequenceNode.create(startToken, patternList.list);
|
873
|
-
} else {
|
874
|
-
casePattern = patternList.list[0];
|
875
|
-
}
|
876
|
-
|
877
|
-
extendRange(casePattern, nextToken);
|
878
|
-
} else {
|
879
|
-
casePattern = PatternSequenceNode.create(startToken, patternList.list);
|
880
|
-
}
|
881
|
-
|
882
|
-
const endToken = this._peekToken();
|
883
|
-
if (
|
884
|
-
this._consumeTokenIfType(
|
885
|
-
nextToken.type === TokenType.OpenParenthesis ? TokenType.CloseParenthesis : TokenType.CloseBracket
|
886
|
-
)
|
887
|
-
) {
|
888
|
-
extendRange(casePattern, endToken);
|
889
|
-
} else {
|
890
|
-
this._addError(
|
891
|
-
nextToken.type === TokenType.OpenParenthesis
|
892
|
-
? Localizer.Diagnostic.expectedCloseParen()
|
893
|
-
: Localizer.Diagnostic.expectedCloseBracket(),
|
894
|
-
nextToken
|
895
|
-
);
|
896
|
-
this._consumeTokensUntilType([
|
897
|
-
TokenType.Colon,
|
898
|
-
nextToken.type === TokenType.OpenParenthesis ? TokenType.CloseParenthesis : TokenType.CloseBracket,
|
899
|
-
]);
|
900
|
-
}
|
901
|
-
|
902
|
-
return casePattern;
|
903
|
-
} else if (nextToken.type === TokenType.OpenCurlyBrace) {
|
904
|
-
const firstToken = this._getNextToken();
|
905
|
-
const mappingPattern = this._parsePatternMapping(firstToken);
|
906
|
-
const lastToken = this._peekToken();
|
907
|
-
|
908
|
-
if (this._consumeTokenIfType(TokenType.CloseCurlyBrace)) {
|
909
|
-
extendRange(mappingPattern, lastToken);
|
910
|
-
} else {
|
911
|
-
this._addError(Localizer.Diagnostic.expectedCloseBrace(), nextToken);
|
912
|
-
this._consumeTokensUntilType([TokenType.Colon, TokenType.CloseCurlyBrace]);
|
913
|
-
}
|
914
|
-
|
915
|
-
return mappingPattern;
|
916
|
-
}
|
917
|
-
|
918
|
-
return this._handleExpressionParseError(
|
919
|
-
ErrorExpressionCategory.MissingPattern,
|
920
|
-
Localizer.Diagnostic.expectedPatternExpr()
|
921
|
-
);
|
922
|
-
}
|
923
|
-
|
924
|
-
// pattern_arguments:
|
925
|
-
// | positional_patterns [',' keyword_patterns]
|
926
|
-
// | keyword_patterns
|
927
|
-
// positional_patterns: ','.as_pattern+
|
928
|
-
// keyword_patterns: ','.keyword_pattern+
|
929
|
-
private _parseClassPatternArgList(): PatternClassArgumentNode[] {
|
930
|
-
const argList: PatternClassArgumentNode[] = [];
|
931
|
-
let sawKeywordArg = false;
|
932
|
-
|
933
|
-
while (true) {
|
934
|
-
const nextTokenType = this._peekTokenType();
|
935
|
-
if (
|
936
|
-
nextTokenType === TokenType.CloseParenthesis ||
|
937
|
-
nextTokenType === TokenType.NewLine ||
|
938
|
-
nextTokenType === TokenType.EndOfStream
|
939
|
-
) {
|
940
|
-
break;
|
941
|
-
}
|
942
|
-
|
943
|
-
const arg = this._parseClassPatternArgument();
|
944
|
-
if (arg.name) {
|
945
|
-
sawKeywordArg = true;
|
946
|
-
} else if (sawKeywordArg && !arg.name) {
|
947
|
-
this._addError(Localizer.Diagnostic.positionArgAfterNamedArg(), arg);
|
948
|
-
}
|
949
|
-
argList.push(arg);
|
950
|
-
|
951
|
-
if (!this._consumeTokenIfType(TokenType.Comma)) {
|
952
|
-
break;
|
953
|
-
}
|
954
|
-
}
|
955
|
-
|
956
|
-
return argList;
|
957
|
-
}
|
958
|
-
|
959
|
-
// keyword_pattern: NAME '=' as_pattern
|
960
|
-
private _parseClassPatternArgument(): PatternClassArgumentNode {
|
961
|
-
const firstToken = this._peekToken();
|
962
|
-
const secondToken = this._peekToken(1);
|
963
|
-
|
964
|
-
let keywordName: NameNode | undefined;
|
965
|
-
|
966
|
-
if (
|
967
|
-
(firstToken.type === TokenType.Identifier || firstToken.type === TokenType.Keyword) &&
|
968
|
-
secondToken.type === TokenType.Operator &&
|
969
|
-
(secondToken as OperatorToken).operatorType === OperatorType.Assign
|
970
|
-
) {
|
971
|
-
this._getNextToken();
|
972
|
-
keywordName = NameNode.create(firstToken as IdentifierToken);
|
973
|
-
this._getNextToken();
|
974
|
-
}
|
975
|
-
|
976
|
-
const pattern = this._parsePatternAs();
|
977
|
-
|
978
|
-
return PatternClassArgumentNode.create(pattern, keywordName);
|
979
|
-
}
|
980
|
-
|
981
|
-
// literal_pattern:
|
982
|
-
// | signed_number
|
983
|
-
// | signed_number '+' NUMBER
|
984
|
-
// | signed_number '-' NUMBER
|
985
|
-
// | strings
|
986
|
-
// | 'None'
|
987
|
-
// | 'True'
|
988
|
-
// | 'False'
|
989
|
-
private _parsePatternLiteral(): PatternLiteralNode | undefined {
|
990
|
-
const nextToken = this._peekToken();
|
991
|
-
const nextOperator = this._peekOperatorType();
|
992
|
-
|
993
|
-
if (nextToken.type === TokenType.Number || nextOperator === OperatorType.Subtract) {
|
994
|
-
return this._parsePatternLiteralNumber();
|
995
|
-
}
|
996
|
-
|
997
|
-
if (nextToken.type === TokenType.String) {
|
998
|
-
const stringList = this._parseAtom() as StringListNode;
|
999
|
-
assert(stringList.nodeType === ParseNodeType.StringList);
|
1000
|
-
|
1001
|
-
// Check for f-strings, which are not allowed.
|
1002
|
-
stringList.strings.forEach((stringAtom) => {
|
1003
|
-
if (stringAtom.token.flags & StringTokenFlags.Format) {
|
1004
|
-
this._addError(Localizer.Diagnostic.formatStringInPattern(), stringAtom);
|
1005
|
-
}
|
1006
|
-
});
|
1007
|
-
|
1008
|
-
return PatternLiteralNode.create(stringList);
|
1009
|
-
}
|
1010
|
-
|
1011
|
-
if (nextToken.type === TokenType.Keyword) {
|
1012
|
-
const keywordToken = nextToken as KeywordToken;
|
1013
|
-
if (
|
1014
|
-
keywordToken.keywordType === KeywordType.False ||
|
1015
|
-
keywordToken.keywordType === KeywordType.True ||
|
1016
|
-
keywordToken.keywordType === KeywordType.None
|
1017
|
-
) {
|
1018
|
-
return PatternLiteralNode.create(this._parseAtom());
|
1019
|
-
}
|
1020
|
-
}
|
1021
|
-
|
1022
|
-
return undefined;
|
1023
|
-
}
|
1024
|
-
|
1025
|
-
// signed_number: NUMBER | '-' NUMBER
|
1026
|
-
private _parsePatternLiteralNumber(): PatternLiteralNode {
|
1027
|
-
const expression = this._parseArithmeticExpression();
|
1028
|
-
let realValue: ExpressionNode | undefined;
|
1029
|
-
let imagValue: ExpressionNode | undefined;
|
1030
|
-
|
1031
|
-
if (expression.nodeType === ParseNodeType.BinaryOperation) {
|
1032
|
-
if (expression.operator === OperatorType.Subtract || expression.operator === OperatorType.Add) {
|
1033
|
-
realValue = expression.leftExpression;
|
1034
|
-
imagValue = expression.rightExpression;
|
1035
|
-
}
|
1036
|
-
} else {
|
1037
|
-
realValue = expression;
|
1038
|
-
}
|
1039
|
-
|
1040
|
-
if (realValue) {
|
1041
|
-
if (realValue.nodeType === ParseNodeType.UnaryOperation && realValue.operator === OperatorType.Subtract) {
|
1042
|
-
realValue = realValue.expression;
|
1043
|
-
}
|
1044
|
-
|
1045
|
-
if (realValue.nodeType !== ParseNodeType.Number || (imagValue !== undefined && realValue.isImaginary)) {
|
1046
|
-
this._addError(Localizer.Diagnostic.expectedComplexNumberLiteral(), expression);
|
1047
|
-
imagValue = undefined;
|
1048
|
-
}
|
1049
|
-
}
|
1050
|
-
|
1051
|
-
if (imagValue) {
|
1052
|
-
if (imagValue.nodeType === ParseNodeType.UnaryOperation && imagValue.operator === OperatorType.Subtract) {
|
1053
|
-
imagValue = imagValue.expression;
|
1054
|
-
}
|
1055
|
-
|
1056
|
-
if (imagValue.nodeType !== ParseNodeType.Number || !imagValue.isImaginary) {
|
1057
|
-
this._addError(Localizer.Diagnostic.expectedComplexNumberLiteral(), expression);
|
1058
|
-
}
|
1059
|
-
}
|
1060
|
-
|
1061
|
-
return PatternLiteralNode.create(expression);
|
1062
|
-
}
|
1063
|
-
|
1064
|
-
private _parsePatternMapping(firstToken: Token): PatternMappingNode | ErrorNode {
|
1065
|
-
const itemList = this._parseExpressionListGeneric(() => this._parsePatternMappingItem());
|
1066
|
-
|
1067
|
-
if (itemList.list.length > 0) {
|
1068
|
-
// Verify there's at most one ** entry.
|
1069
|
-
const starStarEntries = itemList.list.filter(
|
1070
|
-
(entry) => entry.nodeType === ParseNodeType.PatternMappingExpandEntry
|
1071
|
-
);
|
1072
|
-
if (starStarEntries.length > 1) {
|
1073
|
-
this._addError(Localizer.Diagnostic.duplicateStarStarPattern(), starStarEntries[1]);
|
1074
|
-
}
|
1075
|
-
|
1076
|
-
return PatternMappingNode.create(firstToken, itemList.list);
|
1077
|
-
}
|
1078
|
-
|
1079
|
-
return itemList.parseError || ErrorNode.create(this._peekToken(), ErrorExpressionCategory.MissingPattern);
|
1080
|
-
}
|
1081
|
-
|
1082
|
-
// key_value_pattern:
|
1083
|
-
// | (literal_pattern | attr) ':' as_pattern
|
1084
|
-
// | '**' NAME
|
1085
|
-
private _parsePatternMappingItem(): PatternMappingEntryNode | ErrorNode {
|
1086
|
-
let keyExpression: PatternLiteralNode | PatternValueNode | ErrorNode | undefined;
|
1087
|
-
const doubleStar = this._peekToken();
|
1088
|
-
|
1089
|
-
if (this._consumeTokenIfOperator(OperatorType.Power)) {
|
1090
|
-
const identifierToken = this._getTokenIfIdentifier();
|
1091
|
-
if (!identifierToken) {
|
1092
|
-
this._addError(Localizer.Diagnostic.expectedIdentifier(), this._peekToken());
|
1093
|
-
return ErrorNode.create(this._peekToken(), ErrorExpressionCategory.MissingPattern);
|
1094
|
-
}
|
1095
|
-
|
1096
|
-
const nameNode = NameNode.create(identifierToken);
|
1097
|
-
if (identifierToken.value === '_') {
|
1098
|
-
this._addError(Localizer.Diagnostic.starStarWildcardNotAllowed(), nameNode);
|
1099
|
-
}
|
1100
|
-
|
1101
|
-
return PatternMappingExpandEntryNode.create(doubleStar, nameNode);
|
1102
|
-
}
|
1103
|
-
|
1104
|
-
const patternLiteral = this._parsePatternLiteral();
|
1105
|
-
if (patternLiteral) {
|
1106
|
-
keyExpression = patternLiteral;
|
1107
|
-
} else {
|
1108
|
-
const patternCaptureOrValue = this._parsePatternCaptureOrValue();
|
1109
|
-
if (patternCaptureOrValue) {
|
1110
|
-
if (patternCaptureOrValue.nodeType === ParseNodeType.PatternValue) {
|
1111
|
-
keyExpression = patternCaptureOrValue;
|
1112
|
-
} else {
|
1113
|
-
this._addError(Localizer.Diagnostic.expectedPatternValue(), patternCaptureOrValue);
|
1114
|
-
keyExpression = ErrorNode.create(this._peekToken(), ErrorExpressionCategory.MissingPattern);
|
1115
|
-
}
|
1116
|
-
}
|
1117
|
-
}
|
1118
|
-
|
1119
|
-
if (!keyExpression) {
|
1120
|
-
this._addError(Localizer.Diagnostic.expectedPatternExpr(), this._peekToken());
|
1121
|
-
keyExpression = ErrorNode.create(this._peekToken(), ErrorExpressionCategory.MissingPattern);
|
1122
|
-
}
|
1123
|
-
|
1124
|
-
let valuePattern: PatternAtomNode | undefined;
|
1125
|
-
if (!this._consumeTokenIfType(TokenType.Colon)) {
|
1126
|
-
this._addError(Localizer.Diagnostic.expectedColon(), this._peekToken());
|
1127
|
-
valuePattern = ErrorNode.create(this._peekToken(), ErrorExpressionCategory.MissingPattern);
|
1128
|
-
} else {
|
1129
|
-
valuePattern = this._parsePatternAs();
|
1130
|
-
}
|
1131
|
-
|
1132
|
-
return PatternMappingKeyEntryNode.create(keyExpression, valuePattern);
|
1133
|
-
}
|
1134
|
-
|
1135
|
-
private _parsePatternCaptureOrValue(): PatternCaptureNode | PatternValueNode | ErrorNode | undefined {
|
1136
|
-
const nextToken = this._peekToken();
|
1137
|
-
|
1138
|
-
if (nextToken.type === TokenType.Identifier || nextToken.type === TokenType.Keyword) {
|
1139
|
-
let nameOrMember: NameNode | MemberAccessNode | undefined;
|
1140
|
-
|
1141
|
-
while (true) {
|
1142
|
-
const identifierToken = this._getTokenIfIdentifier();
|
1143
|
-
if (identifierToken) {
|
1144
|
-
const nameNode = NameNode.create(identifierToken);
|
1145
|
-
nameOrMember = nameOrMember ? MemberAccessNode.create(nameOrMember, nameNode) : nameNode;
|
1146
|
-
} else {
|
1147
|
-
this._addError(Localizer.Diagnostic.expectedIdentifier(), this._peekToken());
|
1148
|
-
break;
|
1149
|
-
}
|
1150
|
-
|
1151
|
-
if (!this._consumeTokenIfType(TokenType.Dot)) {
|
1152
|
-
break;
|
1153
|
-
}
|
1154
|
-
}
|
1155
|
-
|
1156
|
-
if (!nameOrMember) {
|
1157
|
-
this._addError(Localizer.Diagnostic.expectedIdentifier(), this._peekToken());
|
1158
|
-
return ErrorNode.create(this._peekToken(), ErrorExpressionCategory.MissingPattern);
|
1159
|
-
}
|
1160
|
-
|
1161
|
-
if (nameOrMember.nodeType === ParseNodeType.MemberAccess) {
|
1162
|
-
return PatternValueNode.create(nameOrMember);
|
1163
|
-
}
|
1164
|
-
|
1165
|
-
return PatternCaptureNode.create(nameOrMember);
|
1166
|
-
}
|
1167
|
-
|
1168
|
-
return undefined;
|
1169
|
-
}
|
1170
|
-
|
1171
|
-
// if_stmt: 'if' test_suite ('elif' test_suite)* ['else' suite]
|
1172
|
-
// test_suite: test suite
|
1173
|
-
// test: or_test ['if' or_test 'else' test] | lambdef
|
1174
|
-
private _parseIfStatement(keywordType: KeywordType.If | KeywordType.Elif = KeywordType.If): IfNode {
|
1175
|
-
const ifOrElifToken = this._getKeywordToken(keywordType);
|
1176
|
-
|
1177
|
-
const test = this._parseTestExpression(/* allowAssignmentExpression */ true);
|
1178
|
-
const suite = this._parseSuite(this._isInFunction);
|
1179
|
-
const ifNode = IfNode.create(ifOrElifToken, test, suite);
|
1180
|
-
|
1181
|
-
if (this._consumeTokenIfKeyword(KeywordType.Else)) {
|
1182
|
-
ifNode.elseSuite = this._parseSuite(this._isInFunction);
|
1183
|
-
ifNode.elseSuite.parent = ifNode;
|
1184
|
-
extendRange(ifNode, ifNode.elseSuite);
|
1185
|
-
} else if (this._peekKeywordType() === KeywordType.Elif) {
|
1186
|
-
// Recursively handle an "elif" statement.
|
1187
|
-
ifNode.elseSuite = this._parseIfStatement(KeywordType.Elif);
|
1188
|
-
ifNode.elseSuite.parent = ifNode;
|
1189
|
-
extendRange(ifNode, ifNode.elseSuite);
|
1190
|
-
}
|
1191
|
-
|
1192
|
-
return ifNode;
|
1193
|
-
}
|
1194
|
-
|
1195
|
-
private _parseLoopSuite(disallowTypeComment = false): SuiteNode {
|
1196
|
-
const wasInLoop = this._isInLoop;
|
1197
|
-
const wasInFinally = this._isInFinally;
|
1198
|
-
this._isInLoop = true;
|
1199
|
-
this._isInFinally = false;
|
1200
|
-
|
1201
|
-
const suite = this._parseSuite(this._isInFunction, /* skipBody */ false, () => {
|
1202
|
-
if (disallowTypeComment) {
|
1203
|
-
this._detectUnsupportedTypeAnnotationComment();
|
1204
|
-
}
|
1205
|
-
});
|
1206
|
-
|
1207
|
-
this._isInLoop = wasInLoop;
|
1208
|
-
this._isInFinally = wasInFinally;
|
1209
|
-
|
1210
|
-
return suite;
|
1211
|
-
}
|
1212
|
-
|
1213
|
-
// suite: ':' (simple_stmt | NEWLINE INDENT stmt+ DEDENT)
|
1214
|
-
private _parseSuite(isFunction = false, skipBody = false, postColonCallback?: () => void): SuiteNode {
|
1215
|
-
const nextToken = this._peekToken();
|
1216
|
-
const suite = SuiteNode.create(nextToken);
|
1217
|
-
|
1218
|
-
if (!this._consumeTokenIfType(TokenType.Colon)) {
|
1219
|
-
this._addError(Localizer.Diagnostic.expectedColon(), nextToken);
|
1220
|
-
|
1221
|
-
// Try to perform parse recovery by consuming tokens.
|
1222
|
-
if (this._consumeTokensUntilType([TokenType.NewLine, TokenType.Colon])) {
|
1223
|
-
if (this._peekTokenType() === TokenType.Colon) {
|
1224
|
-
this._getNextToken();
|
1225
|
-
} else if (this._peekToken(1).type !== TokenType.Indent) {
|
1226
|
-
// Bail so we resume the at the next statement.
|
1227
|
-
// We can't parse as a simple statement as we've skipped all but the newline.
|
1228
|
-
this._getNextToken();
|
1229
|
-
return suite;
|
1230
|
-
}
|
1231
|
-
}
|
1232
|
-
}
|
1233
|
-
|
1234
|
-
if (skipBody) {
|
1235
|
-
if (this._consumeTokenIfType(TokenType.NewLine)) {
|
1236
|
-
let indent = 0;
|
1237
|
-
while (true) {
|
1238
|
-
const nextToken = this._getNextToken();
|
1239
|
-
if (nextToken.type === TokenType.Indent) {
|
1240
|
-
indent++;
|
1241
|
-
}
|
1242
|
-
|
1243
|
-
if (nextToken.type === TokenType.Dedent) {
|
1244
|
-
if ((nextToken as DedentToken).isDedentAmbiguous) {
|
1245
|
-
this._addError(Localizer.Diagnostic.inconsistentTabs(), nextToken);
|
1246
|
-
}
|
1247
|
-
|
1248
|
-
indent--;
|
1249
|
-
|
1250
|
-
if (indent === 0) {
|
1251
|
-
break;
|
1252
|
-
}
|
1253
|
-
}
|
1254
|
-
|
1255
|
-
if (nextToken.type === TokenType.EndOfStream) {
|
1256
|
-
break;
|
1257
|
-
}
|
1258
|
-
}
|
1259
|
-
} else {
|
1260
|
-
// consume tokens
|
1261
|
-
this._parseSimpleStatement();
|
1262
|
-
}
|
1263
|
-
|
1264
|
-
if (this._tokenIndex > 0) {
|
1265
|
-
extendRange(suite, this._tokenizerOutput!.tokens.getItemAt(this._tokenIndex - 1));
|
1266
|
-
}
|
1267
|
-
|
1268
|
-
return suite;
|
1269
|
-
}
|
1270
|
-
|
1271
|
-
if (postColonCallback) {
|
1272
|
-
postColonCallback();
|
1273
|
-
}
|
1274
|
-
|
1275
|
-
const wasFunction = this._isInFunction;
|
1276
|
-
this._isInFunction = isFunction;
|
1277
|
-
|
1278
|
-
if (this._consumeTokenIfType(TokenType.NewLine)) {
|
1279
|
-
if (postColonCallback) {
|
1280
|
-
postColonCallback();
|
1281
|
-
}
|
1282
|
-
|
1283
|
-
const possibleIndent = this._peekToken();
|
1284
|
-
if (!this._consumeTokenIfType(TokenType.Indent)) {
|
1285
|
-
this._addError(Localizer.Diagnostic.expectedIndentedBlock(), this._peekToken());
|
1286
|
-
} else {
|
1287
|
-
const indentToken = possibleIndent as IndentToken;
|
1288
|
-
if (indentToken.isIndentAmbiguous) {
|
1289
|
-
this._addError(Localizer.Diagnostic.inconsistentTabs(), indentToken);
|
1290
|
-
}
|
1291
|
-
}
|
1292
|
-
|
1293
|
-
while (true) {
|
1294
|
-
// Handle a common error here and see if we can recover.
|
1295
|
-
const nextToken = this._peekToken();
|
1296
|
-
if (nextToken.type === TokenType.Indent) {
|
1297
|
-
this._getNextToken();
|
1298
|
-
const indentToken = nextToken as IndentToken;
|
1299
|
-
if (indentToken.isIndentAmbiguous) {
|
1300
|
-
this._addError(Localizer.Diagnostic.inconsistentTabs(), indentToken);
|
1301
|
-
} else {
|
1302
|
-
this._addError(Localizer.Diagnostic.unexpectedIndent(), nextToken);
|
1303
|
-
}
|
1304
|
-
}
|
1305
|
-
|
1306
|
-
const statement = this._parseStatement();
|
1307
|
-
if (!statement) {
|
1308
|
-
// Perform basic error recovery to get to the next line.
|
1309
|
-
this._consumeTokensUntilType([TokenType.NewLine]);
|
1310
|
-
} else {
|
1311
|
-
statement.parent = suite;
|
1312
|
-
suite.statements.push(statement);
|
1313
|
-
}
|
1314
|
-
|
1315
|
-
const dedentToken = this._peekToken() as DedentToken;
|
1316
|
-
if (this._consumeTokenIfType(TokenType.Dedent)) {
|
1317
|
-
if (!dedentToken.matchesIndent) {
|
1318
|
-
this._addError(Localizer.Diagnostic.inconsistentIndent(), dedentToken);
|
1319
|
-
}
|
1320
|
-
if (dedentToken.isDedentAmbiguous) {
|
1321
|
-
this._addError(Localizer.Diagnostic.inconsistentTabs(), dedentToken);
|
1322
|
-
}
|
1323
|
-
break;
|
1324
|
-
}
|
1325
|
-
|
1326
|
-
if (this._peekTokenType() === TokenType.EndOfStream) {
|
1327
|
-
break;
|
1328
|
-
}
|
1329
|
-
}
|
1330
|
-
} else {
|
1331
|
-
const simpleStatement = this._parseSimpleStatement();
|
1332
|
-
suite.statements.push(simpleStatement);
|
1333
|
-
simpleStatement.parent = suite;
|
1334
|
-
}
|
1335
|
-
|
1336
|
-
if (suite.statements.length > 0) {
|
1337
|
-
extendRange(suite, suite.statements[suite.statements.length - 1]);
|
1338
|
-
}
|
1339
|
-
|
1340
|
-
this._isInFunction = wasFunction;
|
1341
|
-
|
1342
|
-
return suite;
|
1343
|
-
}
|
1344
|
-
|
1345
|
-
// for_stmt: [async] 'for' exprlist 'in' testlist suite ['else' suite]
|
1346
|
-
private _parseForStatement(asyncToken?: KeywordToken): ForNode {
|
1347
|
-
const forToken = this._getKeywordToken(KeywordType.For);
|
1348
|
-
|
1349
|
-
const targetExpr = this._parseExpressionListAsPossibleTuple(
|
1350
|
-
ErrorExpressionCategory.MissingExpression,
|
1351
|
-
Localizer.Diagnostic.expectedExpr(),
|
1352
|
-
forToken
|
1353
|
-
);
|
1354
|
-
|
1355
|
-
let seqExpr: ExpressionNode;
|
1356
|
-
let forSuite: SuiteNode;
|
1357
|
-
let elseSuite: SuiteNode | undefined;
|
1358
|
-
|
1359
|
-
if (!this._consumeTokenIfKeyword(KeywordType.In)) {
|
1360
|
-
seqExpr = this._handleExpressionParseError(
|
1361
|
-
ErrorExpressionCategory.MissingIn,
|
1362
|
-
Localizer.Diagnostic.expectedIn()
|
1363
|
-
);
|
1364
|
-
forSuite = SuiteNode.create(this._peekToken());
|
1365
|
-
} else {
|
1366
|
-
seqExpr = this._parseTestOrStarListAsExpression(
|
1367
|
-
/* allowAssignmentExpression */ false,
|
1368
|
-
/* allowMultipleUnpack */ true,
|
1369
|
-
ErrorExpressionCategory.MissingExpression,
|
1370
|
-
Localizer.Diagnostic.expectedInExpr()
|
1371
|
-
);
|
1372
|
-
|
1373
|
-
forSuite = this._parseLoopSuite(/* disallowTypeComment */ true);
|
1374
|
-
|
1375
|
-
// Versions of Python earlier than 3.9 didn't allow unpack operators if the
|
1376
|
-
// tuple wasn't enclosed in parentheses.
|
1377
|
-
if (this._getLanguageVersion() < PythonVersion.V3_9 && !this._parseOptions.isStubFile) {
|
1378
|
-
if (seqExpr.nodeType === ParseNodeType.Tuple && !seqExpr.enclosedInParens) {
|
1379
|
-
let sawStar = false;
|
1380
|
-
seqExpr.expressions.forEach((expr) => {
|
1381
|
-
if (expr.nodeType === ParseNodeType.Unpack && !sawStar) {
|
1382
|
-
this._addError(Localizer.Diagnostic.unpackOperatorNotAllowed(), expr);
|
1383
|
-
sawStar = true;
|
1384
|
-
}
|
1385
|
-
});
|
1386
|
-
}
|
1387
|
-
}
|
1388
|
-
|
1389
|
-
if (this._consumeTokenIfKeyword(KeywordType.Else)) {
|
1390
|
-
elseSuite = this._parseSuite(this._isInFunction);
|
1391
|
-
}
|
1392
|
-
}
|
1393
|
-
|
1394
|
-
const forNode = ForNode.create(forToken, targetExpr, seqExpr, forSuite);
|
1395
|
-
forNode.elseSuite = elseSuite;
|
1396
|
-
if (elseSuite) {
|
1397
|
-
extendRange(forNode, elseSuite);
|
1398
|
-
elseSuite.parent = forNode;
|
1399
|
-
}
|
1400
|
-
|
1401
|
-
if (asyncToken) {
|
1402
|
-
forNode.isAsync = true;
|
1403
|
-
forNode.asyncToken = asyncToken;
|
1404
|
-
extendRange(forNode, asyncToken);
|
1405
|
-
}
|
1406
|
-
|
1407
|
-
return forNode;
|
1408
|
-
}
|
1409
|
-
|
1410
|
-
// comp_iter: comp_for | comp_if
|
1411
|
-
private _tryParseListComprehension(target: ParseNode): ListComprehensionNode | undefined {
|
1412
|
-
const compFor = this._tryParseCompForStatement();
|
1413
|
-
|
1414
|
-
if (!compFor) {
|
1415
|
-
return undefined;
|
1416
|
-
}
|
1417
|
-
|
1418
|
-
if (target.nodeType === ParseNodeType.Unpack) {
|
1419
|
-
this._addError(Localizer.Diagnostic.unpackIllegalInComprehension(), target);
|
1420
|
-
} else if (target.nodeType === ParseNodeType.DictionaryExpandEntry) {
|
1421
|
-
this._addError(Localizer.Diagnostic.dictExpandIllegalInComprehension(), target);
|
1422
|
-
}
|
1423
|
-
|
1424
|
-
const listCompNode = ListComprehensionNode.create(target);
|
1425
|
-
|
1426
|
-
const forIfList: ListComprehensionForIfNode[] = [compFor];
|
1427
|
-
while (true) {
|
1428
|
-
const compIter = this._tryParseCompForStatement() || this._tryParseCompIfStatement();
|
1429
|
-
if (!compIter) {
|
1430
|
-
break;
|
1431
|
-
}
|
1432
|
-
compIter.parent = listCompNode;
|
1433
|
-
forIfList.push(compIter);
|
1434
|
-
}
|
1435
|
-
|
1436
|
-
listCompNode.forIfNodes = forIfList;
|
1437
|
-
if (forIfList.length > 0) {
|
1438
|
-
forIfList.forEach((comp) => {
|
1439
|
-
comp.parent = listCompNode;
|
1440
|
-
});
|
1441
|
-
extendRange(listCompNode, forIfList[forIfList.length - 1]);
|
1442
|
-
}
|
1443
|
-
return listCompNode;
|
1444
|
-
}
|
1445
|
-
|
1446
|
-
// comp_for: ['async'] 'for' exprlist 'in' or_test [comp_iter]
|
1447
|
-
private _tryParseCompForStatement(): ListComprehensionForNode | undefined {
|
1448
|
-
const startTokenKeywordType = this._peekKeywordType();
|
1449
|
-
|
1450
|
-
if (startTokenKeywordType === KeywordType.Async) {
|
1451
|
-
const nextToken = this._peekToken(1) as KeywordToken;
|
1452
|
-
if (nextToken.type !== TokenType.Keyword || nextToken.keywordType !== KeywordType.For) {
|
1453
|
-
return undefined;
|
1454
|
-
}
|
1455
|
-
} else if (startTokenKeywordType !== KeywordType.For) {
|
1456
|
-
return undefined;
|
1457
|
-
}
|
1458
|
-
|
1459
|
-
let asyncToken: KeywordToken | undefined;
|
1460
|
-
if (this._peekKeywordType() === KeywordType.Async) {
|
1461
|
-
asyncToken = this._getKeywordToken(KeywordType.Async);
|
1462
|
-
}
|
1463
|
-
|
1464
|
-
const forToken = this._getKeywordToken(KeywordType.For);
|
1465
|
-
|
1466
|
-
const targetExpr = this._parseExpressionListAsPossibleTuple(
|
1467
|
-
ErrorExpressionCategory.MissingExpression,
|
1468
|
-
Localizer.Diagnostic.expectedExpr(),
|
1469
|
-
forToken
|
1470
|
-
);
|
1471
|
-
let seqExpr: ExpressionNode | undefined;
|
1472
|
-
|
1473
|
-
if (!this._consumeTokenIfKeyword(KeywordType.In)) {
|
1474
|
-
seqExpr = this._handleExpressionParseError(
|
1475
|
-
ErrorExpressionCategory.MissingIn,
|
1476
|
-
Localizer.Diagnostic.expectedIn()
|
1477
|
-
);
|
1478
|
-
} else {
|
1479
|
-
this._disallowAssignmentExpression(() => {
|
1480
|
-
seqExpr = this._parseOrTest();
|
1481
|
-
});
|
1482
|
-
}
|
1483
|
-
|
1484
|
-
const compForNode = ListComprehensionForNode.create(asyncToken || forToken, targetExpr, seqExpr!);
|
1485
|
-
|
1486
|
-
if (asyncToken) {
|
1487
|
-
compForNode.isAsync = true;
|
1488
|
-
compForNode.asyncToken = asyncToken;
|
1489
|
-
}
|
1490
|
-
|
1491
|
-
return compForNode;
|
1492
|
-
}
|
1493
|
-
|
1494
|
-
// comp_if: 'if' test_nocond [comp_iter]
|
1495
|
-
// comp_iter: comp_for | comp_if
|
1496
|
-
private _tryParseCompIfStatement(): ListComprehensionIfNode | undefined {
|
1497
|
-
if (this._peekKeywordType() !== KeywordType.If) {
|
1498
|
-
return undefined;
|
1499
|
-
}
|
1500
|
-
|
1501
|
-
const ifToken = this._getKeywordToken(KeywordType.If);
|
1502
|
-
const ifExpr =
|
1503
|
-
this._tryParseLambdaExpression() ||
|
1504
|
-
this._parseAssignmentExpression(/* disallowAssignmentExpression */ true);
|
1505
|
-
|
1506
|
-
const compIfNode = ListComprehensionIfNode.create(ifToken, ifExpr);
|
1507
|
-
|
1508
|
-
return compIfNode;
|
1509
|
-
}
|
1510
|
-
|
1511
|
-
// while_stmt: 'while' test suite ['else' suite]
|
1512
|
-
private _parseWhileStatement(): WhileNode {
|
1513
|
-
const whileToken = this._getKeywordToken(KeywordType.While);
|
1514
|
-
|
1515
|
-
const whileNode = WhileNode.create(
|
1516
|
-
whileToken,
|
1517
|
-
this._parseTestExpression(/* allowAssignmentExpression */ true),
|
1518
|
-
this._parseLoopSuite()
|
1519
|
-
);
|
1520
|
-
|
1521
|
-
if (this._consumeTokenIfKeyword(KeywordType.Else)) {
|
1522
|
-
whileNode.elseSuite = this._parseSuite(this._isInFunction);
|
1523
|
-
whileNode.elseSuite.parent = whileNode;
|
1524
|
-
extendRange(whileNode, whileNode.elseSuite);
|
1525
|
-
}
|
1526
|
-
|
1527
|
-
return whileNode;
|
1528
|
-
}
|
1529
|
-
|
1530
|
-
// try_stmt: ('try' suite
|
1531
|
-
// ((except_clause suite)+
|
1532
|
-
// ['else' suite]
|
1533
|
-
// ['finally' suite] |
|
1534
|
-
// 'finally' suite))
|
1535
|
-
// except_clause: 'except' [test ['as' NAME]]
|
1536
|
-
private _parseTryStatement(): TryNode {
|
1537
|
-
const tryToken = this._getKeywordToken(KeywordType.Try);
|
1538
|
-
const trySuite = this._parseSuite(this._isInFunction);
|
1539
|
-
const tryNode = TryNode.create(tryToken, trySuite);
|
1540
|
-
let sawCatchAllExcept = false;
|
1541
|
-
|
1542
|
-
while (true) {
|
1543
|
-
const exceptToken = this._peekToken();
|
1544
|
-
if (!this._consumeTokenIfKeyword(KeywordType.Except)) {
|
1545
|
-
break;
|
1546
|
-
}
|
1547
|
-
|
1548
|
-
// See if this is a Python 3.11 exception group.
|
1549
|
-
const possibleStarToken = this._peekToken();
|
1550
|
-
let isExceptGroup = false;
|
1551
|
-
if (this._consumeTokenIfOperator(OperatorType.Multiply)) {
|
1552
|
-
if (this._getLanguageVersion() < PythonVersion.V3_11 && !this._parseOptions.isStubFile) {
|
1553
|
-
this._addError(Localizer.Diagnostic.exceptionGroupIncompatible(), possibleStarToken);
|
1554
|
-
}
|
1555
|
-
isExceptGroup = true;
|
1556
|
-
}
|
1557
|
-
|
1558
|
-
let typeExpr: ExpressionNode | undefined;
|
1559
|
-
let symbolName: IdentifierToken | undefined;
|
1560
|
-
if (this._peekTokenType() !== TokenType.Colon) {
|
1561
|
-
typeExpr = this._parseTestExpression(/* allowAssignmentExpression */ true);
|
1562
|
-
|
1563
|
-
if (this._consumeTokenIfKeyword(KeywordType.As)) {
|
1564
|
-
symbolName = this._getTokenIfIdentifier();
|
1565
|
-
if (!symbolName) {
|
1566
|
-
this._addError(Localizer.Diagnostic.expectedNameAfterAs(), this._peekToken());
|
1567
|
-
}
|
1568
|
-
} else {
|
1569
|
-
// Handle the python 2.x syntax in a graceful manner.
|
1570
|
-
const peekToken = this._peekToken();
|
1571
|
-
if (this._consumeTokenIfType(TokenType.Comma)) {
|
1572
|
-
this._addError(Localizer.Diagnostic.expectedAsAfterException(), peekToken);
|
1573
|
-
|
1574
|
-
// Parse the expression expected in python 2.x, but discard it.
|
1575
|
-
this._parseTestExpression(/* allowAssignmentExpression */ false);
|
1576
|
-
}
|
1577
|
-
}
|
1578
|
-
}
|
1579
|
-
|
1580
|
-
if (!typeExpr) {
|
1581
|
-
if (sawCatchAllExcept) {
|
1582
|
-
this._addError(Localizer.Diagnostic.duplicateCatchAll(), exceptToken);
|
1583
|
-
}
|
1584
|
-
sawCatchAllExcept = true;
|
1585
|
-
} else {
|
1586
|
-
if (sawCatchAllExcept) {
|
1587
|
-
this._addError(Localizer.Diagnostic.namedExceptAfterCatchAll(), typeExpr);
|
1588
|
-
}
|
1589
|
-
}
|
1590
|
-
|
1591
|
-
const exceptSuite = this._parseSuite(this._isInFunction);
|
1592
|
-
const exceptNode = ExceptNode.create(exceptToken, exceptSuite, isExceptGroup);
|
1593
|
-
if (typeExpr) {
|
1594
|
-
exceptNode.typeExpression = typeExpr;
|
1595
|
-
exceptNode.typeExpression.parent = exceptNode;
|
1596
|
-
}
|
1597
|
-
|
1598
|
-
if (symbolName) {
|
1599
|
-
exceptNode.name = NameNode.create(symbolName);
|
1600
|
-
exceptNode.name.parent = exceptNode;
|
1601
|
-
}
|
1602
|
-
|
1603
|
-
tryNode.exceptClauses.push(exceptNode);
|
1604
|
-
exceptNode.parent = tryNode;
|
1605
|
-
}
|
1606
|
-
|
1607
|
-
if (tryNode.exceptClauses.length > 0) {
|
1608
|
-
extendRange(tryNode, tryNode.exceptClauses[tryNode.exceptClauses.length - 1]);
|
1609
|
-
|
1610
|
-
if (this._consumeTokenIfKeyword(KeywordType.Else)) {
|
1611
|
-
tryNode.elseSuite = this._parseSuite(this._isInFunction);
|
1612
|
-
tryNode.elseSuite.parent = tryNode;
|
1613
|
-
extendRange(tryNode, tryNode.elseSuite);
|
1614
|
-
}
|
1615
|
-
}
|
1616
|
-
|
1617
|
-
if (this._consumeTokenIfKeyword(KeywordType.Finally)) {
|
1618
|
-
tryNode.finallySuite = this._parseSuite(this._isInFunction);
|
1619
|
-
tryNode.finallySuite.parent = tryNode;
|
1620
|
-
extendRange(tryNode, tryNode.finallySuite);
|
1621
|
-
}
|
1622
|
-
|
1623
|
-
if (!tryNode.finallySuite && tryNode.exceptClauses.length === 0) {
|
1624
|
-
this._addError(Localizer.Diagnostic.tryWithoutExcept(), tryToken);
|
1625
|
-
}
|
1626
|
-
|
1627
|
-
return tryNode;
|
1628
|
-
}
|
1629
|
-
|
1630
|
-
// funcdef: 'def' NAME parameters ['->' test] ':' suite
|
1631
|
-
// parameters: '(' [typedargslist] ')'
|
1632
|
-
private _parseFunctionDef(asyncToken?: KeywordToken, decorators?: DecoratorNode[]): FunctionNode | ErrorNode {
|
1633
|
-
const defToken = this._getKeywordToken(KeywordType.Def);
|
1634
|
-
|
1635
|
-
const nameToken = this._getTokenIfIdentifier();
|
1636
|
-
if (!nameToken) {
|
1637
|
-
this._addError(Localizer.Diagnostic.expectedFunctionName(), defToken);
|
1638
|
-
return ErrorNode.create(
|
1639
|
-
defToken,
|
1640
|
-
ErrorExpressionCategory.MissingFunctionParameterList,
|
1641
|
-
undefined,
|
1642
|
-
decorators
|
1643
|
-
);
|
1644
|
-
}
|
1645
|
-
|
1646
|
-
const openParenToken = this._peekToken();
|
1647
|
-
if (!this._consumeTokenIfType(TokenType.OpenParenthesis)) {
|
1648
|
-
this._addError(Localizer.Diagnostic.expectedOpenParen(), this._peekToken());
|
1649
|
-
return ErrorNode.create(
|
1650
|
-
nameToken,
|
1651
|
-
ErrorExpressionCategory.MissingFunctionParameterList,
|
1652
|
-
NameNode.create(nameToken),
|
1653
|
-
decorators
|
1654
|
-
);
|
1655
|
-
}
|
1656
|
-
|
1657
|
-
const paramList = this._parseVarArgsList(TokenType.CloseParenthesis, /* allowAnnotations */ true);
|
1658
|
-
|
1659
|
-
if (!this._consumeTokenIfType(TokenType.CloseParenthesis)) {
|
1660
|
-
this._addError(Localizer.Diagnostic.expectedCloseParen(), openParenToken);
|
1661
|
-
this._consumeTokensUntilType([TokenType.Colon]);
|
1662
|
-
}
|
1663
|
-
|
1664
|
-
let returnType: ExpressionNode | undefined;
|
1665
|
-
if (this._consumeTokenIfType(TokenType.Arrow)) {
|
1666
|
-
returnType = this._parseTypeAnnotation();
|
1667
|
-
}
|
1668
|
-
|
1669
|
-
let functionTypeAnnotationToken: StringToken | undefined;
|
1670
|
-
const suite = this._parseSuite(/* isFunction */ true, this._parseOptions.skipFunctionAndClassBody, () => {
|
1671
|
-
if (!functionTypeAnnotationToken) {
|
1672
|
-
functionTypeAnnotationToken = this._getTypeAnnotationCommentText();
|
1673
|
-
}
|
1674
|
-
});
|
1675
|
-
|
1676
|
-
const functionNode = FunctionNode.create(defToken, NameNode.create(nameToken), suite);
|
1677
|
-
if (asyncToken) {
|
1678
|
-
functionNode.isAsync = true;
|
1679
|
-
extendRange(functionNode, asyncToken);
|
1680
|
-
}
|
1681
|
-
|
1682
|
-
functionNode.parameters = paramList;
|
1683
|
-
paramList.forEach((param) => {
|
1684
|
-
param.parent = functionNode;
|
1685
|
-
});
|
1686
|
-
|
1687
|
-
if (decorators) {
|
1688
|
-
functionNode.decorators = decorators;
|
1689
|
-
decorators.forEach((decorator) => {
|
1690
|
-
decorator.parent = functionNode;
|
1691
|
-
});
|
1692
|
-
|
1693
|
-
if (decorators.length > 0) {
|
1694
|
-
extendRange(functionNode, decorators[0]);
|
1695
|
-
}
|
1696
|
-
}
|
1697
|
-
|
1698
|
-
if (returnType) {
|
1699
|
-
functionNode.returnTypeAnnotation = returnType;
|
1700
|
-
functionNode.returnTypeAnnotation.parent = functionNode;
|
1701
|
-
extendRange(functionNode, returnType);
|
1702
|
-
}
|
1703
|
-
|
1704
|
-
// If there was a type annotation comment for the function,
|
1705
|
-
// parse it now.
|
1706
|
-
if (functionTypeAnnotationToken) {
|
1707
|
-
this._parseFunctionTypeAnnotationComment(functionTypeAnnotationToken, functionNode);
|
1708
|
-
}
|
1709
|
-
|
1710
|
-
return functionNode;
|
1711
|
-
}
|
1712
|
-
|
1713
|
-
// typedargslist: (
|
1714
|
-
// tfpdef ['=' test] (',' tfpdef ['=' test])*
|
1715
|
-
// [ ','
|
1716
|
-
// [
|
1717
|
-
// '*' [tfpdef] (',' tfpdef ['=' test])* [',' ['**' tfpdef [',']]]
|
1718
|
-
// | '**' tfpdef [',']
|
1719
|
-
// ]
|
1720
|
-
// ]
|
1721
|
-
// | '*' [tfpdef] (',' tfpdef ['=' test])* [',' ['**' tfpdef [',']]]
|
1722
|
-
// | '**' tfpdef [','])
|
1723
|
-
// tfpdef: NAME [':' test]
|
1724
|
-
// vfpdef: NAME;
|
1725
|
-
private _parseVarArgsList(terminator: TokenType, allowAnnotations: boolean): ParameterNode[] {
|
1726
|
-
const paramMap = new Map<string, string>();
|
1727
|
-
const paramList: ParameterNode[] = [];
|
1728
|
-
let sawDefaultParam = false;
|
1729
|
-
let reportedNonDefaultParamErr = false;
|
1730
|
-
let sawKeywordOnlySeparator = false;
|
1731
|
-
let sawPositionOnlySeparator = false;
|
1732
|
-
let sawArgs = false;
|
1733
|
-
let sawKwArgs = false;
|
1734
|
-
|
1735
|
-
while (true) {
|
1736
|
-
if (this._peekTokenType() === terminator) {
|
1737
|
-
break;
|
1738
|
-
}
|
1739
|
-
|
1740
|
-
const param = this._parseParameter(allowAnnotations);
|
1741
|
-
if (!param) {
|
1742
|
-
this._consumeTokensUntilType([terminator]);
|
1743
|
-
break;
|
1744
|
-
}
|
1745
|
-
|
1746
|
-
if (param.name) {
|
1747
|
-
const name = param.name.value;
|
1748
|
-
if (paramMap.has(name)) {
|
1749
|
-
this._addError(Localizer.Diagnostic.duplicateParam().format({ name }), param.name);
|
1750
|
-
} else {
|
1751
|
-
paramMap.set(name, name);
|
1752
|
-
}
|
1753
|
-
} else if (param.category === ParameterCategory.Simple) {
|
1754
|
-
if (paramList.length === 0) {
|
1755
|
-
this._addError(Localizer.Diagnostic.positionOnlyFirstParam(), param);
|
1756
|
-
}
|
1757
|
-
}
|
1758
|
-
|
1759
|
-
if (param.category === ParameterCategory.Simple) {
|
1760
|
-
if (!param.name) {
|
1761
|
-
if (sawPositionOnlySeparator) {
|
1762
|
-
this._addError(Localizer.Diagnostic.duplicatePositionOnly(), param);
|
1763
|
-
} else if (sawKeywordOnlySeparator) {
|
1764
|
-
this._addError(Localizer.Diagnostic.positionOnlyAfterKeywordOnly(), param);
|
1765
|
-
} else if (sawArgs) {
|
1766
|
-
this._addError(Localizer.Diagnostic.positionOnlyAfterArgs(), param);
|
1767
|
-
}
|
1768
|
-
sawPositionOnlySeparator = true;
|
1769
|
-
} else {
|
1770
|
-
if (param.defaultValue) {
|
1771
|
-
sawDefaultParam = true;
|
1772
|
-
} else if (sawDefaultParam && !sawKeywordOnlySeparator && !sawArgs) {
|
1773
|
-
// Report this error only once.
|
1774
|
-
if (!reportedNonDefaultParamErr) {
|
1775
|
-
this._addError(Localizer.Diagnostic.nonDefaultAfterDefault(), param);
|
1776
|
-
reportedNonDefaultParamErr = true;
|
1777
|
-
}
|
1778
|
-
}
|
1779
|
-
}
|
1780
|
-
}
|
1781
|
-
|
1782
|
-
paramList.push(param);
|
1783
|
-
|
1784
|
-
if (param.category === ParameterCategory.VarArgList) {
|
1785
|
-
if (!param.name) {
|
1786
|
-
if (sawKeywordOnlySeparator) {
|
1787
|
-
this._addError(Localizer.Diagnostic.duplicateKeywordOnly(), param);
|
1788
|
-
} else if (sawArgs) {
|
1789
|
-
this._addError(Localizer.Diagnostic.keywordOnlyAfterArgs(), param);
|
1790
|
-
}
|
1791
|
-
sawKeywordOnlySeparator = true;
|
1792
|
-
} else {
|
1793
|
-
if (sawKeywordOnlySeparator || sawArgs) {
|
1794
|
-
this._addError(Localizer.Diagnostic.duplicateArgsParam(), param);
|
1795
|
-
}
|
1796
|
-
sawArgs = true;
|
1797
|
-
}
|
1798
|
-
}
|
1799
|
-
|
1800
|
-
if (param.category === ParameterCategory.VarArgDictionary) {
|
1801
|
-
if (sawKwArgs) {
|
1802
|
-
this._addError(Localizer.Diagnostic.duplicateKwargsParam(), param);
|
1803
|
-
}
|
1804
|
-
sawKwArgs = true;
|
1805
|
-
} else if (sawKwArgs) {
|
1806
|
-
this._addError(Localizer.Diagnostic.paramAfterKwargsParam(), param);
|
1807
|
-
}
|
1808
|
-
|
1809
|
-
const foundComma = this._consumeTokenIfType(TokenType.Comma);
|
1810
|
-
|
1811
|
-
if (allowAnnotations && !param.typeAnnotation) {
|
1812
|
-
// Look for a type annotation comment at the end of the line.
|
1813
|
-
const typeAnnotationComment = this._parseVariableTypeAnnotationComment();
|
1814
|
-
if (typeAnnotationComment) {
|
1815
|
-
param.typeAnnotationComment = typeAnnotationComment;
|
1816
|
-
param.typeAnnotationComment.parent = param;
|
1817
|
-
extendRange(param, param.typeAnnotationComment);
|
1818
|
-
}
|
1819
|
-
}
|
1820
|
-
|
1821
|
-
if (!foundComma) {
|
1822
|
-
break;
|
1823
|
-
}
|
1824
|
-
}
|
1825
|
-
|
1826
|
-
if (paramList.length > 0) {
|
1827
|
-
const lastParam = paramList[paramList.length - 1];
|
1828
|
-
if (lastParam.category === ParameterCategory.VarArgList && !lastParam.name) {
|
1829
|
-
this._addError(Localizer.Diagnostic.expectedNamedParameter(), lastParam);
|
1830
|
-
}
|
1831
|
-
}
|
1832
|
-
|
1833
|
-
return paramList;
|
1834
|
-
}
|
1835
|
-
|
1836
|
-
private _parseParameter(allowAnnotations: boolean): ParameterNode {
|
1837
|
-
let starCount = 0;
|
1838
|
-
let slashCount = 0;
|
1839
|
-
const firstToken = this._peekToken();
|
1840
|
-
|
1841
|
-
if (this._consumeTokenIfOperator(OperatorType.Multiply)) {
|
1842
|
-
starCount = 1;
|
1843
|
-
} else if (this._consumeTokenIfOperator(OperatorType.Power)) {
|
1844
|
-
starCount = 2;
|
1845
|
-
} else if (this._consumeTokenIfOperator(OperatorType.Divide)) {
|
1846
|
-
if (this._getLanguageVersion() < PythonVersion.V3_8 && !this._parseOptions.isStubFile) {
|
1847
|
-
this._addError(Localizer.Diagnostic.positionOnlyIncompatible(), firstToken);
|
1848
|
-
}
|
1849
|
-
slashCount = 1;
|
1850
|
-
}
|
1851
|
-
|
1852
|
-
const paramName = this._getTokenIfIdentifier();
|
1853
|
-
if (!paramName) {
|
1854
|
-
if (starCount === 1) {
|
1855
|
-
const paramNode = ParameterNode.create(firstToken, ParameterCategory.VarArgList);
|
1856
|
-
return paramNode;
|
1857
|
-
} else if (slashCount === 1) {
|
1858
|
-
const paramNode = ParameterNode.create(firstToken, ParameterCategory.Simple);
|
1859
|
-
return paramNode;
|
1860
|
-
}
|
1861
|
-
|
1862
|
-
// Check for the Python 2.x parameter sublist syntax and handle it gracefully.
|
1863
|
-
if (this._peekTokenType() === TokenType.OpenParenthesis) {
|
1864
|
-
const sublistStart = this._getNextToken();
|
1865
|
-
if (this._consumeTokensUntilType([TokenType.CloseParenthesis])) {
|
1866
|
-
this._getNextToken();
|
1867
|
-
}
|
1868
|
-
this._addError(Localizer.Diagnostic.sublistParamsIncompatible(), sublistStart);
|
1869
|
-
} else {
|
1870
|
-
this._addError(Localizer.Diagnostic.expectedParamName(), this._peekToken());
|
1871
|
-
}
|
1872
|
-
}
|
1873
|
-
|
1874
|
-
let paramType = ParameterCategory.Simple;
|
1875
|
-
if (starCount === 1) {
|
1876
|
-
paramType = ParameterCategory.VarArgList;
|
1877
|
-
} else if (starCount === 2) {
|
1878
|
-
paramType = ParameterCategory.VarArgDictionary;
|
1879
|
-
}
|
1880
|
-
const paramNode = ParameterNode.create(firstToken, paramType);
|
1881
|
-
if (paramName) {
|
1882
|
-
paramNode.name = NameNode.create(paramName);
|
1883
|
-
paramNode.name.parent = paramNode;
|
1884
|
-
extendRange(paramNode, paramName);
|
1885
|
-
}
|
1886
|
-
|
1887
|
-
if (allowAnnotations && this._consumeTokenIfType(TokenType.Colon)) {
|
1888
|
-
paramNode.typeAnnotation = this._parseTypeAnnotation(paramType === ParameterCategory.VarArgList);
|
1889
|
-
paramNode.typeAnnotation.parent = paramNode;
|
1890
|
-
extendRange(paramNode, paramNode.typeAnnotation);
|
1891
|
-
}
|
1892
|
-
|
1893
|
-
if (this._consumeTokenIfOperator(OperatorType.Assign)) {
|
1894
|
-
paramNode.defaultValue = this._parseTestExpression(/* allowAssignmentExpression */ false);
|
1895
|
-
paramNode.defaultValue.parent = paramNode;
|
1896
|
-
extendRange(paramNode, paramNode.defaultValue);
|
1897
|
-
|
1898
|
-
if (starCount > 0) {
|
1899
|
-
this._addError(Localizer.Diagnostic.defaultValueNotAllowed(), paramNode.defaultValue);
|
1900
|
-
}
|
1901
|
-
}
|
1902
|
-
|
1903
|
-
return paramNode;
|
1904
|
-
}
|
1905
|
-
|
1906
|
-
// with_stmt: 'with' with_item (',' with_item)* ':' suite
|
1907
|
-
// Python 3.10 adds support for optional parentheses around
|
1908
|
-
// with_item list.
|
1909
|
-
private _parseWithStatement(asyncToken?: KeywordToken): WithNode {
|
1910
|
-
const withToken = this._getKeywordToken(KeywordType.With);
|
1911
|
-
let withItemList: WithItemNode[] = [];
|
1912
|
-
|
1913
|
-
const possibleParen = this._peekToken();
|
1914
|
-
|
1915
|
-
// If the expression starts with a paren, parse it as though the
|
1916
|
-
// paren is enclosing the list of "with items". This is done as a
|
1917
|
-
// "dry run" to determine whether the entire list of "with items"
|
1918
|
-
// is enclosed in parentheses.
|
1919
|
-
let isParenthesizedWithItemList = false;
|
1920
|
-
if (possibleParen.type === TokenType.OpenParenthesis) {
|
1921
|
-
const openParenTokenIndex = this._tokenIndex;
|
1922
|
-
|
1923
|
-
this._suppressErrors(() => {
|
1924
|
-
this._getNextToken();
|
1925
|
-
while (true) {
|
1926
|
-
withItemList.push(this._parseWithItem());
|
1927
|
-
if (!this._consumeTokenIfType(TokenType.Comma)) {
|
1928
|
-
break;
|
1929
|
-
}
|
1930
|
-
|
1931
|
-
if (this._peekToken().type === TokenType.CloseParenthesis) {
|
1932
|
-
break;
|
1933
|
-
}
|
1934
|
-
}
|
1935
|
-
|
1936
|
-
if (
|
1937
|
-
this._peekToken().type === TokenType.CloseParenthesis &&
|
1938
|
-
this._peekToken(1).type === TokenType.Colon
|
1939
|
-
) {
|
1940
|
-
isParenthesizedWithItemList = withItemList.length !== 1 || withItemList[0].target !== undefined;
|
1941
|
-
}
|
1942
|
-
|
1943
|
-
this._tokenIndex = openParenTokenIndex;
|
1944
|
-
withItemList = [];
|
1945
|
-
});
|
1946
|
-
}
|
1947
|
-
|
1948
|
-
if (isParenthesizedWithItemList) {
|
1949
|
-
this._consumeTokenIfType(TokenType.OpenParenthesis);
|
1950
|
-
if (this._getLanguageVersion() < PythonVersion.V3_9) {
|
1951
|
-
this._addError(Localizer.Diagnostic.parenthesizedContextManagerIllegal(), possibleParen);
|
1952
|
-
}
|
1953
|
-
}
|
1954
|
-
|
1955
|
-
while (true) {
|
1956
|
-
withItemList.push(this._parseWithItem());
|
1957
|
-
|
1958
|
-
if (!this._consumeTokenIfType(TokenType.Comma)) {
|
1959
|
-
break;
|
1960
|
-
}
|
1961
|
-
|
1962
|
-
if (this._peekToken().type === TokenType.CloseParenthesis) {
|
1963
|
-
break;
|
1964
|
-
}
|
1965
|
-
}
|
1966
|
-
|
1967
|
-
if (isParenthesizedWithItemList) {
|
1968
|
-
if (!this._consumeTokenIfType(TokenType.CloseParenthesis)) {
|
1969
|
-
this._addError(Localizer.Diagnostic.expectedCloseParen(), possibleParen);
|
1970
|
-
}
|
1971
|
-
}
|
1972
|
-
|
1973
|
-
const withSuite = this._parseSuite(this._isInFunction, /* skipBody */ false, () => {
|
1974
|
-
this._detectUnsupportedTypeAnnotationComment();
|
1975
|
-
});
|
1976
|
-
const withNode = WithNode.create(withToken, withSuite);
|
1977
|
-
if (asyncToken) {
|
1978
|
-
withNode.isAsync = true;
|
1979
|
-
withNode.asyncToken = asyncToken;
|
1980
|
-
extendRange(withNode, asyncToken);
|
1981
|
-
}
|
1982
|
-
|
1983
|
-
withNode.withItems = withItemList;
|
1984
|
-
withItemList.forEach((withItem) => {
|
1985
|
-
withItem.parent = withNode;
|
1986
|
-
});
|
1987
|
-
|
1988
|
-
return withNode;
|
1989
|
-
}
|
1990
|
-
|
1991
|
-
// with_item: test ['as' expr]
|
1992
|
-
private _parseWithItem(): WithItemNode {
|
1993
|
-
const expr = this._parseTestExpression(/* allowAssignmentExpression */ true);
|
1994
|
-
const itemNode = WithItemNode.create(expr);
|
1995
|
-
|
1996
|
-
if (this._consumeTokenIfKeyword(KeywordType.As)) {
|
1997
|
-
itemNode.target = this._parseExpression(/* allowUnpack */ false);
|
1998
|
-
itemNode.target.parent = itemNode;
|
1999
|
-
extendRange(itemNode, itemNode.target);
|
2000
|
-
}
|
2001
|
-
|
2002
|
-
return itemNode;
|
2003
|
-
}
|
2004
|
-
|
2005
|
-
// decorators: decorator+
|
2006
|
-
// decorated: decorators (classdef | funcdef | async_funcdef)
|
2007
|
-
private _parseDecorated(): StatementNode | undefined {
|
2008
|
-
const decoratorList: DecoratorNode[] = [];
|
2009
|
-
|
2010
|
-
while (true) {
|
2011
|
-
if (this._peekOperatorType() === OperatorType.MatrixMultiply) {
|
2012
|
-
decoratorList.push(this._parseDecorator());
|
2013
|
-
} else {
|
2014
|
-
break;
|
2015
|
-
}
|
2016
|
-
}
|
2017
|
-
|
2018
|
-
const nextToken = this._peekToken() as KeywordToken;
|
2019
|
-
if (nextToken.type === TokenType.Keyword) {
|
2020
|
-
if (nextToken.keywordType === KeywordType.Async) {
|
2021
|
-
this._getNextToken();
|
2022
|
-
|
2023
|
-
if (this._peekKeywordType() !== KeywordType.Def) {
|
2024
|
-
this._addError(Localizer.Diagnostic.expectedFunctionAfterAsync(), this._peekToken());
|
2025
|
-
} else {
|
2026
|
-
return this._parseFunctionDef(nextToken, decoratorList);
|
2027
|
-
}
|
2028
|
-
} else if (nextToken.keywordType === KeywordType.Def) {
|
2029
|
-
return this._parseFunctionDef(undefined, decoratorList);
|
2030
|
-
} else if (nextToken.keywordType === KeywordType.Class) {
|
2031
|
-
return this._parseClassDef(decoratorList);
|
2032
|
-
}
|
2033
|
-
}
|
2034
|
-
|
2035
|
-
this._addError(Localizer.Diagnostic.expectedAfterDecorator(), this._peekToken());
|
2036
|
-
|
2037
|
-
// Return a dummy class declaration so the completion provider has
|
2038
|
-
// some parse nodes to work with.
|
2039
|
-
return ClassNode.createDummyForDecorators(decoratorList);
|
2040
|
-
}
|
2041
|
-
|
2042
|
-
// decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE
|
2043
|
-
private _parseDecorator(): DecoratorNode {
|
2044
|
-
const atOperator = this._getNextToken() as OperatorToken;
|
2045
|
-
assert(atOperator.operatorType === OperatorType.MatrixMultiply);
|
2046
|
-
|
2047
|
-
const expression = this._parseTestExpression(/* allowAssignmentExpression */ true);
|
2048
|
-
|
2049
|
-
// Versions of Python prior to 3.9 support a limited set of
|
2050
|
-
// expression forms.
|
2051
|
-
if (this._getLanguageVersion() < PythonVersion.V3_9) {
|
2052
|
-
let isSupportedExpressionForm = false;
|
2053
|
-
if (this._isNameOrMemberAccessExpression(expression)) {
|
2054
|
-
isSupportedExpressionForm = true;
|
2055
|
-
} else if (
|
2056
|
-
expression.nodeType === ParseNodeType.Call &&
|
2057
|
-
this._isNameOrMemberAccessExpression(expression.leftExpression)
|
2058
|
-
) {
|
2059
|
-
isSupportedExpressionForm = true;
|
2060
|
-
}
|
2061
|
-
|
2062
|
-
if (!isSupportedExpressionForm) {
|
2063
|
-
this._addError(Localizer.Diagnostic.expectedDecoratorExpr(), expression);
|
2064
|
-
}
|
2065
|
-
}
|
2066
|
-
|
2067
|
-
const decoratorNode = DecoratorNode.create(atOperator, expression);
|
2068
|
-
|
2069
|
-
if (!this._consumeTokenIfType(TokenType.NewLine)) {
|
2070
|
-
this._addError(Localizer.Diagnostic.expectedDecoratorNewline(), this._peekToken());
|
2071
|
-
this._consumeTokensUntilType([TokenType.NewLine]);
|
2072
|
-
}
|
2073
|
-
|
2074
|
-
return decoratorNode;
|
2075
|
-
}
|
2076
|
-
|
2077
|
-
private _isNameOrMemberAccessExpression(expression: ExpressionNode): boolean {
|
2078
|
-
if (expression.nodeType === ParseNodeType.Name) {
|
2079
|
-
return true;
|
2080
|
-
} else if (expression.nodeType === ParseNodeType.MemberAccess) {
|
2081
|
-
return this._isNameOrMemberAccessExpression(expression.leftExpression);
|
2082
|
-
}
|
2083
|
-
|
2084
|
-
return false;
|
2085
|
-
}
|
2086
|
-
|
2087
|
-
// classdef: 'class' NAME ['(' [arglist] ')'] suite
|
2088
|
-
private _parseClassDef(decorators?: DecoratorNode[]): ClassNode {
|
2089
|
-
const classToken = this._getKeywordToken(KeywordType.Class);
|
2090
|
-
|
2091
|
-
let nameToken = this._getTokenIfIdentifier();
|
2092
|
-
if (!nameToken) {
|
2093
|
-
this._addError(Localizer.Diagnostic.expectedClassName(), this._peekToken());
|
2094
|
-
nameToken = IdentifierToken.create(0, 0, '', /* comments */ undefined);
|
2095
|
-
}
|
2096
|
-
|
2097
|
-
let argList: ArgumentNode[] = [];
|
2098
|
-
const openParenToken = this._peekToken();
|
2099
|
-
if (this._consumeTokenIfType(TokenType.OpenParenthesis)) {
|
2100
|
-
argList = this._parseArgList().args;
|
2101
|
-
|
2102
|
-
if (!this._consumeTokenIfType(TokenType.CloseParenthesis)) {
|
2103
|
-
this._addError(Localizer.Diagnostic.expectedCloseParen(), openParenToken);
|
2104
|
-
}
|
2105
|
-
}
|
2106
|
-
|
2107
|
-
const suite = this._parseSuite(/* isFunction */ false, this._parseOptions.skipFunctionAndClassBody);
|
2108
|
-
|
2109
|
-
const classNode = ClassNode.create(classToken, NameNode.create(nameToken), suite);
|
2110
|
-
classNode.arguments = argList;
|
2111
|
-
argList.forEach((arg) => {
|
2112
|
-
arg.parent = classNode;
|
2113
|
-
});
|
2114
|
-
|
2115
|
-
if (decorators) {
|
2116
|
-
classNode.decorators = decorators;
|
2117
|
-
if (decorators.length > 0) {
|
2118
|
-
decorators.forEach((decorator) => {
|
2119
|
-
decorator.parent = classNode;
|
2120
|
-
});
|
2121
|
-
extendRange(classNode, decorators[0]);
|
2122
|
-
}
|
2123
|
-
}
|
2124
|
-
|
2125
|
-
return classNode;
|
2126
|
-
}
|
2127
|
-
|
2128
|
-
private _parsePassStatement(): PassNode {
|
2129
|
-
return PassNode.create(this._getKeywordToken(KeywordType.Pass));
|
2130
|
-
}
|
2131
|
-
|
2132
|
-
private _parseBreakStatement(): BreakNode {
|
2133
|
-
const breakToken = this._getKeywordToken(KeywordType.Break);
|
2134
|
-
|
2135
|
-
if (!this._isInLoop) {
|
2136
|
-
this._addError(Localizer.Diagnostic.breakOutsideLoop(), breakToken);
|
2137
|
-
}
|
2138
|
-
|
2139
|
-
return BreakNode.create(breakToken);
|
2140
|
-
}
|
2141
|
-
|
2142
|
-
private _parseContinueStatement(): ContinueNode {
|
2143
|
-
const continueToken = this._getKeywordToken(KeywordType.Continue);
|
2144
|
-
|
2145
|
-
if (!this._isInLoop) {
|
2146
|
-
this._addError(Localizer.Diagnostic.continueOutsideLoop(), continueToken);
|
2147
|
-
} else if (this._isInFinally) {
|
2148
|
-
this._addError(Localizer.Diagnostic.continueInFinally(), continueToken);
|
2149
|
-
}
|
2150
|
-
|
2151
|
-
return ContinueNode.create(continueToken);
|
2152
|
-
}
|
2153
|
-
|
2154
|
-
// return_stmt: 'return' [testlist]
|
2155
|
-
private _parseReturnStatement(): ReturnNode {
|
2156
|
-
const returnToken = this._getKeywordToken(KeywordType.Return);
|
2157
|
-
|
2158
|
-
const returnNode = ReturnNode.create(returnToken);
|
2159
|
-
|
2160
|
-
if (!this._isInFunction) {
|
2161
|
-
this._addError(Localizer.Diagnostic.returnOutsideFunction(), returnToken);
|
2162
|
-
}
|
2163
|
-
|
2164
|
-
if (!this._isNextTokenNeverExpression()) {
|
2165
|
-
const returnExpr = this._parseTestOrStarListAsExpression(
|
2166
|
-
/* allowAssignmentExpression */ true,
|
2167
|
-
/* allowMultipleUnpack */ true,
|
2168
|
-
ErrorExpressionCategory.MissingExpression,
|
2169
|
-
Localizer.Diagnostic.expectedReturnExpr()
|
2170
|
-
);
|
2171
|
-
this._reportConditionalErrorForStarTupleElement(returnExpr);
|
2172
|
-
returnNode.returnExpression = returnExpr;
|
2173
|
-
returnNode.returnExpression.parent = returnNode;
|
2174
|
-
extendRange(returnNode, returnExpr);
|
2175
|
-
}
|
2176
|
-
|
2177
|
-
return returnNode;
|
2178
|
-
}
|
2179
|
-
|
2180
|
-
// import_from: ('from' (('.' | '...')* dotted_name | ('.' | '...')+)
|
2181
|
-
// 'import' ('*' | '(' import_as_names ')' | import_as_names))
|
2182
|
-
// import_as_names: import_as_name (',' import_as_name)* [',']
|
2183
|
-
// import_as_name: NAME ['as' NAME]
|
2184
|
-
private _parseFromStatement(): ImportFromNode {
|
2185
|
-
const fromToken = this._getKeywordToken(KeywordType.From);
|
2186
|
-
|
2187
|
-
const modName = this._parseDottedModuleName(/* allowJustDots */ true);
|
2188
|
-
const importFromNode = ImportFromNode.create(fromToken, modName);
|
2189
|
-
|
2190
|
-
// Handle imports from __future__ specially because they can
|
2191
|
-
// change the way we interpret the rest of the file.
|
2192
|
-
const isFutureImport =
|
2193
|
-
modName.leadingDots === 0 && modName.nameParts.length === 1 && modName.nameParts[0].value === '__future__';
|
2194
|
-
|
2195
|
-
const possibleInputToken = this._peekToken();
|
2196
|
-
if (!this._consumeTokenIfKeyword(KeywordType.Import)) {
|
2197
|
-
this._addError(Localizer.Diagnostic.expectedImport(), this._peekToken());
|
2198
|
-
if (!modName.hasTrailingDot) {
|
2199
|
-
importFromNode.missingImportKeyword = true;
|
2200
|
-
}
|
2201
|
-
} else {
|
2202
|
-
extendRange(importFromNode, possibleInputToken);
|
2203
|
-
|
2204
|
-
// Look for "*" token.
|
2205
|
-
const possibleStarToken = this._peekToken();
|
2206
|
-
if (this._consumeTokenIfOperator(OperatorType.Multiply)) {
|
2207
|
-
extendRange(importFromNode, possibleStarToken);
|
2208
|
-
importFromNode.isWildcardImport = true;
|
2209
|
-
importFromNode.wildcardToken = possibleStarToken;
|
2210
|
-
this._containsWildcardImport = true;
|
2211
|
-
} else {
|
2212
|
-
const openParenToken = this._peekToken();
|
2213
|
-
const inParen = this._consumeTokenIfType(TokenType.OpenParenthesis);
|
2214
|
-
let trailingCommaToken: Token | undefined;
|
2215
|
-
|
2216
|
-
while (true) {
|
2217
|
-
const importName = this._getTokenIfIdentifier();
|
2218
|
-
if (!importName) {
|
2219
|
-
break;
|
2220
|
-
}
|
2221
|
-
|
2222
|
-
trailingCommaToken = undefined;
|
2223
|
-
|
2224
|
-
const importFromAsNode = ImportFromAsNode.create(NameNode.create(importName));
|
2225
|
-
|
2226
|
-
if (this._consumeTokenIfKeyword(KeywordType.As)) {
|
2227
|
-
const aliasName = this._getTokenIfIdentifier();
|
2228
|
-
if (!aliasName) {
|
2229
|
-
this._addError(Localizer.Diagnostic.expectedImportAlias(), this._peekToken());
|
2230
|
-
} else {
|
2231
|
-
importFromAsNode.alias = NameNode.create(aliasName);
|
2232
|
-
importFromAsNode.alias.parent = importFromAsNode;
|
2233
|
-
extendRange(importFromAsNode, aliasName);
|
2234
|
-
}
|
2235
|
-
}
|
2236
|
-
|
2237
|
-
importFromNode.imports.push(importFromAsNode);
|
2238
|
-
importFromAsNode.parent = importFromNode;
|
2239
|
-
extendRange(importFromNode, importFromAsNode);
|
2240
|
-
|
2241
|
-
if (isFutureImport) {
|
2242
|
-
// Add the future import to the map.
|
2243
|
-
this._futureImportMap.set(importName.value, true);
|
2244
|
-
}
|
2245
|
-
|
2246
|
-
const nextToken = this._peekToken();
|
2247
|
-
if (!this._consumeTokenIfType(TokenType.Comma)) {
|
2248
|
-
break;
|
2249
|
-
}
|
2250
|
-
trailingCommaToken = nextToken;
|
2251
|
-
}
|
2252
|
-
|
2253
|
-
if (importFromNode.imports.length === 0) {
|
2254
|
-
this._addError(Localizer.Diagnostic.expectedImportSymbols(), this._peekToken());
|
2255
|
-
}
|
2256
|
-
|
2257
|
-
if (inParen) {
|
2258
|
-
importFromNode.usesParens = true;
|
2259
|
-
|
2260
|
-
const nextToken = this._peekToken();
|
2261
|
-
if (!this._consumeTokenIfType(TokenType.CloseParenthesis)) {
|
2262
|
-
this._addError(Localizer.Diagnostic.expectedCloseParen(), openParenToken);
|
2263
|
-
} else {
|
2264
|
-
extendRange(importFromNode, nextToken);
|
2265
|
-
}
|
2266
|
-
} else if (trailingCommaToken) {
|
2267
|
-
this._addError(Localizer.Diagnostic.trailingCommaInFromImport(), trailingCommaToken);
|
2268
|
-
}
|
2269
|
-
}
|
2270
|
-
}
|
2271
|
-
|
2272
|
-
this._importedModules.push({
|
2273
|
-
nameNode: importFromNode.module,
|
2274
|
-
leadingDots: importFromNode.module.leadingDots,
|
2275
|
-
nameParts: importFromNode.module.nameParts.map((p) => p.value),
|
2276
|
-
importedSymbols: importFromNode.imports.map((imp) => imp.name.value),
|
2277
|
-
});
|
2278
|
-
|
2279
|
-
let isTypingImport = false;
|
2280
|
-
if (importFromNode.module.nameParts.length === 1) {
|
2281
|
-
const firstNamePartValue = importFromNode.module.nameParts[0].value;
|
2282
|
-
if (firstNamePartValue === 'typing' || firstNamePartValue === 'typing_extensions') {
|
2283
|
-
isTypingImport = true;
|
2284
|
-
}
|
2285
|
-
}
|
2286
|
-
|
2287
|
-
if (isTypingImport) {
|
2288
|
-
const typingSymbolsOfInterest = ['Literal', 'TypeAlias', 'Annotated'];
|
2289
|
-
|
2290
|
-
if (importFromNode.isWildcardImport) {
|
2291
|
-
typingSymbolsOfInterest.forEach((s) => {
|
2292
|
-
this._typingSymbolAliases.set(s, s);
|
2293
|
-
});
|
2294
|
-
} else {
|
2295
|
-
importFromNode.imports.forEach((imp) => {
|
2296
|
-
if (typingSymbolsOfInterest.some((s) => s === imp.name.value)) {
|
2297
|
-
this._typingSymbolAliases.set(imp.alias?.value || imp.name.value, imp.name.value);
|
2298
|
-
}
|
2299
|
-
});
|
2300
|
-
}
|
2301
|
-
}
|
2302
|
-
|
2303
|
-
return importFromNode;
|
2304
|
-
}
|
2305
|
-
|
2306
|
-
// import_name: 'import' dotted_as_names
|
2307
|
-
// dotted_as_names: dotted_as_name (',' dotted_as_name)*
|
2308
|
-
// dotted_as_name: dotted_name ['as' NAME]
|
2309
|
-
private _parseImportStatement(): ImportNode {
|
2310
|
-
const importToken = this._getKeywordToken(KeywordType.Import);
|
2311
|
-
|
2312
|
-
const importNode = ImportNode.create(importToken);
|
2313
|
-
|
2314
|
-
while (true) {
|
2315
|
-
const modName = this._parseDottedModuleName();
|
2316
|
-
|
2317
|
-
const importAsNode = ImportAsNode.create(modName);
|
2318
|
-
|
2319
|
-
if (this._consumeTokenIfKeyword(KeywordType.As)) {
|
2320
|
-
const aliasToken = this._getTokenIfIdentifier();
|
2321
|
-
if (aliasToken) {
|
2322
|
-
importAsNode.alias = NameNode.create(aliasToken);
|
2323
|
-
importAsNode.alias.parent = importAsNode;
|
2324
|
-
extendRange(importAsNode, importAsNode.alias);
|
2325
|
-
} else {
|
2326
|
-
this._addError(Localizer.Diagnostic.expectedImportAlias(), this._peekToken());
|
2327
|
-
}
|
2328
|
-
}
|
2329
|
-
|
2330
|
-
if (importAsNode.module.leadingDots > 0) {
|
2331
|
-
this._addError(Localizer.Diagnostic.relativeImportNotAllowed(), importAsNode.module);
|
2332
|
-
}
|
2333
|
-
|
2334
|
-
importNode.list.push(importAsNode);
|
2335
|
-
importAsNode.parent = importNode;
|
2336
|
-
|
2337
|
-
this._importedModules.push({
|
2338
|
-
nameNode: importAsNode.module,
|
2339
|
-
leadingDots: importAsNode.module.leadingDots,
|
2340
|
-
nameParts: importAsNode.module.nameParts.map((p) => p.value),
|
2341
|
-
importedSymbols: undefined,
|
2342
|
-
});
|
2343
|
-
|
2344
|
-
if (modName.nameParts.length === 1) {
|
2345
|
-
const firstNamePartValue = modName.nameParts[0].value;
|
2346
|
-
if (firstNamePartValue === 'typing' || firstNamePartValue === 'typing_extensions') {
|
2347
|
-
this._typingImportAliases.push(importAsNode.alias?.value || firstNamePartValue);
|
2348
|
-
}
|
2349
|
-
}
|
2350
|
-
|
2351
|
-
if (!this._consumeTokenIfType(TokenType.Comma)) {
|
2352
|
-
break;
|
2353
|
-
}
|
2354
|
-
}
|
2355
|
-
|
2356
|
-
if (importNode.list.length > 0) {
|
2357
|
-
extendRange(importNode, importNode.list[importNode.list.length - 1]);
|
2358
|
-
}
|
2359
|
-
|
2360
|
-
return importNode;
|
2361
|
-
}
|
2362
|
-
|
2363
|
-
// ('.' | '...')* dotted_name | ('.' | '...')+
|
2364
|
-
// dotted_name: NAME ('.' NAME)*
|
2365
|
-
private _parseDottedModuleName(allowJustDots = false): ModuleNameNode {
|
2366
|
-
const moduleNameNode = ModuleNameNode.create(this._peekToken());
|
2367
|
-
|
2368
|
-
while (true) {
|
2369
|
-
const token = this._getTokenIfType(TokenType.Ellipsis) ?? this._getTokenIfType(TokenType.Dot);
|
2370
|
-
if (token) {
|
2371
|
-
if (token.type === TokenType.Ellipsis) {
|
2372
|
-
moduleNameNode.leadingDots += 3;
|
2373
|
-
} else {
|
2374
|
-
moduleNameNode.leadingDots++;
|
2375
|
-
}
|
2376
|
-
|
2377
|
-
extendRange(moduleNameNode, token);
|
2378
|
-
} else {
|
2379
|
-
break;
|
2380
|
-
}
|
2381
|
-
}
|
2382
|
-
|
2383
|
-
while (true) {
|
2384
|
-
const identifier = this._getTokenIfIdentifier();
|
2385
|
-
if (!identifier) {
|
2386
|
-
if (!allowJustDots || moduleNameNode.leadingDots === 0 || moduleNameNode.nameParts.length > 0) {
|
2387
|
-
this._addError(Localizer.Diagnostic.expectedModuleName(), this._peekToken());
|
2388
|
-
moduleNameNode.hasTrailingDot = true;
|
2389
|
-
}
|
2390
|
-
break;
|
2391
|
-
}
|
2392
|
-
|
2393
|
-
const namePart = NameNode.create(identifier);
|
2394
|
-
moduleNameNode.nameParts.push(namePart);
|
2395
|
-
namePart.parent = moduleNameNode;
|
2396
|
-
extendRange(moduleNameNode, namePart);
|
2397
|
-
|
2398
|
-
const nextToken = this._peekToken();
|
2399
|
-
if (!this._consumeTokenIfType(TokenType.Dot)) {
|
2400
|
-
break;
|
2401
|
-
}
|
2402
|
-
|
2403
|
-
// Extend the module name to include the dot.
|
2404
|
-
extendRange(moduleNameNode, nextToken);
|
2405
|
-
}
|
2406
|
-
|
2407
|
-
return moduleNameNode;
|
2408
|
-
}
|
2409
|
-
|
2410
|
-
private _parseGlobalStatement(): GlobalNode {
|
2411
|
-
const globalToken = this._getKeywordToken(KeywordType.Global);
|
2412
|
-
|
2413
|
-
const globalNode = GlobalNode.create(globalToken);
|
2414
|
-
globalNode.nameList = this._parseNameList();
|
2415
|
-
if (globalNode.nameList.length > 0) {
|
2416
|
-
globalNode.nameList.forEach((name) => {
|
2417
|
-
name.parent = globalNode;
|
2418
|
-
});
|
2419
|
-
extendRange(globalNode, globalNode.nameList[globalNode.nameList.length - 1]);
|
2420
|
-
}
|
2421
|
-
return globalNode;
|
2422
|
-
}
|
2423
|
-
|
2424
|
-
private _parseNonlocalStatement(): NonlocalNode {
|
2425
|
-
const nonlocalToken = this._getKeywordToken(KeywordType.Nonlocal);
|
2426
|
-
|
2427
|
-
const nonlocalNode = NonlocalNode.create(nonlocalToken);
|
2428
|
-
nonlocalNode.nameList = this._parseNameList();
|
2429
|
-
if (nonlocalNode.nameList.length > 0) {
|
2430
|
-
nonlocalNode.nameList.forEach((name) => {
|
2431
|
-
name.parent = nonlocalNode;
|
2432
|
-
});
|
2433
|
-
extendRange(nonlocalNode, nonlocalNode.nameList[nonlocalNode.nameList.length - 1]);
|
2434
|
-
}
|
2435
|
-
return nonlocalNode;
|
2436
|
-
}
|
2437
|
-
|
2438
|
-
private _parseNameList(): NameNode[] {
|
2439
|
-
const nameList: NameNode[] = [];
|
2440
|
-
|
2441
|
-
while (true) {
|
2442
|
-
const name = this._getTokenIfIdentifier();
|
2443
|
-
if (!name) {
|
2444
|
-
this._addError(Localizer.Diagnostic.expectedIdentifier(), this._peekToken());
|
2445
|
-
break;
|
2446
|
-
}
|
2447
|
-
|
2448
|
-
nameList.push(NameNode.create(name));
|
2449
|
-
|
2450
|
-
if (!this._consumeTokenIfType(TokenType.Comma)) {
|
2451
|
-
break;
|
2452
|
-
}
|
2453
|
-
}
|
2454
|
-
|
2455
|
-
return nameList;
|
2456
|
-
}
|
2457
|
-
|
2458
|
-
// raise_stmt: 'raise' [test ['from' test]]
|
2459
|
-
// (old) raise_stmt: 'raise' [test [',' test [',' test]]]
|
2460
|
-
private _parseRaiseStatement(): RaiseNode {
|
2461
|
-
const raiseToken = this._getKeywordToken(KeywordType.Raise);
|
2462
|
-
|
2463
|
-
const raiseNode = RaiseNode.create(raiseToken);
|
2464
|
-
if (!this._isNextTokenNeverExpression()) {
|
2465
|
-
raiseNode.typeExpression = this._parseTestExpression(/* allowAssignmentExpression */ true);
|
2466
|
-
raiseNode.typeExpression.parent = raiseNode;
|
2467
|
-
extendRange(raiseNode, raiseNode.typeExpression);
|
2468
|
-
|
2469
|
-
if (this._consumeTokenIfKeyword(KeywordType.From)) {
|
2470
|
-
raiseNode.valueExpression = this._parseTestExpression(/* allowAssignmentExpression */ true);
|
2471
|
-
raiseNode.valueExpression.parent = raiseNode;
|
2472
|
-
extendRange(raiseNode, raiseNode.valueExpression);
|
2473
|
-
} else {
|
2474
|
-
if (this._consumeTokenIfType(TokenType.Comma)) {
|
2475
|
-
// Handle the Python 2.x variant
|
2476
|
-
raiseNode.valueExpression = this._parseTestExpression(/* allowAssignmentExpression */ true);
|
2477
|
-
raiseNode.valueExpression.parent = raiseNode;
|
2478
|
-
extendRange(raiseNode, raiseNode.valueExpression);
|
2479
|
-
|
2480
|
-
if (this._consumeTokenIfType(TokenType.Comma)) {
|
2481
|
-
raiseNode.tracebackExpression = this._parseTestExpression(/* allowAssignmentExpression */ true);
|
2482
|
-
raiseNode.tracebackExpression.parent = raiseNode;
|
2483
|
-
extendRange(raiseNode, raiseNode.tracebackExpression);
|
2484
|
-
}
|
2485
|
-
}
|
2486
|
-
}
|
2487
|
-
}
|
2488
|
-
|
2489
|
-
return raiseNode;
|
2490
|
-
}
|
2491
|
-
|
2492
|
-
// assert_stmt: 'assert' test [',' test]
|
2493
|
-
private _parseAssertStatement(): AssertNode {
|
2494
|
-
const assertToken = this._getKeywordToken(KeywordType.Assert);
|
2495
|
-
|
2496
|
-
const expr = this._parseTestExpression(/* allowAssignmentExpression */ true);
|
2497
|
-
const assertNode = AssertNode.create(assertToken, expr);
|
2498
|
-
|
2499
|
-
if (this._consumeTokenIfType(TokenType.Comma)) {
|
2500
|
-
const exceptionExpr = this._parseTestExpression(/* allowAssignmentExpression */ true);
|
2501
|
-
assertNode.exceptionExpression = exceptionExpr;
|
2502
|
-
assertNode.exceptionExpression.parent = assertNode;
|
2503
|
-
extendRange(assertNode, exceptionExpr);
|
2504
|
-
}
|
2505
|
-
|
2506
|
-
return assertNode;
|
2507
|
-
}
|
2508
|
-
|
2509
|
-
// del_stmt: 'del' exprlist
|
2510
|
-
private _parseDelStatement(): DelNode {
|
2511
|
-
const delToken = this._getKeywordToken(KeywordType.Del);
|
2512
|
-
|
2513
|
-
const exprListResult = this._parseExpressionList(/* allowStar */ true);
|
2514
|
-
if (!exprListResult.parseError && exprListResult.list.length === 0) {
|
2515
|
-
this._addError(Localizer.Diagnostic.expectedDelExpr(), this._peekToken());
|
2516
|
-
}
|
2517
|
-
const delNode = DelNode.create(delToken);
|
2518
|
-
delNode.expressions = exprListResult.list;
|
2519
|
-
if (delNode.expressions.length > 0) {
|
2520
|
-
delNode.expressions.forEach((expr) => {
|
2521
|
-
expr.parent = delNode;
|
2522
|
-
});
|
2523
|
-
extendRange(delNode, delNode.expressions[delNode.expressions.length - 1]);
|
2524
|
-
}
|
2525
|
-
return delNode;
|
2526
|
-
}
|
2527
|
-
|
2528
|
-
// yield_expr: 'yield' [yield_arg]
|
2529
|
-
// yield_arg: 'from' test | testlist
|
2530
|
-
private _parseYieldExpression(): YieldNode | YieldFromNode {
|
2531
|
-
const yieldToken = this._getKeywordToken(KeywordType.Yield);
|
2532
|
-
|
2533
|
-
const nextToken = this._peekToken();
|
2534
|
-
if (this._consumeTokenIfKeyword(KeywordType.From)) {
|
2535
|
-
if (this._getLanguageVersion() < PythonVersion.V3_3) {
|
2536
|
-
this._addError(Localizer.Diagnostic.yieldFromIllegal(), nextToken);
|
2537
|
-
}
|
2538
|
-
return YieldFromNode.create(yieldToken, this._parseTestExpression(/* allowAssignmentExpression */ true));
|
2539
|
-
}
|
2540
|
-
|
2541
|
-
let exprList: ExpressionNode | undefined;
|
2542
|
-
if (!this._isNextTokenNeverExpression()) {
|
2543
|
-
exprList = this._parseTestOrStarListAsExpression(
|
2544
|
-
/* allowAssignmentExpression */ true,
|
2545
|
-
/* allowMultipleUnpack */ true,
|
2546
|
-
ErrorExpressionCategory.MissingExpression,
|
2547
|
-
Localizer.Diagnostic.expectedYieldExpr()
|
2548
|
-
);
|
2549
|
-
this._reportConditionalErrorForStarTupleElement(exprList);
|
2550
|
-
}
|
2551
|
-
|
2552
|
-
return YieldNode.create(yieldToken, exprList);
|
2553
|
-
}
|
2554
|
-
|
2555
|
-
private _tryParseYieldExpression(): YieldNode | YieldFromNode | undefined {
|
2556
|
-
if (this._peekKeywordType() !== KeywordType.Yield) {
|
2557
|
-
return undefined;
|
2558
|
-
}
|
2559
|
-
|
2560
|
-
return this._parseYieldExpression();
|
2561
|
-
}
|
2562
|
-
|
2563
|
-
// simple_stmt: small_stmt (';' small_stmt)* [';'] NEWLINE
|
2564
|
-
private _parseSimpleStatement(): StatementListNode {
|
2565
|
-
const statement = StatementListNode.create(this._peekToken());
|
2566
|
-
|
2567
|
-
while (true) {
|
2568
|
-
// Swallow invalid tokens to make sure we make forward progress.
|
2569
|
-
if (this._peekTokenType() === TokenType.Invalid) {
|
2570
|
-
const invalidToken = this._getNextToken();
|
2571
|
-
const text = this._fileContents!.substr(invalidToken.start, invalidToken.length);
|
2572
|
-
|
2573
|
-
const firstCharCode = text.charCodeAt(0);
|
2574
|
-
|
2575
|
-
// Remove any non-printable characters.
|
2576
|
-
this._addError(
|
2577
|
-
Localizer.Diagnostic.invalidTokenChars().format({ text: `\\u${firstCharCode.toString(16)}` }),
|
2578
|
-
invalidToken
|
2579
|
-
);
|
2580
|
-
this._consumeTokensUntilType([TokenType.NewLine]);
|
2581
|
-
break;
|
2582
|
-
}
|
2583
|
-
|
2584
|
-
const smallStatement = this._parseSmallStatement();
|
2585
|
-
statement.statements.push(smallStatement);
|
2586
|
-
smallStatement.parent = statement;
|
2587
|
-
extendRange(statement, smallStatement);
|
2588
|
-
|
2589
|
-
if (smallStatement.nodeType === ParseNodeType.Error) {
|
2590
|
-
// No need to log an error here. We assume that
|
2591
|
-
// it was already logged by _parseSmallStatement.
|
2592
|
-
break;
|
2593
|
-
}
|
2594
|
-
|
2595
|
-
// Consume the semicolon if present.
|
2596
|
-
if (!this._consumeTokenIfType(TokenType.Semicolon)) {
|
2597
|
-
break;
|
2598
|
-
}
|
2599
|
-
|
2600
|
-
const nextTokenType = this._peekTokenType();
|
2601
|
-
if (nextTokenType === TokenType.NewLine || nextTokenType === TokenType.EndOfStream) {
|
2602
|
-
break;
|
2603
|
-
}
|
2604
|
-
}
|
2605
|
-
|
2606
|
-
if (!this._consumeTokenIfType(TokenType.NewLine)) {
|
2607
|
-
this._addError(Localizer.Diagnostic.expectedNewlineOrSemicolon(), this._peekToken());
|
2608
|
-
}
|
2609
|
-
|
2610
|
-
return statement;
|
2611
|
-
}
|
2612
|
-
|
2613
|
-
// small_stmt: (expr_stmt | del_stmt | pass_stmt | flow_stmt |
|
2614
|
-
// import_stmt | global_stmt | nonlocal_stmt | assert_stmt)
|
2615
|
-
// flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt | yield_stmt
|
2616
|
-
// import_stmt: import_name | import_from
|
2617
|
-
private _parseSmallStatement(): ParseNode {
|
2618
|
-
switch (this._peekKeywordType()) {
|
2619
|
-
case KeywordType.Pass:
|
2620
|
-
return this._parsePassStatement();
|
2621
|
-
|
2622
|
-
case KeywordType.Break:
|
2623
|
-
return this._parseBreakStatement();
|
2624
|
-
|
2625
|
-
case KeywordType.Continue:
|
2626
|
-
return this._parseContinueStatement();
|
2627
|
-
|
2628
|
-
case KeywordType.Return:
|
2629
|
-
return this._parseReturnStatement();
|
2630
|
-
|
2631
|
-
case KeywordType.From:
|
2632
|
-
return this._parseFromStatement();
|
2633
|
-
|
2634
|
-
case KeywordType.Import:
|
2635
|
-
return this._parseImportStatement();
|
2636
|
-
|
2637
|
-
case KeywordType.Global:
|
2638
|
-
return this._parseGlobalStatement();
|
2639
|
-
|
2640
|
-
case KeywordType.Nonlocal:
|
2641
|
-
return this._parseNonlocalStatement();
|
2642
|
-
|
2643
|
-
case KeywordType.Raise:
|
2644
|
-
return this._parseRaiseStatement();
|
2645
|
-
|
2646
|
-
case KeywordType.Assert:
|
2647
|
-
return this._parseAssertStatement();
|
2648
|
-
|
2649
|
-
case KeywordType.Del:
|
2650
|
-
return this._parseDelStatement();
|
2651
|
-
|
2652
|
-
case KeywordType.Yield:
|
2653
|
-
return this._parseYieldExpression();
|
2654
|
-
}
|
2655
|
-
|
2656
|
-
return this._parseExpressionStatement();
|
2657
|
-
}
|
2658
|
-
|
2659
|
-
private _makeExpressionOrTuple(
|
2660
|
-
exprListResult: ListResult<ExpressionNode>,
|
2661
|
-
enclosedInParens: boolean
|
2662
|
-
): ExpressionNode {
|
2663
|
-
// A single-element tuple with no trailing comma is simply an expression
|
2664
|
-
// that's surrounded by parens.
|
2665
|
-
if (exprListResult.list.length === 1 && !exprListResult.trailingComma) {
|
2666
|
-
if (exprListResult.list[0].nodeType === ParseNodeType.Unpack) {
|
2667
|
-
this._addError(Localizer.Diagnostic.unpackOperatorNotAllowed(), exprListResult.list[0]);
|
2668
|
-
}
|
2669
|
-
return exprListResult.list[0];
|
2670
|
-
}
|
2671
|
-
|
2672
|
-
// To accommodate empty tuples ("()"), we will reach back to get
|
2673
|
-
// the opening parenthesis as the opening token.
|
2674
|
-
|
2675
|
-
const tupleStartRange: TextRange =
|
2676
|
-
exprListResult.list.length > 0 ? exprListResult.list[0] : this._peekToken(-1);
|
2677
|
-
|
2678
|
-
const tupleNode = TupleNode.create(tupleStartRange, enclosedInParens);
|
2679
|
-
tupleNode.expressions = exprListResult.list;
|
2680
|
-
if (exprListResult.list.length > 0) {
|
2681
|
-
exprListResult.list.forEach((expr) => {
|
2682
|
-
expr.parent = tupleNode;
|
2683
|
-
});
|
2684
|
-
extendRange(tupleNode, exprListResult.list[exprListResult.list.length - 1]);
|
2685
|
-
}
|
2686
|
-
|
2687
|
-
return tupleNode;
|
2688
|
-
}
|
2689
|
-
|
2690
|
-
private _parseExpressionListAsPossibleTuple(
|
2691
|
-
errorCategory: ErrorExpressionCategory,
|
2692
|
-
errorString: string,
|
2693
|
-
errorToken: Token
|
2694
|
-
): ExpressionNode {
|
2695
|
-
if (this._isNextTokenNeverExpression()) {
|
2696
|
-
this._addError(errorString, errorToken);
|
2697
|
-
return ErrorNode.create(errorToken, errorCategory);
|
2698
|
-
}
|
2699
|
-
|
2700
|
-
const exprListResult = this._parseExpressionList(/* allowStar */ true);
|
2701
|
-
if (exprListResult.parseError) {
|
2702
|
-
return exprListResult.parseError;
|
2703
|
-
}
|
2704
|
-
return this._makeExpressionOrTuple(exprListResult, /* enclosedInParens */ false);
|
2705
|
-
}
|
2706
|
-
|
2707
|
-
private _parseTestListAsExpression(errorCategory: ErrorExpressionCategory, errorString: string): ExpressionNode {
|
2708
|
-
if (this._isNextTokenNeverExpression()) {
|
2709
|
-
return this._handleExpressionParseError(errorCategory, errorString);
|
2710
|
-
}
|
2711
|
-
|
2712
|
-
const exprListResult = this._parseTestExpressionList();
|
2713
|
-
if (exprListResult.parseError) {
|
2714
|
-
return exprListResult.parseError;
|
2715
|
-
}
|
2716
|
-
return this._makeExpressionOrTuple(exprListResult, /* enclosedInParens */ false);
|
2717
|
-
}
|
2718
|
-
|
2719
|
-
private _parseTestOrStarListAsExpression(
|
2720
|
-
allowAssignmentExpression: boolean,
|
2721
|
-
allowMultipleUnpack: boolean,
|
2722
|
-
errorCategory: ErrorExpressionCategory,
|
2723
|
-
errorString: string
|
2724
|
-
): ExpressionNode {
|
2725
|
-
if (this._isNextTokenNeverExpression()) {
|
2726
|
-
return this._handleExpressionParseError(errorCategory, errorString);
|
2727
|
-
}
|
2728
|
-
|
2729
|
-
const exprListResult = this._parseTestOrStarExpressionList(allowAssignmentExpression, allowMultipleUnpack);
|
2730
|
-
if (exprListResult.parseError) {
|
2731
|
-
return exprListResult.parseError;
|
2732
|
-
}
|
2733
|
-
return this._makeExpressionOrTuple(exprListResult, /* enclosedInParens */ false);
|
2734
|
-
}
|
2735
|
-
|
2736
|
-
private _parseExpressionList(allowStar: boolean): ListResult<ExpressionNode> {
|
2737
|
-
return this._parseExpressionListGeneric(() => this._parseExpression(allowStar));
|
2738
|
-
}
|
2739
|
-
|
2740
|
-
// testlist: test (',' test)* [',']
|
2741
|
-
private _parseTestExpressionList(): ListResult<ExpressionNode> {
|
2742
|
-
return this._parseExpressionListGeneric(() => this._parseTestExpression(/* allowAssignmentExpression */ false));
|
2743
|
-
}
|
2744
|
-
|
2745
|
-
private _parseTestOrStarExpressionList(
|
2746
|
-
allowAssignmentExpression: boolean,
|
2747
|
-
allowMultipleUnpack: boolean
|
2748
|
-
): ListResult<ExpressionNode> {
|
2749
|
-
const exprListResult = this._parseExpressionListGeneric(() =>
|
2750
|
-
this._parseTestOrStarExpression(allowAssignmentExpression)
|
2751
|
-
);
|
2752
|
-
|
2753
|
-
if (!allowMultipleUnpack && !exprListResult.parseError) {
|
2754
|
-
let sawStar = false;
|
2755
|
-
for (const expr of exprListResult.list) {
|
2756
|
-
if (expr.nodeType === ParseNodeType.Unpack) {
|
2757
|
-
if (sawStar) {
|
2758
|
-
this._addError(Localizer.Diagnostic.duplicateUnpack(), expr);
|
2759
|
-
break;
|
2760
|
-
}
|
2761
|
-
sawStar = true;
|
2762
|
-
}
|
2763
|
-
}
|
2764
|
-
}
|
2765
|
-
|
2766
|
-
return exprListResult;
|
2767
|
-
}
|
2768
|
-
|
2769
|
-
// exp_or_star: expr | star_expr
|
2770
|
-
// expr: xor_expr ('|' xor_expr)*
|
2771
|
-
// star_expr: '*' expr
|
2772
|
-
private _parseExpression(allowUnpack: boolean): ExpressionNode {
|
2773
|
-
const startToken = this._peekToken();
|
2774
|
-
|
2775
|
-
if (allowUnpack && this._consumeTokenIfOperator(OperatorType.Multiply)) {
|
2776
|
-
return UnpackNode.create(startToken, this._parseExpression(/* allowUnpack */ false));
|
2777
|
-
}
|
2778
|
-
|
2779
|
-
return this._parseBitwiseOrExpression();
|
2780
|
-
}
|
2781
|
-
|
2782
|
-
// test_or_star: test | star_expr
|
2783
|
-
private _parseTestOrStarExpression(allowAssignmentExpression: boolean): ExpressionNode {
|
2784
|
-
if (this._peekOperatorType() === OperatorType.Multiply) {
|
2785
|
-
return this._parseExpression(/* allowUnpack */ true);
|
2786
|
-
}
|
2787
|
-
|
2788
|
-
return this._parseTestExpression(allowAssignmentExpression);
|
2789
|
-
}
|
2790
|
-
|
2791
|
-
// test: or_test ['if' or_test 'else' test] | lambdef
|
2792
|
-
private _parseTestExpression(allowAssignmentExpression: boolean): ExpressionNode {
|
2793
|
-
if (this._peekKeywordType() === KeywordType.Lambda) {
|
2794
|
-
return this._parseLambdaExpression();
|
2795
|
-
}
|
2796
|
-
|
2797
|
-
const ifExpr = allowAssignmentExpression ? this._parseAssignmentExpression() : this._parseOrTest();
|
2798
|
-
if (ifExpr.nodeType === ParseNodeType.Error) {
|
2799
|
-
return ifExpr;
|
2800
|
-
}
|
2801
|
-
|
2802
|
-
if (!this._consumeTokenIfKeyword(KeywordType.If)) {
|
2803
|
-
return ifExpr;
|
2804
|
-
}
|
2805
|
-
|
2806
|
-
const testExpr = this._parseOrTest();
|
2807
|
-
if (testExpr.nodeType === ParseNodeType.Error) {
|
2808
|
-
return testExpr;
|
2809
|
-
}
|
2810
|
-
|
2811
|
-
if (!this._consumeTokenIfKeyword(KeywordType.Else)) {
|
2812
|
-
return this._handleExpressionParseError(
|
2813
|
-
ErrorExpressionCategory.MissingElse,
|
2814
|
-
Localizer.Diagnostic.expectedElse()
|
2815
|
-
);
|
2816
|
-
}
|
2817
|
-
|
2818
|
-
const elseExpr = this._parseTestExpression(/* allowAssignmentExpression */ true);
|
2819
|
-
if (elseExpr.nodeType === ParseNodeType.Error) {
|
2820
|
-
return elseExpr;
|
2821
|
-
}
|
2822
|
-
|
2823
|
-
return TernaryNode.create(ifExpr, testExpr, elseExpr);
|
2824
|
-
}
|
2825
|
-
|
2826
|
-
// assign_expr: NAME := test
|
2827
|
-
private _parseAssignmentExpression(disallowAssignmentExpression = false) {
|
2828
|
-
const leftExpr = this._parseOrTest();
|
2829
|
-
if (leftExpr.nodeType === ParseNodeType.Error) {
|
2830
|
-
return leftExpr;
|
2831
|
-
}
|
2832
|
-
|
2833
|
-
if (leftExpr.nodeType !== ParseNodeType.Name) {
|
2834
|
-
return leftExpr;
|
2835
|
-
}
|
2836
|
-
|
2837
|
-
const walrusToken = this._peekToken();
|
2838
|
-
if (!this._consumeTokenIfOperator(OperatorType.Walrus)) {
|
2839
|
-
return leftExpr;
|
2840
|
-
}
|
2841
|
-
|
2842
|
-
if (!this._assignmentExpressionsAllowed || this._isParsingTypeAnnotation || disallowAssignmentExpression) {
|
2843
|
-
this._addError(Localizer.Diagnostic.walrusNotAllowed(), walrusToken);
|
2844
|
-
}
|
2845
|
-
|
2846
|
-
if (this._getLanguageVersion() < PythonVersion.V3_8) {
|
2847
|
-
this._addError(Localizer.Diagnostic.walrusIllegal(), walrusToken);
|
2848
|
-
}
|
2849
|
-
|
2850
|
-
const rightExpr = this._parseTestExpression(/* allowAssignmentExpression */ false);
|
2851
|
-
|
2852
|
-
return AssignmentExpressionNode.create(leftExpr, rightExpr);
|
2853
|
-
}
|
2854
|
-
|
2855
|
-
// or_test: and_test ('or' and_test)*
|
2856
|
-
private _parseOrTest(): ExpressionNode {
|
2857
|
-
let leftExpr = this._parseAndTest();
|
2858
|
-
if (leftExpr.nodeType === ParseNodeType.Error) {
|
2859
|
-
return leftExpr;
|
2860
|
-
}
|
2861
|
-
|
2862
|
-
while (true) {
|
2863
|
-
const peekToken = this._peekToken();
|
2864
|
-
if (!this._consumeTokenIfKeyword(KeywordType.Or)) {
|
2865
|
-
break;
|
2866
|
-
}
|
2867
|
-
const rightExpr = this._parseAndTest();
|
2868
|
-
leftExpr = this._createBinaryOperationNode(leftExpr, rightExpr, peekToken, OperatorType.Or);
|
2869
|
-
}
|
2870
|
-
|
2871
|
-
return leftExpr;
|
2872
|
-
}
|
2873
|
-
|
2874
|
-
// and_test: not_test ('and' not_test)*
|
2875
|
-
private _parseAndTest(): ExpressionNode {
|
2876
|
-
let leftExpr = this._parseNotTest();
|
2877
|
-
if (leftExpr.nodeType === ParseNodeType.Error) {
|
2878
|
-
return leftExpr;
|
2879
|
-
}
|
2880
|
-
|
2881
|
-
while (true) {
|
2882
|
-
const peekToken = this._peekToken();
|
2883
|
-
if (!this._consumeTokenIfKeyword(KeywordType.And)) {
|
2884
|
-
break;
|
2885
|
-
}
|
2886
|
-
const rightExpr = this._parseNotTest();
|
2887
|
-
leftExpr = this._createBinaryOperationNode(leftExpr, rightExpr, peekToken, OperatorType.And);
|
2888
|
-
}
|
2889
|
-
|
2890
|
-
return leftExpr;
|
2891
|
-
}
|
2892
|
-
|
2893
|
-
// not_test: 'not' not_test | comparison
|
2894
|
-
private _parseNotTest(): ExpressionNode {
|
2895
|
-
const notToken = this._peekToken();
|
2896
|
-
if (this._consumeTokenIfKeyword(KeywordType.Not)) {
|
2897
|
-
const notExpr = this._parseNotTest();
|
2898
|
-
return this._createUnaryOperationNode(notToken, notExpr, OperatorType.Not);
|
2899
|
-
}
|
2900
|
-
|
2901
|
-
return this._parseComparison();
|
2902
|
-
}
|
2903
|
-
|
2904
|
-
// comparison: expr (comp_op expr)*
|
2905
|
-
// comp_op: '<'|'>'|'=='|'>='|'<='|'<>'|'!='|'in'|'not' 'in'|'is'|'is' 'not'
|
2906
|
-
private _parseComparison(): ExpressionNode {
|
2907
|
-
let leftExpr = this._parseBitwiseOrExpression();
|
2908
|
-
if (leftExpr.nodeType === ParseNodeType.Error) {
|
2909
|
-
return leftExpr;
|
2910
|
-
}
|
2911
|
-
|
2912
|
-
while (true) {
|
2913
|
-
let comparisonOperator: OperatorType | undefined;
|
2914
|
-
const peekToken = this._peekToken();
|
2915
|
-
|
2916
|
-
if (Tokenizer.isOperatorComparison(this._peekOperatorType())) {
|
2917
|
-
comparisonOperator = this._peekOperatorType();
|
2918
|
-
if (comparisonOperator === OperatorType.LessOrGreaterThan) {
|
2919
|
-
this._addError(Localizer.Diagnostic.operatorLessOrGreaterDeprecated(), peekToken);
|
2920
|
-
comparisonOperator = OperatorType.NotEquals;
|
2921
|
-
}
|
2922
|
-
this._getNextToken();
|
2923
|
-
} else if (this._consumeTokenIfKeyword(KeywordType.In)) {
|
2924
|
-
comparisonOperator = OperatorType.In;
|
2925
|
-
} else if (this._consumeTokenIfKeyword(KeywordType.Is)) {
|
2926
|
-
if (this._consumeTokenIfKeyword(KeywordType.Not)) {
|
2927
|
-
comparisonOperator = OperatorType.IsNot;
|
2928
|
-
} else {
|
2929
|
-
comparisonOperator = OperatorType.Is;
|
2930
|
-
}
|
2931
|
-
} else if (this._peekKeywordType() === KeywordType.Not) {
|
2932
|
-
const tokenAfterNot = this._peekToken(1);
|
2933
|
-
if (
|
2934
|
-
tokenAfterNot.type === TokenType.Keyword &&
|
2935
|
-
(tokenAfterNot as KeywordToken).keywordType === KeywordType.In
|
2936
|
-
) {
|
2937
|
-
this._getNextToken();
|
2938
|
-
this._getNextToken();
|
2939
|
-
comparisonOperator = OperatorType.NotIn;
|
2940
|
-
}
|
2941
|
-
}
|
2942
|
-
|
2943
|
-
if (comparisonOperator === undefined) {
|
2944
|
-
break;
|
2945
|
-
}
|
2946
|
-
|
2947
|
-
const rightExpr = this._parseComparison();
|
2948
|
-
leftExpr = this._createBinaryOperationNode(leftExpr, rightExpr, peekToken, comparisonOperator);
|
2949
|
-
}
|
2950
|
-
|
2951
|
-
return leftExpr;
|
2952
|
-
}
|
2953
|
-
|
2954
|
-
// expr: xor_expr ('|' xor_expr)*
|
2955
|
-
private _parseBitwiseOrExpression(): ExpressionNode {
|
2956
|
-
let leftExpr = this._parseBitwiseXorExpression();
|
2957
|
-
if (leftExpr.nodeType === ParseNodeType.Error) {
|
2958
|
-
return leftExpr;
|
2959
|
-
}
|
2960
|
-
|
2961
|
-
while (true) {
|
2962
|
-
const peekToken = this._peekToken();
|
2963
|
-
if (!this._consumeTokenIfOperator(OperatorType.BitwiseOr)) {
|
2964
|
-
break;
|
2965
|
-
}
|
2966
|
-
const rightExpr = this._parseBitwiseXorExpression();
|
2967
|
-
leftExpr = this._createBinaryOperationNode(leftExpr, rightExpr, peekToken, OperatorType.BitwiseOr);
|
2968
|
-
}
|
2969
|
-
|
2970
|
-
return leftExpr;
|
2971
|
-
}
|
2972
|
-
|
2973
|
-
// xor_expr: and_expr ('^' and_expr)*
|
2974
|
-
private _parseBitwiseXorExpression(): ExpressionNode {
|
2975
|
-
let leftExpr = this._parseBitwiseAndExpression();
|
2976
|
-
if (leftExpr.nodeType === ParseNodeType.Error) {
|
2977
|
-
return leftExpr;
|
2978
|
-
}
|
2979
|
-
|
2980
|
-
while (true) {
|
2981
|
-
const peekToken = this._peekToken();
|
2982
|
-
if (!this._consumeTokenIfOperator(OperatorType.BitwiseXor)) {
|
2983
|
-
break;
|
2984
|
-
}
|
2985
|
-
const rightExpr = this._parseBitwiseAndExpression();
|
2986
|
-
leftExpr = this._createBinaryOperationNode(leftExpr, rightExpr, peekToken, OperatorType.BitwiseXor);
|
2987
|
-
}
|
2988
|
-
|
2989
|
-
return leftExpr;
|
2990
|
-
}
|
2991
|
-
|
2992
|
-
// and_expr: shift_expr ('&' shift_expr)*
|
2993
|
-
private _parseBitwiseAndExpression(): ExpressionNode {
|
2994
|
-
let leftExpr = this._parseShiftExpression();
|
2995
|
-
if (leftExpr.nodeType === ParseNodeType.Error) {
|
2996
|
-
return leftExpr;
|
2997
|
-
}
|
2998
|
-
|
2999
|
-
while (true) {
|
3000
|
-
const peekToken = this._peekToken();
|
3001
|
-
if (!this._consumeTokenIfOperator(OperatorType.BitwiseAnd)) {
|
3002
|
-
break;
|
3003
|
-
}
|
3004
|
-
const rightExpr = this._parseShiftExpression();
|
3005
|
-
leftExpr = this._createBinaryOperationNode(leftExpr, rightExpr, peekToken, OperatorType.BitwiseAnd);
|
3006
|
-
}
|
3007
|
-
|
3008
|
-
return leftExpr;
|
3009
|
-
}
|
3010
|
-
|
3011
|
-
// shift_expr: arith_expr (('<<'|'>>') arith_expr)*
|
3012
|
-
private _parseShiftExpression(): ExpressionNode {
|
3013
|
-
let leftExpr = this._parseArithmeticExpression();
|
3014
|
-
if (leftExpr.nodeType === ParseNodeType.Error) {
|
3015
|
-
return leftExpr;
|
3016
|
-
}
|
3017
|
-
|
3018
|
-
let peekToken = this._peekToken();
|
3019
|
-
let nextOperator = this._peekOperatorType();
|
3020
|
-
while (nextOperator === OperatorType.LeftShift || nextOperator === OperatorType.RightShift) {
|
3021
|
-
this._getNextToken();
|
3022
|
-
const rightExpr = this._parseArithmeticExpression();
|
3023
|
-
leftExpr = this._createBinaryOperationNode(leftExpr, rightExpr, peekToken, nextOperator);
|
3024
|
-
peekToken = this._peekToken();
|
3025
|
-
nextOperator = this._peekOperatorType();
|
3026
|
-
}
|
3027
|
-
|
3028
|
-
return leftExpr;
|
3029
|
-
}
|
3030
|
-
|
3031
|
-
// arith_expr: term (('+'|'-') term)*
|
3032
|
-
private _parseArithmeticExpression(): ExpressionNode {
|
3033
|
-
let leftExpr = this._parseArithmeticTerm();
|
3034
|
-
if (leftExpr.nodeType === ParseNodeType.Error) {
|
3035
|
-
return leftExpr;
|
3036
|
-
}
|
3037
|
-
|
3038
|
-
let peekToken = this._peekToken();
|
3039
|
-
let nextOperator = this._peekOperatorType();
|
3040
|
-
while (nextOperator === OperatorType.Add || nextOperator === OperatorType.Subtract) {
|
3041
|
-
this._getNextToken();
|
3042
|
-
const rightExpr = this._parseArithmeticTerm();
|
3043
|
-
if (rightExpr.nodeType === ParseNodeType.Error) {
|
3044
|
-
return rightExpr;
|
3045
|
-
}
|
3046
|
-
|
3047
|
-
leftExpr = this._createBinaryOperationNode(leftExpr, rightExpr, peekToken, nextOperator);
|
3048
|
-
peekToken = this._peekToken();
|
3049
|
-
nextOperator = this._peekOperatorType();
|
3050
|
-
}
|
3051
|
-
|
3052
|
-
return leftExpr;
|
3053
|
-
}
|
3054
|
-
|
3055
|
-
// term: factor (('*'|'@'|'/'|'%'|'//') factor)*
|
3056
|
-
private _parseArithmeticTerm(): ExpressionNode {
|
3057
|
-
let leftExpr = this._parseArithmeticFactor();
|
3058
|
-
if (leftExpr.nodeType === ParseNodeType.Error) {
|
3059
|
-
return leftExpr;
|
3060
|
-
}
|
3061
|
-
|
3062
|
-
let peekToken = this._peekToken();
|
3063
|
-
let nextOperator = this._peekOperatorType();
|
3064
|
-
while (
|
3065
|
-
nextOperator === OperatorType.Multiply ||
|
3066
|
-
nextOperator === OperatorType.MatrixMultiply ||
|
3067
|
-
nextOperator === OperatorType.Divide ||
|
3068
|
-
nextOperator === OperatorType.Mod ||
|
3069
|
-
nextOperator === OperatorType.FloorDivide
|
3070
|
-
) {
|
3071
|
-
this._getNextToken();
|
3072
|
-
const rightExpr = this._parseArithmeticFactor();
|
3073
|
-
leftExpr = this._createBinaryOperationNode(leftExpr, rightExpr, peekToken, nextOperator);
|
3074
|
-
peekToken = this._peekToken();
|
3075
|
-
nextOperator = this._peekOperatorType();
|
3076
|
-
}
|
3077
|
-
|
3078
|
-
return leftExpr;
|
3079
|
-
}
|
3080
|
-
|
3081
|
-
// factor: ('+'|'-'|'~') factor | power
|
3082
|
-
// power: atom_expr ['**' factor]
|
3083
|
-
private _parseArithmeticFactor(): ExpressionNode {
|
3084
|
-
const nextToken = this._peekToken();
|
3085
|
-
const nextOperator = this._peekOperatorType();
|
3086
|
-
if (
|
3087
|
-
nextOperator === OperatorType.Add ||
|
3088
|
-
nextOperator === OperatorType.Subtract ||
|
3089
|
-
nextOperator === OperatorType.BitwiseInvert
|
3090
|
-
) {
|
3091
|
-
this._getNextToken();
|
3092
|
-
const expression = this._parseArithmeticFactor();
|
3093
|
-
return this._createUnaryOperationNode(nextToken, expression, nextOperator);
|
3094
|
-
}
|
3095
|
-
|
3096
|
-
const leftExpr = this._parseAtomExpression();
|
3097
|
-
if (leftExpr.nodeType === ParseNodeType.Error) {
|
3098
|
-
return leftExpr;
|
3099
|
-
}
|
3100
|
-
|
3101
|
-
const peekToken = this._peekToken();
|
3102
|
-
if (this._consumeTokenIfOperator(OperatorType.Power)) {
|
3103
|
-
const rightExpr = this._parseArithmeticFactor();
|
3104
|
-
return this._createBinaryOperationNode(leftExpr, rightExpr, peekToken, OperatorType.Power);
|
3105
|
-
}
|
3106
|
-
|
3107
|
-
return leftExpr;
|
3108
|
-
}
|
3109
|
-
|
3110
|
-
// Determines whether the expression refers to a type exported by the typing
|
3111
|
-
// or typing_extensions modules. We can directly evaluate the types at binding
|
3112
|
-
// time. We assume here that the code isn't making use of some custom type alias
|
3113
|
-
// to refer to the typing types.
|
3114
|
-
private _isTypingAnnotation(typeAnnotation: ExpressionNode, name: string): boolean {
|
3115
|
-
if (typeAnnotation.nodeType === ParseNodeType.Name) {
|
3116
|
-
const alias = this._typingSymbolAliases.get(typeAnnotation.value);
|
3117
|
-
if (alias === name) {
|
3118
|
-
return true;
|
3119
|
-
}
|
3120
|
-
} else if (typeAnnotation.nodeType === ParseNodeType.MemberAccess) {
|
3121
|
-
if (
|
3122
|
-
typeAnnotation.leftExpression.nodeType === ParseNodeType.Name &&
|
3123
|
-
typeAnnotation.memberName.value === name
|
3124
|
-
) {
|
3125
|
-
const baseName = typeAnnotation.leftExpression.value;
|
3126
|
-
return this._typingImportAliases.some((alias) => alias === baseName);
|
3127
|
-
}
|
3128
|
-
}
|
3129
|
-
|
3130
|
-
return false;
|
3131
|
-
}
|
3132
|
-
|
3133
|
-
// atom_expr: ['await'] atom trailer*
|
3134
|
-
// trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
|
3135
|
-
private _parseAtomExpression(): ExpressionNode {
|
3136
|
-
let awaitToken: KeywordToken | undefined;
|
3137
|
-
if (this._peekKeywordType() === KeywordType.Await && !this._isParsingTypeAnnotation) {
|
3138
|
-
awaitToken = this._getKeywordToken(KeywordType.Await);
|
3139
|
-
if (this._getLanguageVersion() < PythonVersion.V3_5) {
|
3140
|
-
this._addError(Localizer.Diagnostic.awaitIllegal(), awaitToken);
|
3141
|
-
}
|
3142
|
-
}
|
3143
|
-
|
3144
|
-
let atomExpression = this._parseAtom();
|
3145
|
-
if (atomExpression.nodeType === ParseNodeType.Error) {
|
3146
|
-
return atomExpression;
|
3147
|
-
}
|
3148
|
-
|
3149
|
-
// Consume trailers.
|
3150
|
-
while (true) {
|
3151
|
-
// Is it a function call?
|
3152
|
-
const startOfTrailerToken = this._peekToken();
|
3153
|
-
if (this._consumeTokenIfType(TokenType.OpenParenthesis)) {
|
3154
|
-
// Generally, function calls are not allowed within type annotations,
|
3155
|
-
// but they are permitted in "Annotated" annotations.
|
3156
|
-
const wasParsingTypeAnnotation = this._isParsingTypeAnnotation;
|
3157
|
-
this._isParsingTypeAnnotation = false;
|
3158
|
-
|
3159
|
-
const argListResult = this._parseArgList();
|
3160
|
-
const callNode = CallNode.create(atomExpression, argListResult.args, argListResult.trailingComma);
|
3161
|
-
|
3162
|
-
if (argListResult.args.length > 1 || argListResult.trailingComma) {
|
3163
|
-
argListResult.args.forEach((arg) => {
|
3164
|
-
if (arg.valueExpression.nodeType === ParseNodeType.ListComprehension) {
|
3165
|
-
if (!arg.valueExpression.isParenthesized) {
|
3166
|
-
this._addError(Localizer.Diagnostic.generatorNotParenthesized(), arg.valueExpression);
|
3167
|
-
}
|
3168
|
-
}
|
3169
|
-
});
|
3170
|
-
}
|
3171
|
-
|
3172
|
-
const nextToken = this._peekToken();
|
3173
|
-
let isArgListTerminated = false;
|
3174
|
-
if (!this._consumeTokenIfType(TokenType.CloseParenthesis)) {
|
3175
|
-
this._addError(Localizer.Diagnostic.expectedCloseParen(), startOfTrailerToken);
|
3176
|
-
|
3177
|
-
// Consume the remainder of tokens on the line for error
|
3178
|
-
// recovery.
|
3179
|
-
this._consumeTokensUntilType([TokenType.NewLine]);
|
3180
|
-
|
3181
|
-
// Extend the node's range to include the rest of the line.
|
3182
|
-
// This helps the signatureHelpProvider.
|
3183
|
-
extendRange(callNode, this._peekToken());
|
3184
|
-
} else {
|
3185
|
-
extendRange(callNode, nextToken);
|
3186
|
-
isArgListTerminated = true;
|
3187
|
-
}
|
3188
|
-
|
3189
|
-
this._isParsingTypeAnnotation = wasParsingTypeAnnotation;
|
3190
|
-
|
3191
|
-
if (this._isParsingTypeAnnotation) {
|
3192
|
-
const diag = new DiagnosticAddendum();
|
3193
|
-
if (atomExpression.nodeType === ParseNodeType.Name && atomExpression.value === 'type') {
|
3194
|
-
diag.addMessage(Localizer.DiagnosticAddendum.useTypeInstead());
|
3195
|
-
this._addError(Localizer.Diagnostic.typeCallNotAllowed() + diag.getString(), callNode);
|
3196
|
-
}
|
3197
|
-
}
|
3198
|
-
|
3199
|
-
atomExpression = callNode;
|
3200
|
-
|
3201
|
-
if (atomExpression.maxChildDepth !== undefined && atomExpression.maxChildDepth >= maxChildNodeDepth) {
|
3202
|
-
atomExpression = ErrorNode.create(atomExpression, ErrorExpressionCategory.MaxDepthExceeded);
|
3203
|
-
this._addError(Localizer.Diagnostic.maxParseDepthExceeded(), atomExpression);
|
3204
|
-
}
|
3205
|
-
|
3206
|
-
// If the argument list wasn't terminated, break out of the loop
|
3207
|
-
if (!isArgListTerminated) {
|
3208
|
-
break;
|
3209
|
-
}
|
3210
|
-
} else if (this._consumeTokenIfType(TokenType.OpenBracket)) {
|
3211
|
-
// Is it an index operator?
|
3212
|
-
|
3213
|
-
// This is an unfortunate hack that's necessary to accommodate 'Literal'
|
3214
|
-
// and 'Annotated' type annotations properly. We need to suspend treating
|
3215
|
-
// strings as type annotations within a Literal or Annotated subscript.
|
3216
|
-
const wasParsingIndexTrailer = this._isParsingIndexTrailer;
|
3217
|
-
const wasParsingTypeAnnotation = this._isParsingTypeAnnotation;
|
3218
|
-
|
3219
|
-
if (
|
3220
|
-
this._isTypingAnnotation(atomExpression, 'Literal') ||
|
3221
|
-
this._isTypingAnnotation(atomExpression, 'Annotated')
|
3222
|
-
) {
|
3223
|
-
this._isParsingTypeAnnotation = false;
|
3224
|
-
}
|
3225
|
-
|
3226
|
-
this._isParsingIndexTrailer = true;
|
3227
|
-
const subscriptList = this._parseSubscriptList();
|
3228
|
-
this._isParsingTypeAnnotation = wasParsingTypeAnnotation;
|
3229
|
-
this._isParsingIndexTrailer = wasParsingIndexTrailer;
|
3230
|
-
|
3231
|
-
const closingToken = this._peekToken();
|
3232
|
-
|
3233
|
-
const indexNode = IndexNode.create(
|
3234
|
-
atomExpression,
|
3235
|
-
subscriptList.list,
|
3236
|
-
subscriptList.trailingComma,
|
3237
|
-
closingToken
|
3238
|
-
);
|
3239
|
-
extendRange(indexNode, indexNode);
|
3240
|
-
|
3241
|
-
if (!this._consumeTokenIfType(TokenType.CloseBracket)) {
|
3242
|
-
// Handle the error case, but don't use the error node in this
|
3243
|
-
// case because it creates problems for the completion provider.
|
3244
|
-
this._handleExpressionParseError(
|
3245
|
-
ErrorExpressionCategory.MissingIndexCloseBracket,
|
3246
|
-
Localizer.Diagnostic.expectedCloseBracket(),
|
3247
|
-
startOfTrailerToken,
|
3248
|
-
indexNode
|
3249
|
-
);
|
3250
|
-
}
|
3251
|
-
|
3252
|
-
atomExpression = indexNode;
|
3253
|
-
|
3254
|
-
if (atomExpression.maxChildDepth !== undefined && atomExpression.maxChildDepth >= maxChildNodeDepth) {
|
3255
|
-
atomExpression = ErrorNode.create(atomExpression, ErrorExpressionCategory.MaxDepthExceeded);
|
3256
|
-
this._addError(Localizer.Diagnostic.maxParseDepthExceeded(), atomExpression);
|
3257
|
-
}
|
3258
|
-
} else if (this._consumeTokenIfType(TokenType.Dot)) {
|
3259
|
-
// Is it a member access?
|
3260
|
-
const memberName = this._getTokenIfIdentifier();
|
3261
|
-
if (!memberName) {
|
3262
|
-
return this._handleExpressionParseError(
|
3263
|
-
ErrorExpressionCategory.MissingMemberAccessName,
|
3264
|
-
Localizer.Diagnostic.expectedMemberName(),
|
3265
|
-
startOfTrailerToken,
|
3266
|
-
atomExpression,
|
3267
|
-
[TokenType.Keyword]
|
3268
|
-
);
|
3269
|
-
}
|
3270
|
-
atomExpression = MemberAccessNode.create(atomExpression, NameNode.create(memberName));
|
3271
|
-
|
3272
|
-
if (atomExpression.maxChildDepth !== undefined && atomExpression.maxChildDepth >= maxChildNodeDepth) {
|
3273
|
-
atomExpression = ErrorNode.create(atomExpression, ErrorExpressionCategory.MaxDepthExceeded);
|
3274
|
-
this._addError(Localizer.Diagnostic.maxParseDepthExceeded(), atomExpression);
|
3275
|
-
}
|
3276
|
-
} else {
|
3277
|
-
break;
|
3278
|
-
}
|
3279
|
-
}
|
3280
|
-
|
3281
|
-
if (awaitToken) {
|
3282
|
-
return AwaitNode.create(awaitToken, atomExpression);
|
3283
|
-
}
|
3284
|
-
|
3285
|
-
return atomExpression;
|
3286
|
-
}
|
3287
|
-
|
3288
|
-
// subscriptlist: subscript (',' subscript)* [',']
|
3289
|
-
private _parseSubscriptList(): SubscriptListResult {
|
3290
|
-
const argList: ArgumentNode[] = [];
|
3291
|
-
let sawKeywordArg = false;
|
3292
|
-
let trailingComma = false;
|
3293
|
-
|
3294
|
-
while (true) {
|
3295
|
-
const firstToken = this._peekToken();
|
3296
|
-
|
3297
|
-
if (firstToken.type !== TokenType.Colon && this._isNextTokenNeverExpression()) {
|
3298
|
-
break;
|
3299
|
-
}
|
3300
|
-
|
3301
|
-
let argType = ArgumentCategory.Simple;
|
3302
|
-
if (this._consumeTokenIfOperator(OperatorType.Multiply)) {
|
3303
|
-
argType = ArgumentCategory.UnpackedList;
|
3304
|
-
} else if (this._consumeTokenIfOperator(OperatorType.Power)) {
|
3305
|
-
argType = ArgumentCategory.UnpackedDictionary;
|
3306
|
-
}
|
3307
|
-
|
3308
|
-
const startOfSubscriptIndex = this._tokenIndex;
|
3309
|
-
let valueExpr = this._parsePossibleSlice();
|
3310
|
-
let nameIdentifier: IdentifierToken | undefined;
|
3311
|
-
|
3312
|
-
// Is this a keyword argument?
|
3313
|
-
if (argType === ArgumentCategory.Simple) {
|
3314
|
-
if (this._consumeTokenIfOperator(OperatorType.Assign)) {
|
3315
|
-
const nameExpr = valueExpr;
|
3316
|
-
valueExpr = this._parsePossibleSlice();
|
3317
|
-
|
3318
|
-
if (nameExpr.nodeType === ParseNodeType.Name) {
|
3319
|
-
nameIdentifier = nameExpr.token;
|
3320
|
-
} else {
|
3321
|
-
this._addError(Localizer.Diagnostic.expectedParamName(), nameExpr);
|
3322
|
-
}
|
3323
|
-
} else if (
|
3324
|
-
valueExpr.nodeType === ParseNodeType.Name &&
|
3325
|
-
this._peekOperatorType() === OperatorType.Walrus
|
3326
|
-
) {
|
3327
|
-
this._tokenIndex = startOfSubscriptIndex;
|
3328
|
-
valueExpr = this._parseTestExpression(/* allowAssignmentExpression */ true);
|
3329
|
-
|
3330
|
-
// Python 3.10 and newer allow assignment expressions to be used inside of a subscript.
|
3331
|
-
if (!this._parseOptions.isStubFile && this._getLanguageVersion() < PythonVersion.V3_10) {
|
3332
|
-
this._addError(Localizer.Diagnostic.assignmentExprInSubscript(), valueExpr);
|
3333
|
-
}
|
3334
|
-
}
|
3335
|
-
}
|
3336
|
-
|
3337
|
-
const argNode = ArgumentNode.create(firstToken, valueExpr, argType);
|
3338
|
-
if (nameIdentifier) {
|
3339
|
-
argNode.name = NameNode.create(nameIdentifier);
|
3340
|
-
argNode.name.parent = argNode;
|
3341
|
-
}
|
3342
|
-
|
3343
|
-
if (argNode.name) {
|
3344
|
-
sawKeywordArg = true;
|
3345
|
-
} else if (sawKeywordArg && argNode.argumentCategory === ArgumentCategory.Simple) {
|
3346
|
-
this._addError(Localizer.Diagnostic.positionArgAfterNamedArg(), argNode);
|
3347
|
-
}
|
3348
|
-
argList.push(argNode);
|
3349
|
-
|
3350
|
-
if (argNode.name) {
|
3351
|
-
this._addError(Localizer.Diagnostic.keywordSubscriptIllegal(), argNode.name);
|
3352
|
-
}
|
3353
|
-
|
3354
|
-
if (argType !== ArgumentCategory.Simple) {
|
3355
|
-
const unpackAllowed =
|
3356
|
-
this._parseOptions.isStubFile || this._getLanguageVersion() >= PythonVersion.V3_11;
|
3357
|
-
|
3358
|
-
if (argType === ArgumentCategory.UnpackedDictionary || !unpackAllowed) {
|
3359
|
-
this._addError(Localizer.Diagnostic.unpackedSubscriptIllegal(), argNode);
|
3360
|
-
}
|
3361
|
-
}
|
3362
|
-
|
3363
|
-
if (!this._consumeTokenIfType(TokenType.Comma)) {
|
3364
|
-
trailingComma = false;
|
3365
|
-
break;
|
3366
|
-
}
|
3367
|
-
|
3368
|
-
trailingComma = true;
|
3369
|
-
}
|
3370
|
-
|
3371
|
-
// An empty subscript list is illegal.
|
3372
|
-
if (argList.length === 0) {
|
3373
|
-
const errorNode = this._handleExpressionParseError(
|
3374
|
-
ErrorExpressionCategory.MissingIndexOrSlice,
|
3375
|
-
Localizer.Diagnostic.expectedSliceIndex(),
|
3376
|
-
/* targetToken */ undefined,
|
3377
|
-
/* childNode */ undefined,
|
3378
|
-
[TokenType.CloseBracket]
|
3379
|
-
);
|
3380
|
-
argList.push(ArgumentNode.create(this._peekToken(), errorNode, ArgumentCategory.Simple));
|
3381
|
-
}
|
3382
|
-
|
3383
|
-
return {
|
3384
|
-
list: argList,
|
3385
|
-
trailingComma,
|
3386
|
-
};
|
3387
|
-
}
|
3388
|
-
|
3389
|
-
// subscript: test | [test] ':' [test] [sliceop]
|
3390
|
-
// sliceop: ':' [test]
|
3391
|
-
private _parsePossibleSlice(): ExpressionNode {
|
3392
|
-
const firstToken = this._peekToken();
|
3393
|
-
const sliceExpressions: (ExpressionNode | undefined)[] = [undefined, undefined, undefined];
|
3394
|
-
let sliceIndex = 0;
|
3395
|
-
let sawColon = false;
|
3396
|
-
|
3397
|
-
while (true) {
|
3398
|
-
const nextTokenType = this._peekTokenType();
|
3399
|
-
if (nextTokenType === TokenType.CloseBracket || nextTokenType === TokenType.Comma) {
|
3400
|
-
break;
|
3401
|
-
}
|
3402
|
-
|
3403
|
-
if (nextTokenType !== TokenType.Colon) {
|
3404
|
-
sliceExpressions[sliceIndex] = this._parseTestExpression(/* allowAssignmentExpression */ false);
|
3405
|
-
}
|
3406
|
-
sliceIndex++;
|
3407
|
-
|
3408
|
-
if (sliceIndex >= 3 || !this._consumeTokenIfType(TokenType.Colon)) {
|
3409
|
-
break;
|
3410
|
-
}
|
3411
|
-
sawColon = true;
|
3412
|
-
}
|
3413
|
-
|
3414
|
-
// If this was a simple expression with no colons return it.
|
3415
|
-
if (!sawColon) {
|
3416
|
-
if (sliceExpressions[0]) {
|
3417
|
-
return sliceExpressions[0];
|
3418
|
-
}
|
3419
|
-
|
3420
|
-
return ErrorNode.create(this._peekToken(), ErrorExpressionCategory.MissingIndexOrSlice);
|
3421
|
-
}
|
3422
|
-
|
3423
|
-
const sliceNode = SliceNode.create(firstToken);
|
3424
|
-
sliceNode.startValue = sliceExpressions[0];
|
3425
|
-
if (sliceNode.startValue) {
|
3426
|
-
sliceNode.startValue.parent = sliceNode;
|
3427
|
-
}
|
3428
|
-
sliceNode.endValue = sliceExpressions[1];
|
3429
|
-
if (sliceNode.endValue) {
|
3430
|
-
sliceNode.endValue.parent = sliceNode;
|
3431
|
-
}
|
3432
|
-
sliceNode.stepValue = sliceExpressions[2];
|
3433
|
-
if (sliceNode.stepValue) {
|
3434
|
-
sliceNode.stepValue.parent = sliceNode;
|
3435
|
-
}
|
3436
|
-
const extension = sliceExpressions[2] || sliceExpressions[1] || sliceExpressions[0];
|
3437
|
-
if (extension) {
|
3438
|
-
extendRange(sliceNode, extension);
|
3439
|
-
}
|
3440
|
-
|
3441
|
-
return sliceNode;
|
3442
|
-
}
|
3443
|
-
|
3444
|
-
// arglist: argument (',' argument)* [',']
|
3445
|
-
private _parseArgList(): ArgListResult {
|
3446
|
-
const argList: ArgumentNode[] = [];
|
3447
|
-
let sawKeywordArg = false;
|
3448
|
-
let trailingComma = false;
|
3449
|
-
|
3450
|
-
while (true) {
|
3451
|
-
const nextTokenType = this._peekTokenType();
|
3452
|
-
if (
|
3453
|
-
nextTokenType === TokenType.CloseParenthesis ||
|
3454
|
-
nextTokenType === TokenType.NewLine ||
|
3455
|
-
nextTokenType === TokenType.EndOfStream
|
3456
|
-
) {
|
3457
|
-
break;
|
3458
|
-
}
|
3459
|
-
|
3460
|
-
trailingComma = false;
|
3461
|
-
const arg = this._parseArgument();
|
3462
|
-
if (arg.name) {
|
3463
|
-
sawKeywordArg = true;
|
3464
|
-
} else if (sawKeywordArg && arg.argumentCategory === ArgumentCategory.Simple) {
|
3465
|
-
this._addError(Localizer.Diagnostic.positionArgAfterNamedArg(), arg);
|
3466
|
-
}
|
3467
|
-
argList.push(arg);
|
3468
|
-
|
3469
|
-
if (!this._consumeTokenIfType(TokenType.Comma)) {
|
3470
|
-
break;
|
3471
|
-
}
|
3472
|
-
|
3473
|
-
trailingComma = true;
|
3474
|
-
}
|
3475
|
-
|
3476
|
-
return { args: argList, trailingComma };
|
3477
|
-
}
|
3478
|
-
|
3479
|
-
// argument: ( test [comp_for] |
|
3480
|
-
// test '=' test |
|
3481
|
-
// '**' test |
|
3482
|
-
// '*' test )
|
3483
|
-
private _parseArgument(): ArgumentNode {
|
3484
|
-
const firstToken = this._peekToken();
|
3485
|
-
|
3486
|
-
let argType = ArgumentCategory.Simple;
|
3487
|
-
if (this._consumeTokenIfOperator(OperatorType.Multiply)) {
|
3488
|
-
argType = ArgumentCategory.UnpackedList;
|
3489
|
-
} else if (this._consumeTokenIfOperator(OperatorType.Power)) {
|
3490
|
-
argType = ArgumentCategory.UnpackedDictionary;
|
3491
|
-
}
|
3492
|
-
|
3493
|
-
let valueExpr = this._parseTestExpression(/* allowAssignmentExpression */ true);
|
3494
|
-
let nameIdentifier: IdentifierToken | undefined;
|
3495
|
-
|
3496
|
-
if (argType === ArgumentCategory.Simple) {
|
3497
|
-
if (this._consumeTokenIfOperator(OperatorType.Assign)) {
|
3498
|
-
const nameExpr = valueExpr;
|
3499
|
-
valueExpr = this._parseTestExpression(/* allowAssignmentExpression */ false);
|
3500
|
-
|
3501
|
-
if (nameExpr.nodeType === ParseNodeType.Name) {
|
3502
|
-
nameIdentifier = nameExpr.token;
|
3503
|
-
} else {
|
3504
|
-
this._addError(Localizer.Diagnostic.expectedParamName(), nameExpr);
|
3505
|
-
}
|
3506
|
-
} else {
|
3507
|
-
const listComp = this._tryParseListComprehension(valueExpr);
|
3508
|
-
if (listComp) {
|
3509
|
-
valueExpr = listComp;
|
3510
|
-
}
|
3511
|
-
}
|
3512
|
-
}
|
3513
|
-
|
3514
|
-
const argNode = ArgumentNode.create(firstToken, valueExpr, argType);
|
3515
|
-
if (nameIdentifier) {
|
3516
|
-
argNode.name = NameNode.create(nameIdentifier);
|
3517
|
-
argNode.name.parent = argNode;
|
3518
|
-
}
|
3519
|
-
|
3520
|
-
return argNode;
|
3521
|
-
}
|
3522
|
-
|
3523
|
-
// atom: ('(' [yield_expr | testlist_comp] ')' |
|
3524
|
-
// '[' [testlist_comp] ']' |
|
3525
|
-
// '{' [dictorsetmaker] '}' |
|
3526
|
-
// NAME | NUMBER | STRING+ | '...' | 'None' | 'True' | 'False' | '__debug__')
|
3527
|
-
private _parseAtom(): ExpressionNode {
|
3528
|
-
const nextToken = this._peekToken();
|
3529
|
-
|
3530
|
-
if (nextToken.type === TokenType.Ellipsis) {
|
3531
|
-
return EllipsisNode.create(this._getNextToken());
|
3532
|
-
}
|
3533
|
-
|
3534
|
-
if (nextToken.type === TokenType.Number) {
|
3535
|
-
return NumberNode.create(this._getNextToken() as NumberToken);
|
3536
|
-
}
|
3537
|
-
|
3538
|
-
if (nextToken.type === TokenType.Identifier) {
|
3539
|
-
return NameNode.create(this._getNextToken() as IdentifierToken);
|
3540
|
-
}
|
3541
|
-
|
3542
|
-
if (nextToken.type === TokenType.String) {
|
3543
|
-
return this._parseStringList();
|
3544
|
-
}
|
3545
|
-
|
3546
|
-
if (nextToken.type === TokenType.Backtick) {
|
3547
|
-
this._getNextToken();
|
3548
|
-
|
3549
|
-
// Atoms with backticks are no longer allowed in Python 3.x, but they
|
3550
|
-
// were a thing in Python 2.x. We'll parse them to improve parse recovery
|
3551
|
-
// and emit an error.
|
3552
|
-
this._addError(Localizer.Diagnostic.backticksIllegal(), nextToken);
|
3553
|
-
|
3554
|
-
const expressionNode = this._parseTestListAsExpression(
|
3555
|
-
ErrorExpressionCategory.MissingExpression,
|
3556
|
-
Localizer.Diagnostic.expectedExpr()
|
3557
|
-
);
|
3558
|
-
|
3559
|
-
this._consumeTokenIfType(TokenType.Backtick);
|
3560
|
-
return expressionNode;
|
3561
|
-
}
|
3562
|
-
|
3563
|
-
if (nextToken.type === TokenType.OpenParenthesis) {
|
3564
|
-
const possibleTupleNode = this._parseTupleAtom();
|
3565
|
-
if (
|
3566
|
-
possibleTupleNode.nodeType === ParseNodeType.Tuple &&
|
3567
|
-
this._isParsingTypeAnnotation &&
|
3568
|
-
!this._isParsingIndexTrailer
|
3569
|
-
) {
|
3570
|
-
// This is allowed inside of an index trailer, specifically
|
3571
|
-
// to support Tuple[()], which is the documented way to annotate
|
3572
|
-
// a zero-length tuple.
|
3573
|
-
const diag = new DiagnosticAddendum();
|
3574
|
-
diag.addMessage(Localizer.DiagnosticAddendum.useTupleInstead());
|
3575
|
-
this._addError(Localizer.Diagnostic.tupleInAnnotation() + diag.getString(), possibleTupleNode);
|
3576
|
-
}
|
3577
|
-
|
3578
|
-
if (possibleTupleNode.nodeType === ParseNodeType.BinaryOperation) {
|
3579
|
-
// Mark the binary expression as parenthesized so we don't attempt
|
3580
|
-
// to use comparison chaining, which isn't appropriate when the
|
3581
|
-
// expression is parenthesized.
|
3582
|
-
possibleTupleNode.parenthesized = true;
|
3583
|
-
}
|
3584
|
-
|
3585
|
-
if (possibleTupleNode.nodeType === ParseNodeType.StringList) {
|
3586
|
-
possibleTupleNode.isParenthesized = true;
|
3587
|
-
}
|
3588
|
-
|
3589
|
-
if (possibleTupleNode.nodeType === ParseNodeType.ListComprehension) {
|
3590
|
-
possibleTupleNode.isParenthesized = true;
|
3591
|
-
}
|
3592
|
-
|
3593
|
-
return possibleTupleNode;
|
3594
|
-
} else if (nextToken.type === TokenType.OpenBracket) {
|
3595
|
-
const listNode = this._parseListAtom();
|
3596
|
-
if (this._isParsingTypeAnnotation && !this._isParsingIndexTrailer) {
|
3597
|
-
const diag = new DiagnosticAddendum();
|
3598
|
-
diag.addMessage(Localizer.DiagnosticAddendum.useListInstead());
|
3599
|
-
this._addError(Localizer.Diagnostic.listInAnnotation() + diag.getString(), listNode);
|
3600
|
-
}
|
3601
|
-
return listNode;
|
3602
|
-
} else if (nextToken.type === TokenType.OpenCurlyBrace) {
|
3603
|
-
const dictNode = this._parseDictionaryOrSetAtom();
|
3604
|
-
if (this._isParsingTypeAnnotation) {
|
3605
|
-
const diag = new DiagnosticAddendum();
|
3606
|
-
diag.addMessage(Localizer.DiagnosticAddendum.useDictInstead());
|
3607
|
-
this._addError(Localizer.Diagnostic.dictInAnnotation() + diag.getString(), dictNode);
|
3608
|
-
}
|
3609
|
-
return dictNode;
|
3610
|
-
}
|
3611
|
-
|
3612
|
-
if (nextToken.type === TokenType.Keyword) {
|
3613
|
-
const keywordToken = nextToken as KeywordToken;
|
3614
|
-
if (
|
3615
|
-
keywordToken.keywordType === KeywordType.False ||
|
3616
|
-
keywordToken.keywordType === KeywordType.True ||
|
3617
|
-
keywordToken.keywordType === KeywordType.Debug ||
|
3618
|
-
keywordToken.keywordType === KeywordType.None
|
3619
|
-
) {
|
3620
|
-
return ConstantNode.create(this._getNextToken() as KeywordToken);
|
3621
|
-
}
|
3622
|
-
|
3623
|
-
// Make an identifier out of the keyword.
|
3624
|
-
const keywordAsIdentifier = this._getTokenIfIdentifier();
|
3625
|
-
if (keywordAsIdentifier) {
|
3626
|
-
return NameNode.create(keywordAsIdentifier);
|
3627
|
-
}
|
3628
|
-
}
|
3629
|
-
|
3630
|
-
return this._handleExpressionParseError(
|
3631
|
-
ErrorExpressionCategory.MissingExpression,
|
3632
|
-
Localizer.Diagnostic.expectedExpr()
|
3633
|
-
);
|
3634
|
-
}
|
3635
|
-
|
3636
|
-
// Allocates a dummy "error expression" and consumes the remainder
|
3637
|
-
// of the tokens on the line for error recovery. A partially-completed
|
3638
|
-
// child node can be passed to help the completion provider determine
|
3639
|
-
// what to do.
|
3640
|
-
private _handleExpressionParseError(
|
3641
|
-
category: ErrorExpressionCategory,
|
3642
|
-
errorMsg: string,
|
3643
|
-
targetToken?: Token,
|
3644
|
-
childNode?: ExpressionNode,
|
3645
|
-
additionalStopTokens?: TokenType[]
|
3646
|
-
): ErrorNode {
|
3647
|
-
this._addError(errorMsg, targetToken ?? this._peekToken());
|
3648
|
-
const expr = ErrorNode.create(this._peekToken(), category, childNode);
|
3649
|
-
const stopTokens = [TokenType.NewLine];
|
3650
|
-
if (additionalStopTokens) {
|
3651
|
-
appendArray(stopTokens, additionalStopTokens);
|
3652
|
-
}
|
3653
|
-
this._consumeTokensUntilType(stopTokens);
|
3654
|
-
return expr;
|
3655
|
-
}
|
3656
|
-
|
3657
|
-
// lambdef: 'lambda' [varargslist] ':' test
|
3658
|
-
private _parseLambdaExpression(allowConditional = true): LambdaNode {
|
3659
|
-
const lambdaToken = this._getKeywordToken(KeywordType.Lambda);
|
3660
|
-
|
3661
|
-
const argList = this._parseVarArgsList(TokenType.Colon, /* allowAnnotations */ false);
|
3662
|
-
|
3663
|
-
if (!this._consumeTokenIfType(TokenType.Colon)) {
|
3664
|
-
this._addError(Localizer.Diagnostic.expectedColon(), this._peekToken());
|
3665
|
-
}
|
3666
|
-
|
3667
|
-
let testExpr: ExpressionNode;
|
3668
|
-
if (allowConditional) {
|
3669
|
-
testExpr = this._parseTestExpression(/* allowAssignmentExpression */ false);
|
3670
|
-
} else {
|
3671
|
-
testExpr = this._tryParseLambdaExpression(/* allowConditional */ false) || this._parseOrTest();
|
3672
|
-
}
|
3673
|
-
|
3674
|
-
const lambdaNode = LambdaNode.create(lambdaToken, testExpr);
|
3675
|
-
lambdaNode.parameters = argList;
|
3676
|
-
argList.forEach((arg) => {
|
3677
|
-
arg.parent = lambdaNode;
|
3678
|
-
});
|
3679
|
-
return lambdaNode;
|
3680
|
-
}
|
3681
|
-
|
3682
|
-
private _tryParseLambdaExpression(allowConditional = true): LambdaNode | undefined {
|
3683
|
-
if (this._peekKeywordType() !== KeywordType.Lambda) {
|
3684
|
-
return undefined;
|
3685
|
-
}
|
3686
|
-
|
3687
|
-
return this._parseLambdaExpression(allowConditional);
|
3688
|
-
}
|
3689
|
-
|
3690
|
-
// ('(' [yield_expr | testlist_comp] ')'
|
3691
|
-
// testlist_comp: (test | star_expr) (comp_for | (',' (test | star_expr))* [','])
|
3692
|
-
private _parseTupleAtom(): ExpressionNode {
|
3693
|
-
const startParen = this._getNextToken();
|
3694
|
-
assert(startParen.type === TokenType.OpenParenthesis);
|
3695
|
-
|
3696
|
-
const yieldExpr = this._tryParseYieldExpression();
|
3697
|
-
if (yieldExpr) {
|
3698
|
-
if (this._peekTokenType() !== TokenType.CloseParenthesis) {
|
3699
|
-
return this._handleExpressionParseError(
|
3700
|
-
ErrorExpressionCategory.MissingTupleCloseParen,
|
3701
|
-
Localizer.Diagnostic.expectedCloseParen(),
|
3702
|
-
startParen,
|
3703
|
-
yieldExpr
|
3704
|
-
);
|
3705
|
-
} else {
|
3706
|
-
extendRange(yieldExpr, this._getNextToken());
|
3707
|
-
}
|
3708
|
-
|
3709
|
-
return yieldExpr;
|
3710
|
-
}
|
3711
|
-
|
3712
|
-
const exprListResult = this._parseTestListWithComprehension();
|
3713
|
-
const tupleOrExpression = this._makeExpressionOrTuple(exprListResult, /* enclosedInParens */ true);
|
3714
|
-
const isExpression = exprListResult.list.length === 1 && !exprListResult.trailingComma;
|
3715
|
-
|
3716
|
-
if (!isExpression) {
|
3717
|
-
extendRange(tupleOrExpression, startParen);
|
3718
|
-
}
|
3719
|
-
|
3720
|
-
if (this._peekTokenType() !== TokenType.CloseParenthesis) {
|
3721
|
-
return this._handleExpressionParseError(
|
3722
|
-
ErrorExpressionCategory.MissingTupleCloseParen,
|
3723
|
-
Localizer.Diagnostic.expectedCloseParen(),
|
3724
|
-
startParen,
|
3725
|
-
exprListResult.parseError ?? tupleOrExpression
|
3726
|
-
);
|
3727
|
-
} else {
|
3728
|
-
const nextToken = this._getNextToken();
|
3729
|
-
if (!isExpression) {
|
3730
|
-
extendRange(tupleOrExpression, nextToken);
|
3731
|
-
}
|
3732
|
-
}
|
3733
|
-
|
3734
|
-
return tupleOrExpression;
|
3735
|
-
}
|
3736
|
-
|
3737
|
-
// '[' [testlist_comp] ']'
|
3738
|
-
// testlist_comp: (test | star_expr) (comp_for | (',' (test | star_expr))* [','])
|
3739
|
-
private _parseListAtom(): ListNode | ErrorNode {
|
3740
|
-
const startBracket = this._getNextToken();
|
3741
|
-
assert(startBracket.type === TokenType.OpenBracket);
|
3742
|
-
|
3743
|
-
const exprListResult = this._parseTestListWithComprehension();
|
3744
|
-
const closeBracket: Token | undefined = this._peekToken();
|
3745
|
-
if (!this._consumeTokenIfType(TokenType.CloseBracket)) {
|
3746
|
-
return this._handleExpressionParseError(
|
3747
|
-
ErrorExpressionCategory.MissingListCloseBracket,
|
3748
|
-
Localizer.Diagnostic.expectedCloseBracket(),
|
3749
|
-
startBracket,
|
3750
|
-
exprListResult.parseError ?? _createList()
|
3751
|
-
);
|
3752
|
-
}
|
3753
|
-
|
3754
|
-
return _createList();
|
3755
|
-
|
3756
|
-
function _createList() {
|
3757
|
-
const listAtom = ListNode.create(startBracket);
|
3758
|
-
|
3759
|
-
if (closeBracket) {
|
3760
|
-
extendRange(listAtom, closeBracket);
|
3761
|
-
}
|
3762
|
-
|
3763
|
-
if (exprListResult.list.length > 0) {
|
3764
|
-
exprListResult.list.forEach((expr) => {
|
3765
|
-
expr.parent = listAtom;
|
3766
|
-
});
|
3767
|
-
extendRange(listAtom, exprListResult.list[exprListResult.list.length - 1]);
|
3768
|
-
}
|
3769
|
-
|
3770
|
-
listAtom.entries = exprListResult.list;
|
3771
|
-
return listAtom;
|
3772
|
-
}
|
3773
|
-
}
|
3774
|
-
|
3775
|
-
private _parseTestListWithComprehension(): ListResult<ExpressionNode> {
|
3776
|
-
let sawComprehension = false;
|
3777
|
-
|
3778
|
-
return this._parseExpressionListGeneric(
|
3779
|
-
() => {
|
3780
|
-
let expr = this._parseTestOrStarExpression(/* allowAssignmentExpression */ true);
|
3781
|
-
const listComp = this._tryParseListComprehension(expr);
|
3782
|
-
if (listComp) {
|
3783
|
-
expr = listComp;
|
3784
|
-
sawComprehension = true;
|
3785
|
-
}
|
3786
|
-
return expr;
|
3787
|
-
},
|
3788
|
-
() => this._isNextTokenNeverExpression(),
|
3789
|
-
() => sawComprehension
|
3790
|
-
);
|
3791
|
-
}
|
3792
|
-
|
3793
|
-
// '{' [dictorsetmaker] '}'
|
3794
|
-
// dictorsetmaker: (
|
3795
|
-
// (dictentry (comp_for | (',' dictentry)* [',']))
|
3796
|
-
// | (setentry (comp_for | (',' setentry)* [',']))
|
3797
|
-
// )
|
3798
|
-
// dictentry: (test ':' test | '**' expr)
|
3799
|
-
// setentry: test | star_expr
|
3800
|
-
private _parseDictionaryOrSetAtom(): DictionaryNode | SetNode {
|
3801
|
-
const startBrace = this._getNextToken();
|
3802
|
-
assert(startBrace.type === TokenType.OpenCurlyBrace);
|
3803
|
-
|
3804
|
-
const dictionaryEntries: DictionaryEntryNode[] = [];
|
3805
|
-
const setEntries: ExpressionNode[] = [];
|
3806
|
-
let isDictionary = false;
|
3807
|
-
let isSet = false;
|
3808
|
-
let sawListComprehension = false;
|
3809
|
-
let isFirstEntry = true;
|
3810
|
-
let trailingCommaToken: Token | undefined;
|
3811
|
-
|
3812
|
-
while (true) {
|
3813
|
-
if (this._peekTokenType() === TokenType.CloseCurlyBrace) {
|
3814
|
-
break;
|
3815
|
-
}
|
3816
|
-
|
3817
|
-
trailingCommaToken = undefined;
|
3818
|
-
|
3819
|
-
let doubleStarExpression: ExpressionNode | undefined;
|
3820
|
-
let keyExpression: ExpressionNode | undefined;
|
3821
|
-
let valueExpression: ExpressionNode | undefined;
|
3822
|
-
const doubleStar = this._peekToken();
|
3823
|
-
|
3824
|
-
if (this._consumeTokenIfOperator(OperatorType.Power)) {
|
3825
|
-
doubleStarExpression = this._parseExpression(/* allowUnpack */ false);
|
3826
|
-
} else {
|
3827
|
-
keyExpression = this._parseTestOrStarExpression(/* allowAssignmentExpression */ true);
|
3828
|
-
|
3829
|
-
if (this._consumeTokenIfType(TokenType.Colon)) {
|
3830
|
-
valueExpression = this._parseTestExpression(/* allowAssignmentExpression */ false);
|
3831
|
-
}
|
3832
|
-
}
|
3833
|
-
|
3834
|
-
if (keyExpression && valueExpression) {
|
3835
|
-
if (keyExpression.nodeType === ParseNodeType.Unpack) {
|
3836
|
-
this._addError(Localizer.Diagnostic.unpackInDict(), keyExpression);
|
3837
|
-
}
|
3838
|
-
|
3839
|
-
if (isSet) {
|
3840
|
-
this._addError(Localizer.Diagnostic.keyValueInSet(), valueExpression);
|
3841
|
-
} else {
|
3842
|
-
const keyEntryNode = DictionaryKeyEntryNode.create(keyExpression, valueExpression);
|
3843
|
-
let dictEntry: DictionaryEntryNode = keyEntryNode;
|
3844
|
-
const listComp = this._tryParseListComprehension(keyEntryNode);
|
3845
|
-
if (listComp) {
|
3846
|
-
dictEntry = listComp;
|
3847
|
-
sawListComprehension = true;
|
3848
|
-
|
3849
|
-
if (!isFirstEntry) {
|
3850
|
-
this._addError(Localizer.Diagnostic.comprehensionInDict(), dictEntry);
|
3851
|
-
}
|
3852
|
-
}
|
3853
|
-
dictionaryEntries.push(dictEntry);
|
3854
|
-
isDictionary = true;
|
3855
|
-
}
|
3856
|
-
} else if (doubleStarExpression) {
|
3857
|
-
if (isSet) {
|
3858
|
-
this._addError(Localizer.Diagnostic.unpackInSet(), doubleStarExpression);
|
3859
|
-
} else {
|
3860
|
-
const listEntryNode = DictionaryExpandEntryNode.create(doubleStarExpression);
|
3861
|
-
extendRange(listEntryNode, doubleStar);
|
3862
|
-
let expandEntryNode: DictionaryEntryNode = listEntryNode;
|
3863
|
-
const listComp = this._tryParseListComprehension(listEntryNode);
|
3864
|
-
if (listComp) {
|
3865
|
-
expandEntryNode = listComp;
|
3866
|
-
sawListComprehension = true;
|
3867
|
-
|
3868
|
-
if (!isFirstEntry) {
|
3869
|
-
this._addError(Localizer.Diagnostic.comprehensionInDict(), doubleStarExpression);
|
3870
|
-
}
|
3871
|
-
}
|
3872
|
-
dictionaryEntries.push(expandEntryNode);
|
3873
|
-
isDictionary = true;
|
3874
|
-
}
|
3875
|
-
} else {
|
3876
|
-
assert(keyExpression !== undefined);
|
3877
|
-
if (keyExpression) {
|
3878
|
-
if (isDictionary) {
|
3879
|
-
const missingValueErrorNode = ErrorNode.create(
|
3880
|
-
this._peekToken(),
|
3881
|
-
ErrorExpressionCategory.MissingDictValue
|
3882
|
-
);
|
3883
|
-
const keyEntryNode = DictionaryKeyEntryNode.create(keyExpression, missingValueErrorNode);
|
3884
|
-
dictionaryEntries.push(keyEntryNode);
|
3885
|
-
this._addError(Localizer.Diagnostic.dictKeyValuePairs(), keyExpression);
|
3886
|
-
} else {
|
3887
|
-
const listComp = this._tryParseListComprehension(keyExpression);
|
3888
|
-
if (listComp) {
|
3889
|
-
keyExpression = listComp;
|
3890
|
-
sawListComprehension = true;
|
3891
|
-
|
3892
|
-
if (!isFirstEntry) {
|
3893
|
-
this._addError(Localizer.Diagnostic.comprehensionInSet(), keyExpression);
|
3894
|
-
}
|
3895
|
-
}
|
3896
|
-
setEntries.push(keyExpression);
|
3897
|
-
isSet = true;
|
3898
|
-
}
|
3899
|
-
}
|
3900
|
-
}
|
3901
|
-
|
3902
|
-
// List comprehension statements always end the list.
|
3903
|
-
if (sawListComprehension) {
|
3904
|
-
break;
|
3905
|
-
}
|
3906
|
-
|
3907
|
-
if (this._peekTokenType() !== TokenType.Comma) {
|
3908
|
-
break;
|
3909
|
-
}
|
3910
|
-
|
3911
|
-
trailingCommaToken = this._getNextToken();
|
3912
|
-
|
3913
|
-
isFirstEntry = false;
|
3914
|
-
}
|
3915
|
-
|
3916
|
-
let closeCurlyBrace: Token | undefined = this._peekToken();
|
3917
|
-
if (!this._consumeTokenIfType(TokenType.CloseCurlyBrace)) {
|
3918
|
-
this._addError(Localizer.Diagnostic.expectedCloseBrace(), startBrace);
|
3919
|
-
closeCurlyBrace = undefined;
|
3920
|
-
}
|
3921
|
-
|
3922
|
-
if (isSet) {
|
3923
|
-
const setAtom = SetNode.create(startBrace);
|
3924
|
-
if (closeCurlyBrace) {
|
3925
|
-
extendRange(setAtom, closeCurlyBrace);
|
3926
|
-
}
|
3927
|
-
|
3928
|
-
if (setEntries.length > 0) {
|
3929
|
-
extendRange(setAtom, setEntries[setEntries.length - 1]);
|
3930
|
-
}
|
3931
|
-
|
3932
|
-
setEntries.forEach((entry) => {
|
3933
|
-
entry.parent = setAtom;
|
3934
|
-
});
|
3935
|
-
|
3936
|
-
setAtom.entries = setEntries;
|
3937
|
-
return setAtom;
|
3938
|
-
}
|
3939
|
-
|
3940
|
-
const dictionaryAtom = DictionaryNode.create(startBrace);
|
3941
|
-
|
3942
|
-
if (trailingCommaToken) {
|
3943
|
-
dictionaryAtom.trailingCommaToken = trailingCommaToken;
|
3944
|
-
extendRange(dictionaryAtom, trailingCommaToken);
|
3945
|
-
}
|
3946
|
-
|
3947
|
-
if (closeCurlyBrace) {
|
3948
|
-
extendRange(dictionaryAtom, closeCurlyBrace);
|
3949
|
-
}
|
3950
|
-
|
3951
|
-
if (dictionaryEntries.length > 0) {
|
3952
|
-
dictionaryEntries.forEach((entry) => {
|
3953
|
-
entry.parent = dictionaryAtom;
|
3954
|
-
});
|
3955
|
-
extendRange(dictionaryAtom, dictionaryEntries[dictionaryEntries.length - 1]);
|
3956
|
-
}
|
3957
|
-
dictionaryAtom.entries = dictionaryEntries;
|
3958
|
-
return dictionaryAtom;
|
3959
|
-
}
|
3960
|
-
|
3961
|
-
private _parseExpressionListGeneric<T extends ParseNode = ExpressionNode>(
|
3962
|
-
parser: () => T | ErrorNode,
|
3963
|
-
terminalCheck: () => boolean = () => this._isNextTokenNeverExpression(),
|
3964
|
-
finalEntryCheck: () => boolean = () => false
|
3965
|
-
): ListResult<T> {
|
3966
|
-
let trailingComma = false;
|
3967
|
-
const list: T[] = [];
|
3968
|
-
let parseError: ErrorNode | undefined;
|
3969
|
-
|
3970
|
-
while (true) {
|
3971
|
-
if (terminalCheck()) {
|
3972
|
-
break;
|
3973
|
-
}
|
3974
|
-
|
3975
|
-
const expr = parser();
|
3976
|
-
if (expr.nodeType === ParseNodeType.Error) {
|
3977
|
-
parseError = expr as ErrorNode;
|
3978
|
-
break;
|
3979
|
-
}
|
3980
|
-
list.push(expr);
|
3981
|
-
|
3982
|
-
// Should we stop without checking for a trailing comma?
|
3983
|
-
if (finalEntryCheck()) {
|
3984
|
-
break;
|
3985
|
-
}
|
3986
|
-
|
3987
|
-
if (!this._consumeTokenIfType(TokenType.Comma)) {
|
3988
|
-
trailingComma = false;
|
3989
|
-
break;
|
3990
|
-
}
|
3991
|
-
|
3992
|
-
trailingComma = true;
|
3993
|
-
}
|
3994
|
-
|
3995
|
-
return { trailingComma, list, parseError };
|
3996
|
-
}
|
3997
|
-
|
3998
|
-
// expr_stmt: testlist_star_expr (annassign | augassign (yield_expr | testlist) |
|
3999
|
-
// ('=' (yield_expr | testlist_star_expr))*)
|
4000
|
-
// testlist_star_expr: (test|star_expr) (',' (test|star_expr))* [',']
|
4001
|
-
// annassign: ':' test ['=' (yield_expr | testlist_star_expr)]
|
4002
|
-
// augassign: ('+=' | '-=' | '*=' | '@=' | '/=' | '%=' | '&=' | '|=' | '^=' |
|
4003
|
-
// '<<=' | '>>=' | '**=' | '//=')
|
4004
|
-
private _parseExpressionStatement(): ExpressionNode {
|
4005
|
-
let leftExpr = this._parseTestOrStarListAsExpression(
|
4006
|
-
/* allowAssignmentExpression */ false,
|
4007
|
-
/* allowMultipleUnpack */ false,
|
4008
|
-
ErrorExpressionCategory.MissingExpression,
|
4009
|
-
Localizer.Diagnostic.expectedExpr()
|
4010
|
-
);
|
4011
|
-
let annotationExpr: ExpressionNode | undefined;
|
4012
|
-
|
4013
|
-
if (leftExpr.nodeType === ParseNodeType.Error) {
|
4014
|
-
return leftExpr;
|
4015
|
-
}
|
4016
|
-
|
4017
|
-
// Is this a type annotation assignment?
|
4018
|
-
if (this._consumeTokenIfType(TokenType.Colon)) {
|
4019
|
-
annotationExpr = this._parseTypeAnnotation();
|
4020
|
-
leftExpr = TypeAnnotationNode.create(leftExpr, annotationExpr);
|
4021
|
-
|
4022
|
-
if (!this._parseOptions.isStubFile && this._getLanguageVersion() < PythonVersion.V3_6) {
|
4023
|
-
this._addError(Localizer.Diagnostic.varAnnotationIllegal(), annotationExpr);
|
4024
|
-
}
|
4025
|
-
|
4026
|
-
if (!this._consumeTokenIfOperator(OperatorType.Assign)) {
|
4027
|
-
return leftExpr;
|
4028
|
-
}
|
4029
|
-
|
4030
|
-
// This is an unfortunate hack that's necessary to accommodate 'TypeAlias'
|
4031
|
-
// declarations properly. We need to treat this assignment differently than
|
4032
|
-
// most because the expression on the right side is treated like a type
|
4033
|
-
// annotation and therefore allows string-literal forward declarations.
|
4034
|
-
const isTypeAliasDeclaration = this._isTypingAnnotation(annotationExpr, 'TypeAlias');
|
4035
|
-
|
4036
|
-
const wasParsingTypeAnnotation = this._isParsingTypeAnnotation;
|
4037
|
-
if (isTypeAliasDeclaration) {
|
4038
|
-
this._isParsingTypeAnnotation = true;
|
4039
|
-
}
|
4040
|
-
|
4041
|
-
const rightExpr =
|
4042
|
-
this._tryParseYieldExpression() ||
|
4043
|
-
this._parseTestOrStarListAsExpression(
|
4044
|
-
/* allowAssignmentExpression */ false,
|
4045
|
-
/* allowMultipleUnpack */ true,
|
4046
|
-
ErrorExpressionCategory.MissingExpression,
|
4047
|
-
Localizer.Diagnostic.expectedAssignRightHandExpr()
|
4048
|
-
);
|
4049
|
-
|
4050
|
-
this._isParsingTypeAnnotation = wasParsingTypeAnnotation;
|
4051
|
-
|
4052
|
-
return AssignmentNode.create(leftExpr, rightExpr);
|
4053
|
-
}
|
4054
|
-
|
4055
|
-
// Is this a simple assignment?
|
4056
|
-
if (this._consumeTokenIfOperator(OperatorType.Assign)) {
|
4057
|
-
return this._parseChainAssignments(leftExpr);
|
4058
|
-
}
|
4059
|
-
|
4060
|
-
if (Tokenizer.isOperatorAssignment(this._peekOperatorType())) {
|
4061
|
-
const operatorToken = this._getNextToken() as OperatorToken;
|
4062
|
-
|
4063
|
-
const rightExpr =
|
4064
|
-
this._tryParseYieldExpression() ||
|
4065
|
-
this._parseTestListAsExpression(
|
4066
|
-
ErrorExpressionCategory.MissingExpression,
|
4067
|
-
Localizer.Diagnostic.expectedBinaryRightHandExpr()
|
4068
|
-
);
|
4069
|
-
|
4070
|
-
// Make a shallow copy of the dest expression but give it a new ID.
|
4071
|
-
const destExpr = Object.assign({}, leftExpr);
|
4072
|
-
destExpr.id = getNextNodeId();
|
4073
|
-
|
4074
|
-
return AugmentedAssignmentNode.create(leftExpr, rightExpr, operatorToken.operatorType, destExpr);
|
4075
|
-
}
|
4076
|
-
|
4077
|
-
return leftExpr;
|
4078
|
-
}
|
4079
|
-
|
4080
|
-
private _parseChainAssignments(leftExpr: ExpressionNode): ExpressionNode {
|
4081
|
-
let rightExpr =
|
4082
|
-
this._tryParseYieldExpression() ||
|
4083
|
-
this._parseTestOrStarListAsExpression(
|
4084
|
-
/* allowAssignmentExpression */ false,
|
4085
|
-
/* allowMultipleUnpack */ true,
|
4086
|
-
ErrorExpressionCategory.MissingExpression,
|
4087
|
-
Localizer.Diagnostic.expectedAssignRightHandExpr()
|
4088
|
-
);
|
4089
|
-
|
4090
|
-
if (rightExpr.nodeType === ParseNodeType.Error) {
|
4091
|
-
return AssignmentNode.create(leftExpr, rightExpr);
|
4092
|
-
}
|
4093
|
-
|
4094
|
-
// Recur until we've consumed the entire chain.
|
4095
|
-
if (this._consumeTokenIfOperator(OperatorType.Assign)) {
|
4096
|
-
rightExpr = this._parseChainAssignments(rightExpr);
|
4097
|
-
if (rightExpr.nodeType === ParseNodeType.Error) {
|
4098
|
-
return rightExpr;
|
4099
|
-
}
|
4100
|
-
}
|
4101
|
-
|
4102
|
-
const assignmentNode = AssignmentNode.create(leftExpr, rightExpr);
|
4103
|
-
|
4104
|
-
// Look for a type annotation comment at the end of the line.
|
4105
|
-
const typeAnnotationComment = this._parseVariableTypeAnnotationComment();
|
4106
|
-
if (typeAnnotationComment) {
|
4107
|
-
assignmentNode.typeAnnotationComment = typeAnnotationComment;
|
4108
|
-
assignmentNode.typeAnnotationComment.parent = assignmentNode;
|
4109
|
-
extendRange(assignmentNode, assignmentNode.typeAnnotationComment);
|
4110
|
-
}
|
4111
|
-
|
4112
|
-
return assignmentNode;
|
4113
|
-
}
|
4114
|
-
|
4115
|
-
private _parseFunctionTypeAnnotation(): FunctionAnnotationNode | undefined {
|
4116
|
-
const openParenToken = this._peekToken();
|
4117
|
-
if (!this._consumeTokenIfType(TokenType.OpenParenthesis)) {
|
4118
|
-
this._addError(Localizer.Diagnostic.expectedOpenParen(), this._peekToken());
|
4119
|
-
return undefined;
|
4120
|
-
}
|
4121
|
-
|
4122
|
-
let paramAnnotations: ExpressionNode[] = [];
|
4123
|
-
|
4124
|
-
while (true) {
|
4125
|
-
const nextTokenType = this._peekTokenType();
|
4126
|
-
if (
|
4127
|
-
nextTokenType === TokenType.CloseParenthesis ||
|
4128
|
-
nextTokenType === TokenType.NewLine ||
|
4129
|
-
nextTokenType === TokenType.EndOfStream
|
4130
|
-
) {
|
4131
|
-
break;
|
4132
|
-
}
|
4133
|
-
|
4134
|
-
// Consume "*" or "**" indicators but don't do anything with them.
|
4135
|
-
// (We don't enforce that these are present, absent, or match
|
4136
|
-
// the corresponding parameter types.)
|
4137
|
-
this._consumeTokenIfOperator(OperatorType.Multiply) || this._consumeTokenIfOperator(OperatorType.Power);
|
4138
|
-
|
4139
|
-
const paramAnnotation = this._parseTypeAnnotation();
|
4140
|
-
paramAnnotations.push(paramAnnotation);
|
4141
|
-
|
4142
|
-
if (!this._consumeTokenIfType(TokenType.Comma)) {
|
4143
|
-
break;
|
4144
|
-
}
|
4145
|
-
}
|
4146
|
-
|
4147
|
-
if (!this._consumeTokenIfType(TokenType.CloseParenthesis)) {
|
4148
|
-
this._addError(Localizer.Diagnostic.expectedCloseParen(), openParenToken);
|
4149
|
-
this._consumeTokensUntilType([TokenType.Colon]);
|
4150
|
-
}
|
4151
|
-
|
4152
|
-
if (!this._consumeTokenIfType(TokenType.Arrow)) {
|
4153
|
-
this._addError(Localizer.Diagnostic.expectedArrow(), this._peekToken());
|
4154
|
-
return undefined;
|
4155
|
-
}
|
4156
|
-
|
4157
|
-
const returnType = this._parseTypeAnnotation();
|
4158
|
-
|
4159
|
-
let isParamListEllipsis = false;
|
4160
|
-
if (paramAnnotations.length === 1 && paramAnnotations[0].nodeType === ParseNodeType.Ellipsis) {
|
4161
|
-
paramAnnotations = [];
|
4162
|
-
isParamListEllipsis = true;
|
4163
|
-
}
|
4164
|
-
|
4165
|
-
return FunctionAnnotationNode.create(openParenToken, isParamListEllipsis, paramAnnotations, returnType);
|
4166
|
-
}
|
4167
|
-
|
4168
|
-
private _parseTypeAnnotation(allowUnpack = false): ExpressionNode {
|
4169
|
-
// Temporary set a flag that indicates we're parsing a type annotation.
|
4170
|
-
const wasParsingTypeAnnotation = this._isParsingTypeAnnotation;
|
4171
|
-
this._isParsingTypeAnnotation = true;
|
4172
|
-
|
4173
|
-
// Allow unpack operators.
|
4174
|
-
const startToken = this._peekToken();
|
4175
|
-
const isUnpack = this._consumeTokenIfOperator(OperatorType.Multiply);
|
4176
|
-
|
4177
|
-
if (isUnpack) {
|
4178
|
-
if (!allowUnpack) {
|
4179
|
-
this._addError(Localizer.Diagnostic.unpackInAnnotation(), startToken);
|
4180
|
-
} else if (!this._parseOptions.isStubFile && this._getLanguageVersion() < PythonVersion.V3_11) {
|
4181
|
-
this._addError(Localizer.Diagnostic.unpackedSubscriptIllegal(), startToken);
|
4182
|
-
}
|
4183
|
-
}
|
4184
|
-
|
4185
|
-
let result = this._parseTestExpression(/* allowAssignmentExpression */ false);
|
4186
|
-
if (isUnpack && allowUnpack) {
|
4187
|
-
result = UnpackNode.create(startToken, result);
|
4188
|
-
}
|
4189
|
-
|
4190
|
-
this._isParsingTypeAnnotation = wasParsingTypeAnnotation;
|
4191
|
-
|
4192
|
-
return result;
|
4193
|
-
}
|
4194
|
-
|
4195
|
-
private _reportStringTokenErrors(stringToken: StringToken, unescapedResult: StringTokenUtils.UnescapedString) {
|
4196
|
-
if (stringToken.flags & StringTokenFlags.Unterminated) {
|
4197
|
-
this._addError(Localizer.Diagnostic.stringUnterminated(), stringToken);
|
4198
|
-
}
|
4199
|
-
|
4200
|
-
if (unescapedResult.nonAsciiInBytes) {
|
4201
|
-
this._addError(Localizer.Diagnostic.stringNonAsciiBytes(), stringToken);
|
4202
|
-
}
|
4203
|
-
|
4204
|
-
if (stringToken.flags & StringTokenFlags.Format) {
|
4205
|
-
if (this._getLanguageVersion() < PythonVersion.V3_6) {
|
4206
|
-
this._addError(Localizer.Diagnostic.formatStringIllegal(), stringToken);
|
4207
|
-
}
|
4208
|
-
|
4209
|
-
if (stringToken.flags & StringTokenFlags.Bytes) {
|
4210
|
-
this._addError(Localizer.Diagnostic.formatStringBytes(), stringToken);
|
4211
|
-
}
|
4212
|
-
|
4213
|
-
if (stringToken.flags & StringTokenFlags.Unicode) {
|
4214
|
-
this._addError(Localizer.Diagnostic.formatStringUnicode(), stringToken);
|
4215
|
-
}
|
4216
|
-
}
|
4217
|
-
}
|
4218
|
-
|
4219
|
-
private _makeStringNode(stringToken: StringToken): StringNode {
|
4220
|
-
const unescapedResult = StringTokenUtils.getUnescapedString(stringToken);
|
4221
|
-
this._reportStringTokenErrors(stringToken, unescapedResult);
|
4222
|
-
return StringNode.create(stringToken, unescapedResult.value, unescapedResult.unescapeErrors.length > 0);
|
4223
|
-
}
|
4224
|
-
|
4225
|
-
private _getTypeAnnotationCommentText(): StringToken | undefined {
|
4226
|
-
if (this._tokenIndex === 0) {
|
4227
|
-
return undefined;
|
4228
|
-
}
|
4229
|
-
|
4230
|
-
const curToken = this._tokenizerOutput!.tokens.getItemAt(this._tokenIndex - 1);
|
4231
|
-
const nextToken = this._tokenizerOutput!.tokens.getItemAt(this._tokenIndex);
|
4232
|
-
|
4233
|
-
if (curToken.start + curToken.length === nextToken.start) {
|
4234
|
-
return undefined;
|
4235
|
-
}
|
4236
|
-
|
4237
|
-
const interTokenContents = this._fileContents!.substring(curToken.start + curToken.length, nextToken.start);
|
4238
|
-
const commentRegEx = /^(\s*#\s*type:\s*)([^\r\n]*)/;
|
4239
|
-
const match = interTokenContents.match(commentRegEx);
|
4240
|
-
if (!match) {
|
4241
|
-
return undefined;
|
4242
|
-
}
|
4243
|
-
|
4244
|
-
// Synthesize a string token and StringNode.
|
4245
|
-
const typeString = match[2];
|
4246
|
-
|
4247
|
-
// Ignore all "ignore" comments. Include "[" in the regular
|
4248
|
-
// expression because mypy supports ignore comments of the
|
4249
|
-
// form ignore[errorCode, ...]. We'll treat these as regular
|
4250
|
-
// ignore statements (as though no errorCodes were included).
|
4251
|
-
if (typeString.trim().match(/^ignore(\s|\[|$)/)) {
|
4252
|
-
return undefined;
|
4253
|
-
}
|
4254
|
-
|
4255
|
-
const tokenOffset = curToken.start + curToken.length + match[1].length;
|
4256
|
-
return StringToken.create(
|
4257
|
-
tokenOffset,
|
4258
|
-
typeString.length,
|
4259
|
-
StringTokenFlags.None,
|
4260
|
-
typeString,
|
4261
|
-
0,
|
4262
|
-
/* comments */ undefined
|
4263
|
-
);
|
4264
|
-
}
|
4265
|
-
|
4266
|
-
private _detectUnsupportedTypeAnnotationComment(): void {
|
4267
|
-
const stringToken = this._getTypeAnnotationCommentText();
|
4268
|
-
if (!stringToken) {
|
4269
|
-
return undefined;
|
4270
|
-
}
|
4271
|
-
|
4272
|
-
this._addError(Localizer.Diagnostic.annotationNotSupported(), stringToken);
|
4273
|
-
}
|
4274
|
-
|
4275
|
-
private _parseVariableTypeAnnotationComment(): ExpressionNode | undefined {
|
4276
|
-
const stringToken = this._getTypeAnnotationCommentText();
|
4277
|
-
if (!stringToken) {
|
4278
|
-
return undefined;
|
4279
|
-
}
|
4280
|
-
|
4281
|
-
const stringNode = this._makeStringNode(stringToken);
|
4282
|
-
const stringListNode = StringListNode.create([stringNode]);
|
4283
|
-
const parser = new Parser();
|
4284
|
-
const parseResults = parser.parseTextExpression(
|
4285
|
-
this._fileContents!,
|
4286
|
-
stringToken.start,
|
4287
|
-
stringToken.length,
|
4288
|
-
this._parseOptions,
|
4289
|
-
ParseTextMode.VariableAnnotation,
|
4290
|
-
/* initialParenDepth */ undefined,
|
4291
|
-
this._typingSymbolAliases
|
4292
|
-
);
|
4293
|
-
|
4294
|
-
parseResults.diagnostics.forEach((diag) => {
|
4295
|
-
this._addError(diag.message, stringListNode);
|
4296
|
-
});
|
4297
|
-
|
4298
|
-
if (!parseResults.parseTree) {
|
4299
|
-
return undefined;
|
4300
|
-
}
|
4301
|
-
|
4302
|
-
assert(parseResults.parseTree.nodeType !== ParseNodeType.FunctionAnnotation);
|
4303
|
-
return parseResults.parseTree;
|
4304
|
-
}
|
4305
|
-
|
4306
|
-
private _parseFunctionTypeAnnotationComment(stringToken: StringToken, functionNode: FunctionNode): void {
|
4307
|
-
const stringNode = this._makeStringNode(stringToken);
|
4308
|
-
const stringListNode = StringListNode.create([stringNode]);
|
4309
|
-
const parser = new Parser();
|
4310
|
-
const parseResults = parser.parseTextExpression(
|
4311
|
-
this._fileContents!,
|
4312
|
-
stringToken.start,
|
4313
|
-
stringToken.length,
|
4314
|
-
this._parseOptions,
|
4315
|
-
ParseTextMode.FunctionAnnotation,
|
4316
|
-
/* initialParenDepth */ undefined,
|
4317
|
-
this._typingSymbolAliases
|
4318
|
-
);
|
4319
|
-
|
4320
|
-
parseResults.diagnostics.forEach((diag) => {
|
4321
|
-
this._addError(diag.message, stringListNode);
|
4322
|
-
});
|
4323
|
-
|
4324
|
-
if (!parseResults.parseTree || parseResults.parseTree.nodeType !== ParseNodeType.FunctionAnnotation) {
|
4325
|
-
return;
|
4326
|
-
}
|
4327
|
-
|
4328
|
-
const functionAnnotation = parseResults.parseTree;
|
4329
|
-
|
4330
|
-
functionNode.functionAnnotationComment = functionAnnotation;
|
4331
|
-
functionAnnotation.parent = functionNode;
|
4332
|
-
extendRange(functionNode, functionAnnotation);
|
4333
|
-
}
|
4334
|
-
|
4335
|
-
private _parseFormatStringSegment(
|
4336
|
-
stringToken: StringToken,
|
4337
|
-
segment: StringTokenUtils.FormatStringSegment,
|
4338
|
-
segmentOffset: number,
|
4339
|
-
segmentLength: number
|
4340
|
-
) {
|
4341
|
-
assert(segment.isExpression);
|
4342
|
-
const parser = new Parser();
|
4343
|
-
const parseResults = parser.parseTextExpression(
|
4344
|
-
this._fileContents!,
|
4345
|
-
stringToken.start + stringToken.prefixLength + stringToken.quoteMarkLength + segment.offset + segmentOffset,
|
4346
|
-
segmentLength,
|
4347
|
-
this._parseOptions,
|
4348
|
-
ParseTextMode.Expression,
|
4349
|
-
/* initialParenDepth */ 1,
|
4350
|
-
this._typingSymbolAliases
|
4351
|
-
);
|
4352
|
-
|
4353
|
-
parseResults.diagnostics.forEach((diag) => {
|
4354
|
-
const textRangeStart =
|
4355
|
-
(diag.range ? convertPositionToOffset(diag.range.start, parseResults.lines) : stringToken.start) ||
|
4356
|
-
stringToken.start;
|
4357
|
-
const textRangeEnd =
|
4358
|
-
(diag.range
|
4359
|
-
? (convertPositionToOffset(diag.range.end, parseResults.lines) || 0) + 1
|
4360
|
-
: stringToken.start + stringToken.length) || stringToken.start + stringToken.length;
|
4361
|
-
const textRange = { start: textRangeStart, length: textRangeEnd - textRangeStart };
|
4362
|
-
this._addError(diag.message, textRange);
|
4363
|
-
});
|
4364
|
-
|
4365
|
-
return parseResults.parseTree;
|
4366
|
-
}
|
4367
|
-
|
4368
|
-
private _parseFormatString(stringToken: StringToken): FormatStringNode {
|
4369
|
-
const unescapedResult = StringTokenUtils.getUnescapedString(stringToken);
|
4370
|
-
this._reportStringTokenErrors(stringToken, unescapedResult);
|
4371
|
-
|
4372
|
-
const formatExpressions: ExpressionNode[] = [];
|
4373
|
-
|
4374
|
-
for (const segment of unescapedResult.formatStringSegments) {
|
4375
|
-
if (segment.isExpression) {
|
4376
|
-
// Determine if we need to truncate the expression because it
|
4377
|
-
// contains formatting directives that start with a ! or :.
|
4378
|
-
const segmentExprLength = this._getFormatStringExpressionLength(segment.value.trimEnd());
|
4379
|
-
const parseTree = this._parseFormatStringSegment(stringToken, segment, 0, segmentExprLength);
|
4380
|
-
if (parseTree) {
|
4381
|
-
assert(parseTree.nodeType !== ParseNodeType.FunctionAnnotation);
|
4382
|
-
formatExpressions.push(parseTree);
|
4383
|
-
}
|
4384
|
-
|
4385
|
-
// Look for additional expressions within the format directive.
|
4386
|
-
const formatDirective = segment.value.substr(segmentExprLength);
|
4387
|
-
let braceDepth = 0;
|
4388
|
-
let startOfExprOffset = 0;
|
4389
|
-
for (let i = 0; i < formatDirective.length; i++) {
|
4390
|
-
if (formatDirective.charCodeAt(i) === Char.OpenBrace) {
|
4391
|
-
if (braceDepth === 0) {
|
4392
|
-
startOfExprOffset = i + 1;
|
4393
|
-
}
|
4394
|
-
braceDepth++;
|
4395
|
-
} else if (formatDirective.charCodeAt(i) === Char.CloseBrace) {
|
4396
|
-
if (braceDepth > 0) {
|
4397
|
-
braceDepth--;
|
4398
|
-
if (braceDepth === 0) {
|
4399
|
-
const formatSegmentLength = this._getFormatStringExpressionLength(
|
4400
|
-
segment.value.substr(segmentExprLength + startOfExprOffset, i - startOfExprOffset)
|
4401
|
-
);
|
4402
|
-
const parseTree = this._parseFormatStringSegment(
|
4403
|
-
stringToken,
|
4404
|
-
segment,
|
4405
|
-
segmentExprLength + startOfExprOffset,
|
4406
|
-
formatSegmentLength
|
4407
|
-
);
|
4408
|
-
if (parseTree) {
|
4409
|
-
assert(parseTree.nodeType !== ParseNodeType.FunctionAnnotation);
|
4410
|
-
formatExpressions.push(parseTree);
|
4411
|
-
}
|
4412
|
-
}
|
4413
|
-
}
|
4414
|
-
}
|
4415
|
-
}
|
4416
|
-
}
|
4417
|
-
}
|
4418
|
-
|
4419
|
-
return FormatStringNode.create(
|
4420
|
-
stringToken,
|
4421
|
-
unescapedResult.value,
|
4422
|
-
unescapedResult.unescapeErrors.length > 0,
|
4423
|
-
formatExpressions
|
4424
|
-
);
|
4425
|
-
}
|
4426
|
-
|
4427
|
-
private _getFormatStringExpressionLength(segmentValue: string): number {
|
4428
|
-
let segmentExprLength = 0;
|
4429
|
-
|
4430
|
-
// PEP 498 says: Expressions cannot contain ':' or '!' outside of
|
4431
|
-
// strings or parentheses, brackets, or braces. The exception is
|
4432
|
-
// that the '!=' operator is allowed as a special case.
|
4433
|
-
const quoteStack: string[] = [];
|
4434
|
-
let braceCount = 0;
|
4435
|
-
let parenCount = 0;
|
4436
|
-
let bracketCount = 0;
|
4437
|
-
let indexOfDebugEqual: number | undefined;
|
4438
|
-
|
4439
|
-
while (segmentExprLength < segmentValue.length) {
|
4440
|
-
const curChar = segmentValue[segmentExprLength];
|
4441
|
-
const ignoreSeparator = quoteStack.length > 0 || braceCount > 0 || parenCount > 0 || bracketCount > 0;
|
4442
|
-
const inString = quoteStack.length > 0;
|
4443
|
-
|
4444
|
-
if (curChar === '=') {
|
4445
|
-
indexOfDebugEqual = segmentExprLength;
|
4446
|
-
} else {
|
4447
|
-
if (curChar === ':') {
|
4448
|
-
if (!ignoreSeparator) {
|
4449
|
-
break;
|
4450
|
-
}
|
4451
|
-
} else if (curChar === '!') {
|
4452
|
-
if (!ignoreSeparator) {
|
4453
|
-
// Allow !=, as per PEP 498
|
4454
|
-
if (
|
4455
|
-
segmentExprLength === segmentValue.length - 1 ||
|
4456
|
-
segmentValue[segmentExprLength + 1] !== '='
|
4457
|
-
) {
|
4458
|
-
break;
|
4459
|
-
}
|
4460
|
-
}
|
4461
|
-
} else if (curChar === "'" || curChar === '"') {
|
4462
|
-
let quoteSequence = curChar;
|
4463
|
-
if (
|
4464
|
-
segmentExprLength + 2 < segmentValue.length &&
|
4465
|
-
segmentValue[segmentExprLength + 1] === curChar &&
|
4466
|
-
segmentValue[segmentExprLength + 2] === curChar
|
4467
|
-
) {
|
4468
|
-
quoteSequence = curChar + curChar + curChar;
|
4469
|
-
segmentExprLength += 2;
|
4470
|
-
}
|
4471
|
-
|
4472
|
-
if (quoteStack.length > 0 && quoteStack[quoteStack.length - 1] === quoteSequence) {
|
4473
|
-
quoteStack.pop();
|
4474
|
-
} else if (quoteStack.length === 0) {
|
4475
|
-
quoteStack.push(quoteSequence);
|
4476
|
-
}
|
4477
|
-
} else if (curChar === '(') {
|
4478
|
-
if (!inString) {
|
4479
|
-
parenCount++;
|
4480
|
-
}
|
4481
|
-
} else if (curChar === ')') {
|
4482
|
-
if (!inString && parenCount > 0) {
|
4483
|
-
parenCount--;
|
4484
|
-
}
|
4485
|
-
} else if (curChar === '{') {
|
4486
|
-
if (!inString) {
|
4487
|
-
braceCount++;
|
4488
|
-
}
|
4489
|
-
} else if (curChar === '}') {
|
4490
|
-
if (!inString && braceCount > 0) {
|
4491
|
-
braceCount--;
|
4492
|
-
}
|
4493
|
-
} else if (curChar === '[') {
|
4494
|
-
if (!inString) {
|
4495
|
-
bracketCount++;
|
4496
|
-
}
|
4497
|
-
} else if (curChar === ']') {
|
4498
|
-
if (!inString && bracketCount > 0) {
|
4499
|
-
bracketCount--;
|
4500
|
-
}
|
4501
|
-
}
|
4502
|
-
|
4503
|
-
if (curChar !== ' ') {
|
4504
|
-
indexOfDebugEqual = undefined;
|
4505
|
-
}
|
4506
|
-
}
|
4507
|
-
|
4508
|
-
segmentExprLength++;
|
4509
|
-
}
|
4510
|
-
|
4511
|
-
// Handle Python 3.8 f-string formatting expressions that
|
4512
|
-
// end in an "=".
|
4513
|
-
if (this._parseOptions.pythonVersion >= PythonVersion.V3_8 && indexOfDebugEqual !== undefined) {
|
4514
|
-
segmentExprLength = indexOfDebugEqual;
|
4515
|
-
}
|
4516
|
-
|
4517
|
-
return segmentExprLength;
|
4518
|
-
}
|
4519
|
-
|
4520
|
-
private _createBinaryOperationNode(
|
4521
|
-
leftExpression: ExpressionNode,
|
4522
|
-
rightExpression: ExpressionNode,
|
4523
|
-
operatorToken: Token,
|
4524
|
-
operator: OperatorType
|
4525
|
-
) {
|
4526
|
-
// Determine if we're exceeding the max parse depth. If so, replace
|
4527
|
-
// the subnode with an error node. Otherwise we risk crashing in the binder
|
4528
|
-
// or type evaluator.
|
4529
|
-
if (leftExpression.maxChildDepth !== undefined && leftExpression.maxChildDepth >= maxChildNodeDepth) {
|
4530
|
-
leftExpression = ErrorNode.create(leftExpression, ErrorExpressionCategory.MaxDepthExceeded);
|
4531
|
-
this._addError(Localizer.Diagnostic.maxParseDepthExceeded(), leftExpression);
|
4532
|
-
}
|
4533
|
-
|
4534
|
-
if (rightExpression.maxChildDepth !== undefined && rightExpression.maxChildDepth >= maxChildNodeDepth) {
|
4535
|
-
rightExpression = ErrorNode.create(rightExpression, ErrorExpressionCategory.MaxDepthExceeded);
|
4536
|
-
this._addError(Localizer.Diagnostic.maxParseDepthExceeded(), rightExpression);
|
4537
|
-
}
|
4538
|
-
|
4539
|
-
return BinaryOperationNode.create(leftExpression, rightExpression, operatorToken, operator);
|
4540
|
-
}
|
4541
|
-
|
4542
|
-
private _createUnaryOperationNode(operatorToken: Token, expression: ExpressionNode, operator: OperatorType) {
|
4543
|
-
// Determine if we're exceeding the max parse depth. If so, replace
|
4544
|
-
// the subnode with an error node. Otherwise we risk crashing in the binder
|
4545
|
-
// or type evaluator.
|
4546
|
-
if (expression.maxChildDepth !== undefined && expression.maxChildDepth >= maxChildNodeDepth) {
|
4547
|
-
expression = ErrorNode.create(expression, ErrorExpressionCategory.MaxDepthExceeded);
|
4548
|
-
this._addError(Localizer.Diagnostic.maxParseDepthExceeded(), expression);
|
4549
|
-
}
|
4550
|
-
|
4551
|
-
return UnaryOperationNode.create(operatorToken, expression, operator);
|
4552
|
-
}
|
4553
|
-
|
4554
|
-
private _parseStringList(): StringListNode {
|
4555
|
-
const stringList: (StringNode | FormatStringNode)[] = [];
|
4556
|
-
|
4557
|
-
while (this._peekTokenType() === TokenType.String) {
|
4558
|
-
const stringToken = this._getNextToken() as StringToken;
|
4559
|
-
if (stringToken.flags & StringTokenFlags.Format) {
|
4560
|
-
stringList.push(this._parseFormatString(stringToken));
|
4561
|
-
} else {
|
4562
|
-
stringList.push(this._makeStringNode(stringToken));
|
4563
|
-
}
|
4564
|
-
}
|
4565
|
-
|
4566
|
-
const stringNode = StringListNode.create(stringList);
|
4567
|
-
|
4568
|
-
// If we're parsing a type annotation, parse the contents of the string.
|
4569
|
-
if (this._isParsingTypeAnnotation) {
|
4570
|
-
// Don't allow multiple strings because we have no way of reporting
|
4571
|
-
// parse errors that span strings.
|
4572
|
-
if (stringNode.strings.length > 1) {
|
4573
|
-
this._addError(Localizer.Diagnostic.annotationSpansStrings(), stringNode);
|
4574
|
-
} else if (stringNode.strings[0].token.flags & StringTokenFlags.Format) {
|
4575
|
-
this._addError(Localizer.Diagnostic.annotationFormatString(), stringNode);
|
4576
|
-
} else {
|
4577
|
-
const stringToken = stringNode.strings[0].token;
|
4578
|
-
const stringValue = StringTokenUtils.getUnescapedString(stringNode.strings[0].token);
|
4579
|
-
const unescapedString = stringValue.value;
|
4580
|
-
const tokenOffset = stringToken.start;
|
4581
|
-
const prefixLength = stringToken.prefixLength + stringToken.quoteMarkLength;
|
4582
|
-
|
4583
|
-
// Don't allow escape characters because we have no way of mapping
|
4584
|
-
// error ranges back to the escaped text.
|
4585
|
-
if (unescapedString.length !== stringToken.length - prefixLength - stringToken.quoteMarkLength) {
|
4586
|
-
this._addError(Localizer.Diagnostic.annotationStringEscape(), stringNode);
|
4587
|
-
} else {
|
4588
|
-
const parser = new Parser();
|
4589
|
-
const parseResults = parser.parseTextExpression(
|
4590
|
-
this._fileContents!,
|
4591
|
-
tokenOffset + prefixLength,
|
4592
|
-
unescapedString.length,
|
4593
|
-
this._parseOptions,
|
4594
|
-
ParseTextMode.VariableAnnotation,
|
4595
|
-
(stringNode.strings[0].token.flags & StringTokenFlags.Triplicate) !== 0 ? 1 : 0,
|
4596
|
-
this._typingSymbolAliases
|
4597
|
-
);
|
4598
|
-
|
4599
|
-
if (
|
4600
|
-
parseResults.diagnostics.length === 0 ||
|
4601
|
-
this._parseOptions.reportErrorsForParsedStringContents
|
4602
|
-
) {
|
4603
|
-
parseResults.diagnostics.forEach((diag) => {
|
4604
|
-
this._addError(diag.message, stringNode);
|
4605
|
-
});
|
4606
|
-
|
4607
|
-
if (parseResults.parseTree) {
|
4608
|
-
assert(parseResults.parseTree.nodeType !== ParseNodeType.FunctionAnnotation);
|
4609
|
-
stringNode.typeAnnotation = parseResults.parseTree;
|
4610
|
-
stringNode.typeAnnotation.parent = stringNode;
|
4611
|
-
}
|
4612
|
-
}
|
4613
|
-
}
|
4614
|
-
}
|
4615
|
-
}
|
4616
|
-
|
4617
|
-
return stringNode;
|
4618
|
-
}
|
4619
|
-
|
4620
|
-
// Python 3.8 added support for star (unpack) expressions in tuples
|
4621
|
-
// following a return or yield statement in cases where the tuple
|
4622
|
-
// wasn't surrounded in parentheses.
|
4623
|
-
private _reportConditionalErrorForStarTupleElement(possibleTupleExpr: ExpressionNode) {
|
4624
|
-
if (possibleTupleExpr.nodeType !== ParseNodeType.Tuple) {
|
4625
|
-
return;
|
4626
|
-
}
|
4627
|
-
|
4628
|
-
if (possibleTupleExpr.enclosedInParens) {
|
4629
|
-
return;
|
4630
|
-
}
|
4631
|
-
|
4632
|
-
if (this._parseOptions.pythonVersion >= PythonVersion.V3_8) {
|
4633
|
-
return;
|
4634
|
-
}
|
4635
|
-
|
4636
|
-
for (const expr of possibleTupleExpr.expressions) {
|
4637
|
-
if (expr.nodeType === ParseNodeType.Unpack) {
|
4638
|
-
this._addError(Localizer.Diagnostic.unpackTuplesIllegal(), expr);
|
4639
|
-
return;
|
4640
|
-
}
|
4641
|
-
}
|
4642
|
-
}
|
4643
|
-
|
4644
|
-
// Peeks at the next token and returns true if it can never
|
4645
|
-
// represent the start of an expression.
|
4646
|
-
private _isNextTokenNeverExpression(): boolean {
|
4647
|
-
const nextToken = this._peekToken();
|
4648
|
-
switch (nextToken.type) {
|
4649
|
-
case TokenType.Keyword: {
|
4650
|
-
switch (this._peekKeywordType()) {
|
4651
|
-
case KeywordType.For:
|
4652
|
-
case KeywordType.In:
|
4653
|
-
case KeywordType.If:
|
4654
|
-
return true;
|
4655
|
-
}
|
4656
|
-
break;
|
4657
|
-
}
|
4658
|
-
|
4659
|
-
case TokenType.Operator: {
|
4660
|
-
switch (this._peekOperatorType()) {
|
4661
|
-
case OperatorType.AddEqual:
|
4662
|
-
case OperatorType.SubtractEqual:
|
4663
|
-
case OperatorType.MultiplyEqual:
|
4664
|
-
case OperatorType.DivideEqual:
|
4665
|
-
case OperatorType.ModEqual:
|
4666
|
-
case OperatorType.BitwiseAndEqual:
|
4667
|
-
case OperatorType.BitwiseOrEqual:
|
4668
|
-
case OperatorType.BitwiseXorEqual:
|
4669
|
-
case OperatorType.LeftShiftEqual:
|
4670
|
-
case OperatorType.RightShiftEqual:
|
4671
|
-
case OperatorType.PowerEqual:
|
4672
|
-
case OperatorType.FloorDivideEqual:
|
4673
|
-
case OperatorType.Assign:
|
4674
|
-
return true;
|
4675
|
-
}
|
4676
|
-
break;
|
4677
|
-
}
|
4678
|
-
|
4679
|
-
case TokenType.Indent:
|
4680
|
-
case TokenType.Dedent:
|
4681
|
-
case TokenType.NewLine:
|
4682
|
-
case TokenType.EndOfStream:
|
4683
|
-
case TokenType.Semicolon:
|
4684
|
-
case TokenType.CloseParenthesis:
|
4685
|
-
case TokenType.CloseBracket:
|
4686
|
-
case TokenType.CloseCurlyBrace:
|
4687
|
-
case TokenType.Comma:
|
4688
|
-
case TokenType.Colon:
|
4689
|
-
return true;
|
4690
|
-
}
|
4691
|
-
|
4692
|
-
return false;
|
4693
|
-
}
|
4694
|
-
|
4695
|
-
private _disallowAssignmentExpression(callback: () => void) {
|
4696
|
-
const wasAllowed = this._assignmentExpressionsAllowed;
|
4697
|
-
this._assignmentExpressionsAllowed = false;
|
4698
|
-
|
4699
|
-
callback();
|
4700
|
-
|
4701
|
-
this._assignmentExpressionsAllowed = wasAllowed;
|
4702
|
-
}
|
4703
|
-
|
4704
|
-
private _getNextToken(): Token {
|
4705
|
-
const token = this._tokenizerOutput!.tokens.getItemAt(this._tokenIndex);
|
4706
|
-
if (!this._atEof()) {
|
4707
|
-
this._tokenIndex++;
|
4708
|
-
}
|
4709
|
-
|
4710
|
-
return token;
|
4711
|
-
}
|
4712
|
-
|
4713
|
-
private _atEof(): boolean {
|
4714
|
-
// Are we pointing at the last token in the stream (which is
|
4715
|
-
// assumed to be an end-of-stream token)?
|
4716
|
-
return this._tokenIndex >= this._tokenizerOutput!.tokens.count - 1;
|
4717
|
-
}
|
4718
|
-
|
4719
|
-
private _peekToken(count = 0): Token {
|
4720
|
-
if (this._tokenIndex + count < 0) {
|
4721
|
-
return this._tokenizerOutput!.tokens.getItemAt(0);
|
4722
|
-
}
|
4723
|
-
|
4724
|
-
if (this._tokenIndex + count >= this._tokenizerOutput!.tokens.count) {
|
4725
|
-
return this._tokenizerOutput!.tokens.getItemAt(this._tokenizerOutput!.tokens.count - 1);
|
4726
|
-
}
|
4727
|
-
|
4728
|
-
return this._tokenizerOutput!.tokens.getItemAt(this._tokenIndex + count);
|
4729
|
-
}
|
4730
|
-
|
4731
|
-
private _peekTokenType(): TokenType {
|
4732
|
-
return this._peekToken().type;
|
4733
|
-
}
|
4734
|
-
|
4735
|
-
private _peekKeywordType(): KeywordType | undefined {
|
4736
|
-
const nextToken = this._peekToken();
|
4737
|
-
if (nextToken.type !== TokenType.Keyword) {
|
4738
|
-
return undefined;
|
4739
|
-
}
|
4740
|
-
|
4741
|
-
return (nextToken as KeywordToken).keywordType;
|
4742
|
-
}
|
4743
|
-
|
4744
|
-
private _peekOperatorType(): OperatorType | undefined {
|
4745
|
-
const nextToken = this._peekToken();
|
4746
|
-
if (nextToken.type !== TokenType.Operator) {
|
4747
|
-
return undefined;
|
4748
|
-
}
|
4749
|
-
|
4750
|
-
return (nextToken as OperatorToken).operatorType;
|
4751
|
-
}
|
4752
|
-
|
4753
|
-
private _getTokenIfIdentifier(): IdentifierToken | undefined {
|
4754
|
-
const nextToken = this._peekToken();
|
4755
|
-
if (nextToken.type === TokenType.Identifier) {
|
4756
|
-
return this._getNextToken() as IdentifierToken;
|
4757
|
-
}
|
4758
|
-
|
4759
|
-
// If the next token is invalid, treat it as an identifier.
|
4760
|
-
if (nextToken.type === TokenType.Invalid) {
|
4761
|
-
this._getNextToken();
|
4762
|
-
this._addError(Localizer.Diagnostic.invalidIdentifierChar(), nextToken);
|
4763
|
-
return IdentifierToken.create(nextToken.start, nextToken.length, '', nextToken.comments);
|
4764
|
-
}
|
4765
|
-
|
4766
|
-
// If this is a "soft keyword", it can be converted into an identifier.
|
4767
|
-
if (nextToken.type === TokenType.Keyword) {
|
4768
|
-
const keywordType = this._peekKeywordType();
|
4769
|
-
const softKeywords = [KeywordType.Debug, KeywordType.Match, KeywordType.Case];
|
4770
|
-
if (softKeywords.find((type) => type === keywordType)) {
|
4771
|
-
const keywordText = this._fileContents!.substr(nextToken.start, nextToken.length);
|
4772
|
-
this._getNextToken();
|
4773
|
-
return IdentifierToken.create(nextToken.start, nextToken.length, keywordText, nextToken.comments);
|
4774
|
-
}
|
4775
|
-
}
|
4776
|
-
|
4777
|
-
return undefined;
|
4778
|
-
}
|
4779
|
-
|
4780
|
-
// Consumes tokens until the next one in the stream is
|
4781
|
-
// either a specified terminator or the end-of-stream
|
4782
|
-
// token.
|
4783
|
-
private _consumeTokensUntilType(terminators: TokenType[]): boolean {
|
4784
|
-
while (true) {
|
4785
|
-
const token = this._peekToken();
|
4786
|
-
if (terminators.some((term) => term === token.type)) {
|
4787
|
-
return true;
|
4788
|
-
}
|
4789
|
-
|
4790
|
-
if (token.type === TokenType.EndOfStream) {
|
4791
|
-
return false;
|
4792
|
-
}
|
4793
|
-
|
4794
|
-
this._getNextToken();
|
4795
|
-
}
|
4796
|
-
}
|
4797
|
-
|
4798
|
-
private _getTokenIfType(tokenType: TokenType): Token | undefined {
|
4799
|
-
if (this._peekTokenType() === tokenType) {
|
4800
|
-
return this._getNextToken();
|
4801
|
-
}
|
4802
|
-
|
4803
|
-
return undefined;
|
4804
|
-
}
|
4805
|
-
|
4806
|
-
private _consumeTokenIfType(tokenType: TokenType): boolean {
|
4807
|
-
return !!this._getTokenIfType(tokenType);
|
4808
|
-
}
|
4809
|
-
|
4810
|
-
private _consumeTokenIfKeyword(keywordType: KeywordType): boolean {
|
4811
|
-
if (this._peekKeywordType() === keywordType) {
|
4812
|
-
this._getNextToken();
|
4813
|
-
return true;
|
4814
|
-
}
|
4815
|
-
|
4816
|
-
return false;
|
4817
|
-
}
|
4818
|
-
|
4819
|
-
private _consumeTokenIfOperator(operatorType: OperatorType): boolean {
|
4820
|
-
if (this._peekOperatorType() === operatorType) {
|
4821
|
-
this._getNextToken();
|
4822
|
-
return true;
|
4823
|
-
}
|
4824
|
-
|
4825
|
-
return false;
|
4826
|
-
}
|
4827
|
-
|
4828
|
-
private _getKeywordToken(keywordType: KeywordType): KeywordToken {
|
4829
|
-
const keywordToken = this._getNextToken() as KeywordToken;
|
4830
|
-
assert(keywordToken.type === TokenType.Keyword);
|
4831
|
-
assert(keywordToken.keywordType === keywordType);
|
4832
|
-
return keywordToken;
|
4833
|
-
}
|
4834
|
-
|
4835
|
-
private _getLanguageVersion() {
|
4836
|
-
return this._parseOptions.pythonVersion;
|
4837
|
-
}
|
4838
|
-
|
4839
|
-
private _suppressErrors(callback: () => void) {
|
4840
|
-
const errorsWereSuppressed = this._areErrorsSuppressed;
|
4841
|
-
try {
|
4842
|
-
this._areErrorsSuppressed = true;
|
4843
|
-
callback();
|
4844
|
-
} finally {
|
4845
|
-
this._areErrorsSuppressed = errorsWereSuppressed;
|
4846
|
-
}
|
4847
|
-
}
|
4848
|
-
|
4849
|
-
private _addError(message: string, range: TextRange) {
|
4850
|
-
assert(range !== undefined);
|
4851
|
-
|
4852
|
-
if (!this._areErrorsSuppressed) {
|
4853
|
-
this._diagSink.addError(
|
4854
|
-
message,
|
4855
|
-
convertOffsetsToRange(range.start, range.start + range.length, this._tokenizerOutput!.lines)
|
4856
|
-
);
|
4857
|
-
}
|
4858
|
-
}
|
4859
|
-
}
|