Fork me on GitHub
#clj-yaml
<
2023-07-27
>
Ingy döt Net12:07:51

Greetings all!

Ingy döt Net12:07:41

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

Ingy döt Net12:07:56

Might need some help. 🙂

lread12:07:44

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 Net12:07:58

Good to be here!

Ingy döt Net12:07:56

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

Ingy döt Net12:07:07

Feel free to ask me anything about it.

Ingy döt Net12:07:41

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 Net12:07:57

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

lread12:07:41

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 Net12:07:07

YAML sucks.

simple_smile 4
😂 2
Ingy döt Net12:07:18

There, we got that out of the way!

❤️ 2
Ingy döt Net13:07:52

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 Net13:07:52

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 Net13:07:10

Can you point me at other custom readers for clojure?

lread13:07:47

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 Net13:07:11

Nope. Unlikely.

4
Ingy döt Net13:07:22

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

Ingy döt Net13:07:50

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

Ingy döt Net13:07:15

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

Ingy döt Net13:07:17

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

Ingy döt Net13:07:15

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.

lread13:07:27

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 Net13:07:14

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

borkdude13:07:16

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 Net13:07:39

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

borkdude13:07:04

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

❤️ 2
Ingy döt Net13:07:14

yep, makes sense

lread13:07:35

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

lread13:07:17

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.

lread13:07:57

> 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.

lread13:07:17

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

Ingy döt Net13:07:25

@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 Net13:07:12

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

Ingy döt Net13:07:03

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

Ingy döt Net13:07:31

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

borkdude13:07:01

Do you have one specific question?

Ingy döt Net13:07:04

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

borkdude13:07:39

yes, but not vice versa

2
Ingy döt Net13:07:09

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.

borkdude13:07:17

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

borkdude13:07:33

@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 Net13:07:56

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'
...

borkdude13:07:30

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

Ingy döt Net13:07:34

Sorry wasn't finished

Ingy döt Net13:07:59

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

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

Ingy döt Net13:07:22

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

borkdude13:07:20

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 Net13:07:24

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

borkdude13:07:40

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

Ingy döt Net13:07:00

I don't know the differences I guess

borkdude13:07:20

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

Ingy döt Net13:07:01

ok, so yes I would produce clojure forms

borkdude13:07:58

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