1
0
mirror of https://github.com/janet-lang/janet synced 2024-06-26 07:03:16 +00:00

add checks to & _ pattern in match macro

This commit adds 2 checks for & rest pattern in the match macro:
- & is followed by exactly 1 item
- & is followed by a symbol
This commit is contained in:
Ian Shehadeh 2022-01-20 09:16:02 -05:00
parent 651e12cfe4
commit 82688b9a44

View File

@ -1760,7 +1760,15 @@
(get-length-sym s)
(eachp [i sub-pattern] pattern
(when (= sub-pattern '&)
# TODO: check that & is followed by something
(when (<= (length pattern) (inc i))
(errorf "expected symbol following & in pattern"))
(when (< (+ i 2) (length pattern))
(errorf "expected a single symbol follow '& in pattern, found %q" (slice pattern (inc i))))
(when (not= (type (pattern (inc i))) :symbol)
(errorf "expected symbol following & in pattern, found %q" (pattern (inc i))))
(put b2g (pattern (inc i)) @[[slice s i]])
(break))
(visit-pattern-1 b2g s i sub-pattern)))