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

Commit

Permalink
reflect comment to model
Browse files Browse the repository at this point in the history
  • Loading branch information
kamiazya committed Jun 20, 2021
1 parent 0907719 commit 56e16df
Show file tree
Hide file tree
Showing 4 changed files with 209 additions and 151 deletions.
4 changes: 2 additions & 2 deletions grammar/dot.peggy
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ _slash_comment
}

_slash_comment_line
= "//" v:(!_newline v:. { return v; })* _newline? {
= _ "//" v:(!_newline v:. { return v; })* _newline? {
return v.join('');
}

Expand All @@ -200,7 +200,7 @@ _macro_comment
}

_macro_comment_line
= "#" v:(!_newline v:. { return v; })* _newline? {
= _ "#" v:(!_newline v:. { return v; })* _newline? {
return v.join('');
}

Expand Down
5 changes: 3 additions & 2 deletions src/ast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { parse as _parse, IFileRange } from './dot.peggy';
* @alpha
*/
export namespace AST {
export type FileRange = IFileRange;
type ValueOf<T> = T[keyof T];

/**
Expand Down Expand Up @@ -42,7 +43,7 @@ export namespace AST {
* must specify a type property.
*/
type: string;
location: IFileRange;
location: FileRange;
}

export interface ASTBaseParent<STMT extends ASTBaseNode = ASTBaseNode> extends ASTBaseNode {
Expand All @@ -64,7 +65,7 @@ export namespace AST {
*/
export interface Graph extends ASTBaseParent<ClusterStatement> {
type: typeof Types.Graph;
id?: string;
id?: Literal;
directed: boolean;
strict: boolean;
}
Expand Down
67 changes: 56 additions & 11 deletions src/convert.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,60 @@
import { ICluster, Digraph, Graph, RootCluster, Subgraph, Node, Edge } from 'ts-graphviz';
import { ICluster, Digraph, Graph, RootCluster, Subgraph, Node, Edge, IHasComment } from 'ts-graphviz';
import { AST } from './ast';

class CommentHolder {
public comment: AST.Comment | null = null;

public set(comment: AST.Comment): void {
this.comment = comment;
}

public reset(): void {
this.comment = null;
}

public apply(model: IHasComment, location: AST.FileRange): void {
if (this.comment?.kind === AST.Comment.Kind.Block) {
if (this.comment?.location.end.line === location.start.line - 1) {
model.comment = this.comment.value;
}
} else {
if (this.comment?.location.end.line === location.start.line) {
model.comment = this.comment.value;
}
}
this.reset();
}
}

function applyStatements(cluster: ICluster, statements: AST.ClusterStatement[]): void {
const commentHolder = new CommentHolder();
for (const stmt of statements) {
switch (stmt.type) {
case AST.Types.Subgraph:
const subgraph = stmt.id ? cluster.subgraph(stmt.id.value) : cluster.subgraph();
applyStatements(subgraph, stmt.body);
commentHolder.apply(subgraph, stmt.location);
break;
case AST.Types.Attribute:
cluster.set(stmt.key.value, stmt.value.value);
commentHolder.reset();
break;
case AST.Types.Node:
cluster.node(
stmt.id.value,
stmt.body.reduce((prev, curr) => ({ ...prev, [curr.key.value]: curr.value.value }), {}),
commentHolder.apply(
cluster.node(
stmt.id.value,
stmt.body.reduce((prev, curr) => ({ ...prev, [curr.key.value]: curr.value.value }), {}),
),
stmt.location,
);
break;
case AST.Types.Edge:
cluster.edge(
stmt.targets.map((t) => ({ id: t.id.value, port: t.port?.value, compass: t.commpass?.value })),
stmt.body.reduce((prev, curr) => ({ ...prev, [curr.key.value]: curr.value.value }), {}),
commentHolder.apply(
cluster.edge(
stmt.targets.map((t) => ({ id: t.id.value, port: t.port?.value, compass: t.commpass?.value })),
stmt.body.reduce((prev, curr) => ({ ...prev, [curr.key.value]: curr.value.value }), {}),
),
stmt.location,
);
break;
case AST.Types.Attributes:
Expand All @@ -38,7 +72,10 @@ function applyStatements(cluster: ICluster, statements: AST.ClusterStatement[]):
cluster.graph(attrs);
break;
}
commentHolder.reset();
break;
case AST.Types.Comment:
commentHolder.set(stmt);
}
}
}
Expand All @@ -57,7 +94,7 @@ export function convert(
switch (ast.type) {
case AST.Types.Graph:
const Root = ast.directed ? Digraph : Graph;
const root = new Root(ast.id, ast.strict);
const root = new Root(ast.id?.value, ast.strict);
applyStatements(root, ast.body);
return root;
case AST.Types.Subgraph:
Expand All @@ -77,9 +114,17 @@ export function convert(
);
return node;
case AST.Types.Dot:
const graph = ast.body.find((n): n is AST.Graph => n.type === AST.Types.Graph);
if (graph) {
return convert(graph);
const commentHolder = new CommentHolder();
for (const stmt of ast.body) {
switch (stmt.type) {
case AST.Types.Comment:
commentHolder.set(stmt);
break;
case AST.Types.Graph:
const graph = convert(stmt);
commentHolder.apply(graph, stmt.location);
return graph;
}
}
default:
throw Error();
Expand Down
Loading

0 comments on commit 56e16df

Please sign in to comment.