Is there a language specification for clojure? Something that precisely defines the lexical syntax and grammar in EBNF or something similar?
The closest thing that I could find is the clojure website, but that doesn't really quite meet the requirements of a language spec (despite being an absolutely wonderful resource). If there is no spec, has our BDFL made mention of any plans for one?
spec is a Clojure library to describe the structure of data and functions. Specs can be used to validate data, conform (destructure) data, explain invalid data, generate examples that conform to the specs, and automatically use generative testing to test functions.
Clojure is the result of all this. It's a LISP functional programming language running on the JVM designed for concurrent programs.
Clojure is not a scripting language: Clojure compiles directly to JVM bytecode, and thus runs at native speed. Thus one must think of Clojure as a true alternative to Java the language, but much more expressive, flexible and powerful.
Clojure is a practical, general-purpose language that offers expressivity rivaling other dynamic languages like Ruby and Python, while seamlessly taking advantage of Java libraries, services, and all of the resources of the JVM ecosystem.
The grammar linked by fogus, which was within the Eclipse plugin Counterclockwise, is no longer used by that project and has been removed from it.
Clojure.g4
, an ANTLR grammarFor a more up-to-date ANTLR grammar for Clojure, see Clojure.g4
(permalink) from grammars-v4, a collection of “grammars written for ANTLR v4”. Clojure.g4
is small and easy to read, and it has successfully parsed Compojure and clojure.core
in the past, but that does not guarantee that it can parse all Clojure code correctly.
LispReader.java
The most authorative specification of Clojure’s syntax is Clojure’s source code itself. Clojure does not use an abstract grammar, only a custom parser, but you can understand the grammar after careful study of the parser’s implementation. Here is Clojure’s parser: LispReader.java
(permalink).
LispReader.java
uses a few classes from other files in the same directory, such as LineNumberingPushbackReader
, but most of the code is in that file. In LispReader
, the main function is read
. read
uses isWhitespace
to ignore whitespace and commas. It also detects numbers and hands off the parsing to readNumber
. For most other symbols, such as (
and #
, read
hands off interpretation to the objects in the macros
and dispatchMacros
arrays. You can follow the code from there.
There is also a Clojure reimplementation of LispReader.java
called clojure.tools.reader. Its source code might be easier to read than LispReader
, since it is in Clojure, not Java. clojure.tools.reader has some differences from LispReader.java
, which are mostly being able to read some minor extra syntaxes proposed for Clojure and handling errors better.
This is the closest thing to an official Clojure EBNF that you are likely to find.
https://github.com/laurentpetit/ccw/blob/3738a4fd768bcb0399630b7f6a6427a3066bdaa9/clojure-antlr-grammar/src/Clojure.g
There is no language specification. If there are any plans for one in the future, I haven't heard of them.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With