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

v0.1.0 🌈

Compare
Choose a tag to compare
@github-actions github-actions released this 08 May 14:04
· 96 commits to main since this release

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: []
        }
      ]
    }
  ]
}