tree-sitter-commonlisp
tree-sitter-commonlisp copied to clipboard
feature: loop named XX
Did you check the tree-sitter docs?
- [ ] I have read all the tree-sitter docs if it relates to using the parser
Is your feature request related to a problem? Please describe.
The loop syntax fails on loop named xxx. Here is an example. remove the named .. and you're fine. sbcl runs it. I don't know whteher its part of common-lisp or not (yet).
(defun find-item-in-list-of-lists (lists target)
"Searches a list of lists for the target value.
Returns the target immediately upon finding it."
(loop named xx ; 1. Name the outer loop's block as XX
for sublist in lists ; Outer loop over the list of lists
do ; Execute arbitrary code
(loop for item in sublist ; Inner loop over the elements of the sublist
do
(when (eq item target)
(return-from xx target)) ; 2. Exit the loop named XX immediately and return TARGET
)
finally (return-from xx nil) ; If both loops complete without finding the item, return NIL
)
)
;; Test Cases
(format t "Found 'E'? ~A~%" (find-item-in-list-of-lists '((A B) (C D E) (F G)) 'E))
; Output: Found 'E'? E
; (Stops iteration after E is found)
(format t "Found 'Z'? ~A~%" (find-item-in-list-of-lists '((A B) (C D E) (F G)) 'Z))
; Output: Found 'Z'? NIL
; (Completes all iterations and returns NIL from the FINALLY clause)
diff --git a/grammar.js b/grammar.js
index 7752631..4ecb4e6 100644
--- a/grammar.js
+++ b/grammar.js
@@ -248,6 +248,7 @@ module.exports = grammar(clojure, {
accumulation_clause: $ => seq($.accumulation_verb, repeat($._gap), $._form, optional(seq(repeat($._gap), loopSymbol('into'), repeat($._gap), $._form))),
termination_clause: $ => prec.left(seq(choice(loopSymbol('finally'), loopSymbol('return'), loopSymbol('initially')), repeat($._gap), $._form)),
+ named_clause: $ => seq(loopSymbol('named'), repeat($._gap), $.sym_lit),
loop_clause: $ =>
seq(choice(
@@ -268,6 +269,7 @@ module.exports = grammar(clojure, {
seq(field('open', "("),
optional($._gap),
clSymbol('loop'),
+ optional($.named_clause),
repeat(choice($.loop_clause, $._gap)),
field('close', ")"))),
The patch works for the sample given. No idea whether its perfect.
Describe the solution you'd like
Something like the provided patch to allow reading code without everything below the loop turning yellow.
Describe alternatives you've considered
No response
Additional context
No response