Copypasta splat patterns to dict, struct, tuple in parser

This commit is contained in:
Scott Richmond 2022-06-19 14:52:38 -04:00
parent 68bc37ef61
commit 4e7590226b

View File

@ -522,7 +522,7 @@
:token (current origin) :token (current origin)
:length (count ms) :length (count ms)
:members ms}) :members ms})
(panic parser "A splat my only appear once in a pattern, at the end of a pattern."))) (panic parser "A splat my only appear once in a list pattern, at the end of the pattern.")))
(::token/comma ::token/newline) (::token/comma ::token/newline)
(recur (recur
@ -548,11 +548,15 @@
current_member nil] current_member nil]
(let [curr (current parser)] (let [curr (current parser)]
(case (token-type parser) (case (token-type parser)
::token/rbrace (let [ms (add-member members current_member)] ::token/rbrace
(assoc (advance parser) ::ast (let [ms (add-member members current_member)]
{::ast/type ::ast/dict (if (not-any? #(= (::ast/type %) ::ast/splat) (drop-last ms))
:token (current origin) (assoc (advance parser) ::ast
:members ms})) {::ast/type ::ast/tuple
:token (current origin)
:length (count ms)
:members ms})
(panic parser "A splat my only appear once in a dict pattern, at the end of the pattern.")))
(::token/comma ::token/newline) (::token/comma ::token/newline)
(recur (recur
@ -565,6 +569,10 @@
::token/eof ::token/eof
(panic (assoc origin ::errors (::errors parser)) "Unterminated dict pattern" ::token/eof) (panic (assoc origin ::errors (::errors parser)) "Unterminated dict pattern" ::token/eof)
::token/splat
(let [splatted (parse-splat-pattern parser)]
(recur splatted members (::ast splatted)))
::token/word ::token/word
(if (not current_member) (if (not current_member)
(let [parsed (parse-word parser) word (get-in parsed [::ast :word])] (let [parsed (parse-word parser) word (get-in parsed [::ast :word])]
@ -585,11 +593,15 @@
current_member nil] current_member nil]
(let [curr (current parser)] (let [curr (current parser)]
(case (token-type parser) (case (token-type parser)
::token/rbrace (let [ms (add-member members current_member)] ::token/rbrace
(assoc (advance parser) ::ast (let [ms (add-member members current_member)]
{::ast/type ::ast/struct (if (not-any? #(= (::ast/type %) ::ast/splat) (drop-last ms))
:token (current origin) (assoc (advance parser) ::ast
:members ms})) {::ast/type ::ast/tuple
:token (current origin)
:length (count ms)
:members ms})
(panic parser "A splat my only appear once in a struct pattern, at the end of the pattern.")))
(::token/comma ::token/newline) (::token/comma ::token/newline)
(recur (recur
@ -602,6 +614,10 @@
::token/eof ::token/eof
(panic (assoc origin ::errors (::errors parser)) "Unterminated struct pattern" ::token/eof) (panic (assoc origin ::errors (::errors parser)) "Unterminated struct pattern" ::token/eof)
::token/splat
(let [splatted (parse-splat-pattern parser)]
(recur splatted members (::ast splatted)))
::token/word ::token/word
(if (not current_member) (if (not current_member)
(let [parsed (parse-word parser) word (get-in parsed [::ast :word])] (let [parsed (parse-word parser) word (get-in parsed [::ast :word])]
@ -625,11 +641,13 @@
(case (token-type parser) (case (token-type parser)
::token/rparen ::token/rparen
(let [ms (add-member members current_member)] (let [ms (add-member members current_member)]
(if (not-any? #(= (::ast/type %) ::ast/splat) (drop-last ms))
(assoc (advance parser) ::ast (assoc (advance parser) ::ast
{::ast/type ::ast/tuple {::ast/type ::ast/tuple
:token (current origin) :token (current origin)
:length (count ms) :length (count ms)
:members ms})) :members ms})
(panic parser "A splat my only appear once in a tuple pattern, at the end of the pattern.")))
(::token/comma ::token/newline) (::token/comma ::token/newline)
@ -643,6 +661,10 @@
::token/eof ::token/eof
(panic (assoc origin ::errors (::errors parser)) "Unterminated tuple" ::token/eof) (panic (assoc origin ::errors (::errors parser)) "Unterminated tuple" ::token/eof)
::token/splat
(let [splatted (parse-splat-pattern parser)]
(recur splatted members (::ast splatted)))
(let [parsed (parse-pattern parser)] (let [parsed (parse-pattern parser)]
(recur parsed members (::ast parsed))))))) (recur parsed members (::ast parsed)))))))