3
votes

I have asked this question before (Locating unmatched delimiters in Clojurescript) and I still do not have a sufficient answer.

I would like to get some warning of the line where an error such as a parameter mismatch is being detected by the reader. Clojure usually just throws a very general error naming the file only but no line, etc.

I would like to have either a

  • clojure specific tool (something like a lint tool) that provides this information
  • advice on reading those stack traces

I have found my problem now and would like to be more precise about the problem I had. I was compiling Clojurescript distributed over code over several files. I knew I had made changes in about three of them when I discovered that the code would not compile anymore. The error message was about a mismatched ) in file a.cljs on line 1. However I was quite sure, that I did not change a.cljs substantially.

So I was taking the usual way that I perform when such an error occurs (that is not too often).

  • Indent whole files with vim to see if indentation is going "nuts".
  • Use git diff to look for changes in my working copy and the most recent history (I do granular commits) to find spots I changed recently.

This time I did not spot the change that easily. In the end I found out that the issue was a missing ] in a part of the file where closing parentheses piled up, i.e. something like )]))]) in the middle of some other file c.cljs. For some reason, vim clojure did not indent the following functions more or wrongly, therefore the indentation based "detection" did not work.

I reallize that there is probably a good reason why the clojure reader is not very good at spitting out the locations of these paren mismatches. Please do not understand my question as being condascending. After having looked for a place for quite some time, I wrote a small (~50 lines) stack-based parentheses matching program (in C++ I have to admit) which handles {([ ;-comments and simple " strings -- nothing close to a correct parser, yet it pointed me to the right line and characters.

In terms of the quality of Clojure's error messages: Some are poor, yet I am mostly not complaining since Clojure's syntax is so straightforward that mostly I just get the syntax right (semantics is a different topic). Nevertheless for a lisp giving such poor error messages on mismatched parens is a bit unlucky especially for people haven't worked with Lisp before. I know paredit, I have used it (vim-paredit at least) and its a good tool to work with s-expressions. However it does make a few things more complicated and I have had bad experiences mixing paredit and vim-fugitive.

Hacking and contributing to the clojure core as suggested: Might be an option actually, although when browsing through that source code it really takes a lot of effort to dive in. Although at the moment I feel rather like writing a small single-purpose tool to be used with leiningen, it might be simpler that way.

I feel that my post has provoked some people in a way. It did not mean to talk down the clojure project, I am really greatful for clojure developers for providing me programming language.

1
The error messages that I've seen usually something like "starting at line XX". Still though, the fastest thing for me is to just indent the whole file in Emacs and see where it starts to go off kilter.Bill
sometimes (with clojurescript) the starting at line comment is totally off. Sometimes even mentioning another file. The indenting works fine, however with lots of nested closing parens it is not working properly. The last hour I have hacked together a very basic C++ program to do some parsing and spits out unmatched and wrongly nested parentheses yet I really wonder if there is no such tool out there since this approach is really quick and dirty.wirrbel
Currently a good editor is your best bet. I see from your other question you use VIM and a paredit plugin was already recommended. I also recommend using a syntax highlighter and indenter. I use vim-clojure. Applying indentation usually makes it quite visually obvious where the nesting levels are off.A. Webb

1 Answers

2
votes

One reason the reporting of error messages is not better here, and this is a reason not an excuse, is that the majority of people who with with Clojure use paredit-mode or an equivalent that adds the parentheses automatically and makes it really hard to get them mismatched. While I understand that it's does not suit everyone and every editor, the existence of paredit may help explain why there has not been more of a concerted push to work on this.

Clojure is a realtivly young language and, believe it or not, the error messages are much better than they used to be. There is an increasing number of Clojurians that are looking at error reporting within the compiler seriously and these sort of error messages are expected to improve. Sorry about the frustration. There are many parts of the language to work on and I understand to at least some degree that this makes. As always patches are whole heartedly welcome!