|
25 | 25 | [ (expression_list) (tuple) (tuple_pattern) (pattern_list) ] @tuple |
26 | 26 | { let @tuple.node = (ast-node @tuple "Tuple") } |
27 | 27 |
|
| 28 | +(list_pattern) @list |
| 29 | +{ let @list.node = (ast-node @list "List") } |
| 30 | + |
28 | 31 | (call) @call { let @call.node = (ast-node @call "Call") } |
29 | 32 |
|
30 | 33 | (for_statement) @for |
|
1059 | 1062 | let @genexpr.result = tuple |
1060 | 1063 | } |
1061 | 1064 |
|
1062 | | -; For the final `if` clause, we need to hook it up with the `yield` expression and with its associated `for` clause. |
| 1065 | +; For the final clause, we need to hook it up with the rest of the expression. |
| 1066 | +; If it's an `if` clause, we need to hook it up with the `yield` expression and with its associated |
| 1067 | +; `for` clause. |
| 1068 | +; If it's a `for` clause, we only need to create and hook it up with the `yield` expression. |
| 1069 | +; |
| 1070 | +; It would be tempting to use anchors here, but they just don't work. In particular, an anchor of |
| 1071 | +; the form `. (comment)* . )` (which would be needed in order to handle the case where there are |
| 1072 | +; comments after the last clause) cause the `tree-sitter` query engine to match _all_ clauses, not |
| 1073 | +; just the last one. |
| 1074 | +; Instead, we gather up all clauses in a list (these will be in the order they appear in the source |
| 1075 | +; code), and extract the last element using a custom Rust function. |
1063 | 1076 | [ |
1064 | 1077 | (generator_expression |
1065 | 1078 | body: (_) @body |
1066 | | - (if_clause) @last |
1067 | | - . |
| 1079 | + [(if_clause) (for_in_clause)]+ @last_candidates |
1068 | 1080 | ) @genexpr |
1069 | 1081 | (list_comprehension |
1070 | 1082 | body: (_) @body |
1071 | | - (if_clause) @last |
1072 | | - . |
| 1083 | + [(if_clause) (for_in_clause)]+ @last_candidates |
1073 | 1084 | ) @genexpr |
1074 | 1085 | (set_comprehension |
1075 | 1086 | body: (_) @body |
1076 | | - (if_clause) @last |
1077 | | - . |
| 1087 | + [(if_clause) (for_in_clause)]+ @last_candidates |
1078 | 1088 | ) @genexpr |
1079 | 1089 | (dictionary_comprehension |
1080 | 1090 | body: (_) @body |
1081 | | - (if_clause) @last |
1082 | | - . |
| 1091 | + [(if_clause) (for_in_clause)]+ @last_candidates |
1083 | 1092 | ) @genexpr |
1084 | 1093 | ] |
1085 | 1094 | { |
| 1095 | + let last = (get-last-element @last_candidates) |
| 1096 | + |
1086 | 1097 | let expr = (ast-node @body "Expr") |
1087 | 1098 | let yield = (ast-node @body "Yield") |
1088 | 1099 |
|
|
1093 | 1104 |
|
1094 | 1105 | attr (yield) value = @genexpr.result |
1095 | 1106 | attr (@body.node) ctx = "load" |
1096 | | - edge @last.first_if -> expr |
1097 | | - attr (@last.first_if -> expr) body = 0 |
1098 | 1107 |
|
1099 | | - ; Hook up this `if` clause with its `for` clause |
1100 | | - edge @last.for -> @last.node |
1101 | | - attr (@last.for -> @last.node) body = 0 |
1102 | | -} |
| 1108 | + if (instance-of last "if_clause") { |
| 1109 | + edge last.first_if -> expr |
| 1110 | + attr (last.first_if -> expr) body = 0 |
1103 | 1111 |
|
1104 | | -; If the last clause is a `for`, we only have to create and hook up the `yield` expression. |
1105 | | -[ |
1106 | | - (generator_expression |
1107 | | - body: (_) @body |
1108 | | - (for_in_clause) @last |
1109 | | - . |
1110 | | - ) @genexpr |
1111 | | - (list_comprehension |
1112 | | - body: (_) @body |
1113 | | - (for_in_clause) @last |
1114 | | - . |
1115 | | - ) @genexpr |
1116 | | - (set_comprehension |
1117 | | - body: (_) @body |
1118 | | - (for_in_clause) @last |
1119 | | - . |
1120 | | - ) @genexpr |
1121 | | - (dictionary_comprehension |
1122 | | - body: (_) @body |
1123 | | - (for_in_clause) @last |
1124 | | - . |
1125 | | - ) @genexpr |
1126 | | -] |
1127 | | -{ |
1128 | | - let expr = (ast-node @body "Expr") |
1129 | | - let yield = (ast-node @body "Yield") |
1130 | | - |
1131 | | - let @genexpr.expr = expr |
1132 | | - let @genexpr.yield = yield |
1133 | | - |
1134 | | - attr (expr) value = yield |
1135 | | - |
1136 | | - attr (yield) value = @genexpr.result |
1137 | | - attr (@body.node) ctx = "load" |
1138 | | - edge @last.node -> expr |
1139 | | - attr (@last.node -> expr) body = 0 |
| 1112 | + ; Hook up this `if` clause with its `for` clause |
| 1113 | + edge last.for -> last.node |
| 1114 | + attr (last.for -> last.node) body = 0 |
| 1115 | + } else { |
| 1116 | + ; If the last clause is a `for`, we only have to create and hook up the `yield` expression. |
| 1117 | + edge last.node -> expr |
| 1118 | + attr (last.node -> expr) body = 0 |
| 1119 | + } |
1140 | 1120 | } |
1141 | 1121 |
|
1142 | 1122 | ; For whatever reason, we do not consider parentheses around the yielded expression if they are present, so |
|
3180 | 3160 | (typed_parameter |
3181 | 3161 | (identifier) @name |
3182 | 3162 | . |
3183 | | - type: (type (expression) @type) |
| 3163 | + type: (type (_) @type) |
3184 | 3164 | ) |
3185 | 3165 | (typed_default_parameter |
3186 | 3166 | name: (_) @name |
3187 | | - type: (type (expression) @type) |
| 3167 | + type: (type (_) @type) |
3188 | 3168 | value: (_) @value |
3189 | 3169 | ) |
3190 | 3170 | ] @param |
|
3239 | 3219 | (list_splat_pattern vararg: (_) @name) @starred |
3240 | 3220 | (typed_parameter |
3241 | 3221 | (list_splat_pattern vararg: (_) @name) @starred |
3242 | | - type: (type (expression) @type) |
| 3222 | + type: (type (_) @type) |
3243 | 3223 | ) |
3244 | 3224 | ] |
3245 | 3225 | ) @params |
|
3256 | 3236 |
|
3257 | 3237 | ; Return type |
3258 | 3238 | (function_definition |
3259 | | - return_type: (type (expression) @type) |
| 3239 | + return_type: (type (_) @type) |
3260 | 3240 | ) @funcdef |
3261 | 3241 | { |
3262 | 3242 | attr (@funcdef.funcexpr) returns = @type.node |
|
3270 | 3250 | (dictionary_splat_pattern kwarg: (identifier) @name) |
3271 | 3251 | (typed_parameter |
3272 | 3252 | (dictionary_splat_pattern kwarg: (identifier) @name) |
3273 | | - type: (type (expression) @type) |
| 3253 | + type: (type (_) @type) |
3274 | 3254 | ) |
3275 | 3255 | ] |
3276 | 3256 | ) @params |
|
3447 | 3427 | ; Left hand side of an assignment such as `foo, bar = ...` |
3448 | 3428 | (pattern_list element: (_) @elt) @parent |
3449 | 3429 |
|
| 3430 | + ; Left hand side of an assignment such as `[foo, bar] = ...` |
| 3431 | + (list_pattern element: (_) @elt) @parent |
| 3432 | + |
3450 | 3433 | ; An unadorned tuple (such as in `x = y, z`) |
3451 | 3434 | (expression_list element: (_) @elt) @parent |
3452 | 3435 |
|
|
3483 | 3466 | (tuple element: (_) @elt) |
3484 | 3467 | (tuple_pattern element: (_) @elt) |
3485 | 3468 | (pattern_list element: (_) @elt) |
| 3469 | + (list_pattern element: (_) @elt) |
3486 | 3470 | (expression_list element: (_) @elt) |
3487 | 3471 | (parenthesized_expression inner: (_) @elt) |
3488 | 3472 | (set element: (_) @elt) |
|
0 commit comments