Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support exception branches in match #42

Closed
ghost opened this issue May 4, 2015 · 3 comments
Closed

Support exception branches in match #42

ghost opened this issue May 4, 2015 · 3 comments

Comments

@ghost
Copy link

ghost commented May 4, 2015

In OCaml 4.02, match expressions have been extended to allow exception branches. This isn't recognized yet, and using the syntax throws off font-lock and indentation.

let n = match List.find ((=) 3) [1;2] with
  | x -> x
  | exception Not_found -> 0
              in n               (* Should be at leftmost indentation *)
@ghost
Copy link

ghost commented Oct 1, 2015

Placing braces around exception specification alleviates the problem somewhat, still a proper solution would be nice.

@monnier
Copy link
Contributor

monnier commented Oct 1, 2015

The patch below should add support for that new syntax.

diff --git a/tuareg.el b/tuareg.el
index 9bd6d95..ed20b64 100644
--- a/tuareg.el
+++ b/tuareg.el
@@ -1272,7 +1272,10 @@ by |, insert one |."
               (iddef (id "f=" exp1))
               (branches (branches "|" branches) (branch))
               (branch (patterns "->" exp1))
-              (patterns (pattern) (pattern "when" exp1))
+              (patterns (pattern) (pattern "when" exp1)
+                        ;; Since OCaml 4.02, `match' expressions allow
+                        ;; `exception' branches.
+                        ("exception-case" pattern))
               (pattern (id) (pattern "as" id) (pattern "," pattern))
               (class-body (class-body "inherit" class-body)
                           (class-body "method" class-body)
@@ -1452,7 +1455,8 @@ of the token."
         (goto-char (match-end 0))
         (match-string 0)))
      ((or (member tok '("let" "=" "->"
-                        "module" "class" "open" "type" "with" "and"))
+                        "module" "class" "open" "type" "with" "and"
+                        "exception"))
           ;; http://caml.inria.fr/pub/docs/manual-ocaml/expr.html lists
           ;; the tokens whose precedence is based on their prefix.
           (memq (aref tok 0) '(?* ?/ ?% ?+ ?- ?@ ?^ ?= ?< ?> ?| ?& ?$)))
@@ -1610,6 +1614,9 @@ Return values can be
             "label:"
           (goto-char pos)
           tok)))
+     ((equal tok "exception")
+      (if (save-excursion (equal (tuareg-smie--backward-token) "|"))
+          "exception-case" tok))
      ((string-match "\\`[[:alpha:]_].*\\.\\'"  tok)
       (forward-char (1- (length tok))) ".")
      (t tok))))

monnier added a commit that referenced this issue Oct 1, 2015
(tuareg-smie-grammar): Add OCaml-4.02's new "exception pattern".
(tuareg-smie-forward-token, tuareg-smie-backward-token): Tweak the
lexer for the new "exception" case.
(tuareg--beginning-of-phrase): Don't jump over a let-d if we happen
to find one before smie-backward-sexp bumps into it.
(tuareg-reset-and-kwop): Move from tuareg_indent.el.
(tuareg-imenu-create-index): Demote the scan error to a warning.

* .gitignore: Add ELPA-generated files.

* sample.ml (count_append): Add "exception case" test.
@Chris00
Copy link
Member

Chris00 commented Nov 6, 2015

@roysc The Tuareg version at the time of this comment indents your code as

let f =
  let n =
    match List.find ((=) 3) [1;2] with
    | x -> x
    | exception Not_found -> 0
  in n

so I think we can consider this fixed.

@Chris00 Chris00 closed this as completed Nov 6, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants