diff --git a/packages/mermaid-parser/src/language/generated/grammar.ts b/packages/mermaid-parser/src/language/generated/grammar.ts index af8c1ee..efc6ed5 100644 --- a/packages/mermaid-parser/src/language/generated/grammar.ts +++ b/packages/mermaid-parser/src/language/generated/grammar.ts @@ -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 }, @@ -345,7 +355,7 @@ export const MermaidGrammar = (): Grammar => loadedMermaidGrammar ?? (loadedMerm "$type": "CharacterRange", "left": { "$type": "Keyword", - "value": "}%%\\r?\\n" + "value": "}%%\\n" } } } @@ -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 }, diff --git a/packages/mermaid-parser/src/language/grammars/common.langium b/packages/mermaid-parser/src/language/grammars/common.langium index 6af57a2..d98184c 100644 --- a/packages/mermaid-parser/src/language/grammars/common.langium +++ b/packages/mermaid-parser/src/language/grammars/common.langium @@ -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"; diff --git a/packages/mermaid-parser/src/language/matchers/index.ts b/packages/mermaid-parser/src/language/matchers/index.ts index a3492e0..4611beb 100644 --- a/packages/mermaid-parser/src/language/matchers/index.ts +++ b/packages/mermaid-parser/src/language/matchers/index.ts @@ -1,3 +1,4 @@ export * from './accdescr'; export * from './acctitle'; +export * from './newline'; export * from './title'; diff --git a/packages/mermaid-parser/src/language/matchers/newline.ts b/packages/mermaid-parser/src/language/matchers/newline.ts new file mode 100644 index 0000000..aa21b70 --- /dev/null +++ b/packages/mermaid-parser/src/language/matchers/newline.ts @@ -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'), + ]; +}; diff --git a/packages/mermaid-parser/src/language/mermaid-token-builder.ts b/packages/mermaid-parser/src/language/mermaid-token-builder.ts index c8bee5f..f7d7e89 100644 --- a/packages/mermaid-parser/src/language/mermaid-token-builder.ts +++ b/packages/mermaid-parser/src/language/mermaid-token-builder.ts @@ -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'; @@ -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; @@ -39,4 +44,3 @@ export class MermiadTokenBuilder extends DefaultTokenBuilder { return tokenType; } } -/* eslint-enable unicorn/no-null */