diff --git a/src/languages/language_python.rs b/src/languages/language_python.rs index 610d1eb0a..9e52f728e 100644 --- a/src/languages/language_python.rs +++ b/src/languages/language_python.rs @@ -37,51 +37,51 @@ pub enum Python { With = 32, Def = 33, DASHGT = 34, - EQ = 35, - STARSTAR = 36, - Global = 37, - Nonlocal = 38, - Exec = 39, - Class = 40, - AT = 41, - Not = 42, - And = 43, - Or = 44, - PLUS = 45, - DASH = 46, - SLASH = 47, - PERCENT = 48, - SLASHSLASH = 49, - PIPE = 50, - AMP = 51, - CARET = 52, - LTLT = 53, - TILDE = 54, - LT = 55, - LTEQ = 56, - EQEQ = 57, - BANGEQ = 58, - GTEQ = 59, - GT = 60, - LTGT = 61, - Is = 62, - Lambda3 = 63, - PLUSEQ = 64, - DASHEQ = 65, - STAREQ = 66, - SLASHEQ = 67, - ATEQ = 68, - SLASHSLASHEQ = 69, - PERCENTEQ = 70, - STARSTAREQ = 71, - GTGTEQ = 72, - LTLTEQ = 73, - AMPEQ = 74, - CARETEQ = 75, - PIPEEQ = 76, - Yield2 = 77, - LBRACK = 78, - RBRACK = 79, + STARSTAR = 35, + Global = 36, + Nonlocal = 37, + Exec = 38, + Class = 39, + AT = 40, + LBRACK = 41, + RBRACK = 42, + EQ = 43, + Not = 44, + And = 45, + Or = 46, + PLUS = 47, + DASH = 48, + SLASH = 49, + PERCENT = 50, + SLASHSLASH = 51, + PIPE = 52, + AMP = 53, + CARET = 54, + LTLT = 55, + TILDE = 56, + LT = 57, + LTEQ = 58, + EQEQ = 59, + BANGEQ = 60, + GTEQ = 61, + GT = 62, + LTGT = 63, + Is = 64, + Lambda3 = 65, + PLUSEQ = 66, + DASHEQ = 67, + STAREQ = 68, + SLASHEQ = 69, + ATEQ = 70, + SLASHSLASHEQ = 71, + PERCENTEQ = 72, + STARSTAREQ = 73, + GTGTEQ = 74, + LTLTEQ = 75, + AMPEQ = 76, + CARETEQ = 77, + PIPEEQ = 78, + Yield2 = 79, Ellipsis = 80, LBRACE = 81, RBRACE = 82, @@ -138,91 +138,97 @@ pub enum Python { FunctionDefinition = 133, Parameters = 134, LambdaParameters = 135, - Parameters2 = 136, - DefaultParameter = 137, - TypedDefaultParameter = 138, - ListSplat = 139, - DictionarySplat = 140, - GlobalStatement = 141, - NonlocalStatement = 142, - ExecStatement = 143, - ClassDefinition = 144, - ParenthesizedListSplat = 145, - ArgumentList = 146, - DecoratedDefinition = 147, - Decorator = 148, - Block = 149, - Variables = 150, - ExpressionList = 151, - DottedName = 152, - ExpressionWithinForInClause = 153, - Expression = 154, - PrimaryExpression = 155, - NotOperator = 156, - BooleanOperator = 157, - BinaryOperator = 158, - UnaryOperator = 159, - ComparisonOperator = 160, - Lambda = 161, - Lambda2 = 162, - Assignment = 163, - AugmentedAssignment = 164, - RightHandSide = 165, - Yield = 166, - Attribute = 167, - Subscript = 168, - Slice = 169, - Call = 170, - TypedParameter = 171, - Type = 172, - KeywordArgument = 173, - List = 174, - ComprehensionClauses = 175, - ListComprehension = 176, - Dictionary = 177, - DictionaryComprehension = 178, - Pair = 179, - Set = 180, - SetComprehension = 181, - ParenthesizedExpression = 182, - CollectionElements = 183, - Tuple = 184, - GeneratorExpression = 185, - ForInClause = 186, - IfClause = 187, - ConditionalExpression = 188, - ConcatenatedString = 189, - String = 190, - Interpolation = 191, - FormatSpecifier = 192, - FormatExpression = 193, - Await = 194, - ModuleRepeat1 = 195, - SimpleStatementsRepeat1 = 196, - ImportPrefixRepeat1 = 197, - ImportListRepeat1 = 198, - PrintStatementRepeat1 = 199, - AssertStatementRepeat1 = 200, - IfStatementRepeat1 = 201, - TryStatementRepeat1 = 202, - WithStatementRepeat1 = 203, - ParametersRepeat1 = 204, - GlobalStatementRepeat1 = 205, - ArgumentListRepeat1 = 206, - DecoratedDefinitionRepeat1 = 207, - VariablesRepeat1 = 208, - DottedNameRepeat1 = 209, - ComparisonOperatorRepeat1 = 210, - SubscriptRepeat1 = 211, - ComprehensionClausesRepeat1 = 212, - DictionaryRepeat1 = 213, - SetRepeat1 = 214, - CollectionElementsRepeat1 = 215, - ForInClauseRepeat1 = 216, - ConcatenatedStringRepeat1 = 217, - StringRepeat1 = 218, - FormatSpecifierRepeat1 = 219, - Error = 220, + ListSplat = 136, + DictionarySplat = 137, + GlobalStatement = 138, + NonlocalStatement = 139, + ExecStatement = 140, + ClassDefinition = 141, + ParenthesizedListSplat = 142, + ArgumentList = 143, + DecoratedDefinition = 144, + Decorator = 145, + Block = 146, + ExpressionList = 147, + DottedName = 148, + Parameters2 = 149, + Patterns = 150, + Parameter = 151, + Pattern = 152, + TuplePattern = 153, + ListPattern = 154, + DefaultParameter = 155, + TypedDefaultParameter = 156, + ListSplatPattern = 157, + DictionarySplatPattern = 158, + ExpressionWithinForInClause = 159, + Expression = 160, + PrimaryExpression = 161, + NotOperator = 162, + BooleanOperator = 163, + BinaryOperator = 164, + UnaryOperator = 165, + ComparisonOperator = 166, + Lambda = 167, + Lambda2 = 168, + Assignment = 169, + AugmentedAssignment = 170, + PatternList = 171, + RightHandSide = 172, + Yield = 173, + Attribute = 174, + Subscript = 175, + Slice = 176, + Call = 177, + TypedParameter = 178, + Type = 179, + KeywordArgument = 180, + List = 181, + Set = 182, + Tuple = 183, + Dictionary = 184, + Pair = 185, + ListComprehension = 186, + DictionaryComprehension = 187, + SetComprehension = 188, + GeneratorExpression = 189, + ComprehensionClauses = 190, + ParenthesizedExpression = 191, + CollectionElements = 192, + ForInClause = 193, + IfClause = 194, + ConditionalExpression = 195, + ConcatenatedString = 196, + String = 197, + Interpolation = 198, + FormatSpecifier = 199, + FormatExpression = 200, + Await = 201, + ModuleRepeat1 = 202, + SimpleStatementsRepeat1 = 203, + ImportPrefixRepeat1 = 204, + ImportListRepeat1 = 205, + PrintStatementRepeat1 = 206, + AssertStatementRepeat1 = 207, + IfStatementRepeat1 = 208, + TryStatementRepeat1 = 209, + WithStatementRepeat1 = 210, + GlobalStatementRepeat1 = 211, + ArgumentListRepeat1 = 212, + DecoratedDefinitionRepeat1 = 213, + DottedNameRepeat1 = 214, + ParametersRepeat1 = 215, + PatternsRepeat1 = 216, + ComparisonOperatorRepeat1 = 217, + SubscriptRepeat1 = 218, + DictionaryRepeat1 = 219, + ComprehensionClausesRepeat1 = 220, + CollectionElementsRepeat1 = 221, + ForInClauseRepeat1 = 222, + ConcatenatedStringRepeat1 = 223, + StringRepeat1 = 224, + FormatSpecifierRepeat1 = 225, + Error = 226, } impl Into<&'static str> for Python { @@ -263,13 +269,15 @@ impl Into<&'static str> for Python { Python::With => "with", Python::Def => "def", Python::DASHGT => "->", - Python::EQ => "=", Python::STARSTAR => "**", Python::Global => "global", Python::Nonlocal => "nonlocal", Python::Exec => "exec", Python::Class => "class", Python::AT => "@", + Python::LBRACK => "[", + Python::RBRACK => "]", + Python::EQ => "=", Python::Not => "not", Python::And => "and", Python::Or => "or", @@ -306,8 +314,6 @@ impl Into<&'static str> for Python { Python::CARETEQ => "^=", Python::PIPEEQ => "|=", Python::Yield2 => "yield", - Python::LBRACK => "[", - Python::RBRACK => "]", Python::Ellipsis => "ellipsis", Python::LBRACE => "{", Python::RBRACE => "}", @@ -364,9 +370,6 @@ impl Into<&'static str> for Python { Python::FunctionDefinition => "function_definition", Python::Parameters => "parameters", Python::LambdaParameters => "lambda_parameters", - Python::Parameters2 => "_parameters", - Python::DefaultParameter => "default_parameter", - Python::TypedDefaultParameter => "typed_default_parameter", Python::ListSplat => "list_splat", Python::DictionarySplat => "dictionary_splat", Python::GlobalStatement => "global_statement", @@ -378,12 +381,21 @@ impl Into<&'static str> for Python { Python::DecoratedDefinition => "decorated_definition", Python::Decorator => "decorator", Python::Block => "block", - Python::Variables => "variables", Python::ExpressionList => "expression_list", Python::DottedName => "dotted_name", + Python::Parameters2 => "_parameters", + Python::Patterns => "_patterns", + Python::Parameter => "parameter", + Python::Pattern => "pattern", + Python::TuplePattern => "tuple_pattern", + Python::ListPattern => "list_pattern", + Python::DefaultParameter => "default_parameter", + Python::TypedDefaultParameter => "typed_default_parameter", + Python::ListSplatPattern => "list_splat_pattern", + Python::DictionarySplatPattern => "dictionary_splat_pattern", Python::ExpressionWithinForInClause => "_expression_within_for_in_clause", - Python::Expression => "_expression", - Python::PrimaryExpression => "_primary_expression", + Python::Expression => "expression", + Python::PrimaryExpression => "primary_expression", Python::NotOperator => "not_operator", Python::BooleanOperator => "boolean_operator", Python::BinaryOperator => "binary_operator", @@ -393,6 +405,7 @@ impl Into<&'static str> for Python { Python::Lambda2 => "lambda", Python::Assignment => "assignment", Python::AugmentedAssignment => "augmented_assignment", + Python::PatternList => "pattern_list", Python::RightHandSide => "_right_hand_side", Python::Yield => "yield", Python::Attribute => "attribute", @@ -403,17 +416,17 @@ impl Into<&'static str> for Python { Python::Type => "type", Python::KeywordArgument => "keyword_argument", Python::List => "list", - Python::ComprehensionClauses => "_comprehension_clauses", - Python::ListComprehension => "list_comprehension", + Python::Set => "set", + Python::Tuple => "tuple", Python::Dictionary => "dictionary", - Python::DictionaryComprehension => "dictionary_comprehension", Python::Pair => "pair", - Python::Set => "set", + Python::ListComprehension => "list_comprehension", + Python::DictionaryComprehension => "dictionary_comprehension", Python::SetComprehension => "set_comprehension", + Python::GeneratorExpression => "generator_expression", + Python::ComprehensionClauses => "_comprehension_clauses", Python::ParenthesizedExpression => "parenthesized_expression", Python::CollectionElements => "_collection_elements", - Python::Tuple => "tuple", - Python::GeneratorExpression => "generator_expression", Python::ForInClause => "for_in_clause", Python::IfClause => "if_clause", Python::ConditionalExpression => "conditional_expression", @@ -432,17 +445,16 @@ impl Into<&'static str> for Python { Python::IfStatementRepeat1 => "if_statement_repeat1", Python::TryStatementRepeat1 => "try_statement_repeat1", Python::WithStatementRepeat1 => "with_statement_repeat1", - Python::ParametersRepeat1 => "_parameters_repeat1", Python::GlobalStatementRepeat1 => "global_statement_repeat1", Python::ArgumentListRepeat1 => "argument_list_repeat1", Python::DecoratedDefinitionRepeat1 => "decorated_definition_repeat1", - Python::VariablesRepeat1 => "variables_repeat1", Python::DottedNameRepeat1 => "dotted_name_repeat1", + Python::ParametersRepeat1 => "_parameters_repeat1", + Python::PatternsRepeat1 => "_patterns_repeat1", Python::ComparisonOperatorRepeat1 => "comparison_operator_repeat1", Python::SubscriptRepeat1 => "subscript_repeat1", - Python::ComprehensionClausesRepeat1 => "_comprehension_clauses_repeat1", Python::DictionaryRepeat1 => "dictionary_repeat1", - Python::SetRepeat1 => "set_repeat1", + Python::ComprehensionClausesRepeat1 => "_comprehension_clauses_repeat1", Python::CollectionElementsRepeat1 => "_collection_elements_repeat1", Python::ForInClauseRepeat1 => "for_in_clause_repeat1", Python::ConcatenatedStringRepeat1 => "concatenated_string_repeat1", @@ -457,287 +469,295 @@ impl Into<&'static str> for Python { static KEYS: phf::Map<&'static str, Python> = ::phf::Map { key: 3213172566270843353, disps: ::phf::Slice::Static(&[ - (0, 8), - (0, 0), - (0, 7), - (0, 77), - (1, 81), - (0, 3), - (3, 1), - (0, 13), - (0, 32), - (0, 67), - (0, 211), - (0, 9), - (0, 11), - (9, 147), - (8, 109), - (0, 206), - (0, 7), - (0, 3), - (1, 112), - (10, 66), - (0, 6), - (0, 104), - (8, 185), + (2, 0), + (5, 98), (0, 0), - (15, 122), + (1, 185), + (0, 21), + (0, 82), + (0, 2), + (0, 20), (0, 0), - (0, 22), - (0, 85), + (0, 28), + (0, 8), + (18, 205), + (0, 1), + (0, 21), + (0, 38), + (0, 10), + (0, 164), + (0, 40), + (0, 52), + (1, 107), + (0, 5), + (3, 167), (0, 0), - (0, 42), - (1, 2), + (0, 136), + (0, 30), + (0, 137), + (0, 29), (0, 0), - (0, 3), - (1, 103), - (0, 13), - (78, 104), - (4, 212), - (26, 152), - (2, 22), - (0, 48), - (1, 117), - (0, 206), - (1, 11), + (21, 85), + (24, 194), + (0, 1), + (3, 117), + (5, 143), + (0, 11), + (0, 96), + (1, 190), + (3, 132), + (0, 20), + (22, 42), + (4, 199), + (19, 186), + (7, 146), + (16, 61), + (0, 149), + (0, 49), ]), entries: ::phf::Slice::Static(&[ - ("module", Python::Module), - ("|=", Python::PIPEEQ), - ("if_statement", Python::IfStatement), - ("if", Python::If), - ("decorated_definition", Python::DecoratedDefinition), - ("not", Python::Not), + ("global_statement", Python::GlobalStatement), + ("__future__", Python::Future), + ("import_prefix", Python::ImportPrefix), + ("_statement", Python::Statement), + ("if_statement_repeat1", Python::IfStatementRepeat1), + ("global_statement_repeat1", Python::GlobalStatementRepeat1), + ("while_statement", Python::WhileStatement), + ("augmented_assignment", Python::AugmentedAssignment), + ("dictionary", Python::Dictionary), + ("function_definition", Python::FunctionDefinition), ("<=", Python::LTEQ), - ("chevron", Python::Chevron), + ("concatenated_string", Python::ConcatenatedString), + ("elif_clause", Python::ElifClause), + ("string_repeat1", Python::StringRepeat1), + (">", Python::GT), + ("with", Python::With), + ("ellipsis", Python::Ellipsis), + ("await", Python::Await), + ("format_specifier", Python::FormatSpecifier), + ("dictionary_splat", Python::DictionarySplat), ("%=", Python::PERCENTEQ), - ("attribute", Python::Attribute), - ("async", Python::Async), - ("named_expression", Python::NamedExpression), - ("if_clause", Python::IfClause), - ("format_specifier_repeat1", Python::FormatSpecifierRepeat1), - ("aliased_import", Python::AliasedImport), + ("raise_statement", Python::RaiseStatement), ("*", Python::STAR), - ("dictionary_splat", Python::DictionarySplat), - ("parameters", Python::Parameters), - ("for_in_clause", Python::ForInClause), - ("]", Python::RBRACK), + ("for", Python::For), + ("pass", Python::Pass), + ("&=", Python::AMPEQ), + ("parameter", Python::Parameter), + ("interpolation", Python::Interpolation), + ("and", Python::And), + ("subscript_repeat1", Python::SubscriptRepeat1), + ("end", Python::End), + ("argument_list_repeat1", Python::ArgumentListRepeat1), + ("nonlocal_statement", Python::NonlocalStatement), + ("list_splat", Python::ListSplat), + (":", Python::COLON), + ("def", Python::Def), + ("chevron", Python::Chevron), + ("type", Python::Type), + (">>=", Python::GTGTEQ), + ("wildcard_import", Python::WildcardImport), + ("pass_statement", Python::PassStatement), + ("block", Python::Block), + ("\\\"", Python::DQUOTE), + ("set", Python::Set), + ("_right_hand_side", Python::RightHandSide), + ("_comprehension_clauses", Python::ComprehensionClauses), + ( + "decorated_definition_repeat1", + Python::DecoratedDefinitionRepeat1, + ), + ("for_statement", Python::ForStatement), + ("if", Python::If), ( "comparison_operator_repeat1", Python::ComparisonOperatorRepeat1, ), - ("escape_sequence", Python::EscapeSequence), - ("=", Python::EQ), - ("else_clause", Python::ElseClause), - ("pass_statement", Python::PassStatement), - ("\\\"", Python::DQUOTE), - ("is", Python::Is), - ("import_prefix_repeat1", Python::ImportPrefixRepeat1), ("else", Python::Else), - ("_expression", Python::Expression), - ("try_statement", Python::TryStatement), - (">=", Python::GTEQ), - ("tuple", Python::Tuple), - ("pair", Python::Pair), - ("__future__", Python::Future), - ("false", Python::False), - ("import_statement", Python::ImportStatement), - ("argument_list_repeat1", Python::ArgumentListRepeat1), ( - "decorated_definition_repeat1", - Python::DecoratedDefinitionRepeat1, + "_comprehension_clauses_repeat1", + Python::ComprehensionClausesRepeat1, ), - ("/=", Python::SLASHEQ), - ("//=", Python::SLASHSLASHEQ), - ("//", Python::SLASHSLASH), - ("string", Python::String), + ("print_statement_repeat1", Python::PrintStatementRepeat1), + ("with_statement_repeat1", Python::WithStatementRepeat1), + ("_dedent", Python::Dedent), + ("continue", Python::Continue), + ("_collection_elements", Python::CollectionElements), + ("_parameters_repeat1", Python::ParametersRepeat1), ("**=", Python::STARSTAREQ), - ("elif_clause", Python::ElifClause), - ("break", Python::Break), - ("function_definition", Python::FunctionDefinition), - ("conditional_expression", Python::ConditionalExpression), - ("<>", Python::LTGT), - ("with", Python::With), - ("none", Python::None), - ("del", Python::Del), + ("@=", Python::ATEQ), + ("]", Python::RBRACK), + ("finally_clause", Python::FinallyClause), + ("//", Python::SLASHSLASH), + ("named_expression", Python::NamedExpression), + ("%", Python::PERCENT), + ("expression_statement", Python::ExpressionStatement), + ("tuple", Python::Tuple), + ("_patterns", Python::Patterns), + ("_import_list_repeat1", Python::ImportListRepeat1), + ("->", Python::DASHGT), + ("identifier", Python::Identifier), ("delete_statement", Python::DeleteStatement), - ("variables", Python::Variables), - ("_simple_statements", Python::SimpleStatements), - ("exec", Python::Exec), - ("==", Python::EQEQ), - ("variables_repeat1", Python::VariablesRepeat1), - ("_semicolon", Python::Semicolon), - ("global_statement_repeat1", Python::GlobalStatementRepeat1), + ("/", Python::SLASH), + ("as", Python::As), + ("for_in_clause", Python::ForInClause), + ("<>", Python::LTGT), + ("float", Python::Float), ("subscript", Python::Subscript), - ("[", Python::LBRACK), - ("print", Python::Print), + ("expression", Python::Expression), + ("+", Python::PLUS), + ("global", Python::Global), + ("yield", Python::Yield), + ("elif", Python::Elif), + ("|=", Python::PIPEEQ), + ("decorator", Python::Decorator), + ("except", Python::Except), + ("class", Python::Class), + ("list", Python::List), + ("decorated_definition", Python::DecoratedDefinition), + ("//=", Python::SLASHSLASHEQ), + ("with_statement", Python::WithStatement), ( - "_simple_statements_repeat1", - Python::SimpleStatementsRepeat1, + "_expression_within_for_in_clause", + Python::ExpressionWithinForInClause, ), - ("future_import_statement", Python::FutureImportStatement), - ("format_specifier_token1", Python::FormatSpecifierToken1), - ("return", Python::Return), - ("break_statement", Python::BreakStatement), - ("finally_clause", Python::FinallyClause), - ("format_specifier", Python::FormatSpecifier), - ("expression_list", Python::ExpressionList), - (">>=", Python::GTGTEQ), - ("^", Python::CARET), - ("parenthesized_expression", Python::ParenthesizedExpression), - ("augmented_assignment", Python::AugmentedAssignment), - ("{", Python::LBRACE), - ("parenthesized_list_splat", Python::ParenthesizedListSplat), + ("_simple_statements", Python::SimpleStatements), ( "_collection_elements_repeat1", Python::CollectionElementsRepeat1, ), - ("set_repeat1", Python::SetRepeat1), - ("raise", Python::Raise), - ("nonlocal", Python::Nonlocal), - ("comment", Python::Comment), - ("|", Python::PIPE), - ("unary_operator", Python::UnaryOperator), - ("identifier", Python::Identifier), - ("global_statement", Python::GlobalStatement), - ("_right_hand_side", Python::RightHandSide), - (">>", Python::GTGT), - ("yield", Python::Yield), - ("for_statement", Python::ForStatement), - ("lambda_parameters", Python::LambdaParameters), + ("not_operator", Python::NotOperator), (")", Python::RPAREN), - ("except", Python::Except), - ("nonlocal_statement", Python::NonlocalStatement), - ("integer", Python::Integer), - ( - "_comprehension_clauses_repeat1", - Python::ComprehensionClausesRepeat1, - ), - ("type_conversion", Python::TypeConversion), - ("string_repeat1", Python::StringRepeat1), - ("%", Python::PERCENT), - ("continue_statement", Python::ContinueStatement), - ("as", Python::As), - ("call", Python::Call), - ("slice", Python::Slice), - ("end", Python::End), - ("/", Python::SLASH), - ("<<", Python::LTLT), - ("}", Python::RBRACE), - ("concatenated_string", Python::ConcatenatedString), - ("dotted_name", Python::DottedName), - ("if_statement_repeat1", Python::IfStatementRepeat1), - ("return_statement", Python::ReturnStatement), - ("in", Python::In), - ("@=", Python::ATEQ), - ("binary_operator", Python::BinaryOperator), - ("continue", Python::Continue), - ("with_statement", Python::WithStatement), - ("ERROR", Python::Error), - ("_dedent", Python::Dedent), - ("<", Python::LT), - ("with_item", Python::WithItem), - ("^=", Python::CARETEQ), - ("set_comprehension", Python::SetComprehension), - ("import_from_statement", Python::ImportFromStatement), - ("class", Python::Class), - ("ellipsis", Python::Ellipsis), - ("expression_statement", Python::ExpressionStatement), - ("comparison_operator", Python::ComparisonOperator), + ("=", Python::EQ), ("print_statement", Python::PrintStatement), - ("for_in_clause_repeat1", Python::ForInClauseRepeat1), - ("lambda", Python::Lambda), - ("elif", Python::Elif), - ("_collection_elements", Python::CollectionElements), - (":", Python::COLON), - ("for", Python::For), - ("try", Python::Try), ( "concatenated_string_repeat1", Python::ConcatenatedStringRepeat1, ), - ("dictionary", Python::Dictionary), - ("_primary_expression", Python::PrimaryExpression), - ("raise_statement", Python::RaiseStatement), - ("try_statement_repeat1", Python::TryStatementRepeat1), - ("assert", Python::Assert), - ("->", Python::DASHGT), - ("-=", Python::DASHEQ), - ("finally", Python::Finally), - ("&", Python::AMP), - ("exec_statement", Python::ExecStatement), - ("**", Python::STARSTAR), - ("import_prefix", Python::ImportPrefix), - ("_comprehension_clauses", Python::ComprehensionClauses), - ("decorator", Python::Decorator), - ("except_clause", Python::ExceptClause), - ("type", Python::Type), - ("list", Python::List), - ("_statement", Python::Statement), - ( - "_expression_within_for_in_clause", - Python::ExpressionWithinForInClause, - ), + ("format_specifier_token1", Python::FormatSpecifierToken1), + ("print", Python::Print), + ("in", Python::In), + ("typed_default_parameter", Python::TypedDefaultParameter), + ("{", Python::LBRACE), + ("@", Python::AT), + ("try_statement", Python::TryStatement), + ("*=", Python::STAREQ), + ("raise", Python::Raise), + ("true", Python::True), + (".", Python::DOT), + ("integer", Python::Integer), + (":=", Python::COLONEQ), ("-", Python::DASH), - ("await", Python::Await), - ("_indent", Python::Indent), - ("!=", Python::BANGEQ), - ("_import_list_repeat1", Python::ImportListRepeat1), - ("module_repeat1", Python::ModuleRepeat1), - ("_parameters_repeat1", Python::ParametersRepeat1), + (",", Python::COMMA), + ("async", Python::Async), + ("==", Python::EQEQ), + ("except_clause", Python::ExceptClause), + ("list_comprehension", Python::ListComprehension), + ("|", Python::PIPE), + ("escape_sequence", Python::EscapeSequence), + ("pattern_list", Python::PatternList), + ("is", Python::Is), + ("finally", Python::Finally), + ("lambda", Python::Lambda), + ("break_statement", Python::BreakStatement), + ("false", Python::False), + ("comparison_operator", Python::ComparisonOperator), + ("/=", Python::SLASHEQ), + ("continue_statement", Python::ContinueStatement), + ("_semicolon", Python::Semicolon), + ("dictionary_repeat1", Python::DictionaryRepeat1), + ("if_statement", Python::IfStatement), + ("typed_parameter", Python::TypedParameter), + ("<<=", Python::LTLTEQ), + ("return_statement", Python::ReturnStatement), + ("type_conversion", Python::TypeConversion), + ("from", Python::From), + ("import_statement", Python::ImportStatement), + ("}", Python::RBRACE), + ("boolean_operator", Python::BooleanOperator), + ("nonlocal", Python::Nonlocal), + ("dotted_name_repeat1", Python::DottedNameRepeat1), + ("parenthesized_list_splat", Python::ParenthesizedListSplat), + ("none", Python::None), ("_import_list", Python::ImportList), - ("print_statement_repeat1", Python::PrintStatementRepeat1), - ("_not_escape_sequence", Python::NotEscapeSequence), + ("module", Python::Module), + ("+=", Python::PLUSEQ), + ("assert", Python::Assert), + ("call", Python::Call), + ("for_in_clause_repeat1", Python::ForInClauseRepeat1), + ("parameters", Python::Parameters), + ("^", Python::CARET), ("or", Python::Or), + ("if_clause", Python::IfClause), + ("assert_statement", Python::AssertStatement), ("generator_expression", Python::GeneratorExpression), - ("from", Python::From), - ("with_statement_repeat1", Python::WithStatementRepeat1), - ("&=", Python::AMPEQ), - ("true", Python::True), - ("_newline", Python::Newline), - ("boolean_operator", Python::BooleanOperator), - ("float", Python::Float), + ("comment", Python::Comment), + ("relative_import", Python::RelativeImport), + ("tuple_pattern", Python::TuplePattern), + ("_patterns_repeat1", Python::PatternsRepeat1), ("(", Python::LPAREN), - ("assert_statement", Python::AssertStatement), - ("@", Python::AT), + ("unary_operator", Python::UnaryOperator), + ("slice", Python::Slice), + ("**", Python::STARSTAR), + ("else_clause", Python::ElseClause), + ("ERROR", Python::Error), + ("aliased_import", Python::AliasedImport), + ("attribute", Python::Attribute), + ("with_item", Python::WithItem), + ("list_splat_pattern", Python::ListSplatPattern), + ("exec", Python::Exec), + ("format_specifier_repeat1", Python::FormatSpecifierRepeat1), + ("[", Python::LBRACK), + ("lambda_parameters", Python::LambdaParameters), + ("conditional_expression", Python::ConditionalExpression), + (">>", Python::GTGT), + ("class_definition", Python::ClassDefinition), + ("del", Python::Del), + ("dictionary_splat_pattern", Python::DictionarySplatPattern), + ("break", Python::Break), + ("dictionary_comprehension", Python::DictionaryComprehension), + ("_indent", Python::Indent), + ("string", Python::String), + ("primary_expression", Python::PrimaryExpression), + ("^=", Python::CARETEQ), + ("!=", Python::BANGEQ), + ("~", Python::TILDE), ("assert_statement_repeat1", Python::AssertStatementRepeat1), - ("block", Python::Block), + ("_newline", Python::Newline), + ("format_expression", Python::FormatExpression), + ("binary_operator", Python::BinaryOperator), + ("expression_list", Python::ExpressionList), + ("list_pattern", Python::ListPattern), + ("import_prefix_repeat1", Python::ImportPrefixRepeat1), + ("_not_escape_sequence", Python::NotEscapeSequence), + ("dotted_name", Python::DottedName), + ("default_parameter", Python::DefaultParameter), + ("return", Python::Return), + ("module_repeat1", Python::ModuleRepeat1), + ("future_import_statement", Python::FutureImportStatement), + ("-=", Python::DASHEQ), ("assignment", Python::Assignment), + ("keyword_argument", Python::KeywordArgument), + ("parenthesized_expression", Python::ParenthesizedExpression), + ("import_from_statement", Python::ImportFromStatement), + ("while", Python::While), ("import", Python::Import), - ("not_operator", Python::NotOperator), - ("+", Python::PLUS), + ("pattern", Python::Pattern), + ("not", Python::Not), + ("<<", Python::LTLT), + ( + "_simple_statements_repeat1", + Python::SimpleStatementsRepeat1, + ), + (">=", Python::GTEQ), ("argument_list", Python::ArgumentList), - ("while_statement", Python::WhileStatement), + ("try_statement_repeat1", Python::TryStatementRepeat1), ("_string_content", Python::StringContent), - ("typed_parameter", Python::TypedParameter), - ("<<=", Python::LTLTEQ), - ("relative_import", Python::RelativeImport), - (":=", Python::COLONEQ), - ("def", Python::Def), - ("default_parameter", Python::DefaultParameter), - ("+=", Python::PLUSEQ), - ("wildcard_import", Python::WildcardImport), - ("typed_default_parameter", Python::TypedDefaultParameter), - ("pass", Python::Pass), - (">", Python::GT), - ("interpolation", Python::Interpolation), - ("dictionary_comprehension", Python::DictionaryComprehension), - ("keyword_argument", Python::KeywordArgument), - ("format_expression", Python::FormatExpression), - (".", Python::DOT), - ("global", Python::Global), - (",", Python::COMMA), - ("and", Python::And), - ("set", Python::Set), - ("*=", Python::STAREQ), - ("~", Python::TILDE), - ("subscript_repeat1", Python::SubscriptRepeat1), - ("class_definition", Python::ClassDefinition), - ("while", Python::While), - ("list_splat", Python::ListSplat), - ("dotted_name_repeat1", Python::DottedNameRepeat1), - ("list_comprehension", Python::ListComprehension), - ("dictionary_repeat1", Python::DictionaryRepeat1), + ("<", Python::LT), + ("set_comprehension", Python::SetComprehension), + ("&", Python::AMP), + ("pair", Python::Pair), + ("try", Python::Try), + ("exec_statement", Python::ExecStatement), ]), }; diff --git a/src/metrics/cognitive.rs b/src/metrics/cognitive.rs index 13ff2f181..1e61051ec 100644 --- a/src/metrics/cognitive.rs +++ b/src/metrics/cognitive.rs @@ -207,7 +207,14 @@ impl Cognitive for PythonCode { [IfStatement | ForStatement | WhileStatement | ExceptClause => FunctionDefinition] ); } - ElifClause | ElseClause | FinallyClause => { + ElifClause => { + // No nesting increment for them because their cost has already + // been paid by the if construct + increment_by_one(stats); + // Reset the boolean sequence + stats.boolean_seq.reset(); + } + ElseClause | FinallyClause => { // No nesting increment for them because their cost has already // been paid by the if construct increment_by_one(stats); @@ -215,7 +222,7 @@ impl Cognitive for PythonCode { ExceptClause => { increment(stats); } - ExpressionList => { + ExpressionList | ExpressionStatement | Tuple => { stats.boolean_seq.reset(); } NotOperator => { @@ -477,6 +484,74 @@ mod tests { ); } + #[test] + fn python_expression_statement() { + // Boolean expressions containing `And` and `Or` operators were not + // considered in assignments + check_metrics!( + "def f(a, b): + c = True and True", + "foo.py", + PythonParser, + cognitive, + [(cognitive, 1, usize)], + [(cognitive_average, 1.0)] + ); + } + + #[test] + fn python_tuple() { + // Boolean expressions containing `And` and `Or` operators were not + // considered inside tuples + check_metrics!( + "def f(a, b): + return \"%s%s\" % (a and \"Get\" or \"Set\", b)", + "foo.py", + PythonParser, + cognitive, + [(cognitive, 2, usize)], + [(cognitive_average, 2.0)] + ); + } + + #[test] + fn python_elif_function() { + // Boolean expressions containing `And` and `Or` operators were not + // considered in `elif` statements + check_metrics!( + "def f(a, b): + if a and b: # +2 (+1 and) + return 1 + elif c and d: # +2 (+1 and) + return 1", + "foo.py", + PythonParser, + cognitive, + [(cognitive, 4, usize)], + [(cognitive_average, 4.0)] + ); + } + + #[test] + fn python_more_elifs_function() { + // Boolean expressions containing `And` and `Or` operators were not + // considered when there were more `elif` statements + check_metrics!( + "def f(a, b): + if a and b: # +2 (+1 and) + return 1 + elif c and d: # +2 (+1 and) + return 1 + elif e and f: # +2 (+1 and) + return 1", + "foo.py", + PythonParser, + cognitive, + [(cognitive, 6, usize)], + [(cognitive_average, 6.0)] + ); + } + #[test] fn rust_simple_function() { check_metrics!( diff --git a/src/metrics/loc.rs b/src/metrics/loc.rs index 6eb2d19fc..5ec368998 100644 --- a/src/metrics/loc.rs +++ b/src/metrics/loc.rs @@ -157,6 +157,8 @@ impl Loc for PythonCode { let parent = node.object().parent().unwrap(); if let ExpressionStatement = parent.kind_id().into() { stats.comment_lines += (end - start) + 1; + } else if parent.start_position().row != start { + stats.lines.insert(start); } } Statement @@ -541,6 +543,25 @@ mod tests { ); } + #[test] + fn python_string_on_new_line() { + // More lines of the same instruction were counted as blank lines + check_metrics!( + "capabilities[\"goog:chromeOptions\"][\"androidPackage\"] = \\ + \"org.chromium.weblayer.shell\"", + "foo.py", + PythonParser, + loc, + [ + (sloc, 2, usize), + (ploc, 2, usize), + (lloc, 1, usize), + (cloc, 0, usize), + (blank, 0, usize) + ] + ); + } + #[test] fn python_general_loc() { check_metrics!( diff --git a/tree-sitter-python b/tree-sitter-python index 58f572408..10eaa00fe 160000 --- a/tree-sitter-python +++ b/tree-sitter-python @@ -1 +1 @@ -Subproject commit 58f57240834d6b88624e32ad0ab9531d55fb7a5d +Subproject commit 10eaa00fecfd1ef7a92e7b839a3f4067844c1e1e