Is there a language spec for clojure?

前端 未结 4 1742
执念已碎
执念已碎 2021-02-18 21:05

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 co

4条回答
  •  野趣味
    野趣味 (楼主)
    2021-02-18 21:19

    Let's look at a syntax error or two:

    user=> (defn)
    Syntax error macroexpanding clojure.core/defn at (REPL:1:1). 
    () - failed: Insufficient input at: [:fn-name] spec: :clojure.core.specs.alpha/defn-args 
    

    And

    user=> (fn [3]) Syntax error macroexpanding clojure.core/fn at (REPL:1:1).
    (3) - failed: Extra input at: [:fn-tail :arity-1 :params] spec: :clojure.core.specs.alpha/param-list
    3 - failed: vector? at: [:fn-tail :arity-n :params] spec: :clojure.core.specs.alpha/param-list
    

    It is clear that the syntax of the core macros is now (version 1.10) checked using clojure.spec. If and when the Clojure in Clojure project advances, we can expect spec to extend its reach into the compiler proper.

    The point is that spec has full EBNF power, so the source code will then contain a full EBNF of the language. The notation is explained in Clojure - clojure.spec: Rationale and Overview:

    Sequences

    Specs for sequences/vectors use a set of standard regular expression operators, with the standard semantics of regular expressions:

    cat - a concatenation of predicates/patterns
    
    alt - a choice of one among a set of predicates/patterns
    
    * - zero or more occurrences of a predicate/pattern
    
    + - one or more
    
    ? - one or none
    
    & - takes a regex op and further constrains it with one or more predicates
    

    When will this happen? My (utterly uninformed) impression is that the core team are drowning in alligators and have almost forgotten their original intention to drain this swamp.


    A previous answer referring to spec in general terms for Clojure 1.9 was deleted. I think its use to define and check macro syntax is new with 1.10.

提交回复
热议问题