-
-
Notifications
You must be signed in to change notification settings - Fork 6.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a39122b
commit bf6d957
Showing
34 changed files
with
617 additions
and
400 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,3 +6,5 @@ cypress/plugins/index.js | |
coverage | ||
*.json | ||
node_modules | ||
# autogenereated by langium-cli | ||
generated/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -46,3 +46,6 @@ stats/ | |
|
||
demos/dev/** | ||
!/demos/dev/example.html | ||
|
||
# autogenereated by langium-cli | ||
generated/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -69,6 +69,7 @@ | |
"knut", | ||
"knutsveidqvist", | ||
"laganeckas", | ||
"langium", | ||
"linetype", | ||
"lintstagedrc", | ||
"logmsg", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}`); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './language/index.js'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]*/; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
67
packages/parser/src/language/common/commonValueConverters.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from './commonLexer.js'; | ||
export * from './commonValueConverters.js'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
grammar Info | ||
import "../common/common"; | ||
|
||
interface Info extends Common { | ||
version?: string; | ||
} | ||
|
||
entry Info returns Info: | ||
NEWLINE* | ||
"info" NEWLINE* | ||
TitleAndAccessibilities? | ||
; |
Oops, something went wrong.