Skip to content
This repository has been archived by the owner on Dec 12, 2022. It is now read-only.

Releases: ts-graphviz/parser

v0.6.0 🌈

14 Aug 03:21
Compare
Choose a tag to compare

πŸš€ Features

Changes

v0.5.0 🌈

02 Aug 11:54
Compare
Choose a tag to compare

πŸš€ Features

Changes

Special thanks for @ChristianMurphy.

v0.4.2 🌈

17 Jul 10:55
Compare
Choose a tag to compare

πŸ› Bug Fixes

Changes

v0.4.1 🌈

17 Jul 08:47
Compare
Choose a tag to compare

Changes

v0.4.0 🌈

04 Jul 13:25
Compare
Choose a tag to compare

πŸš€ Features

Changes

v0.3.0 🌈

06 Jun 17:37
Compare
Choose a tag to compare

Changes

v0.2.0 🌈

15 May 16:06
Compare
Choose a tag to compare

Changes

v0.1.0 🌈

08 May 14:04
Compare
Choose a tag to compare

Graphviz dot language parser for ts-graphviz.

Installation

The module can then be installed using npm:

NPM

# yarn
$ yarn add @ts-graphviz/parser
# or npm
$ npm install -S @ts-graphviz/parser

High level API

parse function

Parse a string written in dot language and convert it to a model.

The return value is a Graph or Digraph that inherits from RootCluster.

import { parse } from '@ts-graphviz/parser';

const G = parse(`
digraph hoge {
  a -> b;
}`);

This is equivalent to the code below when using ts-graphviz.

import { digraph } from 'ts-graphviz';

const G = digraph('hoge', (g) => {
  g.edge(['a', 'b']);
});

If the given string is invalid, a SyntaxError exception will be thrown.

import { parse, SyntaxError } from '@ts-graphviz/parser';

try {
  parse(`invalid`);
} catch (e) {
  if (e instanceof SyntaxError) {
    console.log(e.message);
  }
}

dot tagged template

This is an experimental API.
Behavior may change in the future.

A tag template version of the parse function.

Returns a Graph or Digraph object based on the parsed result.

If the given string is invalid, a SyntaxError exception will be thrown.

import { dot } from '@ts-graphviz/parser';

const G = dot`
  graph {
    a -- b
  }
`;

Low lebel API

AST module

The AST module provides the ability to handle the AST as a result of parsing the dot language
for lower level operations.

AST.parse function

The basic usage is the same as the parse function, except that it returns the dot language AST.

import { inspect } from 'util';
import { AST } from '@ts-graphviz/parser';

const ast = AST.parse(`
  strict digraph example {
    subgraph cluster_0 {
      label="Subgraph A";
      a -> b -> c -> d;
    }

    subgraph cluster_1 {
      label="Subgraph B";
      a -> f;
      f -> c;
    }
  }
`);

console.log(inspect(ast, false, 6));

In the case of the above code, the structure of AST is as follows.

{
  type: 'graph',
  id: 'example',
  directed: true,
  strict: true,
  children: [
    {
      type: 'subgraph',
      id: 'cluster_0',
      children: [
        { type: 'attribute', key: 'label', value: 'Subgraph A' },
        {
          type: 'edge',
          targets: [ { id: 'a' }, { id: 'b' }, { id: 'c' }, { id: 'd' } ],
          attributes: []
        }
      ]
    },
    {
      type: 'subgraph',
      id: 'cluster_1',
      children: [
        { type: 'attribute', key: 'label', value: 'Subgraph B' },
        {
          type: 'edge',
          targets: [ { id: 'a' }, { id: 'f' } ],
          attributes: []
        },
        {
          type: 'edge',
          targets: [ { id: 'f' }, { id: 'c' } ],
          attributes: []
        }
      ]
    }
  ]
}