@ts-graphviz/ast 0.0.0-pr956-20240225073457 → 0.0.0-pr956-20240225160253

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (71) hide show
  1. package/CHANGELOG.md +291 -3
  2. package/lib/ast.cjs +25 -60
  3. package/lib/ast.js +25 -60
  4. package/package.json +43 -24
  5. package/src/ast.ts +0 -8
  6. package/src/builder/__snapshots__/create-element.test.ts.snap +0 -166
  7. package/src/builder/builder.test.ts +0 -36
  8. package/src/builder/builder.ts +0 -44
  9. package/src/builder/create-element.test.ts +0 -110
  10. package/src/builder/create-element.ts +0 -13
  11. package/src/builder/index.ts +0 -3
  12. package/src/builder/types.ts +0 -226
  13. package/src/dot-shim/index.ts +0 -2
  14. package/src/dot-shim/parser/__snapshots__/parse.test.ts.snap +0 -2613
  15. package/src/dot-shim/parser/dot.peggy +0 -396
  16. package/src/dot-shim/parser/index.ts +0 -1
  17. package/src/dot-shim/parser/parse.test.ts +0 -263
  18. package/src/dot-shim/parser/parse.ts +0 -121
  19. package/src/dot-shim/parser/peggy.options.json +0 -15
  20. package/src/dot-shim/printer/index.ts +0 -6
  21. package/src/dot-shim/printer/plugins/AttributeListPrintPlugin.ts +0 -22
  22. package/src/dot-shim/printer/plugins/AttributePrintPlugin.ts +0 -11
  23. package/src/dot-shim/printer/plugins/CommentPrintPlugin.ts +0 -40
  24. package/src/dot-shim/printer/plugins/DotPrintPlugin.ts +0 -12
  25. package/src/dot-shim/printer/plugins/EdgePrintPlugin.ts +0 -27
  26. package/src/dot-shim/printer/plugins/GraphPrintPlugin.ts +0 -32
  27. package/src/dot-shim/printer/plugins/LiteralPrintPlugin.ts +0 -24
  28. package/src/dot-shim/printer/plugins/NodePrintPlugin.ts +0 -24
  29. package/src/dot-shim/printer/plugins/NodeRefGroupPrintPlugin.ts +0 -17
  30. package/src/dot-shim/printer/plugins/NodeRefPrintPlugin.ts +0 -18
  31. package/src/dot-shim/printer/plugins/SubgraphPrintPlugin.ts +0 -27
  32. package/src/dot-shim/printer/plugins/index.ts +0 -26
  33. package/src/dot-shim/printer/plugins/utils/index.ts +0 -1
  34. package/src/dot-shim/printer/plugins/utils/tokens.ts +0 -51
  35. package/src/dot-shim/printer/printer.ts +0 -46
  36. package/src/dot-shim/printer/stringify.test.ts +0 -823
  37. package/src/dot-shim/printer/stringify.ts +0 -19
  38. package/src/dot-shim/printer/types.ts +0 -74
  39. package/src/model-shim/from-model/converter.ts +0 -43
  40. package/src/model-shim/from-model/from-model.ts +0 -19
  41. package/src/model-shim/from-model/index.ts +0 -3
  42. package/src/model-shim/from-model/plugins/AttributeListPlugin.ts +0 -19
  43. package/src/model-shim/from-model/plugins/EdgePlugin.ts +0 -140
  44. package/src/model-shim/from-model/plugins/GraphPlugin.ts +0 -36
  45. package/src/model-shim/from-model/plugins/NodePlugin.ts +0 -33
  46. package/src/model-shim/from-model/plugins/SubraphPlugin.ts +0 -28
  47. package/src/model-shim/from-model/plugins/index.ts +0 -13
  48. package/src/model-shim/from-model/plugins/utils/convert-attribute.ts +0 -47
  49. package/src/model-shim/from-model/plugins/utils/convert-cluster-children.ts +0 -44
  50. package/src/model-shim/from-model/plugins/utils/convert-comment.ts +0 -16
  51. package/src/model-shim/from-model/plugins/utils/index.ts +0 -3
  52. package/src/model-shim/from-model/types.ts +0 -62
  53. package/src/model-shim/index.ts +0 -2
  54. package/src/model-shim/to-model/converter.ts +0 -42
  55. package/src/model-shim/to-model/index.ts +0 -3
  56. package/src/model-shim/to-model/plugins/DotPlugin.ts +0 -25
  57. package/src/model-shim/to-model/plugins/EdgePlugin.ts +0 -26
  58. package/src/model-shim/to-model/plugins/GraphPlugin.ts +0 -15
  59. package/src/model-shim/to-model/plugins/NodePlugin.ts +0 -25
  60. package/src/model-shim/to-model/plugins/SubgraphPlugin.ts +0 -14
  61. package/src/model-shim/to-model/plugins/index.ts +0 -13
  62. package/src/model-shim/to-model/plugins/utils/apply-statments.ts +0 -96
  63. package/src/model-shim/to-model/plugins/utils/comment-holder.ts +0 -31
  64. package/src/model-shim/to-model/plugins/utils/convert-to-edge-target-tuple.ts +0 -21
  65. package/src/model-shim/to-model/to-model.test.ts +0 -34
  66. package/src/model-shim/to-model/to-model.ts +0 -16
  67. package/src/model-shim/to-model/types.ts +0 -71
  68. package/src/types.ts +0 -370
  69. package/tsconfig.json +0 -8
  70. package/typedoc.json +0 -4
  71. package/vite.config.ts +0 -22
@@ -1,25 +0,0 @@
1
- import { DotASTNode } from '../../../types.js';
2
- import { ConvertToModelPlugin } from '../types.js';
3
- import { CommentHolder } from './utils/comment-holder.js';
4
-
5
- export const DotPlugin: ConvertToModelPlugin<DotASTNode> = {
6
- match(ast) {
7
- return ast.type === 'Dot';
8
- },
9
- convert(context, ast) {
10
- const commentHolder = new CommentHolder();
11
- for (const stmt of ast.children) {
12
- switch (stmt.type) {
13
- case 'Comment':
14
- commentHolder.set(stmt);
15
- break;
16
- case 'Graph': {
17
- const graph = context.convert(stmt);
18
- commentHolder.apply(graph, stmt.location);
19
- return graph;
20
- }
21
- }
22
- }
23
- throw Error();
24
- },
25
- };
@@ -1,26 +0,0 @@
1
- import { AttributeASTNode, EdgeASTNode } from '../../../types.js';
2
- import { ConvertToModelPlugin } from '../types.js';
3
- import { convertToEdgeTargetTuple } from './utils/convert-to-edge-target-tuple.js';
4
-
5
- export const EdgePlugin: ConvertToModelPlugin<EdgeASTNode> = {
6
- match(ast) {
7
- return ast.type === 'Edge';
8
- },
9
- convert(context, ast) {
10
- const edge = new context.models.Edge(
11
- convertToEdgeTargetTuple(ast),
12
- ast.children
13
- .filter<AttributeASTNode>(
14
- (v): v is AttributeASTNode => v.type === 'Attribute',
15
- )
16
- .reduce(
17
- (acc, curr) => {
18
- acc[curr.key.value] = curr.value.value;
19
- return acc;
20
- },
21
- {} as { [key: string]: string },
22
- ),
23
- );
24
- return edge;
25
- },
26
- };
@@ -1,15 +0,0 @@
1
- import { GraphASTNode } from '../../../types.js';
2
- import { ConvertToModelPlugin } from '../types.js';
3
- import { applyStatements } from './utils/apply-statments.js';
4
-
5
- export const GraphPlugin: ConvertToModelPlugin<GraphASTNode> = {
6
- match(ast) {
7
- return ast.type === 'Graph';
8
- },
9
- convert(context, ast) {
10
- const G = ast.directed ? context.models.Digraph : context.models.Graph;
11
- const graph = new G(ast.id?.value, ast.strict);
12
- applyStatements(graph, ast.children);
13
- return graph;
14
- },
15
- };
@@ -1,25 +0,0 @@
1
- import { AttributeASTNode, NodeASTNode } from '../../../types.js';
2
- import { ConvertToModelPlugin } from '../types.js';
3
-
4
- export const NodePlugin: ConvertToModelPlugin<NodeASTNode> = {
5
- match(ast) {
6
- return ast.type === 'Node';
7
- },
8
- convert(context, ast) {
9
- const node = new context.models.Node(
10
- ast.id.value,
11
- ast.children
12
- .filter<AttributeASTNode>(
13
- (v): v is AttributeASTNode => v.type === 'Attribute',
14
- )
15
- .reduce(
16
- (acc, curr) => {
17
- acc[curr.key.value] = curr.value.value;
18
- return acc;
19
- },
20
- {} as { [key: string]: string },
21
- ),
22
- );
23
- return node;
24
- },
25
- };
@@ -1,14 +0,0 @@
1
- import { SubgraphASTNode } from '../../../types.js';
2
- import { ConvertToModelPlugin } from '../types.js';
3
- import { applyStatements } from './utils/apply-statments.js';
4
-
5
- export const SubgraphPlugin: ConvertToModelPlugin<SubgraphASTNode> = {
6
- match(ast) {
7
- return ast.type === 'Subgraph';
8
- },
9
- convert(context, ast) {
10
- const subgraph = new context.models.Subgraph(ast.id?.value);
11
- applyStatements(subgraph, ast.children);
12
- return subgraph;
13
- },
14
- };
@@ -1,13 +0,0 @@
1
- import { DotPlugin } from './DotPlugin.js';
2
- import { EdgePlugin } from './EdgePlugin.js';
3
- import { GraphPlugin } from './GraphPlugin.js';
4
- import { NodePlugin } from './NodePlugin.js';
5
- import { SubgraphPlugin } from './SubgraphPlugin.js';
6
-
7
- export const defaultPlugins = [
8
- NodePlugin,
9
- EdgePlugin,
10
- SubgraphPlugin,
11
- GraphPlugin,
12
- DotPlugin,
13
- ];
@@ -1,96 +0,0 @@
1
- import { GraphBaseModel } from '@ts-graphviz/common';
2
- import {
3
- AttributeASTNode,
4
- ClusterStatementASTNode,
5
- } from '../../../../types.js';
6
- import { CommentHolder } from './comment-holder.js';
7
- import { convertToEdgeTargetTuple } from './convert-to-edge-target-tuple.js';
8
-
9
- export function applyStatements(
10
- graph: GraphBaseModel,
11
- statements: ClusterStatementASTNode[],
12
- ): void {
13
- const commentHolder = new CommentHolder();
14
- for (const stmt of statements) {
15
- switch (stmt.type) {
16
- case 'Subgraph': {
17
- const subgraph = stmt.id
18
- ? graph.subgraph(stmt.id.value)
19
- : graph.subgraph();
20
- applyStatements(subgraph, stmt.children);
21
- commentHolder.apply(subgraph, stmt.location);
22
- break;
23
- }
24
- case 'Attribute':
25
- graph.set(stmt.key.value, stmt.value.value);
26
- commentHolder.reset();
27
- break;
28
- case 'Node':
29
- commentHolder.apply(
30
- graph.node(
31
- stmt.id.value,
32
- stmt.children
33
- .filter<AttributeASTNode>(
34
- (v): v is AttributeASTNode => v.type === 'Attribute',
35
- )
36
- .reduce(
37
- (acc, curr) => {
38
- acc[curr.key.value] = curr.value.value;
39
- return acc;
40
- },
41
- {} as { [key: string]: string },
42
- ),
43
- ),
44
- stmt.location,
45
- );
46
- break;
47
- case 'Edge':
48
- commentHolder.apply(
49
- graph.edge(
50
- convertToEdgeTargetTuple(stmt),
51
- stmt.children
52
- .filter<AttributeASTNode>(
53
- (v): v is AttributeASTNode => v.type === 'Attribute',
54
- )
55
- .reduce(
56
- (acc, curr) => {
57
- acc[curr.key.value] = curr.value.value;
58
- return acc;
59
- },
60
- {} as { [key: string]: string },
61
- ),
62
- ),
63
- stmt.location,
64
- );
65
- break;
66
- case 'AttributeList': {
67
- const attrs = stmt.children
68
- .filter<AttributeASTNode>(
69
- (v): v is AttributeASTNode => v.type === 'Attribute',
70
- )
71
- .reduce(
72
- (acc, curr) => {
73
- acc[curr.key.value] = curr.value.value;
74
- return acc;
75
- },
76
- {} as { [key: string]: string },
77
- );
78
- switch (stmt.kind) {
79
- case 'Edge':
80
- graph.edge(attrs);
81
- break;
82
- case 'Node':
83
- graph.node(attrs);
84
- break;
85
- case 'Graph':
86
- graph.graph(attrs);
87
- break;
88
- }
89
- commentHolder.reset();
90
- break;
91
- }
92
- case 'Comment':
93
- commentHolder.set(stmt);
94
- }
95
- }
96
- }
@@ -1,31 +0,0 @@
1
- import type { HasComment } from '@ts-graphviz/common';
2
- import { CommentASTNode, FileRange } from '../../../../types.js';
3
-
4
- export class CommentHolder {
5
- public comment: CommentASTNode | null = null;
6
-
7
- public set(comment: CommentASTNode): void {
8
- this.comment = comment;
9
- }
10
-
11
- public reset(): void {
12
- this.comment = null;
13
- }
14
-
15
- public apply(model: HasComment, location?: FileRange): void {
16
- if (location && this.comment?.location) {
17
- if (this.comment?.kind === 'Block') {
18
- if (this.comment.location.end.line === location.start.line - 1) {
19
- model.comment = this.comment.value;
20
- }
21
- } else {
22
- if (this.comment.location.end.line === location.start.line) {
23
- model.comment = this.comment.value;
24
- }
25
- }
26
- } else {
27
- model.comment = this.comment?.value;
28
- }
29
- this.reset();
30
- }
31
- }
@@ -1,21 +0,0 @@
1
- import { EdgeTarget, EdgeTargetTuple } from '@ts-graphviz/common';
2
- import { EdgeASTNode } from '../../../../types.js';
3
-
4
- export function convertToEdgeTargetTuple(edge: EdgeASTNode): EdgeTargetTuple {
5
- return edge.targets.map((t): EdgeTarget => {
6
- switch (t.type) {
7
- case 'NodeRef':
8
- return {
9
- id: t.id.value,
10
- port: t.port?.value,
11
- compass: t.compass?.value,
12
- };
13
- case 'NodeRefGroup':
14
- return t.children.map((t) => ({
15
- id: t.id.value,
16
- port: t.port?.value,
17
- compass: t.compass?.value,
18
- }));
19
- }
20
- }) as EdgeTargetTuple;
21
- }
@@ -1,34 +0,0 @@
1
- import '@ts-graphviz/common';
2
- import '@ts-graphviz/core';
3
- import { expect, test } from 'vitest';
4
- import { createElement } from '../../builder/create-element.js';
5
- import { toModel } from './to-model.js';
6
-
7
- test('brank ast throw error', () => {
8
- const ast = createElement('Dot', {}, []);
9
-
10
- expect(() => toModel(ast)).toThrow();
11
- });
12
-
13
- test('comment', () => {
14
- const ast = createElement('Dot', {}, [
15
- createElement(
16
- 'Comment',
17
- {
18
- kind: 'Macro',
19
- value: 'This is comment',
20
- },
21
- [],
22
- ),
23
- createElement(
24
- 'Graph',
25
- {
26
- strict: false,
27
- directed: true,
28
- },
29
- [],
30
- ),
31
- ]);
32
- const model = toModel(ast);
33
- expect(model.comment).toStrictEqual('This is comment');
34
- });
@@ -1,16 +0,0 @@
1
- import { ToModelConverter } from './converter.js';
2
- import {
3
- ASTToModel,
4
- ConvertToModelOptions,
5
- ToModelConvertableASTNode,
6
- } from './types.js';
7
-
8
- /**
9
- * @group Convert AST to Model
10
- */
11
- export function toModel<T extends ToModelConvertableASTNode>(
12
- ast: T,
13
- options?: ConvertToModelOptions,
14
- ): ASTToModel<T> {
15
- return new ToModelConverter(options).convert(ast);
16
- }
@@ -1,71 +0,0 @@
1
- import {
2
- EdgeModel,
3
- ModelsContext,
4
- NodeModel,
5
- RootGraphModel,
6
- SubgraphModel,
7
- } from '@ts-graphviz/common';
8
- import {
9
- DotASTNode,
10
- EdgeASTNode,
11
- GraphASTNode,
12
- NodeASTNode,
13
- SubgraphASTNode,
14
- } from '../../types.js';
15
-
16
- /**
17
- * ModelOf is a type that determines the type of model to use depending on the value of T.
18
- * @group AST
19
- */
20
- export type ModelOf<T> = T extends 'Dot' | 'Graph'
21
- ? RootGraphModel
22
- : T extends 'Edge'
23
- ? EdgeModel
24
- : T extends 'Node'
25
- ? NodeModel
26
- : T extends 'Subgraph'
27
- ? SubgraphModel
28
- : never;
29
-
30
- /**
31
- * ASTToModel is a type that determines a model type from an AST.
32
- *
33
- * @group AST
34
- */
35
- export type ASTToModel<T> = T extends { type: infer U } ? ModelOf<U> : never;
36
-
37
- /**
38
- * This type is used to define what AST nodes can be converted to a model.
39
- * @group Convert AST to Model
40
- */
41
- export type ToModelConvertableASTNode =
42
- | DotASTNode
43
- | GraphASTNode
44
- | SubgraphASTNode
45
- | NodeASTNode
46
- | EdgeASTNode;
47
-
48
- /**
49
- * @group Convert AST to Model
50
- */
51
- export interface ConvertToModelOptions {
52
- models?: Partial<ModelsContext>;
53
- }
54
-
55
- /**
56
- * @group Convert AST to Model
57
- */
58
- export interface ConvertToModelContext {
59
- models: ModelsContext;
60
- convert<T extends ToModelConvertableASTNode>(ast: T): ASTToModel<T>;
61
- }
62
-
63
- /**
64
- * @group Convert AST to Model
65
- */
66
- export interface ConvertToModelPlugin<
67
- T extends ToModelConvertableASTNode = ToModelConvertableASTNode,
68
- > {
69
- match(ast: T): boolean;
70
- convert(context: ConvertToModelContext, ast: T): ASTToModel<T>;
71
- }