ocaml: phase 3 exception declarations (+4 tests, 304 total)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 50s

exception NAME [of TYPE] parses to (:exception-def NAME [ARG-SRC]).
Runtime is a no-op: raise/match already work on tagged ctor values, so
'exception E of int;; try raise (E 5) with | E n -> n' end-to-end with
zero new eval logic.
This commit is contained in:
2026-05-08 12:37:58 +00:00
parent d8f6250962
commit bc557a5ad2
4 changed files with 62 additions and 2 deletions

View File

@@ -909,6 +909,7 @@
((at-kw? "include") nil)
((at-kw? "and") nil)
((at-kw? "type") nil)
((at-kw? "exception") nil)
(else (begin (advance-tok!) (skip-to-boundary!))))))
(define
parse-decl-let
@@ -978,6 +979,35 @@
;; module M = struct DECLS end
;; Parsed by sub-tokenising the body source between `struct` and
;; the matching `end`. Nested modules / sigs increment depth.
;; exception NAME [of TYPE [* TYPE]*]
(define
parse-decl-exception
(fn ()
(advance-tok!) ;; consume 'exception'
(let ((name (ocaml-tok-value (consume! "ctor" nil)))
(arg-srcs (list)))
(begin
(when (at-kw? "of")
(begin
(advance-tok!)
(let ((arg-start (cur-pos)))
(begin
(define skip-type
(fn ()
(cond
((>= idx tok-len) nil)
((= (ocaml-tok-type (peek-tok)) "eof") nil)
((at-op? ";;") nil)
((at-kw? "let") nil)
((at-kw? "type") nil)
((at-kw? "and") nil)
((at-kw? "module") nil)
((at-kw? "exception") nil)
(else (begin (advance-tok!) (skip-type))))))
(skip-type)
(append! arg-srcs (slice src arg-start (cur-pos)))))))
(cons :exception-def (cons name arg-srcs))))))
;; type [PARAMS] NAME = | Ctor [of T1 [* T2]*] | …
;;
;; PARAMS is `'a` or `('a, 'b)` (single or paren-tuple of tyvars).
@@ -1217,6 +1247,8 @@
(begin (append! decls (parse-decl-open true)) (loop)))
((at-kw? "type")
(begin (append! decls (parse-decl-type)) (loop)))
((at-kw? "exception")
(begin (append! decls (parse-decl-exception)) (loop)))
(else (begin (append! decls (parse-decl-expr)) (loop))))))))
(loop)
(cons :program decls)))))