ELisp Syntax Table Comments for Haskell style comments

403 views Asked by At

I am attempting to set up Haskell style comments using ELisp Syntax-Table-Comments but I can't quite get it.

In Haskell, there are 2 basic types of comments

-- denotes a single line comment ending with \n

{- denotes a multi line comment ending with -} It should be noted that these can be nested

Multiline comments can be done this way:

(defvar my-syntax-table
  (let ((st (make-syntax-table)))
    (modify-syntax-entry ?{ ". 1n" st)
    (modify-syntax-entry ?- ". 23n" st)
    (modify-syntax-entry ?} ". 4n" st)
   st))

Single line comments can be done this way:

(defvar my-syntax-table
  (let ((st (make-syntax-table)))
    (modify-syntax-entry ?- ". 12" st)
    (modify-syntax-entry ?\n ">" st)
   st))

Based on the documentation, I can set up an alternative using b. However, b applies to the second character of the sequence which in both cases is -. So, when I try to combine these using alternatives, it becomes impossible to do multiline and single line comments.

I have seen this done so I know it is possible. However, I'm not sure how to do it. Any help is appreciated.

2

There are 2 answers

0
Stefan On

Haskell-mode uses

       (modify-syntax-entry ?\{  "(}1nb" table)
       (modify-syntax-entry ?\}  "){4nb" table)
       (modify-syntax-entry ?-  "_ 123" table)))
       (modify-syntax-entry ?\n ">" table)
0
jirassimok On

I ran into this myself when implementing a mode with Haskell-style comments, and this seems to be the solution:

(modify-syntax-entry ?- ".123" table)
(modify-syntax-entry ?\{ "(}1nc" table)
(modify-syntax-entry ?\} "){4nc" table)
(modify-syntax-entry ?\n ">" table)

The definitions in haskell-mode aren't quite correct; they set the "b" comment style with flags 1 and 4, but "b" only applies on 2 and 3. Using the somewhat newer (though by no means new) "c" style, you can make a type of comment that is differentiated by any of the characters, not just the inner ones.

And by marking - as punctuation rather than a comment starter, you can avoid a single dash starting a comment.

For more details on how the comment syntax flags work, refer to the documentation (which has been improved since the question was originally asked).