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

fix(common): reorder hidden rules and remove more than one newline #67

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
20 changes: 15 additions & 5 deletions packages/mermaid-parser/src/language/generated/grammar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -318,10 +318,20 @@ export const MermaidGrammar = (): Grammar => loadedMermaidGrammar ?? (loadedMerm
{
"$type": "TerminalRule",
"hidden": true,
"name": "COMMENT",
"name": "WHITESPACE",
"definition": {
"$type": "RegexToken",
"regex": "[ \\\\r\\\\t]+"
},
"fragment": false
},
{
"$type": "TerminalRule",
"hidden": true,
"name": "WHITESPACES",
"definition": {
"$type": "RegexToken",
"regex": "%%(?!{(.|\\\\n)*}%%).*"
"regex": "\\\\s"
},
"fragment": false
},
Expand All @@ -345,7 +355,7 @@ export const MermaidGrammar = (): Grammar => loadedMermaidGrammar ?? (loadedMerm
"$type": "CharacterRange",
"left": {
"$type": "Keyword",
"value": "}%%\\r?\\n"
"value": "}%%\\n"
}
}
}
Expand All @@ -356,10 +366,10 @@ export const MermaidGrammar = (): Grammar => loadedMermaidGrammar ?? (loadedMerm
{
"$type": "TerminalRule",
"hidden": true,
"name": "WHITESPACE",
"name": "COMMENT",
"definition": {
"$type": "RegexToken",
"regex": "[ \\\\r\\\\t]+"
"regex": "%%.*"
},
"fragment": false
},
Expand Down
5 changes: 3 additions & 2 deletions packages/mermaid-parser/src/language/grammars/common.langium
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ terminal NUMBER returns number: /[0-9]+(\.[0-9]+)?/;
terminal STRING: /"[^"]*"/;

// hidden terminals
hidden terminal COMMENT: /%%(?!{(.|\n)*}%%).*/;
hidden terminal DIRECTIVE: "%%{" -> "}%%\r?\n";
hidden terminal WHITESPACE: /[ \r\t]+/;
hidden terminal WHITESPACES: /\s/;
hidden terminal DIRECTIVE: "%%{" -> "}%%\n";
hidden terminal COMMENT: /%%.*/;
hidden terminal YAML: "---\n" -> "---\n";
1 change: 1 addition & 0 deletions packages/mermaid-parser/src/language/matchers/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './accdescr';
export * from './acctitle';
export * from './newline';
export * from './title';
9 changes: 9 additions & 0 deletions packages/mermaid-parser/src/language/matchers/newline.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { CustomPatternMatcherFunc } from 'chevrotain';

export const removeWhitespaces: CustomPatternMatcherFunc = (
string_: string,
) => {
return [
string_.replaceAll(/^\s*|\s+$/gm, '').replaceAll(/[\n\r]{2,}/g, '\n'),
];
};
8 changes: 6 additions & 2 deletions packages/mermaid-parser/src/language/mermaid-token-builder.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
/* eslint-disable unicorn/no-null */
import { EOF, TokenType } from 'chevrotain';
import { DefaultTokenBuilder } from 'langium';
import { TerminalRule } from 'langium/lib/grammar/generated/ast';
Expand All @@ -7,12 +6,18 @@ import {
matchAccessibilityDescr,
matchAccessibilityTitle,
matchTitle,
removeWhitespaces,
} from './matchers';

export class MermiadTokenBuilder extends DefaultTokenBuilder {
override buildTerminalToken(terminal: TerminalRule): TokenType {
let tokenType = super.buildTerminalToken(terminal);
switch (tokenType.name) {
case 'WHITESPACES': {
tokenType.LINE_BREAKS = true;
tokenType.PATTERN = removeWhitespaces;
break;
}
case 'EOF': {
tokenType = EOF;
break;
Expand All @@ -39,4 +44,3 @@ export class MermiadTokenBuilder extends DefaultTokenBuilder {
return tokenType;
}
}
/* eslint-enable unicorn/no-null */