Skip to content

Latest commit

 

History

History
103 lines (80 loc) · 4.63 KB

scope.md

File metadata and controls

103 lines (80 loc) · 4.63 KB

Scope of tree-sitter-clojure

TLDR

Only "primitives" (e.g. symbols, lists, etc.) are supported, i.e. no higher level constructs like defn.

The Details

Why

For some background, Clojure (and other Lisps) have runtime extensible "syntax" via macros, but AFAIU tree-sitter's current design assumes a fixed syntax.

Keeping the above in mind, below are some of the factors that influenced the current stance on scope:

  • Clojure has no language specification. This means it's unclear what to try to support in the grammar. For example, defn is defined in the clojure.core namespace, but then so are a lot of other things and clojure.core is not a small namespace.

  • Each additional item added to the grammar tends to increase the difficulty of getting the grammar to function correctly (or well enough). In the event that an issue is discovered or a much desired feature surfaces, the more items there already are in the grammar, generally, the harder it may be to accomodate / adjust.

  • Handling more things might lead to degraded performance. Apart from possibly that being a negative for end-user use, that might also lead to more waiting time while testing across large samples of code (which has been essential because of the lack of a specification).

Alternatives

It is possible to use tree-sitter-clojure as a base to add additional constructs to a "derived" grammar. For example, such a grammar might be specialized to look for "definitions". At least in emacs-tree-sitter, it is technically possibly to have multiple grammars be used on single buffer:

If you want 2 parse trees in the same buffer instead, you would need to define an advice for tree-sitter--do-parse, as well as additional buffer-local variables for the secondary grammar.

Apparently it became possible in September of 2020 for queries to match on any of a node's supertypes. It may be possible to make a list supertype that is "composed of" defn and things that are not defn. tree-sitter-clojure-def is an attempt at realizing this apoproach.

However, depending on one's goals, it might make more sense to consider leveraging clj-kondo's analysis capabilities as clj-kondo already understands Clojure pretty well. IIUC, clojure-lsp does this.

Miscellaneous Points

Footnotes

  • [1] Author's opinion :)
  • [2] Two of the previous tree-sitter-clojure attempts (by oakmac and Tavistock) also had unacceptably high error rates. The former of those two grammars tried to handle higher level constructs and it had a notably higher error rate. After trying to modify that grammar to address the error rate unsuccessfully, it seemed like the two points were related. Note though that this is just a suspicion.

References