Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: create sankey parser and integrate sankey parser into mermaid package #4799

Open
wants to merge 35 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
7ca02da
feat: create `sankey` parser and integrate `sankey` parser into `merm…
Yokozuna59 Sep 2, 2023
a10821f
chore: remove sankey links map
Yokozuna59 Sep 2, 2023
9ad5536
chore: remove sankey links map
Yokozuna59 Sep 2, 2023
9a8963a
Merge branch 'next' into add-sankey-langium-parser
Yokozuna59 Sep 2, 2023
8feef1e
chore: `vi.mock` sankey renderer
Yokozuna59 Sep 2, 2023
efbb1f1
styles: refactor `switch..case` into `Record` for matcher regexes
Yokozuna59 Sep 2, 2023
b6906a6
fix: match until comments in `sankey` diagram
Yokozuna59 Sep 2, 2023
8c40eb3
Merge branch next into add-sankey-langium-parser
Yokozuna59 Sep 20, 2023
63430d2
pref(sankey): combine `source` and `target` rules as `node`
Yokozuna59 Sep 20, 2023
5fe951a
chore: update rule name of why we can't use default import in sankey
Yokozuna59 Sep 20, 2023
6187f4b
change test case with double quotes values
Yokozuna59 Sep 20, 2023
709ece5
Merge branch 'next' into add-sankey-langium-parser
Yokozuna59 Feb 12, 2024
aa9e875
update pnpm-lock.yaml
Yokozuna59 Feb 12, 2024
15c8bf8
update sankey parser tests
Yokozuna59 Feb 12, 2024
c364715
update the parser `index.ts` exports
Yokozuna59 Feb 12, 2024
32d82a6
add the approach from #4910 pr
Yokozuna59 Feb 12, 2024
bb84e17
change ISankeyLink into SankeyLink in sankeyDB
Yokozuna59 Feb 12, 2024
e51b7a4
modify sankye matcher
Yokozuna59 Feb 12, 2024
eb55e39
rename sankey langium parser files
Yokozuna59 Feb 12, 2024
4d035ed
simplify `sankey.langium`
Yokozuna59 Feb 13, 2024
56c643d
use `AbstractMermaidTokenBuilder` in sankey token builder
Yokozuna59 Feb 13, 2024
c0535f4
Merge branch 'develop' into add-sankey-langium-parser
Yokozuna59 Mar 24, 2024
0705aff
Merge branch 'develop' into add-sankey-langium-parser
Yokozuna59 Mar 24, 2024
bf95a30
fix lint:fix
Yokozuna59 Mar 24, 2024
434d154
remove unused imports
Yokozuna59 Mar 24, 2024
6c7b0f2
implement the greedy annotation approach
Yokozuna59 Mar 26, 2024
125ec46
Merge branch 'develop' into add-sankey-langium-parser
Yokozuna59 Mar 26, 2024
c9f6816
apply review suggestions
Yokozuna59 Mar 26, 2024
c155434
remove `utils.ts` and related unused imports
Yokozuna59 Apr 14, 2024
3597ffe
reorder sankey.langium rules
Yokozuna59 Apr 14, 2024
e080e96
Merge branch 'develop' into add-sankey-langium-parser
Yokozuna59 Apr 14, 2024
875c4fe
Merge branch 'develop' into add-sankey-langium-parser
Yokozuna59 Apr 15, 2024
51896f3
create separate test case for `CommonTokenBuilder`
Yokozuna59 Apr 15, 2024
8fa0509
run fix lint
Yokozuna59 Apr 15, 2024
681fbd0
Merge branch 'develop' into add-sankey-langium-parser
Yokozuna59 Jun 21, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 23 additions & 4 deletions packages/parser/src/language/common/tokenBuilder.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,33 @@
import type { GrammarAST, Stream, TokenBuilderOptions } from 'langium';
import type { TokenType } from 'chevrotain';

import { DefaultTokenBuilder } from 'langium';
import type {
CommentProvider,
GrammarAST,
LangiumCoreServices,
Stream,
TokenBuilderOptions,
} from 'langium';
import { DefaultTokenBuilder, stream } from 'langium';

export abstract class AbstractMermaidTokenBuilder extends DefaultTokenBuilder {
private keywords: Set<string>;
private commentProvider: CommentProvider;

public constructor(keywords: string[]) {
public constructor(keywords: string[], services: LangiumCoreServices) {
super();
this.keywords = new Set<string>(keywords);
this.commentProvider = services.documentation.CommentProvider;
}

protected override buildTerminalTokens(rules: Stream<GrammarAST.AbstractRule>): TokenType[] {
// put the greedy annotated terminal rules at the end of the array
const rulesArray = rules.toArray();
rules.forEach((rule, index) => {
const comment = this.commentProvider.getComment(rule);
if (comment && /@greedy/.test(comment)) {
rulesArray.push(rulesArray.splice(index, 1)[0]);
}
Yokozuna59 marked this conversation as resolved.
Show resolved Hide resolved
});
return super.buildTerminalTokens(stream(rulesArray));
}

protected override buildKeywordTokens(
Expand Down
2 changes: 1 addition & 1 deletion packages/parser/src/language/info/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export type InfoServices = LangiumCoreServices & InfoAddedServices;
*/
export const InfoModule: Module<InfoServices, PartialLangiumCoreServices & InfoAddedServices> = {
parser: {
TokenBuilder: () => new InfoTokenBuilder(),
TokenBuilder: (services) => new InfoTokenBuilder(services),
ValueConverter: () => new CommonValueConverter(),
},
};
Expand Down
5 changes: 3 additions & 2 deletions packages/parser/src/language/info/tokenBuilder.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { AbstractMermaidTokenBuilder } from '../common/index.js';
import type { InfoServices } from './module.js';

export class InfoTokenBuilder extends AbstractMermaidTokenBuilder {
public constructor() {
super(['info', 'showInfo']);
public constructor(services: InfoServices) {
super(['info', 'showInfo'], services);
}
}
2 changes: 1 addition & 1 deletion packages/parser/src/language/packet/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export const PacketModule: Module<
PartialLangiumCoreServices & PacketAddedServices
> = {
parser: {
TokenBuilder: () => new PacketTokenBuilder(),
TokenBuilder: (services) => new PacketTokenBuilder(services),
ValueConverter: () => new CommonValueConverter(),
},
};
Expand Down
5 changes: 3 additions & 2 deletions packages/parser/src/language/packet/tokenBuilder.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { AbstractMermaidTokenBuilder } from '../common/index.js';
import type { PacketServices } from './module.js';

export class PacketTokenBuilder extends AbstractMermaidTokenBuilder {
public constructor() {
super(['packet-beta']);
public constructor(services: PacketServices) {
super(['packet-beta'], services);
}
}
2 changes: 1 addition & 1 deletion packages/parser/src/language/pie/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export type PieServices = LangiumCoreServices & PieAddedServices;
*/
export const PieModule: Module<PieServices, PartialLangiumCoreServices & PieAddedServices> = {
parser: {
TokenBuilder: () => new PieTokenBuilder(),
TokenBuilder: (services) => new PieTokenBuilder(services),
ValueConverter: () => new PieValueConverter(),
},
};
Expand Down
5 changes: 3 additions & 2 deletions packages/parser/src/language/pie/tokenBuilder.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { AbstractMermaidTokenBuilder } from '../common/index.js';
import type { PieServices } from './module.js';

export class PieTokenBuilder extends AbstractMermaidTokenBuilder {
public constructor() {
super(['pie', 'showData']);
public constructor(services: PieServices) {
super(['pie', 'showData'], services);
}
}
2 changes: 1 addition & 1 deletion packages/parser/src/language/sankey/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export const SankeyModule: Module<
> = {
parser: {
LangiumParser: (services) => createSankeyParser(services),
TokenBuilder: () => new SankeyTokenBuilder(),
TokenBuilder: (services) => new SankeyTokenBuilder(services),
ValueConverter: () => new SankeyValueConverter(),
},
};
Expand Down
3 changes: 3 additions & 0 deletions packages/parser/src/language/sankey/sankey.langium
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,7 @@ SankeyLink:
;

Yokozuna59 marked this conversation as resolved.
Show resolved Hide resolved
terminal SANKEY_LINK_VALUE returns number: /"(0|[1-9][0-9]*)(\.[0-9]+)?"|[\t ]*(0|[1-9][0-9]*)(\.[0-9]+)?/;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems that we have the same regexp here. If this is a limitation of languim that definitely brings a boilerplate to the code.

/**
* @greedy
*/
Yokozuna59 marked this conversation as resolved.
Show resolved Hide resolved
terminal SANKEY_LINK_NODE: /sankey-link-node/;
5 changes: 3 additions & 2 deletions packages/parser/src/language/sankey/tokenBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ import type { TokenType } from 'chevrotain';

import { AbstractMermaidTokenBuilder } from '../common/index.js';
import { matchSankeyLinkNode } from './matcher.js';
import type { SankeyServices } from './module.js';

export class SankeyTokenBuilder extends AbstractMermaidTokenBuilder {
public constructor() {
super(['sankey-beta']);
public constructor(services: SankeyServices) {
super(['sankey-beta'], services);
}

protected override buildTerminalTokens(rules: Stream<GrammarAST.AbstractRule>): TokenType[] {
Expand Down
Loading