YAML Syntax for Racket Languages
1 YAML to Code Mapping
8.12

YAML Syntax for Racket Languages🔗ℹ

Eutro

 #lang yaml-exp package: yaml-exp
The yaml-exp language replaces the reader for any module language with a YAML reader using the yaml package. Here’s an example:
#lang yaml-exp racket/base
- define:
  - fibbi: [x]
  - if:
    - <=:
      - x
      - 1
    - x
    - +:
      - fibbi:
        - -:
          - x
          - 1
      - fibbi:
        - -:
          - x
          - 2
- displayln:
  - fibbi: [10]

1 YAML to Code Mapping🔗ℹ

The reader clearly doesn’t spit out beautiful maps for you like it might for a pure YAML document. In fact, it performs a few conversions to produce "usable" source code. These are:
  • Plain, unquoted strings are converted to symbols when the entire string is a readable symbol as-is. (Otherwise they remain as strings). For example, define would be read as a symbol, but Hello world! would be read as a string (though be careful with commas if you are in a flow-style sequence), and no would be a boolean as usual.

  • Mappings are interpreted as a sequence of forms, one for each key-value pair, where each key is simply consed with its associated value. If the map only has one key-value pair, then it is returned alone, otherwise all the key-value pairs are returned as a list. Pairs and improper lists can be constructed this way. These cannot use !!merge tags, use !hash (below) for that. Examples:
    • YAML:
      - define: [x, 10]
      Becomes:

      (define x 10)

    • YAML:
      - define:
        - foo: args
        - args
      Becomes:
      (define (foo . args)
        args)

    • YAML:
      - define:
        - foo:
           a:
            b: tail
        - tail
      Becomes:
      (define (foo a b . tail)
        tail)

    • YAML:
      - let*:
        - { y: [f: [x]]
          , z: [+: [y, 1]] }
        - !sym "*": [y, z]
      Becomes:
      (let* ((y (f x))
             (z (+ y 1)))
        (* y z))

  • Sequences continue to be lists, flow-style or not.

  • Finally, there are some tags available:
    • !kw keyword produces #:keywords, while !sym symbol produces symbols, without any restrictions on the content.

    • !vector and !hash both produce vector?s and hash?es, from sequences and mappings, respectively.

    • !char <char-constant> reads as #\<char-constant> does; see Reading Characters.

    • All the tags (global and local) from base yaml are also available; see YAML Expressions.