File tree 3 files changed +40
-1
lines changed
3 files changed +40
-1
lines changed Original file line number Diff line number Diff line change @@ -259,3 +259,26 @@ Resolving some of these conflicts (for instance special keywords like `{}` or `%
259
259
requires the use of external scanner. Given the complexities this approach
260
260
brings to the grammar, and consequently the parser, we stick to the simpler
261
261
approach.
262
+
263
+ ### Ref 8. Empty anonymous function
264
+
265
+ As opposed to the Elixir parser, we successfully parse anonymous functions with
266
+ no stab clauses, so this is valid:
267
+
268
+ ```
269
+ x = fn
270
+
271
+ end
272
+ ```
273
+
274
+ This code may appear if an editor extension automatically inserts `end` after
275
+ `fn`. We want it to parse as anonymous function node, so that functionality such
276
+ as indentation works as expected.
277
+
278
+ If we require at least one stab clause, the above would be parsed with an error,
279
+ where `fn` and `end` are both identifiers. That is not useful. Note that both
280
+ `fn` and `end` are reserved keywords, so there is no case where they would
281
+ actually be identifiers, hence no ambiguity.
282
+
283
+ Ideally, this would parse it as an anonymous function node with an error, however
284
+ that does not seem straightforward to achieve.
Original file line number Diff line number Diff line change @@ -829,7 +829,8 @@ module.exports = grammar({
829
829
seq (
830
830
"fn" ,
831
831
optional ( $ . _terminator ) ,
832
- sep1 ( $ . stab_clause , $ . _terminator ) ,
832
+ // See Ref 8. in the docs
833
+ optional ( sep1 ( $ . stab_clause , $ . _terminator ) ) ,
833
834
"end"
834
835
) ,
835
836
Original file line number Diff line number Diff line change 178
178
(body
179
179
(atom)))))
180
180
181
+ =====================================
182
+ no clauses
183
+ =====================================
184
+
185
+ fn
186
+ end
187
+
188
+ fn end
189
+
190
+ ---
191
+
192
+ (source
193
+ (anonymous_function)
194
+ (anonymous_function))
195
+
181
196
=====================================
182
197
with guard / no arguments
183
198
=====================================
You can’t perform that action at this time.
0 commit comments