Parse multi-clause match expressions

This commit is contained in:
Scott Richmond 2022-03-20 13:28:05 -04:00
parent e4e984bacd
commit 069e3b4a7b

View File

@ -448,24 +448,48 @@
(defn- parse-match-clause [parser] (defn- parse-match-clause [parser]
(let [pattern (parse-pattern parser) (let [pattern (parse-pattern parser)
rarrow (expect* #{::token/rarrow} "Expected arrow in match clause" pattern) rarrow (expect* #{::token/rarrow} "Expected arrow after pattern" pattern)
] ]
(if (:success rarrow) (if (:success rarrow)
(let [body (parse-expr (:parser rarrow))] (let [body (parse-expr (:parser rarrow))]
(assoc body ::ast {::ast/type ::ast/clause (assoc body ::ast {::ast/type ::ast/clause
:pattern (::ast pattern) :body (::ast body)}) :pattern (::ast pattern) :body (::ast body)})
) )
(panic rarrow "Expected -> in match clause. Clauses must be in the form pattern -> expression") (panic pattern "Expected -> in match clause. Clauses must be in the form pattern -> expression" #{::token/newline ::token/rbrace})
)
))
(defn- parse-match-clauses [parser]
(loop [
parser (accept-many #{::token/newline} (advance parser))
clauses []]
(let [curr (current parser)]
(case (::token/type curr)
::token/rbrace
(assoc parser ::ast {::ast/type ::ast/clauses :clauses clauses})
::token/newline
(recur (accept-many #{::token/newline} parser) clauses)
(let [clause (parse-match-clause parser)]
(recur (accept-many #{::token/newline} clause) (conj clauses (::ast clause)))
)
)
) )
)) ))
(defn- parse-match [parser] (defn- parse-match [parser]
(let [match-expr (parse-expr (advance parser) #{::token/lbrace ::token/lparen}) (let [match-expr (parse-expr (advance parser) #{::token/with})
match-header (expect* #{::token/with} "Expected with" match-expr)] match-header (expect* #{::token/with} "Expected with" match-expr)]
(if (:success match-header) (if (:success match-header)
(let [clauses (:parser match-header)] (let [clauses (:parser match-header)]
(if (= (token-type clauses) ::token/lbrace) (if (= (token-type clauses) ::token/lbrace)
(println "one or many clauses") ;; match expression with one or many clauses in braces
(let [clauses (parse-match-clauses clauses)]
(assoc clauses ::ast {::ast/type ::ast/match
:expr (::ast match-expr)
:clauses (get-in clauses [::ast :clauses])}))
;; match expression with single match clause
(let [clause (parse-match-clause clauses)] (let [clause (parse-match-clause clauses)]
(assoc clause ::ast {::ast/type ::ast/match (assoc clause ::ast {::ast/type ::ast/match
:expr (::ast match-expr) :expr (::ast match-expr)
@ -478,19 +502,22 @@
(defn- parse-expr (defn- parse-expr
([parser] (parse-expr parser sync-on)) ([parser] (parse-expr parser sync-on))
([parser sync-on] (let [token (current parser)] ([parser sync-on]
(let [token (current parser)]
(case (::token/type token) (case (::token/type token)
(::token/number ::token/string) (::token/number ::token/string)
(parse-atom parser) (parse-atom parser)
::token/keyword (let [next (peek parser) ::token/keyword
(let [next (peek parser)
type (::token/type next)] type (::token/type next)]
(if (= type ::token/lparen) (if (= type ::token/lparen)
(parse-synthetic parser) (parse-synthetic parser)
(parse-atom parser))) (parse-atom parser)))
::token/word (let [next (peek parser) ::token/word
(let [next (peek parser)
type (::token/type next)] type (::token/type next)]
(case type (case type
(::token/lparen ::token/keyword) (parse-synthetic parser) (::token/lparen ::token/keyword) (parse-synthetic parser)
@ -538,10 +565,10 @@
(do (do
(def pp pp/pprint) (def pp pp/pprint)
(def source "match foo with (foo, bar, 0) -> { (def source "match foo with {
let foo = bar _ -> foo
if foo then bar else baz foo () -> bar
:foo () -> baz
}") }")
(def lexed (scanner/scan source)) (def lexed (scanner/scan source))
(def tokens (:tokens lexed)) (def tokens (:tokens lexed))