mirror of
https://github.com/mermaid-js/mermaid.git
synced 2025-09-12 20:09:46 +02:00
feat(arch): converted parser from jison to langium
This commit is contained in:
@@ -69,7 +69,7 @@
|
|||||||
<pre class="mermaid">
|
<pre class="mermaid">
|
||||||
architecture
|
architecture
|
||||||
service db(database)[Database]
|
service db(database)[Database]
|
||||||
service s3(storage)[Storage]
|
service s3(disk)[Storage]
|
||||||
service serv1(server)[Server 1]
|
service serv1(server)[Server 1]
|
||||||
service serv2(server)[Server 2]
|
service serv2(server)[Server 2]
|
||||||
service disk(disk)[Disk]
|
service disk(disk)[Disk]
|
||||||
|
@@ -3,7 +3,6 @@ import type {
|
|||||||
ArchitectureDB,
|
ArchitectureDB,
|
||||||
ArchitectureService,
|
ArchitectureService,
|
||||||
ArchitectureGroup,
|
ArchitectureGroup,
|
||||||
ArchitectureDirection,
|
|
||||||
ArchitectureEdge,
|
ArchitectureEdge,
|
||||||
ArchitectureDirectionPairMap,
|
ArchitectureDirectionPairMap,
|
||||||
ArchitectureDirectionPair,
|
ArchitectureDirectionPair,
|
||||||
@@ -47,21 +46,20 @@ const clear = (): void => {
|
|||||||
commonClear();
|
commonClear();
|
||||||
};
|
};
|
||||||
|
|
||||||
const addService = function (id: string, opts: Omit<ArchitectureService, 'id' | 'edges'> = {}) {
|
const addService = function ({id, icon, in: parent, title}: Omit<ArchitectureService, "edges">) {
|
||||||
const { icon, in: inside, title } = opts;
|
|
||||||
if (state.records.registeredIds[id] !== undefined) {
|
if (state.records.registeredIds[id] !== undefined) {
|
||||||
throw new Error(`The service id [${id}] is already in use by another ${state.records.registeredIds[id]}`);
|
throw new Error(`The service id [${id}] is already in use by another ${state.records.registeredIds[id]}`);
|
||||||
}
|
}
|
||||||
if (inside !== undefined) {
|
if (parent !== undefined) {
|
||||||
if (id === inside) {
|
if (id === parent) {
|
||||||
throw new Error(`The service [${id}] cannot be placed within itself`);
|
throw new Error(`The service [${id}] cannot be placed within itself`);
|
||||||
}
|
}
|
||||||
if (state.records.registeredIds[inside] === undefined) {
|
if (state.records.registeredIds[parent] === undefined) {
|
||||||
throw new Error(
|
throw new Error(
|
||||||
`The service [${id}]'s parent does not exist. Please make sure the parent is created before this service`
|
`The service [${id}]'s parent does not exist. Please make sure the parent is created before this service`
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
if (state.records.registeredIds[inside] === 'service') {
|
if (state.records.registeredIds[parent] === 'service') {
|
||||||
throw new Error(`The service [${id}]'s parent is not a group`);
|
throw new Error(`The service [${id}]'s parent is not a group`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -73,27 +71,27 @@ const addService = function (id: string, opts: Omit<ArchitectureService, 'id' |
|
|||||||
icon,
|
icon,
|
||||||
title,
|
title,
|
||||||
edges: [],
|
edges: [],
|
||||||
in: inside,
|
in: parent,
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
const getServices = (): ArchitectureService[] => Object.values(state.records.services);
|
const getServices = (): ArchitectureService[] => Object.values(state.records.services);
|
||||||
|
|
||||||
const addGroup = function (id: string, opts: Omit<ArchitectureGroup, 'id'> = {}) {
|
const addGroup = function ({id, icon, in: parent, title}: ArchitectureGroup) {
|
||||||
const { icon, in: inside, title } = opts;
|
// const { icon, in: inside, title } = opts;
|
||||||
if (state.records.registeredIds[id] !== undefined) {
|
if (state.records.registeredIds[id] !== undefined) {
|
||||||
throw new Error(`The group id [${id}] is already in use by another ${state.records.registeredIds[id]}`);
|
throw new Error(`The group id [${id}] is already in use by another ${state.records.registeredIds[id]}`);
|
||||||
}
|
}
|
||||||
if (inside !== undefined) {
|
if (parent !== undefined) {
|
||||||
if (id === inside) {
|
if (id === parent) {
|
||||||
throw new Error(`The group [${id}] cannot be placed within itself`);
|
throw new Error(`The group [${id}] cannot be placed within itself`);
|
||||||
}
|
}
|
||||||
if (state.records.registeredIds[inside] === undefined) {
|
if (state.records.registeredIds[parent] === undefined) {
|
||||||
throw new Error(
|
throw new Error(
|
||||||
`The group [${id}]'s parent does not exist. Please make sure the parent is created before this group`
|
`The group [${id}]'s parent does not exist. Please make sure the parent is created before this group`
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
if (state.records.registeredIds[inside] === 'service') {
|
if (state.records.registeredIds[parent] === 'service') {
|
||||||
throw new Error(`The group [${id}]'s parent is not a group`);
|
throw new Error(`The group [${id}]'s parent is not a group`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -104,7 +102,7 @@ const addGroup = function (id: string, opts: Omit<ArchitectureGroup, 'id'> = {})
|
|||||||
id,
|
id,
|
||||||
icon,
|
icon,
|
||||||
title,
|
title,
|
||||||
in: inside,
|
in: parent,
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
const getGroups = (): ArchitectureGroup[] => {
|
const getGroups = (): ArchitectureGroup[] => {
|
||||||
@@ -112,13 +110,8 @@ const getGroups = (): ArchitectureGroup[] => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
const addEdge = function (
|
const addEdge = function (
|
||||||
lhsId: string,
|
{lhsId, rhsId, lhsDir, rhsDir, lhsInto, rhsInto, title}: ArchitectureEdge
|
||||||
lhsDir: ArchitectureDirection,
|
|
||||||
rhsId: string,
|
|
||||||
rhsDir: ArchitectureDirection,
|
|
||||||
opts: Omit<ArchitectureEdge, 'lhsId' | 'lhsDir' | 'rhsId' | 'rhsDir'> = {}
|
|
||||||
) {
|
) {
|
||||||
const { title, lhsInto: lhsInto, rhsInto: rhsInto } = opts;
|
|
||||||
if (!isArchitectureDirection(lhsDir)) {
|
if (!isArchitectureDirection(lhsDir)) {
|
||||||
throw new Error(
|
throw new Error(
|
||||||
`Invalid direction given for left hand side of edge ${lhsId}--${rhsId}. Expected (L,R,T,B) got ${lhsDir}`
|
`Invalid direction given for left hand side of edge ${lhsId}--${rhsId}. Expected (L,R,T,B) got ${lhsDir}`
|
||||||
|
@@ -1,6 +1,5 @@
|
|||||||
import type { DiagramDefinition } from '../../diagram-api/types.js';
|
import type { DiagramDefinition } from '../../diagram-api/types.js';
|
||||||
// @ts-ignore: JISON doesn't support types
|
import { parser } from './architectureParser.js';
|
||||||
import parser from './parser/architecture.jison';
|
|
||||||
import { db } from './architectureDb.js';
|
import { db } from './architectureDb.js';
|
||||||
import styles from './architectureStyles.js';
|
import styles from './architectureStyles.js';
|
||||||
import { renderer } from './architectureRenderer.js';
|
import { renderer } from './architectureRenderer.js';
|
||||||
|
@@ -0,0 +1,23 @@
|
|||||||
|
import type { Architecture } from '@mermaid-js/parser';
|
||||||
|
import { parse } from '@mermaid-js/parser';
|
||||||
|
import { log } from '../../logger.js';
|
||||||
|
import type { ParserDefinition } from '../../diagram-api/types.js';
|
||||||
|
import { populateCommonDb } from '../common/populateCommonDb.js';
|
||||||
|
import type { ArchitectureDB } from './architectureTypes.js';
|
||||||
|
import { db } from './architectureDb.js';
|
||||||
|
|
||||||
|
const populateDb = (ast: Architecture, db: ArchitectureDB) => {
|
||||||
|
populateCommonDb(ast, db);
|
||||||
|
ast.groups.map(db.addGroup);
|
||||||
|
ast.services.map(db.addService);
|
||||||
|
// @ts-ignore TODO our parser guarantees the type is L/R/T/B and not string. How to change to union type?
|
||||||
|
ast.edges.map(db.addEdge);
|
||||||
|
};
|
||||||
|
|
||||||
|
export const parser: ParserDefinition = {
|
||||||
|
parse: async (input: string): Promise<void> => {
|
||||||
|
const ast: Architecture = await parse('architecture', input);
|
||||||
|
log.debug(ast);
|
||||||
|
populateDb(ast, db);
|
||||||
|
},
|
||||||
|
};
|
@@ -18,8 +18,7 @@ const genSections: DiagramStylesProvider = (options) => {
|
|||||||
for (let i = 0; i < options.THEME_COLOR_LIMIT; i++) {
|
for (let i = 0; i < options.THEME_COLOR_LIMIT; i++) {
|
||||||
const sw = '' + (17 - 3 * i);
|
const sw = '' + (17 - 3 * i);
|
||||||
sections += `
|
sections += `
|
||||||
.section-${i - 1} rect, .section-${i - 1} path, .section-${i - 1} circle, .section-${
|
.section-${i - 1} rect, .section-${i - 1} path, .section-${i - 1} circle, .section-${i - 1
|
||||||
i - 1
|
|
||||||
} polygon, .section-${i - 1} path {
|
} polygon, .section-${i - 1} path {
|
||||||
fill: ${options['cScale' + i]};
|
fill: ${options['cScale' + i]};
|
||||||
}
|
}
|
||||||
|
@@ -168,16 +168,12 @@ export interface ArchitectureEdge {
|
|||||||
|
|
||||||
export interface ArchitectureDB extends DiagramDB {
|
export interface ArchitectureDB extends DiagramDB {
|
||||||
clear: () => void;
|
clear: () => void;
|
||||||
addService: (id: string, opts: Omit<ArchitectureService, 'id'>) => void;
|
addService: (service: Omit<ArchitectureService, "edges">) => void;
|
||||||
getServices: () => ArchitectureService[];
|
getServices: () => ArchitectureService[];
|
||||||
addGroup: (id: string, opts: Omit<ArchitectureGroup, 'id'>) => void;
|
addGroup: (group: ArchitectureGroup) => void;
|
||||||
getGroups: () => ArchitectureGroup[];
|
getGroups: () => ArchitectureGroup[];
|
||||||
addEdge: (
|
addEdge: (
|
||||||
lhsId: string,
|
edge: ArchitectureEdge
|
||||||
lhsDir: ArchitectureDirection,
|
|
||||||
rhsId: string,
|
|
||||||
rhsDir: ArchitectureDirection,
|
|
||||||
opts: Omit<ArchitectureEdge, 'lhsId' | 'lhsDir' | 'rhsId' | 'rhsDir'>
|
|
||||||
) => void;
|
) => void;
|
||||||
getEdges: () => ArchitectureEdge[];
|
getEdges: () => ArchitectureEdge[];
|
||||||
setElementForId: (id: string, element: D3Element) => void;
|
setElementForId: (id: string, element: D3Element) => void;
|
||||||
|
@@ -1,3 +1,4 @@
|
|||||||
|
// TODO: delete file
|
||||||
%lex
|
%lex
|
||||||
%options case-insensitive
|
%options case-insensitive
|
||||||
|
|
||||||
|
@@ -15,6 +15,11 @@
|
|||||||
"id": "pie",
|
"id": "pie",
|
||||||
"grammar": "src/language/pie/pie.langium",
|
"grammar": "src/language/pie/pie.langium",
|
||||||
"fileExtensions": [".mmd", ".mermaid"]
|
"fileExtensions": [".mmd", ".mermaid"]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"id": "architecture",
|
||||||
|
"grammar": "src/language/architecture/architecture.langium",
|
||||||
|
"fileExtensions": [".mmd", ".mermaid"]
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"mode": "production",
|
"mode": "production",
|
||||||
|
@@ -0,0 +1,40 @@
|
|||||||
|
grammar Architecture
|
||||||
|
import "../common/common";
|
||||||
|
|
||||||
|
entry Architecture:
|
||||||
|
NEWLINE*
|
||||||
|
"architecture"
|
||||||
|
(
|
||||||
|
NEWLINE* TitleAndAccessibilities
|
||||||
|
| NEWLINE* Statement*
|
||||||
|
| NEWLINE*
|
||||||
|
)
|
||||||
|
;
|
||||||
|
|
||||||
|
fragment Statement:
|
||||||
|
groups+=Group
|
||||||
|
| services+=Service
|
||||||
|
| edges+=Edge
|
||||||
|
;
|
||||||
|
|
||||||
|
fragment Arrow:
|
||||||
|
lhsInto?=ARROW_INTO? lhsDir=ARROW_DIRECTION '--' rhsDir=ARROW_DIRECTION rhsInto?=ARROW_INTO?
|
||||||
|
;
|
||||||
|
|
||||||
|
Group:
|
||||||
|
'group' id=ARCH_ID icon=ARCH_ICON? title=ARCH_TITLE? ('in' in=ARCH_ID)? EOL
|
||||||
|
;
|
||||||
|
|
||||||
|
Service:
|
||||||
|
'service' id=ARCH_ID icon=ARCH_ICON? title=ARCH_TITLE? ('in' in=ARCH_ID)? EOL
|
||||||
|
;
|
||||||
|
|
||||||
|
Edge:
|
||||||
|
lhsId=ARCH_ID Arrow rhsId=ARCH_ID EOL
|
||||||
|
;
|
||||||
|
|
||||||
|
terminal ARROW_DIRECTION: 'L' | 'R' | 'T' | 'B';
|
||||||
|
terminal ARCH_ID: /[\w]+/;
|
||||||
|
terminal ARCH_ICON: /\([\w]+\)/;
|
||||||
|
terminal ARCH_TITLE: /\[[\w ]+\]/;
|
||||||
|
terminal ARROW_INTO: /\(|\)/;
|
1
packages/parser/src/language/architecture/index.ts
Normal file
1
packages/parser/src/language/architecture/index.ts
Normal file
@@ -0,0 +1 @@
|
|||||||
|
export * from './module.js';
|
74
packages/parser/src/language/architecture/module.ts
Normal file
74
packages/parser/src/language/architecture/module.ts
Normal file
@@ -0,0 +1,74 @@
|
|||||||
|
import type {
|
||||||
|
DefaultSharedCoreModuleContext,
|
||||||
|
LangiumCoreServices,
|
||||||
|
LangiumSharedCoreServices,
|
||||||
|
Module,
|
||||||
|
PartialLangiumCoreServices,
|
||||||
|
} from 'langium';
|
||||||
|
import {
|
||||||
|
EmptyFileSystem,
|
||||||
|
createDefaultCoreModule,
|
||||||
|
createDefaultSharedCoreModule,
|
||||||
|
inject,
|
||||||
|
} from 'langium';
|
||||||
|
|
||||||
|
import { MermaidGeneratedSharedModule, ArchitectureGeneratedModule } from '../generated/module.js';
|
||||||
|
import { ArchitectureTokenBuilder } from './tokenBuilder.js';
|
||||||
|
import { ArchitectureValueConverter } from './valueConverter.js';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Declaration of `Architecture` services.
|
||||||
|
*/
|
||||||
|
type ArchitectureAddedServices = {
|
||||||
|
parser: {
|
||||||
|
TokenBuilder: ArchitectureTokenBuilder;
|
||||||
|
ValueConverter: ArchitectureValueConverter;
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Union of Langium default services and `Architecture` services.
|
||||||
|
*/
|
||||||
|
export type ArchitectureServices = LangiumCoreServices & ArchitectureAddedServices;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Dependency injection module that overrides Langium default services and
|
||||||
|
* contributes the declared `Architecture` services.
|
||||||
|
*/
|
||||||
|
export const ArchitectureModule: Module<ArchitectureServices, PartialLangiumCoreServices & ArchitectureAddedServices> = {
|
||||||
|
parser: {
|
||||||
|
TokenBuilder: () => new ArchitectureTokenBuilder(),
|
||||||
|
ValueConverter: () => new ArchitectureValueConverter(),
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create the full set of services required by Langium.
|
||||||
|
*
|
||||||
|
* First inject the shared services by merging two modules:
|
||||||
|
* - Langium default shared services
|
||||||
|
* - Services generated by langium-cli
|
||||||
|
*
|
||||||
|
* Then inject the language-specific services by merging three modules:
|
||||||
|
* - Langium default language-specific services
|
||||||
|
* - Services generated by langium-cli
|
||||||
|
* - Services specified in this file
|
||||||
|
* @param context - Optional module context with the LSP connection
|
||||||
|
* @returns An object wrapping the shared services and the language-specific services
|
||||||
|
*/
|
||||||
|
export function createArchitectureServices(context: DefaultSharedCoreModuleContext = EmptyFileSystem): {
|
||||||
|
shared: LangiumSharedCoreServices;
|
||||||
|
Architecture: ArchitectureServices;
|
||||||
|
} {
|
||||||
|
const shared: LangiumSharedCoreServices = inject(
|
||||||
|
createDefaultSharedCoreModule(context),
|
||||||
|
MermaidGeneratedSharedModule
|
||||||
|
);
|
||||||
|
const Architecture: ArchitectureServices = inject(
|
||||||
|
createDefaultCoreModule({ shared }),
|
||||||
|
ArchitectureGeneratedModule,
|
||||||
|
ArchitectureModule
|
||||||
|
);
|
||||||
|
shared.ServiceRegistry.register(Architecture);
|
||||||
|
return { shared, Architecture };
|
||||||
|
}
|
@@ -0,0 +1,7 @@
|
|||||||
|
import { AbstractMermaidTokenBuilder } from '../common/index.js';
|
||||||
|
|
||||||
|
export class ArchitectureTokenBuilder extends AbstractMermaidTokenBuilder {
|
||||||
|
public constructor() {
|
||||||
|
super(['architecture']);
|
||||||
|
}
|
||||||
|
}
|
19
packages/parser/src/language/architecture/valueConverter.ts
Normal file
19
packages/parser/src/language/architecture/valueConverter.ts
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
import type { CstNode, GrammarAST, ValueType } from 'langium';
|
||||||
|
|
||||||
|
import { AbstractMermaidValueConverter } from '../common/index.js';
|
||||||
|
|
||||||
|
export class ArchitectureValueConverter extends AbstractMermaidValueConverter {
|
||||||
|
protected runCustomConverter(
|
||||||
|
rule: GrammarAST.AbstractRule,
|
||||||
|
input: string,
|
||||||
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||||
|
_cstNode: CstNode
|
||||||
|
): ValueType | undefined {
|
||||||
|
if (rule.name === 'ARCH_ICON') {
|
||||||
|
return input.replace(/[()]/g, '').trim();
|
||||||
|
} else if (rule.name === 'ARCH_TITLE') {
|
||||||
|
return input.replace(/[[\]]/g, '').trim();
|
||||||
|
}
|
||||||
|
return undefined
|
||||||
|
}
|
||||||
|
}
|
@@ -5,21 +5,26 @@ export {
|
|||||||
PacketBlock,
|
PacketBlock,
|
||||||
Pie,
|
Pie,
|
||||||
PieSection,
|
PieSection,
|
||||||
|
Architecture,
|
||||||
isCommon,
|
isCommon,
|
||||||
isInfo,
|
isInfo,
|
||||||
isPacket,
|
isPacket,
|
||||||
isPacketBlock,
|
isPacketBlock,
|
||||||
isPie,
|
isPie,
|
||||||
isPieSection,
|
isPieSection,
|
||||||
|
isArchitecture
|
||||||
} from './generated/ast.js';
|
} from './generated/ast.js';
|
||||||
|
|
||||||
export {
|
export {
|
||||||
InfoGeneratedModule,
|
InfoGeneratedModule,
|
||||||
MermaidGeneratedSharedModule,
|
MermaidGeneratedSharedModule,
|
||||||
PacketGeneratedModule,
|
PacketGeneratedModule,
|
||||||
PieGeneratedModule,
|
PieGeneratedModule,
|
||||||
|
ArchitectureGeneratedModule
|
||||||
} from './generated/module.js';
|
} from './generated/module.js';
|
||||||
|
|
||||||
export * from './common/index.js';
|
export * from './common/index.js';
|
||||||
export * from './info/index.js';
|
export * from './info/index.js';
|
||||||
export * from './packet/index.js';
|
export * from './packet/index.js';
|
||||||
export * from './pie/index.js';
|
export * from './pie/index.js';
|
||||||
|
export * from './architecture/index.js';
|
||||||
|
@@ -1,8 +1,8 @@
|
|||||||
import type { LangiumParser, ParseResult } from 'langium';
|
import type { LangiumParser, ParseResult } from 'langium';
|
||||||
|
|
||||||
import type { Info, Packet, Pie } from './index.js';
|
import type { Info, Packet, Pie, Architecture } from './index.js';
|
||||||
|
|
||||||
export type DiagramAST = Info | Packet | Pie;
|
export type DiagramAST = Info | Packet | Pie | Architecture;
|
||||||
|
|
||||||
const parsers: Record<string, LangiumParser> = {};
|
const parsers: Record<string, LangiumParser> = {};
|
||||||
const initializers = {
|
const initializers = {
|
||||||
@@ -21,11 +21,17 @@ const initializers = {
|
|||||||
const parser = createPieServices().Pie.parser.LangiumParser;
|
const parser = createPieServices().Pie.parser.LangiumParser;
|
||||||
parsers['pie'] = parser;
|
parsers['pie'] = parser;
|
||||||
},
|
},
|
||||||
|
architecture: async () => {
|
||||||
|
const { createArchitectureServices } = await import('./language/architecture/index.js');
|
||||||
|
const parser = createArchitectureServices().Architecture.parser.LangiumParser;
|
||||||
|
parsers['architecture'] = parser;
|
||||||
|
},
|
||||||
} as const;
|
} as const;
|
||||||
|
|
||||||
export async function parse(diagramType: 'info', text: string): Promise<Info>;
|
export async function parse(diagramType: 'info', text: string): Promise<Info>;
|
||||||
export async function parse(diagramType: 'packet', text: string): Promise<Packet>;
|
export async function parse(diagramType: 'packet', text: string): Promise<Packet>;
|
||||||
export async function parse(diagramType: 'pie', text: string): Promise<Pie>;
|
export async function parse(diagramType: 'pie', text: string): Promise<Pie>;
|
||||||
|
export async function parse(diagramType: 'architecture', text: string): Promise<Architecture>;
|
||||||
export async function parse<T extends DiagramAST>(
|
export async function parse<T extends DiagramAST>(
|
||||||
diagramType: keyof typeof initializers,
|
diagramType: keyof typeof initializers,
|
||||||
text: string
|
text: string
|
||||||
|
Reference in New Issue
Block a user