clj-yaml

Ingy döt Net 2023-07-27T12:24:10.665509Z

@ingy has joined the channel

Ingy döt Net 2023-07-27T12:27:51.036369Z

Greetings all!

Ingy döt Net 2023-07-27T12:28:08.366439Z

Ingy döt Net 2023-07-27T12:30:41.642559Z

I'm hoping to port https://metacpan.org/pod/YAMLScript to Clojure using clj-yaml.

Ingy döt Net 2023-07-27T12:30:56.346549Z

Might need some help. 🙂

lread 2023-07-27T12:35:44.794409Z

Welcome @ingy, I think most of us just bumble our way through with YAML. Nice to have the actual inventor of the data format right here with us!

Ingy döt Net 2023-07-27T12:49:58.938709Z

Good to be here!

Ingy döt Net 2023-07-27T12:51:56.357229Z

YAML is a complex beast, despite it mostly being seen as a better way to write JSON.

Ingy döt Net 2023-07-27T12:52:07.389549Z

Feel free to ask me anything about it.

Ingy döt Net 2023-07-27T12:56:41.103289Z

The tiny YAML core team hangs out here: https://matrix.to/#/#chat:yaml.io Andrey (SnakeYAML author) used to hang out there too.

Ingy döt Net 2023-07-27T12:57:57.711349Z

I know enough of SnakeYAML to run it in our tested YAMLs against the suite and to integrate it into this: https://play.yaml.io/main/parser

lread 2023-07-27T12:58:41.593219Z

Oh, and if you read through the history here, please forgive us @ingy for any snarky YAML remarks. I think we were just trying to cope with our confusion, but now that the delightful human being who invented YAML is here... well, some of it feels outright cringey! Sorry about that!

Ingy döt Net 2023-07-27T12:59:07.848239Z

YAML sucks.

😂 1
2
Ingy döt Net 2023-07-27T12:59:18.981309Z

There, we got that out of the way!

❤️ 1
Ingy döt Net 2023-07-27T13:01:52.835889Z

Also SnakeYAML is basically a port of PyYAML iirc. I maintain PyYAML so know that well. PyYAML has all the API things I need to make a YAMLScript reader, so I assume that SnakeYAML does too, and I hope that clj-yaml exposes the right things 🙂

Ingy döt Net 2023-07-27T13:03:52.054129Z

I think I want to write a yamlscript.clj library with a load-file-ys function that reads ys instead of clj.

Ingy döt Net 2023-07-27T13:04:10.116579Z

Can you point me at other custom readers for clojure?

lread 2023-07-27T13:04:47.572719Z

Hope so, but if not, we can figure it out. Or even create a new wrapper lib if that makes sense. Is the fact that SnakeYAML is only a YAML 1.1 parser problematic for you?

Ingy döt Net 2023-07-27T13:05:11.975599Z

Nope. Unlikely.

✅ 2
Ingy döt Net 2023-07-27T13:06:22.006149Z

It will be fun to write the YS reader in YS!

Ingy döt Net 2023-07-27T13:06:50.404449Z

Eventually I want to write a full YAML reference parser in YS.

Ingy döt Net 2023-07-27T13:07:15.525219Z

If there were a pure clojure yaml parser I could do that now.

Ingy döt Net 2023-07-27T13:08:17.503009Z

I like that in YS at about any level you can use pure Clojure syntax.

Ingy döt Net 2023-07-27T13:13:15.672529Z

I certainly don't expect that Clojure people would prefer YS to Clojure. I don't even prefer that. But I see YS as having at least 2 important things to offer. 1. A way to do the anything programmatic with the full power of Clojure inside YAML, instead of 100 different hacks of trying to do this in 100 domains. 2. A gateway drug to Clojure for people who know enough YAML and say Python to be dangerous.

lread 2023-07-27T13:10:27.157469Z

I think the impetus for clj-yaml has been a Clojure developer who needed some way to read some random YAML. We might need to get more sophisticated to support your use case, but that's cool!

Ingy döt Net 2023-07-27T13:14:14.862899Z

Yeah that was my worry. That you would only expose a yaml-load-string and yaml-load-file

borkdude 2023-07-27T13:15:16.040929Z

you can just use raw snakeyml java interop if you need to get more stuff though, in a JVM. In babashka you can't, but usually in babashka people only parse basic yaml

Ingy döt Net 2023-07-27T13:15:39.860089Z

I basically need access to the YAML parser events. The parser is one part of the load stack: read->scan->parse->compose->resolve->construct

borkdude 2023-07-27T13:16:04.004399Z

Just use raw Java interop I'd say, to get started

❤️ 1
Ingy döt Net 2023-07-27T13:16:14.202219Z

yep, makes sense

lread 2023-07-27T13:16:35.754809Z

Yep, that way you won't be bound by clj-yaml limitations at all.

lread 2023-07-27T13:18:17.638489Z

But... as a YAML expert you might see some obvious general/useful things that clj-yaml is missing. Happy to learn about those if you stumble on them.

lread 2023-07-27T13:11:57.833799Z

> Can you point me at other custom readers for clojure? I'm not sure exactly what you are asking but @borkdude's https://github.com/borkdude/edamame comes to mind.

lread 2023-07-27T13:15:17.014659Z

If you are asking about parsing other things, https://github.com/engelberg/instaparse comes to mind.

Ingy döt Net 2023-07-27T13:20:25.423639Z

@borkdude as long as you are here, I was trying to figure out how I could implement a load-file-ys fn that called out to the perl YAMLScript::Reader with the source string and got edn back to eval into clojure. (does that make sense?)

Ingy döt Net 2023-07-27T13:21:12.507869Z

But then yesterday I realized I should just port that Perl code to clojure and be done with it

Ingy döt Net 2023-07-27T13:22:03.013599Z

ClojureScript and ClojureCLR (others?) use the JVM clojure compiler, right?

Ingy döt Net 2023-07-27T13:23:31.078749Z

So then having this port would give YS support to ClojureScript at the same time...

borkdude 2023-07-27T13:24:01.796339Z

Do you have one specific question?

Ingy döt Net 2023-07-27T13:25:04.707619Z

hmmm. ok. Is all EDN eval-able as Clojure?

borkdude 2023-07-27T13:27:39.606879Z

yes, but not vice versa

✅ 1
Ingy döt Net 2023-07-27T13:28:09.364139Z

nvm. I guess I don't have anything I'm stuck on atm. Was just looking for assurance that I was heading in the paths of least resistance to get YS runnable on the JVM and JS.

borkdude 2023-07-27T13:29:17.082679Z

Perhaps #sci could also be useful as part of YS on JVM / JS. It allows you to read values from strings and evaluate them in both of those hosts

borkdude 2023-07-27T13:30:33.242139Z

@ingy Does YS work like this: parse YAML where the clojure code is a string and then you want to process + eval that string? if so, SCI would fit naturally there I think

Ingy döt Net 2023-07-27T13:37:56.385589Z

In Lingy read-string reads clojure code and returns a Lingy AST, like:

user=> (XXX (read-string "(prn 42)"))
--- !perl/array:Lingy::List
- !perl/scalar:Lingy::Symbol
  =: prn
- !perl/scalar:Lingy::Number
  =: '42'
...

borkdude 2023-07-27T13:38:30.872759Z

yeah, but this ain't going to work in CLJS

Ingy döt Net 2023-07-27T13:39:34.772349Z

Sorry wasn't finished

Ingy döt Net 2023-07-27T13:40:59.651369Z

And YS (just a subclass of Lingy) has a read-string-ys

user=> read-string-ys: "prn: 42"
(prn 42)

Ingy döt Net 2023-07-27T13:41:22.633829Z

that does the same thing with YS input instead of clojure.

borkdude 2023-07-27T13:42:20.992299Z

right, so you read the ys syntax and get back out an s-expression (or string of an s-expression) which you then need to evaluate

Ingy döt Net 2023-07-27T13:42:24.001229Z

Obviously for YS in Clojure I'm not going to produce that AST. I would likely produce EDN, and??? eval it?

borkdude 2023-07-27T13:42:40.954099Z

Why would you produce EDN and not just Clojure and eval that?

Ingy döt Net 2023-07-27T13:43:00.042689Z

I don't know the differences I guess

borkdude 2023-07-27T13:44:20.764009Z

well, EDN is a data format, like JSON. It's basically Clojure is to EDN what JavaScript is to JSON

Ingy döt Net 2023-07-27T13:45:01.415369Z

ok, so yes I would produce clojure forms

Ingy döt Net 2023-07-27T13:45:32.959459Z

thanks

👍 1
borkdude 2023-07-27T13:30:58.387239Z

In JVM Clojure you can just use eval as well, but in CLJS this is more tricky

Ingy döt Net 2023-07-27T13:33:46.599219Z

https://github.com/babashka/sci/blob/master/src/sci/core.cljc what's .cljc?

lread 2023-07-27T13:35:59.862939Z

https://clojure.org/guides/reader_conditionals

✅ 1