# What a translation keeps

How dmog translates between any two of its formats without a translator for each pair, and why a translation loses some things and refuses others.

dmog translates between 22 formats, which makes 462 ordered pairs. Nobody wrote 462 translators. Each format brings a few parts of its own, and every pair is composed from them.

## Three shapes

A translation works with three shapes:

- A tree: objects, arrays, and scalars, as in JSON, YAML, or TOML.
- Records: a table of rows under one set of columns, as in CSV.
- Text: characters in order, which is where every translation ends.

Reading is already shared. Every format's grammar builds its value through the same parser engine, and that value becomes one kind of event stream: start and end of an object or an array, keys, and scalars. A CSV file, a YAML file, and a Markdown file all arrive as events.

So translation is a question of writing. Each format says which shape it writes from, and ships a render: an alchemy program that turns that shape into its text.

## The parts of a format

The parts a format ships, which `dmog formats` lists, are these:

| Part | What it does |
|---|---|
| transducer | Reads the format's documents into events as the parser goes, so a document streams. |
| lift | Turns the events into the format's first read shape, where the events do not carry it. Markdown's lift takes a table's header row as its columns. |
| render | Writes the format's text from its write shape. |
| embedding | Maps a tree that is not the format's own into one its render can write. |
| loss | Sentences saying what a document written in the format does not keep. |

A translation from one format to another is the source's events, through the source's lift if it has one, through an adapter where the shapes differ, into the target's render. Between a tree and records there are two adapters: records become a tree as an array of objects, one for each row, and a tree becomes records when its root is an array of objects.

## Why a translation loses things

A format can only write what it can say. YAML has comments, and JSON does not. CSV cells are text, so a number written to CSV and read back is a string. XML elements carry attributes that a JSON object has no place for.

dmog does not hide these losses. Each format's `loss` field lists them, in sentences, and the [formats reference](https://dmog.tabnas.dev/reference/formats.html) prints them for every format. A translation that you need to reverse should be checked against the loss lists of both formats.

## Why a translation refuses some things

Some formats write only their own kind of document. A CSS render writes style sheets, and a list of books is not a style sheet: no mapping from one to the other means anything. C, CSS, PGN, Protocol Buffers, and the grammar notations are like this. Each declares the schema of the tree it writes, and refuses any other tree with `TARGET_VALUE_UNREPRESENTABLE`, before writing anything.

A refusal is better than a guess. A tool that turned any tree into some kind of style sheet would produce output that parses and means nothing, and the mistake would surface far from its cause.

The grammar notations, ABNF, EBNF, and GBNF, share a schema: a grammar's rules in source order. That is why each can write the others' grammars. Each refuses a rule it has no form for, naming the rule, such as a case-insensitive string in EBNF.

A program can bridge the gap. `--with` runs an alchemy program over the source, and a program that builds the target's own tree can be written by any of these renders. For how, see [Reshape a document while translating it](https://dmog.tabnas.dev/how-to/reshape-with-a-program.html).
