mirror of
https://github.com/mermaid-js/mermaid.git
synced 2025-09-14 04:49:44 +02:00
Merge branch 'next' into pr/Yokozuna59/4751
* next: (70 commits) chore: Add comment for `yy`. chore: Increase heap size when building chore: increase `test-util.ts` converage by returning `undefined` chore: add `vitest` imports to `test-util.ts` chore: run `pnpm lint:fix` create `noErrorsOrAlternatives` parser helper function chore: export `InfoModule` from `infoModule.ts` docs: Fix link Update docs fix(pie): align slices and legend orders Mermaid version v10.4.0 unique batches every time, if not repeated tests end up in the same batch Added missed .md Increase JS heap More tests for redirects + prettier Fixed redirects inside vitepress, extended tests chore: Explain redirect.ts clearly docs: Fix npmjs link chore: Update editor.bash to build latest version chore: Build after clone ...
This commit is contained in:
63
packages/parser/README.md
Normal file
63
packages/parser/README.md
Normal file
@@ -0,0 +1,63 @@
|
||||
<p align="center">
|
||||
<img src="https://raw.githubusercontent.com/mermaid-js/mermaid/develop/docs/public/favicon.svg" height="150">
|
||||
|
||||
</p>
|
||||
<h1 align="center">
|
||||
Mermaid Parser
|
||||
</h1>
|
||||
|
||||
<p align="center">
|
||||
Mermaid parser package
|
||||
<p>
|
||||
|
||||
[](https://www.npmjs.com/package/mermaid-parser)
|
||||
|
||||
## How the package works
|
||||
|
||||
The package exports a `parse` function that has two parameters:
|
||||
|
||||
```ts
|
||||
declare function parse<T extends DiagramAST>(
|
||||
diagramType: keyof typeof initializers,
|
||||
text: string
|
||||
): T;
|
||||
```
|
||||
|
||||
## How does a Langium-based parser work?
|
||||
|
||||
```mermaid
|
||||
sequenceDiagram
|
||||
actor Package
|
||||
participant Module
|
||||
participant TokenBuilder
|
||||
participant Lexer
|
||||
participant Parser
|
||||
participant ValueConverter
|
||||
|
||||
|
||||
Package ->> Module: Create services
|
||||
Module ->> TokenBuilder: Override or/and<br>reorder rules
|
||||
TokenBuilder ->> Lexer: Read the string and transform<br>it into a token stream
|
||||
Lexer ->> Parser: Parse token<br>stream into AST
|
||||
Parser ->> ValueConverter: Clean/modify tokenized<br>rules returned value
|
||||
ValueConverter -->> Package: Return AST
|
||||
```
|
||||
|
||||
- When to override `TokenBuilder`?
|
||||
|
||||
- To override keyword rules.
|
||||
- To override terminal rules that need a custom function.
|
||||
- To manually reorder the list of rules.
|
||||
|
||||
- When to override `Lexer`?
|
||||
|
||||
- To modify input before tokenizing.
|
||||
- To insert/modify tokens that cannot or have not been parsed.
|
||||
|
||||
- When to override `LangiumParser`?
|
||||
|
||||
- To insert or modify attributes that can't be parsed.
|
||||
|
||||
- When to override `ValueConverter`?
|
||||
|
||||
- To modify the returned value from the parser.
|
@@ -7,15 +7,14 @@
|
||||
"Yokozuna59",
|
||||
"Sidharth Vinod (https://sidharth.dev)"
|
||||
],
|
||||
"homepage": "https://github.com/mermaid-js/mermaid/#readme",
|
||||
"homepage": "https://github.com/mermaid-js/mermaid/tree/develop/packages/mermaid/parser/#readme",
|
||||
"types": "dist/src/index.d.ts",
|
||||
"type": "module",
|
||||
"exports": {
|
||||
".": {
|
||||
"import": "./dist/mermaid-parser.esm.mjs",
|
||||
"types": "./dist/src/index.d.ts"
|
||||
},
|
||||
"./*": "./*"
|
||||
}
|
||||
},
|
||||
"scripts": {
|
||||
"clean": "rimraf dist src/language/generated",
|
||||
@@ -35,10 +34,10 @@
|
||||
"ast"
|
||||
],
|
||||
"dependencies": {
|
||||
"langium": "2.0.0"
|
||||
"langium": "2.0.1"
|
||||
},
|
||||
"devDependencies": {
|
||||
"langium-cli": "2.0.0"
|
||||
"langium-cli": "2.0.1"
|
||||
},
|
||||
"files": [
|
||||
"dist/"
|
||||
|
@@ -32,7 +32,7 @@ export type InfoServices = LangiumServices & InfoAddedServices;
|
||||
* Dependency injection module that overrides Langium default services and
|
||||
* contributes the declared `Info` services.
|
||||
*/
|
||||
const InfoModule: Module<InfoServices, PartialLangiumServices & InfoAddedServices> = {
|
||||
export const InfoModule: Module<InfoServices, PartialLangiumServices & InfoAddedServices> = {
|
||||
parser: {
|
||||
Lexer: (services) => new CommonLexer(services),
|
||||
TokenBuilder: () => new InfoTokenBuilder(),
|
||||
|
@@ -3,10 +3,11 @@ import type { LangiumParser, ParseResult } from 'langium';
|
||||
|
||||
import type { InfoServices } from '../src/language/index.js';
|
||||
import { Info, createInfoServices } from '../src/language/index.js';
|
||||
import { noErrorsOrAlternatives } from './test-util.js';
|
||||
|
||||
const services: InfoServices = createInfoServices().Info;
|
||||
const parser: LangiumParser = services.parser.LangiumParser;
|
||||
export function createInfoTestServices(): {
|
||||
function createInfoTestServices(): {
|
||||
services: InfoServices;
|
||||
parse: (input: string) => ParseResult<Info>;
|
||||
} {
|
||||
@@ -30,11 +31,10 @@ describe('info', () => {
|
||||
info
|
||||
`,
|
||||
])('should handle empty info', (context: string) => {
|
||||
const { parserErrors, lexerErrors, value } = parse(context);
|
||||
expect(parserErrors).toHaveLength(0);
|
||||
expect(lexerErrors).toHaveLength(0);
|
||||
const result = parse(context);
|
||||
noErrorsOrAlternatives(result);
|
||||
|
||||
expect(value.$type).toBe(Info);
|
||||
expect(result.value.$type).toBe(Info);
|
||||
});
|
||||
|
||||
it.each([
|
||||
@@ -49,10 +49,9 @@ describe('info', () => {
|
||||
showInfo
|
||||
`,
|
||||
])('should handle showInfo', (context: string) => {
|
||||
const { parserErrors, lexerErrors, value } = parse(context);
|
||||
expect(parserErrors).toHaveLength(0);
|
||||
expect(lexerErrors).toHaveLength(0);
|
||||
const result = parse(context);
|
||||
noErrorsOrAlternatives(result);
|
||||
|
||||
expect(value.$type).toBe(Info);
|
||||
expect(result.value.$type).toBe(Info);
|
||||
});
|
||||
});
|
||||
|
18
packages/parser/tests/test-util.ts
Normal file
18
packages/parser/tests/test-util.ts
Normal file
@@ -0,0 +1,18 @@
|
||||
import { expect, vi } from 'vitest';
|
||||
import type { ParseResult } from 'langium';
|
||||
|
||||
const consoleMock = vi.spyOn(console, 'log').mockImplementation(() => undefined);
|
||||
|
||||
/**
|
||||
* A helper test function that validate that the result doesn't have errors
|
||||
* or any ambiguous alternatives from chevrotain.
|
||||
*
|
||||
* @param result - the result `parse` function.
|
||||
*/
|
||||
export function noErrorsOrAlternatives(result: ParseResult) {
|
||||
expect(result.lexerErrors).toHaveLength(0);
|
||||
expect(result.parserErrors).toHaveLength(0);
|
||||
|
||||
expect(consoleMock).not.toHaveBeenCalled();
|
||||
consoleMock.mockReset();
|
||||
}
|
Reference in New Issue
Block a user