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

ts-graphviz/parser

Repository files navigation

NodeCI npm version License: MIT PRs Welcome tested with jest code style: prettier

@ts-graphviz/parser

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.

function AST.parse(dot: string, options?: ParseOption)

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

  • Parameters
    • dot -- string in the dot language to be parsed.
    • options.rule -- Object type of dot string.
      • This can be "node", "edge", "graph", "attributes", "attribute", "cluster_statements".
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(ast);
// {
//   type: 'graph',
//   id: 'example',
//   directed: true,
//   strict: true,
//   body: [
//     {
//       type: 'subgraph',
//       id: 'cluster_0',
//       body: [
//         { type: 'attribute', key: 'label', value: 'Subgraph A' },
//         {
//           type: 'edge',
//           targets: [ { id: 'a' }, { id: 'b' }, { id: 'c' }, { id: 'd' } ],
//           attributes: []
//         }
//       ]
//     },
//     {
//       type: 'subgraph',
//       id: 'cluster_1',
//       body: [
//         { type: 'attribute', key: 'label', value: 'Subgraph B' },
//         {
//           type: 'edge',
//           targets: [ { id: 'a' }, { id: 'f' } ],
//           attributes: []
//         },
//         {
//           type: 'edge',
//           targets: [ { id: 'f' }, { id: 'c' } ],
//           attributes: []
//         }
//       ]
//     }
//   ]
// }
Example: Specifying the rule option
import { AST } from '@ts-graphviz/parser';

const ast = AST.parse(
  `test [
    style=filled;
    color=lightgrey;
    label = "example #1";
  ];`,
  { rule: AST.Types.Node },
);

console.log(ast);
// {
//   type: 'node',
//   id: 'test',
//   attributes: [
//     { key: 'style', value: 'filled' },
//     { key: 'color', value: 'lightgrey' },
//     { key: 'label', value: 'example #1' }
//   ]
// }

See Also

Graphviz-dot Test and Integration

License

This software is released under the MIT License, see LICENSE.