# @gram-data/gram-ast

> AST definitions for gram data graphs

Latest version **0.3.11** (published 2021-01-22) · MIT license · 0 weekly downloads

## Install

```sh
npm install @gram-data/gram-ast
pnpm add @gram-data/gram-ast
yarn add @gram-data/gram-ast
bun add @gram-data/gram-ast
```

## Health

**Score 30/100 (F)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.3.11 |
| Published | 2021-01-22 |
| First published | 2020-07-05 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Node | >=10 |
| Dependencies | 0 |
| Unpacked size | 169.5 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 26 |
| Author | Andreas Kollegger |
| Maintainers | akollegger |
| Keywords | graph, neo4j, gram, unified, ast |

## Links

- npm: https://www.npmjs.com/package/@gram-data/gram-ast
- Repository: https://github.com/gram-data/gram-js
- Homepage: https://gram-data.github.io/gram-js/api/modules/gram-ast
- Issues: https://github.com/gram-data/gram-js/issues
- npm.io page: https://npm.io/package/@gram-data/gram-ast

## Alternatives

- [update-check](https://npm.io/package/update-check.md) — 4.0M weekly downloads
- [react-native-onesignal](https://npm.io/package/react-native-onesignal.md) — 134.5K weekly downloads
- [react-redux-toastr](https://npm.io/package/react-redux-toastr.md) — 33.7K weekly downloads
- [@nocobase/plugin-notification-manager](https://npm.io/package/@nocobase/plugin-notification-manager.md) — 2.0K weekly downloads
- [react-simple-toasts](https://npm.io/package/react-simple-toasts.md) — 1.9K weekly downloads

## Recent versions

- 0.3.11 (latest) — 2021-01-22
- 0.4.0-alpha.8 (alpha) — 2021-01-22
- 0.3.10 — 2021-01-22
- 0.4.0-alpha.6 — 2021-01-22
- 0.4.0-alpha.5 — 2021-01-19
- 0.4.0-alpha.4 — 2021-01-16
- 0.4.0-alpha.3 — 2021-01-15
- 0.4.0-alpha.2 — 2021-01-15
- 0.4.0-alpha.1 — 2021-01-15
- 0.3.9 — 2021-01-13
- 0.3.9-alpha.2 — 2020-12-19
- 0.3.8 — 2020-12-16
- 0.3.7 — 2020-12-16
- 0.3.6 — 2020-12-16
- 0.3.5 — 2020-12-10
- … 25 more at https://npm.io/package/@gram-data/gram-ast/versions

## README

`()-[define]->(ast)`

Gram abstract syntax tree definitions, tokenizing regexes, and utilities like type guards.

## How to gram-ast

### Install:

```bash
npm install @gram-data/gram-ast
```

### Use [[gram-parse]] to create an AST, then introspect with gram-ast:

```TypeScript
import { isGramSeq, isGramNode, isGramEdge } from '@gram-data/gram-ast';
import { toAST } from '@gram-data/gram-parse';  

const src = '(a)-->(b)';
const parsed = toAST(src);

// the top-level of the AST is a sequence of paths
console.assert(isGramSeq(parsed));

// the first path should be an edge
const firstPath = parsed.children[0];
console.assert(isGramEdge(firstPath));

// the children of an edge are nodes
console.assert(isGramNode(firstPath.children[0]));
console.assert(isGramNode(firstPath.children[1]));
```

## Syntax Tree

The `gram` AST is based on the [unist](https://github.com/syntax-tree/unist) specification
for syntax trees. Many of the tools and techniques of the [unified](https://unifiedjs.com)
ecosystem can be applied to working with `gram`.

Gram represents data using two basic elements: paths and sequences.

Paths provide structure. Sequences provide order.

### {@linkcode gram-ast.GramSeq | GramSeq}

A `gram` sequence is the root element of an AST.
It is exactly what it sounds like: a sequence of elements where
each element is a path.

The AST type is useful in returning a well-rooted tree that can be processed
by general-purpose AST tools like [unist-util-visit](https://github.com/syntax-tree/unist-util-visit).

In practice this is equivalent to a `GramPath[]`. Most `gram` functions will accept either.

### {@linkcode gram-ast.GramPath | GramPath}

A `gram` path is either an empty path, or the composition of two other paths.
The data structure of a path is like a list which remembers how it was assembled.
The list elements are other paths.

Each path has its own identity, labels and a data record.

### [record](../interfaces/gram_ast.gramrecord-1.html)

In the AST, records are a multimap presented as an _array_ of name/value
properties. That means a property name _may_ appear more than once, with
different or the same values.

When mapping to a data model, choose one of these strageies for handling the
multimapped properties:

- single-state: pick the first or last value (_recommended_)
- join: accumulate the values into an array
- reduce: aggregate the values into a single value

## Next Steps

- Learn more about parsing with [[gram-ast]]
- Transform to js objects using [[gram-value]]
- Serialize back to text using [[gram-stringify]]

---
_Source: https://npm.io/package/@gram-data/gram-ast · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
