Merge commit 'de221eccf9a221f5b85474a553474a69b4b5784d'

pull/315/head
Wilfred Hughes 2022-07-10 23:24:09 +07:00
commit 36f66fabd8
10 changed files with 70988 additions and 37504 deletions

@ -17,7 +17,7 @@ jobs:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
with:
node-version: 14
node-version: 16
- run: npm install
- run: npm test
test_windows:
@ -26,6 +26,6 @@ jobs:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
with:
node-version: 14
node-version: 16
- run: npm install
- run: npm run-script test-windows

@ -1,7 +1,7 @@
[package]
name = "tree-sitter-python"
description = "Python grammar for the tree-sitter parsing library"
version = "0.19.0"
version = "0.20.2"
authors = [
"Max Brunsfeld <maxbrunsfeld@gmail.com>",
"Douglas Creager <dcreager@dcreager.net>",

@ -7,19 +7,19 @@ const PREC = {
parenthesized_expression: 1,
parenthesized_list_splat: 1,
not: 1,
compare: 2,
or: 10,
and: 11,
bitwise_or: 12,
bitwise_and: 13,
xor: 14,
shift: 15,
plus: 16,
times: 17,
unary: 18,
power: 19,
call: 20,
not: 12,
compare: 13,
bitwise_or: 14,
bitwise_and: 15,
xor: 16,
shift: 17,
plus: 18,
times: 19,
unary: 20,
power: 21,
call: 22,
}
const SEMICOLON = ';'
@ -58,6 +58,19 @@ module.exports = grammar({
$._string_start,
$._string_content,
$._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.
']',
')',
'}',
],
inline: $ => [

@ -1,6 +1,6 @@
{
"name": "tree-sitter-python",
"version": "0.19.0",
"version": "0.20.2",
"description": "Python grammar for tree-sitter",
"main": "bindings/node",
"keywords": [

@ -2633,7 +2633,7 @@
},
"not_operator": {
"type": "PREC",
"value": 1,
"value": 12,
"content": {
"type": "SEQ",
"members": [
@ -2728,7 +2728,7 @@
"members": [
{
"type": "PREC_LEFT",
"value": 16,
"value": 18,
"content": {
"type": "SEQ",
"members": [
@ -2761,7 +2761,7 @@
},
{
"type": "PREC_LEFT",
"value": 16,
"value": 18,
"content": {
"type": "SEQ",
"members": [
@ -2794,7 +2794,7 @@
},
{
"type": "PREC_LEFT",
"value": 17,
"value": 19,
"content": {
"type": "SEQ",
"members": [
@ -2827,7 +2827,7 @@
},
{
"type": "PREC_LEFT",
"value": 17,
"value": 19,
"content": {
"type": "SEQ",
"members": [
@ -2860,7 +2860,7 @@
},
{
"type": "PREC_LEFT",
"value": 17,
"value": 19,
"content": {
"type": "SEQ",
"members": [
@ -2893,7 +2893,7 @@
},
{
"type": "PREC_LEFT",
"value": 17,
"value": 19,
"content": {
"type": "SEQ",
"members": [
@ -2926,7 +2926,7 @@
},
{
"type": "PREC_LEFT",
"value": 17,
"value": 19,
"content": {
"type": "SEQ",
"members": [
@ -2959,7 +2959,7 @@
},
{
"type": "PREC_RIGHT",
"value": 19,
"value": 21,
"content": {
"type": "SEQ",
"members": [
@ -2992,7 +2992,7 @@
},
{
"type": "PREC_LEFT",
"value": 12,
"value": 14,
"content": {
"type": "SEQ",
"members": [
@ -3025,7 +3025,7 @@
},
{
"type": "PREC_LEFT",
"value": 13,
"value": 15,
"content": {
"type": "SEQ",
"members": [
@ -3058,7 +3058,7 @@
},
{
"type": "PREC_LEFT",
"value": 14,
"value": 16,
"content": {
"type": "SEQ",
"members": [
@ -3091,7 +3091,7 @@
},
{
"type": "PREC_LEFT",
"value": 15,
"value": 17,
"content": {
"type": "SEQ",
"members": [
@ -3124,7 +3124,7 @@
},
{
"type": "PREC_LEFT",
"value": 15,
"value": 17,
"content": {
"type": "SEQ",
"members": [
@ -3159,7 +3159,7 @@
},
"unary_operator": {
"type": "PREC",
"value": 18,
"value": 20,
"content": {
"type": "SEQ",
"members": [
@ -3197,7 +3197,7 @@
},
"comparison_operator": {
"type": "PREC_LEFT",
"value": 2,
"value": 13,
"content": {
"type": "SEQ",
"members": [
@ -3666,7 +3666,7 @@
},
"attribute": {
"type": "PREC",
"value": 20,
"value": 22,
"content": {
"type": "SEQ",
"members": [
@ -3695,7 +3695,7 @@
},
"subscript": {
"type": "PREC",
"value": 20,
"value": 22,
"content": {
"type": "SEQ",
"members": [
@ -3849,7 +3849,7 @@
},
"call": {
"type": "PREC",
"value": 20,
"value": 22,
"content": {
"type": "SEQ",
"members": [
@ -5100,7 +5100,7 @@
},
"await": {
"type": "PREC",
"value": 18,
"value": 20,
"content": {
"type": "SEQ",
"members": [
@ -5205,6 +5205,22 @@
{
"type": "SYMBOL",
"name": "_string_end"
},
{
"type": "SYMBOL",
"name": "comment"
},
{
"type": "STRING",
"value": "]"
},
{
"type": "STRING",
"value": ")"
},
{
"type": "STRING",
"value": "}"
}
],
"inline": [

File diff suppressed because it is too large Load Diff

@ -17,6 +17,10 @@ enum TokenType {
STRING_START,
STRING_CONTENT,
STRING_END,
COMMENT,
CLOSE_PAREN,
CLOSE_BRACKET,
CLOSE_BRACE,
};
struct Delimiter {
@ -149,7 +153,10 @@ struct Scanner {
}
bool scan(TSLexer *lexer, const bool *valid_symbols) {
if (valid_symbols[STRING_CONTENT] && !valid_symbols[INDENT] && !delimiter_stack.empty()) {
bool error_recovery_mode = valid_symbols[STRING_CONTENT] && valid_symbols[INDENT];
bool within_brackets = valid_symbols[CLOSE_BRACE] || valid_symbols[CLOSE_PAREN] || valid_symbols[CLOSE_BRACKET];
if (valid_symbols[STRING_CONTENT] && !delimiter_stack.empty() && !error_recovery_mode) {
Delimiter delimiter = delimiter_stack.back();
int32_t end_character = delimiter.end_character();
bool has_content = false;
@ -286,7 +293,7 @@ struct Scanner {
}
if (
valid_symbols[DEDENT] &&
(valid_symbols[DEDENT] || (!valid_symbols[NEWLINE] && !within_brackets)) &&
indent_length < current_indent_length &&
// Wait to create a dedent token until we've consumed any comments
@ -299,7 +306,7 @@ struct Scanner {
}
}
if (valid_symbols[NEWLINE]) {
if (valid_symbols[NEWLINE] && !error_recovery_mode) {
lexer->result_symbol = NEWLINE;
return true;
}

@ -123,6 +123,7 @@ struct TSLanguage {
unsigned (*serialize)(void *, char *);
void (*deserialize)(void *, const char *, unsigned);
} external_scanner;
const TSStateId *primary_state_ids;
};
/*

@ -0,0 +1,27 @@
====================================
An error before a string literal
====================================
def a(b):
c.
"""
d
"""
e
---
(module
(function_definition
(identifier)
(parameters
(identifier))
(ERROR
(identifier))
(block
(expression_statement
(string))
(expression_statement
(identifier)))))

@ -503,6 +503,8 @@ Boolean operators
a or b and c
not d
not a and b or c
a and not b and c
--------------------------------------------------------------------------------
@ -515,6 +517,20 @@ not d
(identifier))))
(expression_statement
(not_operator
(identifier)))
(expression_statement
(boolean_operator
(boolean_operator
(not_operator
(identifier))
(identifier))
(identifier)))
(expression_statement
(boolean_operator
(boolean_operator
(identifier)
(not_operator
(identifier)))
(identifier))))
================================================================================
@ -523,6 +539,8 @@ Comparison operators
a < b <= c == d >= e > f
not a == b or c == d
a not in b
a is not b
--------------------------------------------------------------------------------
@ -536,14 +554,22 @@ not a == b or c == d
(identifier)
(identifier)))
(expression_statement
(not_operator
(boolean_operator
(comparison_operator
(identifier)
(identifier))
(boolean_operator
(not_operator
(comparison_operator
(identifier)
(identifier))))))
(identifier)))
(comparison_operator
(identifier)
(identifier))))
(expression_statement
(comparison_operator
(identifier)
(identifier)))
(expression_statement
(comparison_operator
(identifier)
(identifier))))
================================================================================
Assignments
@ -942,3 +968,36 @@ async with a as b:
(for_in_clause
left: (identifier)
right: (identifier)))))))))
===========================================
Arbitrary indentation between brackets
==========================================
def a():
b(
1,
2
)
c = [
3
]
---
(module
(function_definition
(identifier)
(parameters)
(block
(expression_statement
(call
(identifier)
(argument_list
(integer)
(integer))))
(expression_statement
(assignment
(identifier)
(list
(integer)))))))