# core/converters/grammar.converter.ts

> 37 lines of code and 12 definitions.

Tree: Site tree
Language: typescript
Layer: processing
Canonical: https://banes-lab.com/anatomy/tree#file-core-converters-grammar-converter-ts
Source text: https://banes-lab.com/assets/sources/source.b95ac5703da2c3d95148e412af11548f61eba0466c2561bd75321661224a4c47.generated.txt

## Definitions

- `joinRules` (lexical_declaration, line 11, exported)
- `HEADING` (lexical_declaration, line 5)
- `GAP` (lexical_declaration, line 6)
- `PRODUCES` (lexical_declaration, line 7)
- `OPEN` (lexical_declaration, line 8)
- `CLOSE` (lexical_declaration, line 9)
- `keywordsOf` (lexical_declaration, line 15, exported)
- `group` (lexical_declaration, line 16, exported)
- `productionsOf` (lexical_declaration, line 27, exported)
- `found` (lexical_declaration, line 28, exported)
- `templateBodyOf` (lexical_declaration, line 37, exported)
- `templateConstraintsOf` (lexical_declaration, line 41, exported)

## Used by

- [configuration/strings/bnf.strings.ts](https://banes-lab.com/source/tree/configuration/strings/bnf.strings.ts.md)

## Source

```typescript
import type { GrammarRule, KeywordBlock } from "#types/grammar.types";
import { LINE_BREAK } from "#configuration/constants/code.constants";
import { ONTOLOGY } from "#core/assets/ontology.generated";

const HEADING = "# ";
const GAP = "\n\n";
const PRODUCES = " ::= ";
const OPEN = "<";
const CLOSE = ">";

export const joinRules = function joinRules(rules: readonly GrammarRule[]): string {
    return rules.map((rule) => HEADING + rule.title + LINE_BREAK + rule.grammar).join(GAP);
};

export const keywordsOf = function keywordsOf(category: string): KeywordBlock {
    const group = ONTOLOGY.grammar.categories.find((entry) => entry.category === category);
    return {
        keywords: (group?.keywords ?? []).map((keyword) => ({
            description: keyword.meaning,
            example: keyword.example,
            name: keyword.name,
        })),
        kind: "keyword",
    };
};

export const productionsOf = function productionsOf(group: string, title: string): GrammarRule {
    const found = ONTOLOGY.grammar.groups.find((entry) => entry.group === group);
    return {
        grammar: (found?.productions ?? [])
            .map((production) => OPEN + production.lhs + CLOSE + PRODUCES + production.rhs)
            .join(LINE_BREAK),
        title,
    };
};

export const templateBodyOf = function templateBodyOf(type: string): string {
    return ONTOLOGY.grammar.templates.find((template) => template.type === type)?.body ?? "";
};

export const templateConstraintsOf = function templateConstraintsOf(type: string): readonly string[] {
    return ONTOLOGY.grammar.templates.find((template) => template.type === type)?.constraints ?? [];
};
```
