|
1062 | 1062 | let @genexpr.result = tuple
|
1063 | 1063 | }
|
1064 | 1064 |
|
1065 |
| -; 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. |
1066 | 1076 | [
|
1067 | 1077 | (generator_expression
|
1068 | 1078 | body: (_) @body
|
1069 |
| - (if_clause) @last |
1070 |
| - . |
| 1079 | + [(if_clause) (for_in_clause)]+ @last_candidates |
1071 | 1080 | ) @genexpr
|
1072 | 1081 | (list_comprehension
|
1073 | 1082 | body: (_) @body
|
1074 |
| - (if_clause) @last |
1075 |
| - . |
| 1083 | + [(if_clause) (for_in_clause)]+ @last_candidates |
1076 | 1084 | ) @genexpr
|
1077 | 1085 | (set_comprehension
|
1078 | 1086 | body: (_) @body
|
1079 |
| - (if_clause) @last |
1080 |
| - . |
| 1087 | + [(if_clause) (for_in_clause)]+ @last_candidates |
1081 | 1088 | ) @genexpr
|
1082 | 1089 | (dictionary_comprehension
|
1083 | 1090 | body: (_) @body
|
1084 |
| - (if_clause) @last |
1085 |
| - . |
| 1091 | + [(if_clause) (for_in_clause)]+ @last_candidates |
1086 | 1092 | ) @genexpr
|
1087 | 1093 | ]
|
1088 | 1094 | {
|
| 1095 | + let last = (get-last-element @last_candidates) |
| 1096 | + |
1089 | 1097 | let expr = (ast-node @body "Expr")
|
1090 | 1098 | let yield = (ast-node @body "Yield")
|
1091 | 1099 |
|
|
1096 | 1104 |
|
1097 | 1105 | attr (yield) value = @genexpr.result
|
1098 | 1106 | attr (@body.node) ctx = "load"
|
1099 |
| - edge @last.first_if -> expr |
1100 |
| - attr (@last.first_if -> expr) body = 0 |
1101 |
| - |
1102 |
| - ; Hook up this `if` clause with its `for` clause |
1103 |
| - edge @last.for -> @last.node |
1104 |
| - attr (@last.for -> @last.node) body = 0 |
1105 |
| -} |
1106 |
| - |
1107 |
| -; If the last clause is a `for`, we only have to create and hook up the `yield` expression. |
1108 |
| -[ |
1109 |
| - (generator_expression |
1110 |
| - body: (_) @body |
1111 |
| - (for_in_clause) @last |
1112 |
| - . |
1113 |
| - ) @genexpr |
1114 |
| - (list_comprehension |
1115 |
| - body: (_) @body |
1116 |
| - (for_in_clause) @last |
1117 |
| - . |
1118 |
| - ) @genexpr |
1119 |
| - (set_comprehension |
1120 |
| - body: (_) @body |
1121 |
| - (for_in_clause) @last |
1122 |
| - . |
1123 |
| - ) @genexpr |
1124 |
| - (dictionary_comprehension |
1125 |
| - body: (_) @body |
1126 |
| - (for_in_clause) @last |
1127 |
| - . |
1128 |
| - ) @genexpr |
1129 |
| -] |
1130 |
| -{ |
1131 |
| - let expr = (ast-node @body "Expr") |
1132 |
| - let yield = (ast-node @body "Yield") |
1133 | 1107 |
|
1134 |
| - let @genexpr.expr = expr |
1135 |
| - let @genexpr.yield = yield |
1136 |
| - |
1137 |
| - attr (expr) value = yield |
| 1108 | + if (instance-of last "if_clause") { |
| 1109 | + edge last.first_if -> expr |
| 1110 | + attr (last.first_if -> expr) body = 0 |
1138 | 1111 |
|
1139 |
| - attr (yield) value = @genexpr.result |
1140 |
| - attr (@body.node) ctx = "load" |
1141 |
| - edge @last.node -> expr |
1142 |
| - 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 | + } |
1143 | 1120 | }
|
1144 | 1121 |
|
1145 | 1122 | ; For whatever reason, we do not consider parentheses around the yielded expression if they are present, so
|
|
0 commit comments