Skip to content

Commit

Permalink
fix(common): reorder hidden rules and remove more than one newline (#67)
Browse files Browse the repository at this point in the history
Signed-off-by: Yokozuna59 <u.yokozuna@gmail.com>
  • Loading branch information
Yokozuna59 authored May 25, 2023
1 parent 2dc9d22 commit 1e0563a
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 9 deletions.
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 */

0 comments on commit 1e0563a

Please sign in to comment.