tree-sitter-python
Advanced tools
Comparing version 0.20.1 to 0.20.3
504
grammar.js
@@ -0,1 +1,15 @@ | ||
/** | ||
* @file Python grammar for tree-sitter | ||
* @author Max Brunsfeld <maxbrunsfeld@gmail.com> | ||
* @license MIT | ||
* @see {@link https://docs.python.org/2/reference/grammar.html|Python 2 grammar} | ||
* @see {@link https://docs.python.org/3/reference/grammar.html|Python 3 grammar} | ||
*/ | ||
/* eslint-disable arrow-parens */ | ||
/* eslint-disable camelcase */ | ||
/* eslint-disable-next-line spaced-comment */ | ||
/// <reference types="tree-sitter-cli/dsl" /> | ||
// @ts-check | ||
const PREC = { | ||
@@ -10,18 +24,18 @@ // this resolves a conflict between the usage of ':' in a lambda vs in a | ||
parenthesized_list_splat: 1, | ||
or: 10, | ||
and: 11, | ||
not: 12, | ||
compare: 13, | ||
bitwise_or: 14, | ||
or: 10, | ||
and: 11, | ||
not: 12, | ||
compare: 13, | ||
bitwise_or: 14, | ||
bitwise_and: 15, | ||
xor: 16, | ||
shift: 17, | ||
plus: 18, | ||
times: 19, | ||
unary: 20, | ||
power: 21, | ||
call: 22, | ||
} | ||
xor: 16, | ||
shift: 17, | ||
plus: 18, | ||
times: 19, | ||
unary: 20, | ||
power: 21, | ||
call: 22, | ||
}; | ||
const SEMICOLON = ';' | ||
const SEMICOLON = ';'; | ||
@@ -33,3 +47,4 @@ module.exports = grammar({ | ||
$.comment, | ||
/[\s\f\uFEFF\u2060\u200B]|\\\r?\n/ | ||
/[\s\f\uFEFF\u2060\u200B]|\r?\n/, | ||
$.line_continuation, | ||
], | ||
@@ -45,2 +60,3 @@ | ||
[$.match_statement, $.primary_expression], | ||
[$.print_statement, $.primary_expression], | ||
], | ||
@@ -61,5 +77,18 @@ | ||
$._dedent, | ||
$._string_start, | ||
$.string_start, | ||
$._string_content, | ||
$._string_end, | ||
$.string_end, | ||
// Mark comments as external tokens so that the external scanner is always | ||
// invoked, even if no external token is expected. This allows for better | ||
// error recovery, because the external scanner can maintain the overall | ||
// structure by returning dedent tokens whenever a dedent occurs, even | ||
// if no dedent is expected. | ||
$.comment, | ||
// Allow the external scanner to check for the validity of closing brackets | ||
// so that it can avoid returning dedent tokens between brackets. | ||
']', | ||
')', | ||
'}', | ||
], | ||
@@ -83,3 +112,3 @@ | ||
$._simple_statements, | ||
$._compound_statement | ||
$._compound_statement, | ||
), | ||
@@ -92,3 +121,3 @@ | ||
optional(SEMICOLON), | ||
$._newline | ||
$._newline, | ||
), | ||
@@ -111,3 +140,3 @@ | ||
$.nonlocal_statement, | ||
$.exec_statement | ||
$.exec_statement, | ||
), | ||
@@ -117,10 +146,10 @@ | ||
'import', | ||
$._import_list | ||
$._import_list, | ||
), | ||
import_prefix: $ => repeat1('.'), | ||
import_prefix: _ => repeat1('.'), | ||
relative_import: $ => seq( | ||
$.import_prefix, | ||
optional($.dotted_name) | ||
optional($.dotted_name), | ||
), | ||
@@ -135,3 +164,3 @@ | ||
seq('(', $._import_list, ')'), | ||
) | ||
), | ||
), | ||
@@ -143,3 +172,3 @@ | ||
$.relative_import, | ||
$.dotted_name | ||
$.dotted_name, | ||
)), | ||
@@ -150,4 +179,4 @@ 'import', | ||
$._import_list, | ||
seq('(', $._import_list, ')') | ||
) | ||
seq('(', $._import_list, ')'), | ||
), | ||
), | ||
@@ -158,5 +187,5 @@ | ||
$.dotted_name, | ||
$.aliased_import | ||
$.aliased_import, | ||
))), | ||
optional(',') | ||
optional(','), | ||
), | ||
@@ -167,6 +196,6 @@ | ||
'as', | ||
field('alias', $.identifier) | ||
field('alias', $.identifier), | ||
), | ||
wildcard_import: $ => '*', | ||
wildcard_import: _ => '*', | ||
@@ -178,9 +207,9 @@ print_statement: $ => choice( | ||
repeat(seq(',', field('argument', $.expression))), | ||
optional(',')) | ||
optional(',')), | ||
), | ||
prec(-10, seq( | ||
prec(-3, prec.dynamic(-1, seq( | ||
'print', | ||
commaSep1(field('argument', $.expression)), | ||
optional(',') | ||
)) | ||
optional(','), | ||
))), | ||
), | ||
@@ -190,3 +219,3 @@ | ||
'>>', | ||
$.expression | ||
$.expression, | ||
), | ||
@@ -196,3 +225,3 @@ | ||
'assert', | ||
commaSep1($.expression) | ||
commaSep1($.expression), | ||
), | ||
@@ -205,14 +234,14 @@ | ||
$.augmented_assignment, | ||
$.yield | ||
$.yield, | ||
), | ||
named_expression: $ => seq( | ||
field('name', $._named_expresssion_lhs), | ||
field('name', $._named_expression_lhs), | ||
':=', | ||
field('value', $.expression) | ||
field('value', $.expression), | ||
), | ||
_named_expresssion_lhs: $ => choice( | ||
_named_expression_lhs: $ => choice( | ||
$.identifier, | ||
alias('match', $.identifier), // ambiguity with match statement: only ":" at end of line decides if "match" keyword | ||
$.keyword_identifier, | ||
), | ||
@@ -222,3 +251,3 @@ | ||
'return', | ||
optional($._expressions) | ||
optional($._expressions), | ||
), | ||
@@ -228,3 +257,3 @@ | ||
'del', | ||
$._expressions | ||
$._expressions, | ||
), | ||
@@ -234,3 +263,3 @@ | ||
$.expression, | ||
$.expression_list | ||
$.expression_list, | ||
), | ||
@@ -241,8 +270,8 @@ | ||
optional($._expressions), | ||
optional(seq('from', field('cause', $.expression))) | ||
optional(seq('from', field('cause', $.expression))), | ||
), | ||
pass_statement: $ => prec.left('pass'), | ||
break_statement: $ => prec.left('break'), | ||
continue_statement: $ => prec.left('continue'), | ||
pass_statement: _ => prec.left('pass'), | ||
break_statement: _ => prec.left('break'), | ||
continue_statement: _ => prec.left('continue'), | ||
@@ -269,3 +298,3 @@ // Compound statements | ||
repeat(field('alternative', $.elif_clause)), | ||
optional(field('alternative', $.else_clause)) | ||
optional(field('alternative', $.else_clause)), | ||
), | ||
@@ -277,3 +306,3 @@ | ||
':', | ||
field('consequence', $._suite) | ||
field('consequence', $._suite), | ||
), | ||
@@ -284,3 +313,3 @@ | ||
':', | ||
field('body', $._suite) | ||
field('body', $._suite), | ||
), | ||
@@ -293,4 +322,14 @@ | ||
':', | ||
repeat(field('alternative', $.case_clause))), | ||
field('body', alias($._match_block, $.block)), | ||
), | ||
_match_block: $ => choice( | ||
seq( | ||
$._indent, | ||
repeat(field('alternative', $.case_clause)), | ||
$._dedent, | ||
), | ||
$._newline, | ||
), | ||
case_clause: $ => seq( | ||
@@ -301,4 +340,4 @@ 'case', | ||
'pattern', | ||
alias(choice($.expression, $.list_splat_pattern), $.case_pattern), | ||
) | ||
alias($.expression, $.case_pattern), | ||
), | ||
), | ||
@@ -308,3 +347,3 @@ optional(','), | ||
':', | ||
field('consequence', $._suite) | ||
field('consequence', $._suite), | ||
), | ||
@@ -320,3 +359,3 @@ | ||
field('body', $._suite), | ||
field('alternative', optional($.else_clause)) | ||
field('alternative', optional($.else_clause)), | ||
), | ||
@@ -329,3 +368,3 @@ | ||
field('body', $._suite), | ||
optional(field('alternative', $.else_clause)) | ||
optional(field('alternative', $.else_clause)), | ||
), | ||
@@ -341,6 +380,11 @@ | ||
optional($.else_clause), | ||
optional($.finally_clause) | ||
optional($.finally_clause), | ||
), | ||
$.finally_clause | ||
) | ||
seq( | ||
repeat1($.except_group_clause), | ||
optional($.else_clause), | ||
optional($.finally_clause), | ||
), | ||
$.finally_clause, | ||
), | ||
), | ||
@@ -354,13 +398,26 @@ | ||
choice('as', ','), | ||
$.expression | ||
)) | ||
$.expression, | ||
)), | ||
)), | ||
':', | ||
$._suite | ||
$._suite, | ||
), | ||
except_group_clause: $ => seq( | ||
'except*', | ||
seq( | ||
$.expression, | ||
optional(seq( | ||
'as', | ||
$.expression, | ||
)), | ||
), | ||
':', | ||
$._suite, | ||
), | ||
finally_clause: $ => seq( | ||
'finally', | ||
':', | ||
$._suite | ||
$._suite, | ||
), | ||
@@ -373,11 +430,11 @@ | ||
':', | ||
field('body', $._suite) | ||
field('body', $._suite), | ||
), | ||
with_clause: $ => choice( | ||
commaSep1($.with_item), | ||
seq('(', commaSep1($.with_item), ')') | ||
seq(commaSep1($.with_item), optional(',')), | ||
seq('(', commaSep1($.with_item), optional(','), ')'), | ||
), | ||
with_item: $ => prec.dynamic(-1, seq( | ||
with_item: $ => prec.dynamic(1, seq( | ||
field('value', $.expression), | ||
@@ -394,7 +451,7 @@ )), | ||
'->', | ||
field('return_type', $.type) | ||
) | ||
field('return_type', $.type), | ||
), | ||
), | ||
':', | ||
field('body', $._suite) | ||
field('body', $._suite), | ||
), | ||
@@ -405,3 +462,3 @@ | ||
optional($._parameters), | ||
')' | ||
')', | ||
), | ||
@@ -418,3 +475,3 @@ | ||
'**', | ||
$.expression | ||
$.expression, | ||
), | ||
@@ -424,3 +481,3 @@ | ||
'global', | ||
commaSep1($.identifier) | ||
commaSep1($.identifier), | ||
), | ||
@@ -430,3 +487,3 @@ | ||
'nonlocal', | ||
commaSep1($.identifier) | ||
commaSep1($.identifier), | ||
), | ||
@@ -436,9 +493,9 @@ | ||
'exec', | ||
field('code', $.string), | ||
field('code', choice($.string, $.identifier)), | ||
optional( | ||
seq( | ||
'in', | ||
commaSep1($.expression) | ||
) | ||
) | ||
commaSep1($.expression), | ||
), | ||
), | ||
), | ||
@@ -451,3 +508,3 @@ | ||
':', | ||
field('body', $._suite) | ||
field('body', $._suite), | ||
), | ||
@@ -472,7 +529,7 @@ | ||
alias($.parenthesized_list_splat, $.parenthesized_expression), | ||
$.keyword_argument | ||
) | ||
$.keyword_argument, | ||
), | ||
)), | ||
optional(','), | ||
')' | ||
')', | ||
), | ||
@@ -484,4 +541,4 @@ | ||
$.class_definition, | ||
$.function_definition | ||
)) | ||
$.function_definition, | ||
)), | ||
), | ||
@@ -491,4 +548,4 @@ | ||
'@', | ||
$.primary_expression, | ||
$._newline | ||
$.expression, | ||
$._newline, | ||
), | ||
@@ -499,3 +556,3 @@ | ||
seq($._indent, $.block), | ||
alias($._newline, $.block) | ||
alias($._newline, $.block), | ||
), | ||
@@ -505,3 +562,3 @@ | ||
repeat($._statement), | ||
$._dedent | ||
$._dedent, | ||
), | ||
@@ -516,7 +573,7 @@ | ||
',', | ||
$.expression | ||
$.expression, | ||
)), | ||
optional(',') | ||
optional(','), | ||
), | ||
) | ||
), | ||
)), | ||
@@ -530,3 +587,3 @@ | ||
commaSep1($.parameter), | ||
optional(',') | ||
optional(','), | ||
), | ||
@@ -536,3 +593,3 @@ | ||
commaSep1($.pattern), | ||
optional(',') | ||
optional(','), | ||
), | ||
@@ -549,3 +606,3 @@ | ||
$.positional_separator, | ||
$.dictionary_splat_pattern | ||
$.dictionary_splat_pattern, | ||
), | ||
@@ -555,3 +612,2 @@ | ||
$.identifier, | ||
alias('match', $.identifier), // ambiguity with match statement: only ":" at end of line decides if "match" keyword | ||
$.keyword_identifier, | ||
@@ -562,3 +618,3 @@ $.subscript, | ||
$.tuple_pattern, | ||
$.list_pattern | ||
$.list_pattern, | ||
), | ||
@@ -569,3 +625,3 @@ | ||
optional($._patterns), | ||
')' | ||
')', | ||
), | ||
@@ -576,9 +632,9 @@ | ||
optional($._patterns), | ||
']' | ||
']', | ||
), | ||
default_parameter: $ => seq( | ||
field('name', $.identifier), | ||
field('name', choice($.identifier, $.tuple_pattern)), | ||
'=', | ||
field('value', $.expression) | ||
field('value', $.expression), | ||
), | ||
@@ -591,3 +647,3 @@ | ||
'=', | ||
field('value', $.expression) | ||
field('value', $.expression), | ||
)), | ||
@@ -597,3 +653,3 @@ | ||
'*', | ||
choice($.identifier, $.keyword_identifier, $.subscript, $.attribute) | ||
choice($.identifier, $.keyword_identifier, $.subscript, $.attribute), | ||
), | ||
@@ -603,3 +659,3 @@ | ||
'**', | ||
choice($.identifier, $.keyword_identifier, $.subscript, $.attribute) | ||
choice($.identifier, $.keyword_identifier, $.subscript, $.attribute), | ||
), | ||
@@ -612,3 +668,3 @@ | ||
'as', | ||
field('alias', alias($.expression, $.as_pattern_target)) | ||
field('alias', alias($.expression, $.as_pattern_target)), | ||
)), | ||
@@ -620,3 +676,3 @@ | ||
$.expression, | ||
alias($.lambda_within_for_in_clause, $.lambda) | ||
alias($.lambda_within_for_in_clause, $.lambda), | ||
), | ||
@@ -628,3 +684,2 @@ | ||
$.boolean_operator, | ||
$.await, | ||
$.lambda, | ||
@@ -634,9 +689,9 @@ $.primary_expression, | ||
$.named_expression, | ||
$.as_pattern | ||
$.as_pattern, | ||
), | ||
primary_expression: $ => choice( | ||
$.await, | ||
$.binary_operator, | ||
$.identifier, | ||
alias("match", $.identifier), | ||
$.keyword_identifier, | ||
@@ -663,3 +718,4 @@ $.string, | ||
$.generator_expression, | ||
$.ellipsis | ||
$.ellipsis, | ||
alias($.list_splat_pattern, $.list_splat), | ||
), | ||
@@ -669,3 +725,3 @@ | ||
'not', | ||
field('argument', $.expression) | ||
field('argument', $.expression), | ||
)), | ||
@@ -677,3 +733,3 @@ | ||
field('operator', 'and'), | ||
field('right', $.expression) | ||
field('right', $.expression), | ||
)), | ||
@@ -683,4 +739,4 @@ prec.left(PREC.or, seq( | ||
field('operator', 'or'), | ||
field('right', $.expression) | ||
)) | ||
field('right', $.expression), | ||
)), | ||
), | ||
@@ -705,6 +761,8 @@ | ||
// @ts-ignore | ||
return choice(...table.map(([fn, operator, precedence]) => fn(precedence, seq( | ||
field('left', $.primary_expression), | ||
// @ts-ignore | ||
field('operator', operator), | ||
field('right', $.primary_expression) | ||
field('right', $.primary_expression), | ||
)))); | ||
@@ -715,3 +773,3 @@ }, | ||
field('operator', choice('+', '-', '~')), | ||
field('argument', $.primary_expression) | ||
field('argument', $.primary_expression), | ||
)), | ||
@@ -732,8 +790,8 @@ | ||
'in', | ||
seq('not', 'in'), | ||
alias(seq('not', 'in'), 'not in'), | ||
'is', | ||
seq('is', 'not') | ||
alias(seq('is', 'not'), 'is not'), | ||
)), | ||
$.primary_expression | ||
)) | ||
$.primary_expression, | ||
)), | ||
)), | ||
@@ -745,3 +803,3 @@ | ||
':', | ||
field('body', $.expression) | ||
field('body', $.expression), | ||
)), | ||
@@ -753,3 +811,3 @@ | ||
':', | ||
field('body', $._expression_within_for_in_clause) | ||
field('body', $._expression_within_for_in_clause), | ||
), | ||
@@ -762,4 +820,4 @@ | ||
seq(':', field('type', $.type)), | ||
seq(':', field('type', $.type), '=', field('right', $._right_hand_side)) | ||
) | ||
seq(':', field('type', $.type), '=', field('right', $._right_hand_side)), | ||
), | ||
), | ||
@@ -771,5 +829,5 @@ | ||
'+=', '-=', '*=', '/=', '@=', '//=', '%=', '**=', | ||
'>>=', '<<=', '&=', '^=', '|=' | ||
'>>=', '<<=', '&=', '^=', '|=', | ||
)), | ||
field('right', $._right_hand_side) | ||
field('right', $._right_hand_side), | ||
), | ||
@@ -789,7 +847,7 @@ | ||
',', | ||
$.pattern | ||
$.pattern, | ||
)), | ||
optional(',') | ||
) | ||
) | ||
optional(','), | ||
), | ||
), | ||
), | ||
@@ -802,3 +860,4 @@ | ||
$.augmented_assignment, | ||
$.yield | ||
$.pattern_list, | ||
$.yield, | ||
), | ||
@@ -811,6 +870,6 @@ | ||
'from', | ||
$.expression | ||
$.expression, | ||
), | ||
optional($._expressions) | ||
) | ||
optional($._expressions), | ||
), | ||
)), | ||
@@ -821,3 +880,3 @@ | ||
'.', | ||
field('attribute', $.identifier) | ||
field('attribute', $.identifier), | ||
)), | ||
@@ -830,3 +889,3 @@ | ||
optional(','), | ||
']' | ||
']', | ||
)), | ||
@@ -838,6 +897,6 @@ | ||
optional($.expression), | ||
optional(seq(':', optional($.expression))) | ||
optional(seq(':', optional($.expression))), | ||
), | ||
ellipsis: $ => '...', | ||
ellipsis: _ => '...', | ||
@@ -848,4 +907,4 @@ call: $ => prec(PREC.call, seq( | ||
$.generator_expression, | ||
$.argument_list | ||
)) | ||
$.argument_list, | ||
)), | ||
)), | ||
@@ -857,6 +916,6 @@ | ||
$.list_splat_pattern, | ||
$.dictionary_splat_pattern | ||
$.dictionary_splat_pattern, | ||
), | ||
':', | ||
field('type', $.type) | ||
field('type', $.type), | ||
)), | ||
@@ -867,5 +926,5 @@ | ||
keyword_argument: $ => seq( | ||
field('name', choice($.identifier, $.keyword_identifier, alias("match", $.identifier))), | ||
field('name', choice($.identifier, $.keyword_identifier)), | ||
'=', | ||
field('value', $.expression) | ||
field('value', $.expression), | ||
), | ||
@@ -878,3 +937,3 @@ | ||
optional($._collection_elements), | ||
']' | ||
']', | ||
), | ||
@@ -885,3 +944,3 @@ | ||
$._collection_elements, | ||
'}' | ||
'}', | ||
), | ||
@@ -892,3 +951,3 @@ | ||
optional($._collection_elements), | ||
')' | ||
')', | ||
), | ||
@@ -900,3 +959,3 @@ | ||
optional(','), | ||
'}' | ||
'}', | ||
), | ||
@@ -907,3 +966,3 @@ | ||
':', | ||
field('value', $.expression) | ||
field('value', $.expression), | ||
), | ||
@@ -915,3 +974,3 @@ | ||
$._comprehension_clauses, | ||
']' | ||
']', | ||
), | ||
@@ -923,3 +982,3 @@ | ||
$._comprehension_clauses, | ||
'}' | ||
'}', | ||
), | ||
@@ -931,3 +990,3 @@ | ||
$._comprehension_clauses, | ||
'}' | ||
'}', | ||
), | ||
@@ -939,3 +998,3 @@ | ||
$._comprehension_clauses, | ||
')' | ||
')', | ||
), | ||
@@ -947,4 +1006,4 @@ | ||
$.for_in_clause, | ||
$.if_clause | ||
)) | ||
$.if_clause, | ||
)), | ||
), | ||
@@ -955,3 +1014,3 @@ | ||
choice($.expression, $.yield), | ||
')' | ||
')', | ||
)), | ||
@@ -961,5 +1020,5 @@ | ||
commaSep1(choice( | ||
$.expression, $.yield, $.list_splat, $.parenthesized_list_splat | ||
$.expression, $.yield, $.list_splat, $.parenthesized_list_splat, | ||
)), | ||
optional(',') | ||
optional(','), | ||
), | ||
@@ -973,3 +1032,3 @@ | ||
field('right', commaSep1($._expression_within_for_in_clause)), | ||
optional(',') | ||
optional(','), | ||
)), | ||
@@ -979,3 +1038,3 @@ | ||
'if', | ||
$.expression | ||
$.expression, | ||
), | ||
@@ -988,3 +1047,3 @@ | ||
'else', | ||
$.expression | ||
$.expression, | ||
)), | ||
@@ -994,23 +1053,38 @@ | ||
$.string, | ||
repeat1($.string) | ||
repeat1($.string), | ||
), | ||
string: $ => seq( | ||
alias($._string_start, '"'), | ||
repeat(choice($.interpolation, $._escape_interpolation, $.escape_sequence, $._not_escape_sequence, $._string_content)), | ||
alias($._string_end, '"') | ||
$.string_start, | ||
repeat(choice($.interpolation, $.string_content)), | ||
$.string_end, | ||
), | ||
string_content: $ => prec.right(repeat1( | ||
choice( | ||
$._escape_interpolation, | ||
$.escape_sequence, | ||
$._not_escape_sequence, | ||
$._string_content, | ||
))), | ||
interpolation: $ => seq( | ||
'{', | ||
$.expression, | ||
field('expression', $._f_expression), | ||
optional('='), | ||
optional($.type_conversion), | ||
optional($.format_specifier), | ||
'}' | ||
optional(field('type_conversion', $.type_conversion)), | ||
optional(field('format_specifier', $.format_specifier)), | ||
'}', | ||
), | ||
_escape_interpolation: $ => choice('{{', '}}'), | ||
_f_expression: $ => choice( | ||
$.expression, | ||
$.expression_list, | ||
$.pattern_list, | ||
$.yield, | ||
), | ||
escape_sequence: $ => token(prec(1, seq( | ||
_escape_interpolation: _ => token.immediate(choice('{{', '}}')), | ||
escape_sequence: _ => token.immediate(prec(1, seq( | ||
'\\', | ||
@@ -1024,6 +1098,7 @@ choice( | ||
/['"abfrntv\\]/, | ||
) | ||
/N\{[^}]+\}/, | ||
), | ||
))), | ||
_not_escape_sequence: $ => '\\', | ||
_not_escape_sequence: _ => token.immediate('\\'), | ||
@@ -1034,15 +1109,13 @@ format_specifier: $ => seq( | ||
token(prec(1, /[^{}\n]+/)), | ||
$.format_expression | ||
)) | ||
alias($.interpolation, $.format_expression), | ||
)), | ||
), | ||
format_expression: $ => seq('{', $.expression, '}'), | ||
type_conversion: _ => /![a-z]/, | ||
type_conversion: $ => /![a-z]/, | ||
integer: $ => token(choice( | ||
integer: _ => token(choice( | ||
seq( | ||
choice('0x', '0X'), | ||
repeat1(/_?[A-Fa-f0-9]+/), | ||
optional(/[Ll]/) | ||
optional(/[Ll]/), | ||
), | ||
@@ -1052,3 +1125,3 @@ seq( | ||
repeat1(/_?[0-7]+/), | ||
optional(/[Ll]/) | ||
optional(/[Ll]/), | ||
), | ||
@@ -1058,3 +1131,3 @@ seq( | ||
repeat1(/_?[0-1]+/), | ||
optional(/[Ll]/) | ||
optional(/[Ll]/), | ||
), | ||
@@ -1065,10 +1138,10 @@ seq( | ||
optional(/[Ll]/), // long numbers | ||
optional(/[jJ]/) // complex numbers | ||
) | ||
) | ||
optional(/[jJ]/), // complex numbers | ||
), | ||
), | ||
)), | ||
float: $ => { | ||
float: _ => { | ||
const digits = repeat1(/[0-9]+_?/); | ||
const exponent = seq(/[eE][\+-]?/, digits) | ||
const exponent = seq(/[eE][\+-]?/, digits); | ||
@@ -1079,9 +1152,9 @@ return token(seq( | ||
seq(optional(digits), '.', digits, optional(exponent)), | ||
seq(digits, exponent) | ||
seq(digits, exponent), | ||
), | ||
optional(choice(/[Ll]/, /[jJ]/)) | ||
)) | ||
optional(choice(/[Ll]/, /[jJ]/)), | ||
)); | ||
}, | ||
identifier: $ => /[_\p{XID_Start}][_\p{XID_Continue}]*/, | ||
identifier: _ => /[_\p{XID_Start}][_\p{XID_Continue}]*/, | ||
@@ -1094,28 +1167,51 @@ keyword_identifier: $ => prec(-3, alias( | ||
'await', | ||
'match', | ||
), | ||
$.identifier | ||
$.identifier, | ||
)), | ||
true: $ => 'True', | ||
false: $ => 'False', | ||
none: $ => 'None', | ||
true: _ => 'True', | ||
false: _ => 'False', | ||
none: _ => 'None', | ||
await: $ => prec(PREC.unary, seq( | ||
'await', | ||
$.expression | ||
$.primary_expression, | ||
)), | ||
comment: $ => token(seq('#', /.*/)), | ||
comment: _ => token(seq('#', /.*/)), | ||
positional_separator: $ => '/', | ||
keyword_separator: $ => '*', | ||
} | ||
}) | ||
line_continuation: _ => token(seq('\\', choice(seq(optional('\r'), '\n'), '\0'))), | ||
positional_separator: _ => '/', | ||
keyword_separator: _ => '*', | ||
}, | ||
}); | ||
module.exports.PREC = PREC; | ||
/** | ||
* Creates a rule to match one or more of the rules separated by a comma | ||
* | ||
* @param {RegExp|Rule|String} rule | ||
* | ||
* @return {SeqRule} | ||
* | ||
*/ | ||
function commaSep1(rule) { | ||
return sep1(rule, ',') | ||
return sep1(rule, ','); | ||
} | ||
/** | ||
* Creates a rule to match one or more occurrences of `rule` separated by `sep` | ||
* | ||
* @param {RegExp|Rule|String} rule | ||
* | ||
* @param {RegExp|Rule|String} separator | ||
* | ||
* @return {SeqRule} | ||
* | ||
*/ | ||
function sep1(rule, separator) { | ||
return seq(rule, repeat(seq(separator, rule))) | ||
return seq(rule, repeat(seq(separator, rule))); | ||
} |
{ | ||
"name": "tree-sitter-python", | ||
"version": "0.20.1", | ||
"version": "0.20.3", | ||
"description": "Python grammar for tree-sitter", | ||
@@ -16,2 +16,4 @@ "main": "bindings/node", | ||
"devDependencies": { | ||
"eslint": "^8.45.0", | ||
"eslint-config-google": "^0.14.0", | ||
"tree-sitter-cli": "^0.20.1" | ||
@@ -21,4 +23,5 @@ }, | ||
"build": "tree-sitter generate && node-gyp build", | ||
"lint": "eslint grammar.js", | ||
"parse": "tree-sitter parse", | ||
"test": "tree-sitter test && script/parse-examples", | ||
"parse": "tree-sitter parse", | ||
"test-windows": "tree-sitter test" | ||
@@ -25,0 +28,0 @@ }, |
@@ -1,3 +0,2 @@ | ||
tree-sitter-python | ||
================== | ||
# tree-sitter-python | ||
@@ -10,5 +9,5 @@ [![build](https://github.com/tree-sitter/tree-sitter-python/actions/workflows/ci.yml/badge.svg)](https://github.com/tree-sitter/tree-sitter-python/actions/workflows/ci.yml) | ||
#### References | ||
## References | ||
* [Python 2 Grammar](https://docs.python.org/2/reference/grammar.html) | ||
* [Python 3 Grammar](https://docs.python.org/3/reference/grammar.html) | ||
- [Python 2 Grammar](https://docs.python.org/2/reference/grammar.html) | ||
- [Python 3 Grammar](https://docs.python.org/3/reference/grammar.html) |
@@ -119,6 +119,2 @@ [ | ||
{ | ||
"type": "await", | ||
"named": true | ||
}, | ||
{ | ||
"type": "boolean_operator", | ||
@@ -234,2 +230,6 @@ "named": true | ||
{ | ||
"type": "await", | ||
"named": true | ||
}, | ||
{ | ||
"type": "binary_operator", | ||
@@ -287,2 +287,6 @@ "named": true | ||
{ | ||
"type": "list_splat", | ||
"named": true | ||
}, | ||
{ | ||
"type": "none", | ||
@@ -462,2 +466,6 @@ "named": true | ||
{ | ||
"type": "pattern_list", | ||
"named": true | ||
}, | ||
{ | ||
"type": "yield", | ||
@@ -603,2 +611,6 @@ "named": true | ||
{ | ||
"type": "pattern_list", | ||
"named": true | ||
}, | ||
{ | ||
"type": "yield", | ||
@@ -620,3 +632,3 @@ "named": true | ||
{ | ||
"type": "expression", | ||
"type": "primary_expression", | ||
"named": true | ||
@@ -714,3 +726,14 @@ } | ||
"named": true, | ||
"fields": {}, | ||
"fields": { | ||
"alternative": { | ||
"multiple": true, | ||
"required": false, | ||
"types": [ | ||
{ | ||
"type": "case_clause", | ||
"named": true | ||
} | ||
] | ||
} | ||
}, | ||
"children": { | ||
@@ -843,25 +866,2 @@ "multiple": true, | ||
{ | ||
"type": "case_pattern", | ||
"named": true, | ||
"fields": {}, | ||
"children": { | ||
"multiple": false, | ||
"required": true, | ||
"types": [ | ||
{ | ||
"type": "attribute", | ||
"named": true | ||
}, | ||
{ | ||
"type": "identifier", | ||
"named": true | ||
}, | ||
{ | ||
"type": "subscript", | ||
"named": true | ||
} | ||
] | ||
} | ||
}, | ||
{ | ||
"type": "chevron", | ||
@@ -962,4 +962,8 @@ "named": true, | ||
{ | ||
"type": "not", | ||
"type": "is not", | ||
"named": false | ||
}, | ||
{ | ||
"type": "not in", | ||
"named": false | ||
} | ||
@@ -1054,3 +1058,3 @@ ] | ||
{ | ||
"type": "primary_expression", | ||
"type": "expression", | ||
"named": true | ||
@@ -1072,2 +1076,6 @@ } | ||
"named": true | ||
}, | ||
{ | ||
"type": "tuple_pattern", | ||
"named": true | ||
} | ||
@@ -1271,2 +1279,21 @@ ] | ||
{ | ||
"type": "except_group_clause", | ||
"named": true, | ||
"fields": {}, | ||
"children": { | ||
"multiple": true, | ||
"required": true, | ||
"types": [ | ||
{ | ||
"type": "block", | ||
"named": true | ||
}, | ||
{ | ||
"type": "expression", | ||
"named": true | ||
} | ||
] | ||
} | ||
}, | ||
{ | ||
"type": "exec_statement", | ||
@@ -1280,2 +1307,6 @@ "named": true, | ||
{ | ||
"type": "identifier", | ||
"named": true | ||
}, | ||
{ | ||
"type": "string", | ||
@@ -1446,12 +1477,45 @@ "named": true | ||
"named": true, | ||
"fields": {}, | ||
"children": { | ||
"multiple": false, | ||
"required": true, | ||
"types": [ | ||
{ | ||
"type": "expression", | ||
"named": true | ||
} | ||
] | ||
"fields": { | ||
"expression": { | ||
"multiple": false, | ||
"required": true, | ||
"types": [ | ||
{ | ||
"type": "expression", | ||
"named": true | ||
}, | ||
{ | ||
"type": "expression_list", | ||
"named": true | ||
}, | ||
{ | ||
"type": "pattern_list", | ||
"named": true | ||
}, | ||
{ | ||
"type": "yield", | ||
"named": true | ||
} | ||
] | ||
}, | ||
"format_specifier": { | ||
"multiple": false, | ||
"required": false, | ||
"types": [ | ||
{ | ||
"type": "format_specifier", | ||
"named": true | ||
} | ||
] | ||
}, | ||
"type_conversion": { | ||
"multiple": false, | ||
"required": false, | ||
"types": [ | ||
{ | ||
"type": "type_conversion", | ||
"named": true | ||
} | ||
] | ||
} | ||
} | ||
@@ -1712,20 +1776,45 @@ }, | ||
"named": true, | ||
"fields": {}, | ||
"children": { | ||
"multiple": true, | ||
"required": true, | ||
"types": [ | ||
{ | ||
"type": "expression", | ||
"named": true | ||
}, | ||
{ | ||
"type": "format_specifier", | ||
"named": true | ||
}, | ||
{ | ||
"type": "type_conversion", | ||
"named": true | ||
} | ||
] | ||
"fields": { | ||
"expression": { | ||
"multiple": false, | ||
"required": true, | ||
"types": [ | ||
{ | ||
"type": "expression", | ||
"named": true | ||
}, | ||
{ | ||
"type": "expression_list", | ||
"named": true | ||
}, | ||
{ | ||
"type": "pattern_list", | ||
"named": true | ||
}, | ||
{ | ||
"type": "yield", | ||
"named": true | ||
} | ||
] | ||
}, | ||
"format_specifier": { | ||
"multiple": false, | ||
"required": false, | ||
"types": [ | ||
{ | ||
"type": "format_specifier", | ||
"named": true | ||
} | ||
] | ||
}, | ||
"type_conversion": { | ||
"multiple": false, | ||
"required": false, | ||
"types": [ | ||
{ | ||
"type": "type_conversion", | ||
"named": true | ||
} | ||
] | ||
} | ||
} | ||
@@ -1886,4 +1975,16 @@ }, | ||
{ | ||
"type": "attribute", | ||
"named": true | ||
}, | ||
{ | ||
"type": "expression", | ||
"named": true | ||
}, | ||
{ | ||
"type": "identifier", | ||
"named": true | ||
}, | ||
{ | ||
"type": "subscript", | ||
"named": true | ||
} | ||
@@ -1920,8 +2021,8 @@ ] | ||
"fields": { | ||
"alternative": { | ||
"multiple": true, | ||
"required": false, | ||
"body": { | ||
"multiple": false, | ||
"required": true, | ||
"types": [ | ||
{ | ||
"type": "case_clause", | ||
"type": "block", | ||
"named": true | ||
@@ -2303,11 +2404,19 @@ } | ||
"multiple": true, | ||
"required": false, | ||
"required": true, | ||
"types": [ | ||
{ | ||
"type": "escape_sequence", | ||
"type": "interpolation", | ||
"named": true | ||
}, | ||
{ | ||
"type": "interpolation", | ||
"type": "string_content", | ||
"named": true | ||
}, | ||
{ | ||
"type": "string_end", | ||
"named": true | ||
}, | ||
{ | ||
"type": "string_start", | ||
"named": true | ||
} | ||
@@ -2318,2 +2427,17 @@ ] | ||
{ | ||
"type": "string_content", | ||
"named": true, | ||
"fields": {}, | ||
"children": { | ||
"multiple": true, | ||
"required": false, | ||
"types": [ | ||
{ | ||
"type": "escape_sequence", | ||
"named": true | ||
} | ||
] | ||
} | ||
}, | ||
{ | ||
"type": "subscript", | ||
@@ -2376,2 +2500,6 @@ "named": true, | ||
{ | ||
"type": "except_group_clause", | ||
"named": true | ||
}, | ||
{ | ||
"type": "finally_clause", | ||
@@ -2666,6 +2794,2 @@ "named": true | ||
{ | ||
"type": "\"", | ||
"named": false | ||
}, | ||
{ | ||
"type": "%", | ||
@@ -2907,2 +3031,6 @@ "named": false | ||
{ | ||
"type": "except*", | ||
"named": false | ||
}, | ||
{ | ||
"type": "exec", | ||
@@ -2960,2 +3088,6 @@ "named": false | ||
{ | ||
"type": "is not", | ||
"named": false | ||
}, | ||
{ | ||
"type": "lambda", | ||
@@ -2965,2 +3097,6 @@ "named": false | ||
{ | ||
"type": "line_continuation", | ||
"named": true | ||
}, | ||
{ | ||
"type": "match", | ||
@@ -2982,2 +3118,6 @@ "named": false | ||
{ | ||
"type": "not in", | ||
"named": false | ||
}, | ||
{ | ||
"type": "or", | ||
@@ -3003,2 +3143,10 @@ "named": false | ||
{ | ||
"type": "string_end", | ||
"named": true | ||
}, | ||
{ | ||
"type": "string_start", | ||
"named": true | ||
}, | ||
{ | ||
"type": "true", | ||
@@ -3032,6 +3180,2 @@ "named": true | ||
{ | ||
"type": "{{", | ||
"named": false | ||
}, | ||
{ | ||
"type": "|", | ||
@@ -3049,6 +3193,2 @@ "named": false | ||
{ | ||
"type": "}}", | ||
"named": false | ||
}, | ||
{ | ||
"type": "~", | ||
@@ -3055,0 +3195,0 @@ "named": false |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
3260782
23
9667
3
13