Skip to content

Commit

Permalink
feat: add info langium parser
Browse files Browse the repository at this point in the history
  • Loading branch information
Yokozuna59 committed Aug 12, 2023
1 parent a39122b commit e8620f6
Show file tree
Hide file tree
Showing 35 changed files with 617 additions and 404 deletions.
2 changes: 2 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@ cypress/plugins/index.js
coverage
*.json
node_modules
# autogenereated by langium-cli
generated/
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,6 @@ stats/

demos/dev/**
!/demos/dev/example.html

# autogenereated by langium-cli
generated/
3 changes: 3 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,6 @@ stats
.nyc_output
# Autogenerated by `pnpm run --filter mermaid types:build-config`
packages/mermaid/src/config.type.ts

# autogenereated by langium-cli
generated/
1 change: 1 addition & 0 deletions cSpell.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
"knut",
"knutsveidqvist",
"laganeckas",
"langium",
"linetype",
"lintstagedrc",
"logmsg",
Expand Down
4 changes: 0 additions & 4 deletions cypress/integration/rendering/info.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,4 @@ describe('info diagram', () => {
it('should handle an info definition', () => {
imgSnapshotTest(`info`);
});

it('should handle an info definition with showInfo', () => {
imgSnapshotTest(`info showInfo`);
});
});
5 changes: 0 additions & 5 deletions demos/info.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,6 @@ <h1>Info diagram demos</h1>
info
</pre>

<hr />
<pre class="mermaid">
info showInfo
</pre>

<script type="module">
import mermaid from './mermaid.esm.mjs';
mermaid.initialize({
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@
"git graph"
],
"scripts": {
"build": "pnpm run -r clean && pnpm build:esbuild && pnpm build:types",
"build:esbuild": "pnpm run -r clean && ts-node-esm --transpileOnly .esbuild/build.ts",
"build": "pnpm run -r clean && pnpm build:parser && pnpm build:esbuild && pnpm build:types",
"build:esbuild": "ts-node-esm --transpileOnly .esbuild/build.ts",
"build:parser": "pnpm --filter mermaid-parser build",
"build:mermaid": "pnpm build:esbuild --mermaid",
"build:viz": "pnpm build:esbuild --visualize",
"build:types": "tsc -p ./packages/mermaid/tsconfig.json --emitDeclarationOnly && tsc -p ./packages/mermaid-zenuml/tsconfig.json --emitDeclarationOnly && tsc -p ./packages/mermaid-example-diagram/tsconfig.json --emitDeclarationOnly",
Expand Down
2 changes: 2 additions & 0 deletions packages/mermaid/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,10 @@
"dompurify": "^3.0.5",
"elkjs": "^0.8.2",
"khroma": "^2.0.0",
"langium": "2.0.0-next.a57102f",
"lodash-es": "^4.17.21",
"mdast-util-from-markdown": "^1.3.0",
"mermaid-parser": "workspace:*",
"stylis": "^4.1.3",
"ts-dedent": "^2.2.0",
"uuid": "^9.0.0"
Expand Down
21 changes: 3 additions & 18 deletions packages/mermaid/src/diagrams/info/info.spec.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,9 @@
// @ts-ignore - jison doesn't export types
import { parser } from './parser/info.jison';
import { parse } from './infoParser.js';
import { db } from './infoDb.js';

describe('info diagram', () => {
beforeEach(() => {
parser.yy = db;
parser.yy.clear();
});

describe('info', () => {
it('should handle an info definition', () => {
const str = `info`;
parser.parse(str);

expect(db.getInfo()).toBeFalsy();
});

it('should handle an info definition with showInfo', () => {
const str = `info showInfo`;
parser.parse(str);

expect(db.getInfo()).toBeTruthy();
expect(parse(str)).not.toThrow();
});
});
5 changes: 2 additions & 3 deletions packages/mermaid/src/diagrams/info/infoDiagram.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import type { DiagramDefinition } from '../../diagram-api/types.js';
// @ts-ignore - jison doesn't export types
import parser from './parser/info.jison';
import { parse } from './infoParser.js';
import { db } from './infoDb.js';
import { renderer } from './infoRenderer.js';

export const diagram: DiagramDefinition = {
parser,
parser: { parse, parser: { yy: {} } },
db,
renderer,
};
20 changes: 20 additions & 0 deletions packages/mermaid/src/diagrams/info/infoParser.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { EmptyFileSystem, type LangiumParser, type ParseResult } from 'langium';
import { type Info, type InfoServices, createInfoServices } from 'mermaid-parser';

import { log } from '../../logger.js';

const services: InfoServices = createInfoServices(EmptyFileSystem).Info;
const langiumParser: LangiumParser = services.parser.LangiumParser;

export function parse(input: string): void {
const result: ParseResult<Info> = langiumParser.parse<Info>(input);
if (result.parserErrors.length > 0 || result.lexerErrors.length > 0) {
const parserErrors = result.parserErrors.map((parseError) => parseError);
const lexerErrors = result.lexerErrors.map((lexerError) => lexerError);
log.error(
{ parserErrors: parserErrors, lexerErrors: lexerErrors },
'Error parsing info diagram'
);
throw new Error(`Parser errors: ${parserErrors}\nLex errors: ${lexerErrors}`);
}
}
48 changes: 0 additions & 48 deletions packages/mermaid/src/diagrams/info/parser/info.jison

This file was deleted.

21 changes: 21 additions & 0 deletions packages/parser/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2023 Yokozuna59

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
12 changes: 12 additions & 0 deletions packages/parser/langium-config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"projectName": "Mermaid",
"languages": [
{
"id": "info",
"grammar": "src/language/info/info.langium",
"fileExtensions": [".mmd", ".mermaid"]
}
],
"importExtension": ".js",
"out": "src/language/generated"
}
47 changes: 47 additions & 0 deletions packages/parser/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
{
"name": "mermaid-parser",
"version": "0.2.0",
"description": "MermaidJS parser",
"author": "Yokozuna59",
"homepage": "https://github.com/mermaid-js/mermaid/#readme",
"types": "dist/src/index.d.ts",
"type": "module",
"exports": {
".": {
"import": "./dist/src/index.js",
"types": "./dist/src/index.d.ts"
},
"./*": "./*"
},
"scripts": {
"build": "langium generate && tsc",
"clean": "rimraf dist src/language/generated",
"langium:generate": "langium generate",
"langium:watch": "langium generate --watch",
"prepublishOnly": "pnpm -w run build"
},
"repository": {
"type": "git",
"url": "https://github.com/mermaid-js/mermaid.git",
"directory": "packages/mermaid-parser"
},
"license": "MIT",
"keywords": [
"mermaid",
"parser",
"ast"
],
"dependencies": {
"chevrotain": "^11.0.2",
"langium": "2.0.0-next.a57102f"
},
"devDependencies": {
"langium-cli": "2.0.0-next.a57102f"
},
"files": [
"dist/"
],
"publishConfig": {
"access": "public"
}
}
1 change: 1 addition & 0 deletions packages/parser/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './language/index.js';
20 changes: 20 additions & 0 deletions packages/parser/src/language/common/common.langium
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
interface Common {
accDescr?: string;
accTitle?: string;
title?: string;
}

fragment TitleAndAccessibilities:
((accDescr=ACC_DESCR | accTitle=ACC_TITLE | title=TITLE) NEWLINE+)+
;

terminal NEWLINE: /\r?\n/;
terminal ACC_DESCR: /accDescr(?:[\t ]*:[\t ]*[^\n\r]*?(?=%%)|\s*{[^}]*})|accDescr(?:[\t ]*:[\t ]*[^\n\r]*|\s*{[^}]*})/;
terminal ACC_TITLE: /accTitle[\t ]*:[\t ]*[^\n\r]*?(?=%%)|accTitle[\t ]*:[\t ]*[^\n\r]*/;
terminal TITLE: /title(?:[\t ]+[^\n\r]*?|)(?=%%)|title(?:[\t ]+[^\n\r]*|)/;

hidden terminal WHITESPACE: /[\t ]+/;
// TODO: add YAML_COMMENT hidden rule
hidden terminal YAML: /---[\t ]*\r?\n[\s\S]*?---[\t ]*(?!.)/;
hidden terminal DIRECTIVE: /[\t ]*%%{[\s\S]*?}%%\s*/;
hidden terminal SINGLE_LINE_COMMENT: /[\t ]*%%[^\n\r]*/;
7 changes: 7 additions & 0 deletions packages/parser/src/language/common/commonLexer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { DefaultLexer, type LexerResult } from 'langium';

export class CommonLexer extends DefaultLexer {
public override tokenize(text: string): LexerResult {
return super.tokenize(text + '\n');
}
}
14 changes: 14 additions & 0 deletions packages/parser/src/language/common/commonMatcher.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/**
* Matches single and multiline accessible description
*/
export const accessibilityDescrRegex = /accDescr(?:[\t ]*:[\t ]*([^\n\r]*)|\s*{([^}]*)})/;

/**
* Matches single line accessible title
*/
export const accessibilityTitleRegex = /accTitle[\t ]*:[\t ]*([^\n\r]*)/;

/**
* Matches a single line title
*/
export const titleRegex = /title([\t ]+([^\n\r]*)|)/;
67 changes: 67 additions & 0 deletions packages/parser/src/language/common/commonValueConverters.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import { type CstNode, DefaultValueConverter, type GrammarAST, type ValueType } from 'langium';

import { accessibilityDescrRegex, accessibilityTitleRegex, titleRegex } from './commonMatcher.js';

export class CommonValueConverter extends DefaultValueConverter {
protected override runConverter(
rule: GrammarAST.AbstractRule,
input: string,
cstNode: CstNode
): ValueType {
const value: ValueType | null = CommonValueConverter.customRunConverter(rule, input, cstNode);
if (value === null) {
return super.runConverter(rule, input, cstNode);
} else {
return value;
}
}

/**
* A method contains convert logic to be used by class itself or `MermaidValueConverter`.
*
* @param rule - Parsed rule.
* @param input - Matched string.
* @param _cstNode - Node in the Concrete Syntax Tree (CST).
* @returns converted the value if it's common rule or `null` if it's not.
*/
public static customRunConverter(
rule: GrammarAST.AbstractRule,
input: string,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
_cstNode: CstNode
): ValueType | null {
let regex: RegExp | undefined;
switch (rule.name) {
case 'ACC_DESCR': {
regex = new RegExp(accessibilityDescrRegex.source);
break;
}
case 'ACC_TITLE': {
regex = new RegExp(accessibilityTitleRegex.source);
break;
}
case 'TITLE': {
regex = new RegExp(titleRegex.source);
break;
}
}
if (regex !== undefined) {
const match = regex.exec(input);
if (match !== null) {
// single line title, accTitle, accDescr
if (match[1] !== undefined) {
return match[1].trim().replaceAll(/[\t ]{2,}/gm, ' ');
}
// multi line accDescr
else if (match[2] !== undefined) {
return match[2]
.replaceAll(/^\s*/gm, '')
.replaceAll(/\s+$/gm, '')
.replaceAll(/[\t ]{2,}/gm, ' ')
.replaceAll(/[\n\r]{2,}/gm, '\n');
}
}
}
return null;
}
}
2 changes: 2 additions & 0 deletions packages/parser/src/language/common/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './commonLexer.js';
export * from './commonValueConverters.js';
7 changes: 7 additions & 0 deletions packages/parser/src/language/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export * from './generated/ast.js';
export * from './generated/grammar.js';
export * from './generated/module.js';

export * from './common/index.js';
export * from './info/index.js';
export * from './mermaid/index.js';
3 changes: 3 additions & 0 deletions packages/parser/src/language/info/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export * from './infoModule.js';
export * from './infoParser.js';
export * from './infoTokenBuilder.js';
Loading

0 comments on commit e8620f6

Please sign in to comment.