Skip to content

Conversation

@dmarcotte
Copy link
Contributor

@dmarcotte dmarcotte commented Feb 28, 2025

An essential and long-anticipated piece of the puzzle: Kson now compiles to Yaml (including preserving the input's Kson comments in the compiled Yaml output!). This is expected to be one of the main ways Kson is initially integrated into existing systems: as a better interface on existing Yaml interfaces.

Notes on other compilation targets not yet implemented:

  • Json compilation is also planned and should follow fairly well from the work here
  • Compiling directly to native objects on each platform is also naturally coming at some point, though the Yaml and Json compilations take priority since they are fully interoperable with existing systems, and hence immediately beneficial

The heart of the work is in cfe0d6c, with the surrounding commits being a mix of shoring up this support and improving a few small things spotted around the project while implementing this.

Fix typo so that we're properly noting that _trailing_ comments are
discouraged, not line comments
An essential and long-anticipated piece of the puzzle: Kson now
compiles to Yaml. This is anticipated to be one of the main ways Kson
is integrated into existing systems: as a better interface on existing
Yaml interfaces.

Notes on other compilation targets not yet implemented:
- Json compilation is also planned and should follow fairly well from
  the work in this commit
- Compiling directly to native objects on each platform is also
  naturally coming at some point, though the Yaml and Json
  transpilations take priority since they are fully interoperable with
  existing systems, and hence immediately beneficial
Validate that the compiled YAML we're verifying in our tests is,
in fact, legal Yaml.  We may need deeper verifications at some point,
but for now this is a good guardrail against accidentally writing
bad/invalid Yaml compilation tests
Since we finished refactoring all errors out of the lexer in ede3d3b,
`messageSink` is always guaranteed to have no errors from the Lexer,
so both conditional check and the `// parsing failed at the lexing stage`
comment are stale and possibly confusing.  Clean this up for clarity.
Parsing to Yaml will be a common use case for Kson, so expose it
clearly in our public interface in `Kson.parseToYaml`
Fix a couple of typos and have some second thoughts on the working in
this comment.  I believe these edits improve the clarity.
The [NumberFormatException] reference stopped resolving correctly
after the recent Kotlin upgrade and the warning it created prompted
me to improve what this comment was trying to accomplish:

Replace the comment with an explicit and helpful RuntimeException to
better communicate our expectations if/when anyone ever trips on this.

A developer accidentally tripping this may have missed the comment, but
the new exception really captures the semantics of `NumberNode`'s
demand to only be given strings which are guaranteed to parse as
`Double`
Rather than check the `yamlResult` produced by our compiler, we  instead
check the `expectedYaml` created by the test writer

We want to tell the test writer they are expecting illegal Yaml as early
as possible so they can fix their expectations rather than mistakenly
adjust the underlying code to try to satisfy an invalid expectation
The content of our StringNode and IdentifierNode is better named
`stringContent` since these are strings that sometimes conform to
the unquoted "identifier" syntax, but always contain string content.
`keyword` was especially misleading in the content of StringNode
When compiling to Yaml, we need to preserve  leading whitespace, no
matter what, even on blank/all-whitespace lines. Fix and test.
When compiling Kson to YAML, embed blocks are normally compiled to
multiline strings since a primary use case for the embed tags is to
facilitate better editor tooling, especially in the case of embedded
code, so to use Kson as an interface on some Yaml that expects code
embedded in multiline strings, Kson's embed blocks must collapse down
to Yaml multiline string.

However, the embed tag metadata is valuable, so this provides an option,
`retainEmbedTags`, to preserve it in our compiled Yaml.  With this
enabled, embed blocks with an embed tag compile to Yaml objects like
this:

```yaml
embedTag: "sql"
embedContent: |
  select from table
```
@dmarcotte dmarcotte merged commit a0ff512 into kson-org:main Feb 28, 2025
1 check passed
@dmarcotte dmarcotte deleted the kson-to-yaml branch February 28, 2025 23:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant