Fork me on GitHub
#clojure
<
2022-06-26
>
johndoe17:06:20

I have quite dumb question but anyway: Is this possible to implement somehow a different grammar which could be mixed with regular source code in clojure? css fragments inside clojure code for example. Tagged literals seem not suitable because it operates on single form which already parsed and in clojure grammar. For intermixed grammar it needs mechanism to intercept reading as by itself.

Alex Miller (Clojure team)17:06:06

The key thing to remember with embedded DSLs in Clojure is that you are always beholden to the constraints of the Clojure reader. By design, Clojure does not allow you to extend the basic syntax in a way that others can't read without reader modification. Note that you are not constrained by evaluation (as macros get read but unevaluated data).

Alex Miller (Clojure team)17:06:45

So making a DSL usually means creating a mapping from Clojure data types into your domain

Alex Miller (Clojure team)17:06:27

https://github.com/noprompt/garden is a popular Clojure lib for CSS if you want to see an example in this domain

johndoe17:06:07

It's reasonable as completely as disappointing. Btw garden and it's selectors as keywords thing are exactly what I want to avoid 🙂 It's completely unnatural. (On the other hand the hiccup is nearly perfect and even better than html. It's just doesn't work in this way with css.) Will investigate limitations of reading css by macro. Thank you for quick and comprehensive response!

respatialized18:06:57

You can read a bit more about the rationale in the history of Clojure that Rich Hickey wrote for the History of Programming Languages conference: > One thing conspicuously missing from Clojure are Common Lisp’s reader macros. While they fully empower the user to create a language of their own specification, such languages are islands. Critically, they require the presence of specific code to read particular data, such requirement being in direct conflict with some of the benefits of independent read/print enumerated above. I saw reader macros in the CL style as being in direct conflict with library development, interoperability, Clojure data (edn) as a wire format etc. I am certain that had Clojure had reader macros, and users availed themselves of them, the large, composable, data-driven library ecosystem that arose around Clojure would have been compromised or thwarted. > https://download.clojure.org/papers/clojure-hopl-iv-final.pdf

respatialized18:06:48

One particular macro library that may be of interest to you is infix, which uses macros to do standard infix-style math expressions: https://github.com/rm-hull/infix

johndoe20:06:05

Completely understand the rationale, but it's debatable. Such thing as reader macros is essential feature when it needed and it's just missing. And speaking about islands. For me https://github.com/noprompt/garden looks exactly like the island. Isn't it? So much efforts and time to reinvent the notation which already exists and well serves. Sometime it's cool to incorporate existing stuff when it is good enough instead of reinventing or wasting life to overcoming artificial limitation.

johndoe20:06:52

Shen lang has such thing as compiler-compiler https://shenlanguage.org/OSM/Recognisor.html

respatialized20:06:01

You can debate it all you want on comp.lang.lisp or lambda the ultimate, but it's not up for debate in Clojure itself. I don't think it's "missing" if it's been excluded for a specific reason. I use Garden pretty frequently. Being able to use all my familiar sequential and map-based fns on CSS rules doesn't seem like an island to me when compared with a new syntax to learn.

johndoe20:06:36

I don't want to debate it at all but thanks for advice 🙂

phill10:06:18

Instaparse uses a different grammar in clj/cljs files, but you put it in a string (as far as Clojure is concerned). Since Clojure has always had multi-line strings, it's no bother. Of course there's no syntax coloring, but that's not Clojure's fault - rather it would be an enhancement to the IDE.

enn13:06:07

I think “unnatural” is a funny descriptor here …

enn13:06:21

what feels natural is very different from one person to another

enn13:06:04

for myself, despite missing Common Lisp reader macros occasionally, I have come to like the “everything is data and can be operated on in the same way” approach that Clojure takes. that said, there are certainly use cases for truly getting out of Clojure syntax (e.g., you want to have an artifact that a designer or frontend developer who has never heard of an s-expression can work on). In those cases I have found it simplest to just move that CSS or whatever other thing into its own file, and parse it from Clojure. That also gets you those editor affordances (syntax highlighting, indentation) that phill mentioned too.

johndoe13:06:59

"unnatural" in my message has quite specific meaning: transferred to clojure css is much more complicated than css itself. It even doesn't serve any purpose, just to overcome absence of grammar extensions (as far as i see the garden even has become full featured preprocessor in the end). Just another complicated thing to learn and constantly to figure out how to express whats needed and how it would be translated to target language then.

johndoe13:06:22

I agree that in most cases it good enough just use clojure data structures to model domain. But not in this particular case - css. And anyway, modeling with grammars is superior technique than any other. But what are you doing guys is rationalizing why no one actually needs this. Let's everyone will stay on their own

johndoe14:06:43

Rich showed somewhere how ridiculously modeled http with OOP when it actually was just mappings in the first place and asked "what happend?". That what I feel by looking at clojurish css.

Alex Miller (Clojure team)14:06:10

There are definitely times when it would be useful to create and embed your own syntax. That capability has tradeoffs - it adds substantial complexity to the reader, and creates the possibility that you may have Clojure code that is impossible to read (Clojure read, not human read) without the appropriate reader macros. Rich weighed these and decided the costs were greater than the benefits.

Alex Miller (Clojure team)14:06:38

There are langs that embrace this idea (Racket most obviously). Clojure is not one of them.

henrik4220:06:03

@U44T5FWA3 When I started Clojure I did extend the reader and have never used it ever since. https://github.com/henrik42/extended-lisp-reader

phill21:06:55

It should be noted, for completeness, that Perl allows embedded any-syntax-you-want and the less said about it, the better.